-
Notifications
You must be signed in to change notification settings - Fork 40
/
hooks.go
128 lines (98 loc) · 3.43 KB
/
hooks.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
package list
import (
"encoding/json"
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/bnb-chain/node/common/upgrade"
"github.com/bnb-chain/node/plugins/dex/order"
"github.com/bnb-chain/node/plugins/tokens"
)
type ListHooks struct {
orderKeeper *order.DexKeeper
tokenMapper tokens.Mapper
}
func NewListHooks(orderKeeper *order.DexKeeper, tokenMapper tokens.Mapper) ListHooks {
return ListHooks{
orderKeeper: orderKeeper,
tokenMapper: tokenMapper,
}
}
var _ gov.GovHooks = ListHooks{}
func (hooks ListHooks) OnProposalSubmitted(ctx sdk.Context, proposal gov.Proposal) error {
if proposal.GetProposalType() != gov.ProposalTypeListTradingPair {
panic(fmt.Sprintf("received wrong type of proposal %x", proposal.GetProposalType()))
}
listParams := gov.ListTradingPairParams{}
err := json.Unmarshal([]byte(proposal.GetDescription()), &listParams)
if err != nil {
return fmt.Errorf("unmarshal list params error, err=%s", err.Error())
}
if listParams.BaseAssetSymbol == "" {
return errors.New("base asset symbol should not be empty")
}
if listParams.QuoteAssetSymbol == "" {
return errors.New("quote asset symbol should not be empty")
}
if listParams.BaseAssetSymbol == listParams.QuoteAssetSymbol {
return errors.New("base token and quote token should not be the same")
}
if listParams.InitPrice <= 0 {
return errors.New("init price should larger than zero")
}
if listParams.ExpireTime.Before(ctx.BlockHeader().Time) {
return errors.New("expire time should after now")
}
if !hooks.tokenMapper.ExistsBEP2(ctx, listParams.BaseAssetSymbol) {
return errors.New("base token does not exist")
}
if !hooks.tokenMapper.ExistsBEP2(ctx, listParams.QuoteAssetSymbol) {
return errors.New("quote token does not exist")
}
if err := hooks.orderKeeper.CanListTradingPair(ctx, listParams.BaseAssetSymbol, listParams.QuoteAssetSymbol); err != nil {
return err
}
return nil
}
type DelistHooks struct {
orderKeeper *order.DexKeeper
}
func NewDelistHooks(orderKeeper *order.DexKeeper) DelistHooks {
return DelistHooks{
orderKeeper: orderKeeper,
}
}
var _ gov.GovHooks = DelistHooks{}
func (hooks DelistHooks) OnProposalSubmitted(ctx sdk.Context, proposal gov.Proposal) error {
if proposal.GetProposalType() != gov.ProposalTypeDelistTradingPair {
panic(fmt.Sprintf("received wrong type of proposal %x", proposal.GetProposalType()))
}
if !sdk.IsUpgrade(upgrade.BEP6) {
return fmt.Errorf("proposal type %s is not supported", gov.ProposalTypeDelistTradingPair)
}
delistParams := gov.DelistTradingPairParams{}
err := json.Unmarshal([]byte(proposal.GetDescription()), &delistParams)
if err != nil {
return fmt.Errorf("unmarshal list params error, err=%s", err.Error())
}
if delistParams.BaseAssetSymbol == "" {
return errors.New("base asset symbol should not be empty")
}
if delistParams.QuoteAssetSymbol == "" {
return errors.New("quote asset symbol should not be empty")
}
if delistParams.BaseAssetSymbol == delistParams.QuoteAssetSymbol {
return errors.New("base asset symbol and quote asset symbol should not be the same")
}
if delistParams.Justification == "" {
return errors.New("justification should not be empty")
}
if delistParams.IsExecuted {
return errors.New("is_executed should be false")
}
if err := hooks.orderKeeper.CanDelistTradingPair(ctx, delistParams.BaseAssetSymbol, delistParams.QuoteAssetSymbol); err != nil {
return err
}
return nil
}