Helper function

This commit is contained in:
Christopher Goes 2018-04-09 17:59:01 +02:00
parent ec98545a1b
commit bd626ba94e
No known key found for this signature in database
GPG Key ID: E828D98232D328D3
2 changed files with 19 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package context
import (
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io/ioutil"
@ -47,3 +48,18 @@ func NewCoreContextFromViper() core.CoreContext {
AccountStore: "main",
}
}
// Automatically set sequence number
func AutoSequence(ctx core.CoreContext) (core.CoreContext, error) {
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)
return ctx, nil
}

View File

@ -64,17 +64,9 @@ func (c Commander) sendTxCmd(cmd *cobra.Command, args []string) error {
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)
ctx, err = context.AutoSequence(ctx)
if err != nil {
return err
}
// build and sign the transaction, then broadcast to Tendermint