-
Notifications
You must be signed in to change notification settings - Fork 182
/
handler.go
164 lines (140 loc) · 5.5 KB
/
handler.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
package farm
import (
"fmt"
"strconv"
"github.com/okex/okexchain/x/common"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/okex/okexchain/x/common/perf"
"github.com/okex/okexchain/x/farm/keeper"
"github.com/okex/okexchain/x/farm/types"
)
// NewHandler creates an sdk.Handler for all the farm type messages
func NewHandler(k keeper.Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
var handlerFun func() (*sdk.Result, error)
var name string
switch msg := msg.(type) {
case types.MsgCreatePool:
name = "handleMsgCreatePool"
handlerFun = func() (*sdk.Result, error) {
return handleMsgCreatePool(ctx, k, msg)
}
case types.MsgDestroyPool:
name = "handleMsgDestroyPool"
handlerFun = func() (*sdk.Result, error) {
return handleMsgDestroyPool(ctx, k, msg)
}
case types.MsgProvide:
name = "handleMsgProvide"
handlerFun = func() (*sdk.Result, error) {
return handleMsgProvide(ctx, k, msg)
}
case types.MsgLock:
name = "handleMsgLock"
handlerFun = func() (*sdk.Result, error) {
return handleMsgLock(ctx, k, msg)
}
case types.MsgUnlock:
name = "handleMsgUnlock"
handlerFun = func() (*sdk.Result, error) {
return handleMsgUnlock(ctx, k, msg)
}
case types.MsgClaim:
name = "handleMsgClaim"
handlerFun = func() (*sdk.Result, error) {
return handleMsgClaim(ctx, k, msg)
}
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return types.ErrUnknownFarmMsgType(errMsg).Result()
}
seq := perf.GetPerf().OnDeliverTxEnter(ctx, types.ModuleName, name)
defer perf.GetPerf().OnDeliverTxExit(ctx, types.ModuleName, name, seq)
res, err := handlerFun()
common.SanityCheckHandler(res, err)
return res, err
}
}
func handleMsgProvide(ctx sdk.Context, k keeper.Keeper, msg types.MsgProvide) (*sdk.Result, error) {
// 0. Check if the start_height_to_yield is more than current height
if msg.StartHeightToYield <= ctx.BlockHeight() {
return types.ErrInvalidStartHeight().Result()
}
// 1.1 Check farm pool
pool, found := k.GetFarmPool(ctx, msg.PoolName)
if !found {
return types.ErrNoFarmPoolFound(msg.PoolName).Result()
}
// 1.2 Check owner
if !pool.Owner.Equals(msg.Address) {
return types.ErrInvalidPoolOwner(msg.Address.String(), msg.PoolName).Result()
}
// 1.3 Check if the provided coin denom is the same as the locked coin name
if len(pool.YieldedTokenInfos) != 1 {
panic(fmt.Sprintf("The YieldedTokenInfos length is %d, which should be 1 in current code version",
len(pool.YieldedTokenInfos)))
}
if pool.YieldedTokenInfos[0].RemainingAmount.Denom != msg.Amount.Denom {
return types.ErrInvalidDenom(pool.YieldedTokenInfos[0].RemainingAmount.Denom, msg.Amount.Denom).Result()
}
// 2.1 Calculate how many provided token & native token could be yielded in current period
updatedPool, yieldedTokens := k.CalculateAmountYieldedBetween(ctx, pool)
// 2.2 Check if remaining amount is already zero
remainingAmount := updatedPool.YieldedTokenInfos[0].RemainingAmount
if !remainingAmount.IsZero() {
return types.ErrRemainingAmountNotZero(remainingAmount.String()).Result()
}
// 3. Terminate pool current period
k.IncrementPoolPeriod(ctx, pool.Name, pool.TotalValueLocked, yieldedTokens)
// 4. Transfer coin to farm module account
if err := k.SupplyKeeper().SendCoinsFromAccountToModule(
ctx, msg.Address, YieldFarmingAccount, msg.Amount.ToCoins(),
); err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(err.Error()).Result()
}
// 5. init a new yielded_token_info struct, then set it into store
updatedPool.YieldedTokenInfos[0] = types.NewYieldedTokenInfo(
msg.Amount, msg.StartHeightToYield, msg.AmountYieldedPerBlock,
)
k.SetFarmPool(ctx, updatedPool)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeProvide,
sdk.NewAttribute(types.AttributeKeyAddress, msg.Address.String()),
sdk.NewAttribute(types.AttributeKeyPool, msg.PoolName),
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Amount.String()),
sdk.NewAttribute(types.AttributeKeyStartHeightToYield, strconv.FormatInt(msg.StartHeightToYield, 10)),
sdk.NewAttribute(types.AttributeKeyAmountYieldPerBlock, msg.AmountYieldedPerBlock.String()),
))
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgClaim(ctx sdk.Context, k keeper.Keeper, msg types.MsgClaim) (*sdk.Result, error) {
// 1. Get the pool info
pool, poolFound := k.GetFarmPool(ctx, msg.PoolName)
if !poolFound {
return types.ErrNoFarmPoolFound(msg.PoolName).Result()
}
// 2. Calculate how many provided token & native token could be yielded in current period
updatedPool, yieldedTokens := k.CalculateAmountYieldedBetween(ctx, pool)
// 3. Withdraw rewards
rewards, err := k.WithdrawRewards(ctx, pool.Name, pool.TotalValueLocked, yieldedTokens, msg.Address)
if err != nil {
return nil, err
}
// 4. Update the lock_info data
k.UpdateLockInfo(ctx, msg.Address, pool.Name, sdk.ZeroDec())
// 5. Update farm pool
if updatedPool.TotalAccumulatedRewards.IsAllLT(rewards) {
panic("should not happen")
}
updatedPool.TotalAccumulatedRewards = updatedPool.TotalAccumulatedRewards.Sub(rewards)
k.SetFarmPool(ctx, updatedPool)
// 6. notify backend
k.OnClaim(ctx, msg.Address, pool.Name, rewards)
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeClaim,
sdk.NewAttribute(types.AttributeKeyAddress, msg.Address.String()),
sdk.NewAttribute(types.AttributeKeyPool, msg.PoolName),
))
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}