-
Notifications
You must be signed in to change notification settings - Fork 41
/
helper.go
66 lines (56 loc) · 1.69 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
package client
import (
"github.com/pkg/errors"
"github.com/cosmos/cosmos-sdk/client/context"
txutils "github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
txbuilder "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/bnb-chain/node/common/types"
)
func PrepareCtx(cdc *codec.Codec) (context.CLIContext, txbuilder.TxBuilder) {
txBldr := txbuilder.NewTxBuilderFromCLI().WithCodec(cdc)
cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(types.GetAccountDecoder(cdc))
return cliCtx, txBldr
}
func EnsureSequence(cliCtx context.CLIContext, txBldr *txbuilder.TxBuilder) error {
from, err := cliCtx.GetFromAddress()
if err != nil {
return err
}
if txBldr.Sequence == 0 {
accSeq, err := cliCtx.GetAccountSequence(from)
if err != nil {
return err
}
*txBldr = txBldr.WithSequence(accSeq)
}
return nil
}
func BuildUnsignedTx(builder txbuilder.TxBuilder, acc sdk.Account, msgs []sdk.Msg) (*[]byte, error) {
chainID := builder.ChainID
if chainID == "" {
return nil, errors.Errorf("chain ID required but not specified")
}
accnum := acc.GetAccountNumber()
sequence := acc.GetSequence()
memo := builder.Memo
signMsg := txbuilder.StdSignMsg{
ChainID: chainID,
AccountNumber: accnum,
Sequence: sequence,
Msgs: msgs,
Memo: memo,
}
// sign and build
bz := signMsg.Bytes()
return &bz, nil
}
func SendOrPrintTx(ctx context.CLIContext, builder txbuilder.TxBuilder, msg sdk.Msg) error {
if ctx.GenerateOnly {
return txutils.PrintUnsignedStdTx(builder, ctx, []sdk.Msg{msg})
}
return txutils.CompleteAndBroadcastTxCli(builder, ctx, []sdk.Msg{msg})
}