-
Notifications
You must be signed in to change notification settings - Fork 40
/
handler_mini.go
59 lines (49 loc) · 2.1 KB
/
handler_mini.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
package list
import (
"github.com/bnb-chain/node/common/log"
ctypes "github.com/bnb-chain/node/common/types"
"github.com/bnb-chain/node/common/upgrade"
"github.com/bnb-chain/node/plugins/dex/order"
"github.com/bnb-chain/node/plugins/dex/types"
"github.com/bnb-chain/node/plugins/tokens"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func handleListMini(ctx sdk.Context, dexKeeper *order.DexKeeper, tokenMapper tokens.Mapper,
msg types.ListMiniMsg) sdk.Result {
// before BEP70 upgraded, we only support listing mini token against NativeToken
if sdk.IsUpgrade(upgrade.BEP70) {
if ctypes.NativeTokenSymbol != msg.QuoteAssetSymbol && order.BUSDSymbol != msg.QuoteAssetSymbol {
return sdk.ErrInvalidCoins("quote token is not valid ").Result()
}
} else {
if ctypes.NativeTokenSymbol != msg.QuoteAssetSymbol {
return sdk.ErrInvalidCoins("quote token is not valid ").Result()
}
}
if err := dexKeeper.CanListTradingPair(ctx, msg.BaseAssetSymbol, msg.QuoteAssetSymbol); err != nil {
return sdk.ErrInvalidCoins(err.Error()).Result()
}
baseToken, err := tokenMapper.GetToken(ctx, msg.BaseAssetSymbol)
if err != nil {
return sdk.ErrInvalidCoins(err.Error()).Result()
}
quoteToken, err := tokenMapper.GetToken(ctx, msg.QuoteAssetSymbol)
if err != nil {
return sdk.ErrInvalidCoins(err.Error()).Result()
}
if !baseToken.IsOwner(msg.From) && !quoteToken.IsOwner(msg.From) {
return sdk.ErrUnauthorized("only the owner of the base asset or quote asset can list the trading pair").Result()
}
lotSize := dexKeeper.DetermineLotSize(msg.BaseAssetSymbol, msg.QuoteAssetSymbol, msg.InitPrice)
pair := types.NewTradingPairWithLotSize(msg.BaseAssetSymbol, msg.QuoteAssetSymbol, msg.InitPrice, lotSize)
err = dexKeeper.PairMapper.AddTradingPair(ctx, pair)
if err != nil {
return sdk.ErrInternal(err.Error()).Result()
}
// this is done in memory! we must not run this block in checktx or simulate!
if ctx.IsDeliverTx() { // only add engine during DeliverTx
dexKeeper.AddEngine(pair)
log.With("module", "dex").Info("List new mini-token Pair and created new match engine", "pair", pair)
}
return sdk.Result{}
}