-
Notifications
You must be signed in to change notification settings - Fork 38
/
helper.go
82 lines (64 loc) · 1.63 KB
/
helper.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
package commands
import (
"bytes"
"errors"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/bnb-chain/node/common/client"
"github.com/bnb-chain/node/common/types"
"github.com/bnb-chain/node/wire"
)
const miniTokenKeyPrefix = "mini"
type Commander struct {
Cdc *wire.Codec
}
type msgBuilder func(from sdk.AccAddress, symbol string, amount int64) sdk.Msg
func (c Commander) checkAndSendTx(cmd *cobra.Command, args []string, builder msgBuilder) error {
cliCtx, txBuilder := client.PrepareCtx(c.Cdc)
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
symbol := viper.GetString(flagSymbol)
if !types.IsValidMiniTokenSymbol(symbol) {
err = types.ValidateTokenSymbol(symbol)
if err != nil {
return err
}
}
symbol = strings.ToUpper(symbol)
amountStr := viper.GetString(flagAmount)
amount, err := parseAmount(amountStr)
if err != nil {
return err
}
// build message
msg := builder(from, symbol, amount)
return client.SendOrPrintTx(cliCtx, txBuilder, msg)
}
func parseAmount(amountStr string) (int64, error) {
amount, err := strconv.ParseInt(amountStr, 10, 64)
if err != nil {
return 0, err
}
if amount <= 0 {
return amount, errors.New("the amount should be greater than 0")
}
return amount, nil
}
func validateTokenURI(uri string) error {
if len(uri) > 2048 {
return errors.New("uri cannot be longer than 2048 characters")
}
return nil
}
func calcMiniTokenKey(symbol string) []byte {
var buf bytes.Buffer
buf.WriteString(miniTokenKeyPrefix)
buf.WriteString(":")
buf.WriteString(symbol)
return buf.Bytes()
}