-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
299 lines (266 loc) · 9.06 KB
/
pool.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furynet/furymod/modules/farm/types"
)
// CreatePool creates an new farm pool
func (k Keeper) CreatePool(
ctx sdk.Context,
description string,
lptDenom string,
startHeight int64,
rewardPerBlock sdk.Coins,
totalReward sdk.Coins,
editable bool,
creator sdk.AccAddress,
) (*types.FarmPool, error) {
// deduct the user's fee for creating a farm pool
if err := k.DeductPoolCreationFee(ctx, creator); err != nil {
return nil, err
}
//Escrow total reward
if err := k.bk.SendCoinsFromAccountToModule(ctx,
creator, types.ModuleName, totalReward); err != nil {
return nil, err
}
return k.createPool(ctx, creator, description, startHeight, editable, lptDenom, totalReward, rewardPerBlock)
}
// Destroy destroy an exist farm pool
func (k Keeper) DestroyPool(ctx sdk.Context, poolId string, creator sdk.AccAddress) (sdk.Coins, error) {
pool, exist := k.GetPool(ctx, poolId)
if !exist {
return nil, sdkerrors.Wrapf(types.ErrPoolNotFound, poolId)
}
if creator.String() != pool.Creator {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "creator [%s] is not the creator of the pool", creator.String())
}
if !pool.Editable {
return nil, sdkerrors.Wrapf(
types.ErrInvalidOperate, "pool [%s] is not editable", poolId)
}
if k.Expired(ctx, pool) {
return nil, sdkerrors.Wrapf(types.ErrPoolExpired,
"pool [%s] has expired at height [%d], current [%d]",
poolId,
pool.EndHeight,
ctx.BlockHeight(),
)
}
return k.Refund(ctx, pool)
}
// AdjustPool adjusts farm pool parameters
func (k Keeper) AdjustPool(
ctx sdk.Context,
poolId string,
reward sdk.Coins,
rewardPerBlock sdk.Coins,
creator sdk.AccAddress,
) (err error) {
pool, exist := k.GetPool(ctx, poolId)
//check if the liquidity pool exists
if !exist {
return sdkerrors.Wrapf(types.ErrPoolNotFound, poolId)
}
if !pool.Editable {
return sdkerrors.Wrapf(
types.ErrInvalidOperate, "pool [%s] is not editable", poolId)
}
//check permissions
if creator.String() != pool.Creator {
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "creator [%s] is not the creator of the pool", creator.String())
}
//check for expiration
if k.Expired(ctx, pool) {
return sdkerrors.Wrapf(types.ErrPoolExpired,
"pool [%s] has expired at height[%d], current [%d]",
poolId,
pool.EndHeight,
ctx.BlockHeight(),
)
}
pool.Rules = k.GetRewardRules(ctx, pool.Id)
rules := types.RewardRules(pool.Rules)
if rewardPerBlock != nil && !rewardPerBlock.DenomsSubsetOf(rules.RewardsPerBlock()) {
return sdkerrors.Wrapf(types.ErrInvalidAppend, "rewardPerBlock: %s", rewardPerBlock.String())
}
if reward != nil && !rules.Contains(reward) {
return sdkerrors.Wrapf(types.ErrInvalidAppend, reward.String())
}
startHeight := pool.StartHeight
if pool.Started(ctx) {
startHeight = ctx.BlockHeight()
}
//update pool reward shards
pool, _, err = k.updatePool(ctx, pool, sdk.ZeroInt(), false)
if err != nil {
return err
}
//update pool TotalReward、RemainingReward
rules = types.RewardRules(pool.Rules)
if reward != nil {
if err := k.bk.SendCoinsFromAccountToModule(ctx,
creator, types.ModuleName, reward); err != nil {
return err
}
for i := range rules {
rules[i].TotalReward = rules[i].TotalReward.Add(reward.AmountOf(rules[i].Reward))
rules[i].RemainingReward = rules[i].RemainingReward.Add(reward.AmountOf(rules[i].Reward))
}
}
// Calculate remaining available reward
availableReward := rules.TotalReward()
if pool.Started(ctx) {
remainingHeight := pool.EndHeight - startHeight
remainingReward := sdk.NewCoins()
for i := range rules {
remainingReward = remainingReward.Add(
sdk.NewCoin(
rules[i].Reward,
rules[i].RewardPerBlock.Mul(sdk.NewInt(remainingHeight)),
),
)
}
availableReward = remainingReward.Add(reward...)
}
pool.Rules = types.RewardRules(rules).UpdateWith(rewardPerBlock)
k.SetRewardRules(ctx, pool.Id, pool.Rules)
//expiredHeight = [(srcEndHeight-beginPoint)*srcRewardPerBlock +appendReward]/RewardPerBlock + beginPoint
rewardsPerBlock := types.RewardRules(pool.Rules).RewardsPerBlock()
availableHeight := availableReward[0].Amount.Quo(rewardsPerBlock.AmountOf(availableReward[0].Denom)).Int64()
for _, c := range availableReward[1:] {
rpb := rewardsPerBlock.AmountOf(c.Denom)
inteval := c.Amount.Quo(rpb).Int64()
if availableHeight > inteval {
availableHeight = inteval
}
}
expiredHeight := startHeight + availableHeight
//if the expiration height does not change,
// there is no need to update the pool and the expired queue
if expiredHeight == pool.EndHeight {
return nil
}
// remove from Expired Pool at old height
k.DequeueActivePool(ctx, pool.Id, pool.EndHeight)
pool.EndHeight = expiredHeight
k.SetPool(ctx, pool)
// put to expired farm pool queue at new height
k.EnqueueActivePool(ctx, pool.Id, pool.EndHeight)
return nil
}
func (k Keeper) createPool(
ctx sdk.Context,
creator sdk.AccAddress,
description string,
startHeight int64,
editable bool,
lptDenom string,
totalReward sdk.Coins,
rewardPerBlock sdk.Coins,
) (*types.FarmPool, error) {
pool := types.FarmPool{
Id: k.genPoolId(ctx),
Creator: creator.String(),
Description: description,
StartHeight: startHeight,
Editable: editable,
TotalLptLocked: sdk.NewCoin(lptDenom, sdk.ZeroInt()),
Rules: []types.RewardRule{},
}
for _, total := range totalReward {
rewardRule := types.RewardRule{
Reward: total.Denom,
TotalReward: total.Amount,
RemainingReward: total.Amount,
RewardPerBlock: rewardPerBlock.AmountOf(total.Denom),
RewardPerShare: sdk.ZeroDec(),
}
k.SetRewardRule(ctx, pool.Id, rewardRule)
pool.Rules = append(pool.Rules, rewardRule)
}
endHeight, err := pool.ExpiredHeight()
if err != nil {
return nil, err
}
pool.EndHeight = endHeight
k.SetPool(ctx, pool)
k.EnqueueActivePool(ctx, pool.Id, pool.EndHeight)
return &pool, nil
}
// updatePool is responsible for updating the status information of the farm pool, including the total accumulated bonus from the last time the bonus was distributed to the present, the current remaining bonus in the farm pool, and the ratio of the current liquidity token to the bonus.
// Note that when multiple transactions at the same block height trigger the farm pool update at the same time, only the first transaction will trigger the `RewardPerShare` update operation
// updatePool returns the updated farm pool and the reward collected in this period
func (k Keeper) updatePool(
ctx sdk.Context,
pool types.FarmPool,
amount sdk.Int,
isDestroy bool,
) (types.FarmPool, sdk.Coins, error) {
height := ctx.BlockHeight()
if height < pool.LastHeightDistrRewards {
return pool, nil, sdkerrors.Wrapf(
types.ErrExpiredHeight,
"invalid height: [%d], last distribution height: [%d]",
height, pool.LastHeightDistrRewards,
)
}
rules := k.GetRewardRules(ctx, pool.Id)
if len(rules) == 0 {
return pool, nil, sdkerrors.Wrapf(types.ErrPoolNotFound, pool.Id)
}
var rewardTotal sdk.Coins
//when there are multiple farm operations in the same block, the value needs to be updated once
if height > pool.LastHeightDistrRewards &&
pool.TotalLptLocked.Amount.GT(sdk.ZeroInt()) {
blockInterval := height - pool.LastHeightDistrRewards
for i := range rules {
rewardCollected := rules[i].RewardPerBlock.MulRaw(int64(blockInterval))
coinCollected := sdk.NewCoin(rules[i].Reward, rewardCollected)
if rules[i].RemainingReward.LT(rewardCollected) {
k.Logger(ctx).Error(
"The remaining amount is not enough to pay the bonus",
"poolId", pool.Id,
"remainingReward", rules[i].RemainingReward.String(),
"rewardCollected", rewardCollected.String(),
)
return pool, nil, sdkerrors.Wrapf(
sdkerrors.ErrInsufficientFunds,
"the remaining reward of the pool [%s] is [%s], but got [%s]",
pool.Id, sdk.NewCoin(rules[i].Reward, rules[i].RemainingReward).String(), coinCollected,
)
}
newRewardPerShare := sdk.NewDecFromInt(rewardCollected).QuoInt(pool.TotalLptLocked.Amount)
rules[i].RewardPerShare = rules[i].RewardPerShare.Add(newRewardPerShare)
rules[i].RemainingReward = rules[i].RemainingReward.Sub(rewardCollected)
rewardTotal = rewardTotal.Add(coinCollected)
k.SetRewardRule(ctx, pool.Id, rules[i])
}
}
//escrow the collected rewards to the `RewardCollector` account
if rewardTotal.IsAllPositive() {
if err := k.bk.SendCoinsFromModuleToModule(ctx, types.ModuleName, types.RewardCollector, rewardTotal); err != nil {
return pool, rewardTotal, err
}
}
pool.TotalLptLocked = sdk.NewCoin(
pool.TotalLptLocked.Denom,
pool.TotalLptLocked.Amount.Add(amount),
)
pool.LastHeightDistrRewards = ctx.BlockHeight()
if isDestroy {
pool.EndHeight = ctx.BlockHeight()
if pool.StartHeight > pool.EndHeight {
pool.StartHeight = pool.EndHeight
}
}
pool.Rules = rules
k.SetPool(ctx, pool)
return pool, rewardTotal, nil
}
func (k Keeper) genPoolId(ctx sdk.Context) string {
seq := k.GetSequence(ctx) + 1
k.SetSequence(ctx, seq)
return fmt.Sprintf("%s-%d", types.PrefixFarmPool, seq)
}