Default to next sequence number (closes #807)
This commit is contained in:
parent
0be655b122
commit
ec98545a1b
|
@ -43,5 +43,7 @@ func NewCoreContextFromViper() core.CoreContext {
|
||||||
NodeURI: nodeURI,
|
NodeURI: nodeURI,
|
||||||
Sequence: viper.GetInt64(client.FlagSequence),
|
Sequence: viper.GetInt64(client.FlagSequence),
|
||||||
Client: rpc,
|
Client: rpc,
|
||||||
|
Decoder: nil,
|
||||||
|
AccountStore: "main",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
||||||
|
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CoreContext struct {
|
type CoreContext struct {
|
||||||
|
@ -12,6 +14,8 @@ type CoreContext struct {
|
||||||
FromAddressName string
|
FromAddressName string
|
||||||
Sequence int64
|
Sequence int64
|
||||||
Client rpcclient.Client
|
Client rpcclient.Client
|
||||||
|
Decoder sdk.AccountDecoder
|
||||||
|
AccountStore string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CoreContext) WithChainID(chainID string) CoreContext {
|
func (c CoreContext) WithChainID(chainID string) CoreContext {
|
||||||
|
@ -48,3 +52,13 @@ func (c CoreContext) WithClient(client rpcclient.Client) CoreContext {
|
||||||
c.Client = client
|
c.Client = client
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c CoreContext) WithDecoder(decoder sdk.AccountDecoder) CoreContext {
|
||||||
|
c.Decoder = decoder
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c CoreContext) WithAccountStore(accountStore string) CoreContext {
|
||||||
|
c.AccountStore = accountStore
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
|
@ -140,6 +140,24 @@ func (ctx CoreContext) SignBuildBroadcast(name string, msg sdk.Msg, cdc *wire.Co
|
||||||
return ctx.BroadcastTx(txBytes)
|
return ctx.BroadcastTx(txBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c CoreContext) NextSequence(address []byte) (int64, error) {
|
||||||
|
if c.Decoder == nil {
|
||||||
|
return 0, errors.New("AccountDecoder required but not provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := c.Query(address, c.AccountStore)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
account, err := c.Decoder(res)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return account.GetSequence(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// get passphrase from std input
|
// get passphrase from std input
|
||||||
func (ctx CoreContext) GetPassphraseFromStdin(name string) (pass string, err error) {
|
func (ctx CoreContext) GetPassphraseFromStdin(name string) (pass string, err error) {
|
||||||
buf := client.BufferStdin()
|
buf := client.BufferStdin()
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/client/context"
|
"github.com/cosmos/cosmos-sdk/client/context"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
|
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
|
||||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -36,7 +37,7 @@ type Commander struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
|
func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
|
||||||
ctx := context.NewCoreContextFromViper()
|
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(c.Cdc))
|
||||||
|
|
||||||
// get the from address
|
// get the from address
|
||||||
from, err := ctx.GetFromAddress()
|
from, err := ctx.GetFromAddress()
|
||||||
|
@ -62,6 +63,20 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
|
||||||
// build message
|
// build message
|
||||||
msg := BuildMsg(from, to, coins)
|
msg := BuildMsg(from, to, coins)
|
||||||
|
|
||||||
|
// default to next sequence number if none provided
|
||||||
|
if viper.GetInt64(client.FlagSequence) == 0 {
|
||||||
|
from, err := ctx.GetFromAddress()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
seq, err := ctx.NextSequence(from)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Printf("Defaulting to next sequence number: %d\n", seq)
|
||||||
|
ctx = ctx.WithSequence(seq)
|
||||||
|
}
|
||||||
|
|
||||||
// build and sign the transaction, then broadcast to Tendermint
|
// build and sign the transaction, then broadcast to Tendermint
|
||||||
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.Cdc)
|
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.Cdc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue