-
Notifications
You must be signed in to change notification settings - Fork 201
/
keeper.go
122 lines (99 loc) · 3.72 KB
/
keeper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package keeper
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
dexkeeper "github.com/NibiruChain/nibiru/x/dex/keeper"
"github.com/NibiruChain/nibiru/x/incentivization/types"
lockupkeeper "github.com/NibiruChain/nibiru/x/lockup/keeper"
)
const (
// MinLockupDuration defines the lockup minimum time
// TODO(mercilex): maybe module param
MinLockupDuration = 24 * time.Hour
// MinEpochs defines the minimum number of epochs
// TODO(mercilex): maybe module param
MinEpochs int64 = 7
)
const (
// FundsModuleAccountAddressPrefix defines the prefix
// of module accounts created that contain an
// incentivization program funds.
FundsModuleAccountAddressPrefix = "incentivization_escrow_"
)
func NewKeeper(cdc codec.Codec, storeKey sdk.StoreKey, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper, dk dexkeeper.Keeper, lk lockupkeeper.Keeper) Keeper {
return Keeper{
cdc: cdc,
storeKey: storeKey,
ak: ak,
bk: bk,
dk: dk,
lk: lk,
}
}
type Keeper struct {
cdc codec.Codec
storeKey sdk.StoreKey
ak authkeeper.AccountKeeper
bk bankkeeper.Keeper
dk dexkeeper.Keeper
lk lockupkeeper.Keeper
}
func (k Keeper) CreateIncentivizationProgram(
ctx sdk.Context,
lpDenom string, minLockupDuration time.Duration, startTime time.Time, epochs int64) (*types.IncentivizationProgram, error) {
// TODO(mercilex): assert lp denom from dex keeper
if epochs < MinEpochs {
return nil, types.ErrEpochsTooLow.Wrapf("%d is lower than minimum allowed %d", epochs, MinEpochs)
}
if minLockupDuration < MinLockupDuration {
return nil, types.ErrMinLockupDurationTooLow.Wrapf("%s is lower than minimum allowed %s", minLockupDuration, MinLockupDuration)
}
if ctx.BlockTime().After(startTime) {
return nil, types.ErrStartTimeInPast.Wrapf("current time %s, got: %s", ctx.BlockTime(), startTime)
}
// we create a new instance of an incentivization program
nextID := k.IncentivizationProgramsState(ctx).PeekNextID() // we need to peek the next ID to create a new
escrowAccount := k.ak.NewAccount(ctx, authtypes.NewEmptyModuleAccount(NewEscrowAccountName(nextID))) // module account that holds the escrowed funds.
k.ak.SetAccount(ctx, escrowAccount)
program := &types.IncentivizationProgram{
EscrowAddress: escrowAccount.GetAddress().String(),
RemainingEpochs: epochs,
LpDenom: lpDenom,
MinLockupDuration: minLockupDuration,
StartTime: startTime,
}
k.IncentivizationProgramsState(ctx).Create(program)
return program, nil
}
func (k Keeper) FundIncentivizationProgram(ctx sdk.Context, id uint64, funder sdk.AccAddress, funds sdk.Coins) error {
program, err := k.IncentivizationProgramsState(ctx).Get(id)
if err != nil {
return err
}
escrowAddr, err := sdk.AccAddressFromBech32(program.EscrowAddress)
if err != nil {
panic(err)
}
// we transfer money from funder to the program escrow address
// NOTE(mercilex): can't use send coins from module to account because
// due to how GetModuleAccount works, which fetches information in a
// stateless way. TRAGEDY. ABSOLUTE TRAGEDY.
if err := k.bk.SendCoins(ctx, funder, escrowAddr, funds); err != nil {
return err
}
return nil
}
// Distribute distributes incentivization rewards to accounts
// that meet incentivization program criteria.
func (k Keeper) Distribute(ctx sdk.Context) error {
panic("impl")
}
// NewEscrowAccountName returns the escrow module account name
func NewEscrowAccountName(id uint64) string {
return fmt.Sprintf("%s%d", FundsModuleAccountAddressPrefix, id)
}