-
Notifications
You must be signed in to change notification settings - Fork 49
/
keeper.go
93 lines (76 loc) · 2.55 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
package keeper
import (
"fmt"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/classic-terra/core/v3/x/market/types"
)
// Keeper of the market store
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.BinaryCodec
paramSpace paramstypes.Subspace
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
OracleKeeper types.OracleKeeper
}
// NewKeeper constructs a new keeper for oracle
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
paramstore paramstypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
oracleKeeper types.OracleKeeper,
) Keeper {
// ensure market module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set KeyTable if it has not already been set
if !paramstore.HasKeyTable() {
paramstore = paramstore.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramstore,
AccountKeeper: accountKeeper,
BankKeeper: bankKeeper,
OracleKeeper: oracleKeeper,
}
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
// GetTerraPoolDelta returns the gap between the TerraPool and the TerraBasePool
func (k Keeper) GetTerraPoolDelta(ctx sdk.Context) sdk.Dec {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.TerraPoolDeltaKey)
if bz == nil {
return sdk.ZeroDec()
}
dp := sdk.DecProto{}
k.cdc.MustUnmarshal(bz, &dp)
return dp.Dec
}
// SetTerraPoolDelta updates TerraPoolDelta which is gap between the TerraPool and the BasePool
func (k Keeper) SetTerraPoolDelta(ctx sdk.Context, delta sdk.Dec) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshal(&sdk.DecProto{Dec: delta})
store.Set(types.TerraPoolDeltaKey, bz)
}
// ReplenishPools replenishes each pool(Terra,Luna) to BasePool
func (k Keeper) ReplenishPools(ctx sdk.Context) {
poolDelta := k.GetTerraPoolDelta(ctx)
poolRecoveryPeriod := int64(k.PoolRecoveryPeriod(ctx))
poolRegressionAmt := poolDelta.QuoInt64(poolRecoveryPeriod)
// Replenish pools towards each base pool
// regressionAmt cannot make delta zero
poolDelta = poolDelta.Sub(poolRegressionAmt)
k.SetTerraPoolDelta(ctx, poolDelta)
}