Add AutoSequence to all transaction-sending commands

This commit is contained in:
Christopher Goes 2018-04-09 18:14:33 +02:00
parent bd626ba94e
commit 7383c99026
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
6 changed files with 84 additions and 18 deletions

View File

@ -51,15 +51,17 @@ func NewCoreContextFromViper() core.CoreContext {
// Automatically set sequence number
func AutoSequence(ctx core.CoreContext) (core.CoreContext, error) {
from, err := ctx.GetFromAddress()
if err != nil {
return ctx, err
if !viper.IsSet(client.FlagSequence) {
from, err := ctx.GetFromAddress()
if err != nil {
return ctx, err
}
seq, err := ctx.NextSequence(from)
if err != nil {
return ctx, err
}
fmt.Printf("Defaulting to next sequence number: %d\n", seq)
ctx = ctx.WithSequence(seq)
}
seq, err := ctx.NextSequence(from)
if err != nil {
return ctx, err
}
fmt.Printf("Defaulting to next sequence number: %d\n", seq)
ctx = ctx.WithSequence(seq)
return ctx, nil
}

View File

@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/examples/democoin/x/cool"
)
@ -24,7 +25,7 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag
from, err := ctx.GetFromAddress()
@ -38,6 +39,12 @@ func QuizTxCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := viper.GetString(client.FlagName)
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
// build and sign the transaction, then broadcast to Tendermint
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {
@ -60,7 +67,7 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
return errors.New("You must provide an answer")
}
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// get the from address from the name flag
from, err := ctx.GetFromAddress()
@ -71,6 +78,12 @@ func SetTrendTxCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := viper.GetString(client.FlagName)
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
// create the message
msg := cool.NewSetTrendMsg(from, args[0])

View File

@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/examples/democoin/x/pow"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
)
func MineCmd(cdc *wire.Codec) *cobra.Command {
@ -24,7 +25,7 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
// get from address and parse arguments
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
from, err := ctx.GetFromAddress()
if err != nil {
@ -53,6 +54,12 @@ func MineCmd(cdc *wire.Codec) *cobra.Command {
// get account name
name := ctx.FromAddressName
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
// build and sign the transaction, then broadcast to Tendermint
res, err := ctx.SignBuildBroadcast(name, msg, cdc)
if err != nil {

View File

@ -13,6 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
wire "github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/ibc"
)
@ -39,7 +40,7 @@ type sendCommander struct {
}
func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error {
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(c.cdc))
// get the from address
from, err := ctx.GetFromAddress()
@ -53,6 +54,12 @@ func (c sendCommander) sendIBCTransfer(cmd *cobra.Command, args []string) error
return err
}
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
// get password
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, c.cdc)
if err != nil {

View File

@ -12,6 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/simplestake"
)
@ -94,7 +95,14 @@ func (co commander) unbondTxCmd(cmd *cobra.Command, args []string) error {
}
func (co commander) sendMsg(msg sdk.Msg) error {
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(co.cdc))
// default to next sequence number if none provided
ctx, err := context.AutoSequence(ctx)
if err != nil {
return err
}
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, co.cdc)
if err != nil {
return err

View File

@ -14,6 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/commands"
"github.com/cosmos/cosmos-sdk/x/stake"
)
@ -92,7 +93,14 @@ func GetCmdDeclareCandidacy(cdc *wire.Codec) *cobra.Command {
msg := stake.NewMsgDeclareCandidacy(candidateAddr, pk, amount, description)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
@ -129,7 +137,14 @@ func GetCmdEditCandidacy(cdc *wire.Codec) *cobra.Command {
msg := stake.NewMsgEditCandidacy(candidateAddr, description)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
@ -165,7 +180,14 @@ func GetCmdDelegate(cdc *wire.Codec) *cobra.Command {
msg := stake.NewMsgDelegate(delegatorAddr, candidateAddr, amount)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err
@ -212,7 +234,14 @@ func GetCmdUnbond(cdc *wire.Codec) *cobra.Command {
msg := stake.NewMsgUnbond(delegatorAddr, candidateAddr, sharesStr)
// build and sign the transaction, then broadcast to Tendermint
ctx := context.NewCoreContextFromViper()
ctx := context.NewCoreContextFromViper().WithDecoder(authcmd.GetAccountDecoder(cdc))
// default to next sequence number if none provided
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
res, err := ctx.SignBuildBroadcast(ctx.FromAddressName, msg, cdc)
if err != nil {
return err