-
Notifications
You must be signed in to change notification settings - Fork 182
/
abci.go
106 lines (91 loc) · 3.67 KB
/
abci.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
package farm
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/exchain/x/farm/keeper"
"github.com/okex/exchain/x/farm/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// BeginBlocker allocates the native token to the pools in PoolsYieldNativeToken
// according to the value of locked token in pool
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
logger := k.Logger(ctx)
moduleAcc := k.SupplyKeeper().GetModuleAccount(ctx, MintFarmingAccount)
yieldedNativeTokenAmt := moduleAcc.GetCoins().AmountOf(sdk.DefaultBondDenom)
logger.Debug(fmt.Sprintf("MintFarmingAccount [%s] balance: %s%s",
moduleAcc.GetAddress(), yieldedNativeTokenAmt, sdk.DefaultBondDenom))
if yieldedNativeTokenAmt.LTE(sdk.ZeroDec()) {
return
}
yieldedNativeToken := sdk.NewDecCoinsFromDec(sdk.DefaultBondDenom, yieldedNativeTokenAmt)
// 0. check the YieldNativeToken parameters
params := k.GetParams(ctx)
if !params.YieldNativeToken { // if it is false, only burn the minted native token
if err := k.SupplyKeeper().BurnCoins(ctx, MintFarmingAccount, yieldedNativeToken); err != nil {
panic(err)
}
return
}
// 1. gets all pools in PoolsYieldNativeToken
lockedPoolValueMap, pools, totalPoolsValue := calculateAllocateInfo(ctx, k)
if totalPoolsValue.LTE(sdk.ZeroDec()) {
return
}
// 2. allocate native token to pools according to the value
remainingNativeTokenAmt := yieldedNativeTokenAmt
for i, pool := range pools {
var allocatedAmt sdk.Dec
if i == len(pools)-1 {
allocatedAmt = remainingNativeTokenAmt
} else {
allocatedAmt = lockedPoolValueMap[pool.Name].
MulTruncate(yieldedNativeTokenAmt).QuoTruncate(totalPoolsValue)
}
remainingNativeTokenAmt = remainingNativeTokenAmt.Sub(allocatedAmt)
logger.Debug(
fmt.Sprintf("Pool %s allocate %s yielded native token", pool.Name, allocatedAmt.String()),
)
allocatedCoins := sdk.NewDecCoinsFromDec(sdk.DefaultBondDenom, allocatedAmt)
current := k.GetPoolCurrentRewards(ctx, pool.Name)
current.Rewards = current.Rewards.Add2(allocatedCoins)
k.SetPoolCurrentRewards(ctx, pool.Name, current)
logger.Debug(fmt.Sprintf("Pool %s rewards are %s", pool.Name, current.Rewards))
pool.TotalAccumulatedRewards = pool.TotalAccumulatedRewards.Add2(allocatedCoins)
k.SetFarmPool(ctx, pool)
}
if !remainingNativeTokenAmt.IsZero() {
panic(fmt.Sprintf("there are some tokens %s not to be allocated", remainingNativeTokenAmt))
}
// 3.liquidate native token minted at current block for yield farming
err := k.SupplyKeeper().SendCoinsFromModuleToModule(ctx, MintFarmingAccount, YieldFarmingAccount, yieldedNativeToken)
if err != nil {
panic("should not happen")
}
}
// EndBlocker called every block, process inflation, update validator set.
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
}
// calculateAllocateInfo gets all pools in PoolsYieldNativeToken
func calculateAllocateInfo(ctx sdk.Context, k keeper.Keeper) (map[string]sdk.Dec, []types.FarmPool, sdk.Dec) {
lockedPoolValue := make(map[string]sdk.Dec)
var pools types.FarmPools
totalPoolsValue := sdk.ZeroDec()
store := ctx.KVStore(k.StoreKey())
iterator := sdk.KVStorePrefixIterator(store, types.PoolsYieldNativeTokenPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
poolName := types.SplitPoolsYieldNativeTokenKey(iterator.Key())
pool, found := k.GetFarmPool(ctx, poolName)
if !found {
panic("should not happen")
}
poolValue := k.GetPoolLockedValue(ctx, pool)
if poolValue.LTE(sdk.ZeroDec()) {
continue
}
pools = append(pools, pool)
lockedPoolValue[poolName] = poolValue
totalPoolsValue = totalPoolsValue.Add(poolValue)
}
return lockedPoolValue, pools, totalPoolsValue
}