-
Notifications
You must be signed in to change notification settings - Fork 182
/
handler.go
466 lines (403 loc) · 15.8 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package token
import (
"fmt"
"github.com/okex/exchain/x/common"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
"github.com/okex/exchain/libs/tendermint/libs/log"
"github.com/okex/exchain/x/common/perf"
"github.com/okex/exchain/x/common/version"
"github.com/okex/exchain/x/token/types"
)
// NewTokenHandler returns a handler for "token" type messages.
func NewTokenHandler(keeper Keeper, protocolVersion version.ProtocolVersionType) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx.SetEventManager(sdk.NewEventManager())
//logger := ctx.Logger().With("module", "token")
// NOTE msg already has validate basic run
var name string
var handlerFun func() (*sdk.Result, error)
logger := ctx.Logger().With("module", "token")
switch msg := msg.(type) {
case types.MsgTokenIssue:
name = "handleMsgTokenIssue"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTokenIssue(ctx, keeper, msg, logger)
}
case types.MsgTokenBurn:
name = "handleMsgTokenBurn"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTokenBurn(ctx, keeper, msg, logger)
}
case types.MsgTokenMint:
name = "handleMsgTokenMint"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTokenMint(ctx, keeper, msg, logger)
}
case types.MsgMultiSend:
name = "handleMsgMultiSend"
handlerFun = func() (*sdk.Result, error) {
return handleMsgMultiSend(ctx, keeper, msg, logger)
}
case types.MsgSend:
name = "handleMsgSend"
handlerFun = func() (*sdk.Result, error) {
return handleMsgSend(ctx, keeper, msg, logger)
}
case types.MsgTransferOwnership:
name = "handleMsgTransferOwnership"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTransferOwnership(ctx, keeper, msg, logger)
}
case types.MsgConfirmOwnership:
name = "handleMsgConfirmOwnership"
handlerFun = func() (*sdk.Result, error) {
return handleMsgConfirmOwnership(ctx, keeper, msg, logger)
}
case types.MsgTokenModify:
name = "handleMsgTokenModify"
handlerFun = func() (*sdk.Result, error) {
return handleMsgTokenModify(ctx, keeper, msg, logger)
}
case WalletTokenTransfer:
name = "handleWalletMsgSend"
handlerFun = func() (*sdk.Result, error) {
return handleWalletMsgSend(ctx, keeper, MsgSend{
FromAddress: msg.GetFrom(),
ToAddress: msg.GetTo(),
Amount: msg.GetAmount(),
}, logger)
}
default:
errMsg := fmt.Sprintf("Unrecognized token Msg type: %v", msg.Type())
return sdk.ErrUnknownRequest(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 handleMsgTokenIssue(ctx sdk.Context, keeper Keeper, msg types.MsgTokenIssue, logger log.Logger) (*sdk.Result, error) {
// check upper bound
totalSupply, err := sdk.NewDecFromStr(msg.TotalSupply)
if err != nil {
return types.ErrGetDecimalFromDecimalStringFailed(err.Error()).Result()
}
if totalSupply.GT(sdk.NewDec(types.TotalSupplyUpperbound)) {
return types.ErrAmountBiggerThanTotalSupplyUpperbound().Result()
}
token := types.Token{
Description: msg.Description,
OriginalSymbol: msg.OriginalSymbol,
WholeName: msg.WholeName,
OriginalTotalSupply: totalSupply,
Owner: msg.Owner,
Mintable: msg.Mintable,
}
// generate a random symbol
newName, valid := addTokenSuffix(ctx, keeper, msg.OriginalSymbol)
if !valid {
return types.ErrInvalidCoins(msg.OriginalSymbol).Result()
}
token.Symbol = newName
coins := sdk.MustParseCoins(token.Symbol, msg.TotalSupply)
// set supply
err = keeper.supplyKeeper.MintCoins(ctx, types.ModuleName, coins)
if err != nil {
return types.ErrMintCoinsFailed(err.Error()).Result()
}
// send coins to owner
err = keeper.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, token.Owner, coins)
if err != nil {
return types.ErrSendCoinsFromModuleToAccountFailed(err.Error()).Result()
}
// set token info
keeper.NewToken(ctx, token)
// deduction fee
feeDecCoins := keeper.GetParams(ctx).FeeIssue.ToCoins()
err = keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, token.Owner, keeper.feeCollectorName, feeDecCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(err.Error()).Result()
}
var name = "handleMsgTokenIssue"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Description:%s,Symbol:%s,OriginalSymbol:%s,TotalSupply:%s,Owner:%v,Mintable:%v>\n"+
" result<Owner have enough okts to issue %s>\n",
ctx.BlockHeight(), name,
msg.Description, msg.Symbol, msg.OriginalSymbol, msg.TotalSupply, msg.Owner, msg.Mintable,
token.Symbol))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyFee, keeper.GetParams(ctx).FeeIssue.String()),
sdk.NewAttribute("symbol", token.Symbol),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgTokenBurn(ctx sdk.Context, keeper Keeper, msg types.MsgTokenBurn, logger log.Logger) (*sdk.Result, error) {
token := keeper.GetTokenInfo(ctx, msg.Amount.Denom)
// check owner
if !token.Owner.Equals(msg.Owner) {
return types.ErrInputOwnerIsNotEqualTokenOwner(msg.Owner).Result()
}
subCoins := msg.Amount.ToCoins()
// send coins to moduleAcc
err := keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.Owner, types.ModuleName, subCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(err.Error()).Result()
}
// set supply
err = keeper.supplyKeeper.BurnCoins(ctx, types.ModuleName, subCoins)
if err != nil {
return types.ErrBurnCoinsFailed(err.Error()).Result()
}
// deduction fee
feeDecCoins := keeper.GetParams(ctx).FeeBurn.ToCoins()
err = keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.Owner, keeper.feeCollectorName, feeDecCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(feeDecCoins.String()).Result()
}
var name = "handleMsgTokenBurn"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Owner:%s,Symbol:%s,Amount:%s>\n"+
" result<Owner have enough okts to burn %s>\n",
ctx.BlockHeight(), name,
msg.Owner, msg.Amount.Denom, msg.Amount,
msg.Amount.Denom))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyFee, feeDecCoins.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgTokenMint(ctx sdk.Context, keeper Keeper, msg types.MsgTokenMint, logger log.Logger) (*sdk.Result, error) {
token := keeper.GetTokenInfo(ctx, msg.Amount.Denom)
// check owner
if !token.Owner.Equals(msg.Owner) {
return types.ErrInputOwnerIsNotEqualTokenOwner(msg.Owner).Result()
}
// check whether token is mintable
if !token.Mintable {
return types.ErrTokenIsNotMintable().Result()
}
// check upper bound
totalSupplyAfterMint := keeper.supplyKeeper.GetSupplyByDenom(ctx, msg.Amount.Denom).Add(msg.Amount.Amount)
if totalSupplyAfterMint.GT(sdk.NewDec(types.TotalSupplyUpperbound)) {
return types.ErrCodeTotalsupplyExceedsTheUpperLimit(totalSupplyAfterMint, types.TotalSupplyUpperbound).Result()
}
mintCoins := msg.Amount.ToCoins()
// set supply
err := keeper.supplyKeeper.MintCoins(ctx, types.ModuleName, mintCoins)
if err != nil {
return types.ErrMintCoinsFailed(err.Error()).Result()
}
// send coins to acc
err = keeper.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, msg.Owner, mintCoins)
if err != nil {
return types.ErrSendCoinsFromModuleToAccountFailed(err.Error()).Result()
}
// deduction fee
feeDecCoins := keeper.GetParams(ctx).FeeMint.ToCoins()
err = keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.Owner, keeper.feeCollectorName, feeDecCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(feeDecCoins.String()).Result()
}
name := "handleMsgTokenMint"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Owner:%s,Symbol:%s,Amount:%s>\n"+
" result<Owner have enough okts to Mint %s>\n",
ctx.BlockHeight(), name,
msg.Owner, msg.Amount.Denom, msg.Amount,
msg.Amount.Denom))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyFee, feeDecCoins.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgMultiSend(ctx sdk.Context, keeper Keeper, msg types.MsgMultiSend, logger log.Logger) (*sdk.Result, error) {
if !keeper.bankKeeper.GetSendEnabled(ctx) {
return types.ErrSendDisabled().Result()
}
var transfers string
var coinNum int
for _, transferUnit := range msg.Transfers {
coinNum += len(transferUnit.Coins)
err := keeper.SendCoinsFromAccountToAccount(ctx, msg.From, transferUnit.To, transferUnit.Coins)
if err != nil {
return types.ErrSendCoinsFromAccountToAccountFailed(err.Error()).Result()
}
transfers += fmt.Sprintf(" msg<To:%s,Coin:%s>\n", transferUnit.To, transferUnit.Coins)
}
name := "handleMsgMultiSend"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<From:%s>\n"+
transfers+
" result<Owner have enough okts to send multi txs>\n",
ctx.BlockHeight(), name,
msg.From))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName)),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgSend(ctx sdk.Context, keeper Keeper, msg types.MsgSend, logger log.Logger) (*sdk.Result, error) {
if !keeper.bankKeeper.GetSendEnabled(ctx) {
return types.ErrSendDisabled().Result()
}
err := keeper.SendCoinsFromAccountToAccount(ctx, msg.FromAddress, msg.ToAddress, msg.Amount)
if err != nil {
return types.ErrSendCoinsFromAccountToAccountFailed(err.Error()).Result()
}
var name = "handleMsgSend"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<From:%s,To:%s,Amount:%s>\n"+
" result<Owner have enough okts to send a tx>\n",
ctx.BlockHeight(), name,
msg.FromAddress, msg.ToAddress, msg.Amount))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName)),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgTransferOwnership(ctx sdk.Context, keeper Keeper, msg types.MsgTransferOwnership, logger log.Logger) (*sdk.Result, error) {
tokenInfo := keeper.GetTokenInfo(ctx, msg.Symbol)
if !tokenInfo.Owner.Equals(msg.FromAddress) {
return types.ErrCodeinputFromAddressIsNotEqualTokenInfoOwner(msg.FromAddress).Result()
}
confirmOwnership, exist := keeper.GetConfirmOwnership(ctx, msg.Symbol)
if exist && !ctx.BlockTime().After(confirmOwnership.Expire) {
return types.ErrConfirmOwnershipNotExistOrBlockTimeAfter().Result()
}
if msg.ToAddress.Equals(common.BlackHoleAddress()) { // transfer ownership to black hole
// first remove it from the raw owner
keeper.DeleteUserToken(ctx, tokenInfo.Owner, tokenInfo.Symbol)
tokenInfo.Owner = msg.ToAddress
keeper.NewToken(ctx, tokenInfo)
} else {
// set confirm ownership info
expireTime := ctx.BlockTime().Add(keeper.GetParams(ctx).OwnershipConfirmWindow)
confirmOwnership = &types.ConfirmOwnership{
Symbol: msg.Symbol,
Address: msg.ToAddress,
Expire: expireTime,
}
keeper.SetConfirmOwnership(ctx, confirmOwnership)
}
// deduction fee
feeDecCoins := keeper.GetParams(ctx).FeeChown.ToCoins()
err := keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.FromAddress, keeper.feeCollectorName, feeDecCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(err.Error()).Result()
}
var name = "handleMsgTransferOwnership"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<From:%s,To:%s,Symbol:%s>\n"+
" result<Owner have enough okts to transfer the %s>\n",
ctx.BlockHeight(), name,
msg.FromAddress, msg.ToAddress, msg.Symbol,
msg.Symbol))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyFee, keeper.GetParams(ctx).FeeChown.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgConfirmOwnership(ctx sdk.Context, keeper Keeper, msg types.MsgConfirmOwnership, logger log.Logger) (*sdk.Result, error) {
confirmOwnership, exist := keeper.GetConfirmOwnership(ctx, msg.Symbol)
if !exist {
return types.ErrGetConfirmOwnership().Result()
}
if ctx.BlockTime().After(confirmOwnership.Expire) {
// delete ownership confirming information
keeper.DeleteConfirmOwnership(ctx, confirmOwnership.Symbol)
return types.ErrConfirmOwnershipNotExistOrBlockTimeAfter().Result()
}
if !confirmOwnership.Address.Equals(msg.Address) {
return types.ErrCodeConfirmOwnershipAddressNotEqualsMsgAddress(msg.Address).Result()
}
tokenInfo := keeper.GetTokenInfo(ctx, msg.Symbol)
// first remove it from the raw owner
keeper.DeleteUserToken(ctx, tokenInfo.Owner, tokenInfo.Symbol)
tokenInfo.Owner = msg.Address
keeper.NewToken(ctx, tokenInfo)
// delete ownership confirming information
keeper.DeleteConfirmOwnership(ctx, confirmOwnership.Symbol)
var name = "handleMsgConfirmOwnership"
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<From:%s,Symbol:%s>\n"+
" result<Owner have enough okts to transfer the %s>\n",
ctx.BlockHeight(), name, msg.Address, msg.Symbol, msg.Symbol))
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyFee, keeper.GetParams(ctx).FeeChown.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}
func handleMsgTokenModify(ctx sdk.Context, keeper Keeper, msg types.MsgTokenModify, logger log.Logger) (*sdk.Result, error) {
token := keeper.GetTokenInfo(ctx, msg.Symbol)
// check owner
if !token.Owner.Equals(msg.Owner) {
return types.ErrInputOwnerIsNotEqualTokenOwner(msg.Owner).Result()
}
if !msg.IsWholeNameModified && !msg.IsDescriptionModified {
return types.ErrWholeNameAndDescriptionIsNotModified().Result()
}
// modify
if msg.IsWholeNameModified {
token.WholeName = msg.WholeName
}
if msg.IsDescriptionModified {
token.Description = msg.Description
}
keeper.UpdateToken(ctx, token)
// deduction fee
feeDecCoins := keeper.GetParams(ctx).FeeModify.ToCoins()
err := keeper.supplyKeeper.SendCoinsFromAccountToModule(ctx, msg.Owner, keeper.feeCollectorName, feeDecCoins)
if err != nil {
return types.ErrSendCoinsFromAccountToModuleFailed(feeDecCoins.String()).Result()
}
name := "handleMsgTokenModify"
if logger != nil {
logger.Debug(fmt.Sprintf("BlockHeight<%d>, handler<%s>\n"+
" msg<Owner:%s,Symbol:%s,WholeName:%s,Description:%s>\n"+
" result<Owner have enough okts to edit %s>\n",
ctx.BlockHeight(), name,
msg.Owner, msg.Symbol, msg.WholeName, msg.Description,
msg.Symbol))
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeyFee, keeper.GetParams(ctx).FeeModify.String()),
),
)
return &sdk.Result{Events: ctx.EventManager().Events()}, nil
}