-
Notifications
You must be signed in to change notification settings - Fork 40
/
msg.go
69 lines (59 loc) · 1.98 KB
/
msg.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
package burn
import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/bnb-chain/node/common/types"
"github.com/bnb-chain/node/common/upgrade"
)
// TODO: "route expressions can only contain alphanumeric characters", we need to change the cosmos sdk to support slash
// const BurnRoute = "tokens/burn"
const BurnRoute = "tokensBurn"
var _ sdk.Msg = BurnMsg{}
type BurnMsg struct {
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
}
func NewMsg(from sdk.AccAddress, symbol string, amount int64) BurnMsg {
return BurnMsg{
From: from,
Symbol: symbol,
Amount: amount,
}
}
func (msg BurnMsg) Route() string { return BurnRoute }
func (msg BurnMsg) Type() string { return BurnRoute }
func (msg BurnMsg) String() string {
return fmt.Sprintf("BurnMsg{%v#%v%v}", msg.From, msg.Amount, msg.Symbol)
}
func (msg BurnMsg) GetInvolvedAddresses() []sdk.AccAddress { return msg.GetSigners() }
func (msg BurnMsg) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{msg.From} }
// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
func (msg BurnMsg) ValidateBasic() sdk.Error {
if sdk.IsUpgrade(upgrade.BEP8) && types.IsValidMiniTokenSymbol(msg.Symbol) {
if msg.Amount <= 0 {
return sdk.ErrInsufficientFunds("amount should be more than 0")
}
return nil
}
// if BEP8 not upgraded, we rely on `ValidateTokenSymbol` rejecting the MiniToken.
// expect all msgs that reference a token after issue to use the suffixed form (e.g. "BNB-ABC")
err := types.ValidateTokenSymbol(msg.Symbol)
if err != nil {
return sdk.ErrInvalidCoins(err.Error())
}
if msg.Amount <= 0 {
// TODO: maybe we need to define our own errors
return sdk.ErrInsufficientFunds("amount should be more than 0")
}
return nil
}
func (msg BurnMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg) // XXX: ensure some canonical form
if err != nil {
panic(err)
}
return b
}