2018-04-02 05:05:23 -07:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2018-04-09 08:59:01 -07:00
|
|
|
"fmt"
|
2018-04-18 12:39:32 -07:00
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2018-04-06 09:08:29 -07:00
|
|
|
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
2018-04-03 05:11:49 -07:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
2018-04-06 09:08:29 -07:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-04-03 05:11:49 -07:00
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
)
|
|
|
|
|
2018-04-10 02:15:34 -07:00
|
|
|
// NewCoreContextFromViper - return a new context with parameters from the command line
|
2018-04-18 12:39:32 -07:00
|
|
|
func NewCoreContextFromViper() CoreContext {
|
2018-04-03 05:11:49 -07:00
|
|
|
nodeURI := viper.GetString(client.FlagNode)
|
|
|
|
var rpc rpcclient.Client
|
|
|
|
if nodeURI != "" {
|
|
|
|
rpc = rpcclient.NewHTTP(nodeURI, "/websocket")
|
|
|
|
}
|
2018-04-06 09:08:29 -07:00
|
|
|
chainID := viper.GetString(client.FlagChainID)
|
2018-04-10 02:15:34 -07:00
|
|
|
// if chain ID is not specified manually, read default chain ID
|
2018-04-06 09:08:29 -07:00
|
|
|
if chainID == "" {
|
2018-04-10 02:15:34 -07:00
|
|
|
def, err := defaultChainID()
|
|
|
|
if err != nil {
|
|
|
|
chainID = def
|
2018-04-06 09:08:29 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 17:07:03 -07:00
|
|
|
// TODO: Remove the following deprecation code after Gaia-7000 is launched
|
|
|
|
keyName := viper.GetString(client.FlagName)
|
|
|
|
if keyName != "" {
|
|
|
|
fmt.Println("** Note --name is deprecated and will be removed next release. Please use --from instead **")
|
|
|
|
} else {
|
|
|
|
keyName = viper.GetString(client.FlagFrom)
|
|
|
|
}
|
2018-04-18 12:39:32 -07:00
|
|
|
return CoreContext{
|
2018-04-06 09:08:29 -07:00
|
|
|
ChainID: chainID,
|
2018-04-02 05:05:23 -07:00
|
|
|
Height: viper.GetInt64(client.FlagHeight),
|
2018-06-08 21:44:08 -07:00
|
|
|
Gas: viper.GetInt64(client.FlagGas),
|
2018-06-29 06:37:14 -07:00
|
|
|
Fee: viper.GetString(client.FlagFee),
|
2018-04-02 05:05:23 -07:00
|
|
|
TrustNode: viper.GetBool(client.FlagTrustNode),
|
2018-07-05 17:07:03 -07:00
|
|
|
FromAddressName: keyName,
|
2018-04-03 05:11:49 -07:00
|
|
|
NodeURI: nodeURI,
|
2018-06-11 19:30:54 -07:00
|
|
|
AccountNumber: viper.GetInt64(client.FlagAccountNumber),
|
2018-04-02 05:05:23 -07:00
|
|
|
Sequence: viper.GetInt64(client.FlagSequence),
|
2018-06-20 12:27:36 -07:00
|
|
|
Memo: viper.GetString(client.FlagMemo),
|
2018-04-03 05:11:49 -07:00
|
|
|
Client: rpc,
|
2018-04-09 07:06:24 -07:00
|
|
|
Decoder: nil,
|
2018-05-03 09:35:12 -07:00
|
|
|
AccountStore: "acc",
|
2018-06-28 17:54:47 -07:00
|
|
|
UseLedger: viper.GetBool(client.FlagUseLedger),
|
2018-07-05 20:05:17 -07:00
|
|
|
Async: viper.GetBool(client.FlagAsync),
|
2018-07-05 20:42:41 -07:00
|
|
|
JSON: viper.GetBool(client.FlagJson),
|
2018-07-05 22:19:50 -07:00
|
|
|
PrintResponse: viper.GetBool(client.FlagPrintResponse),
|
2018-04-02 05:05:23 -07:00
|
|
|
}
|
|
|
|
}
|
2018-04-09 08:59:01 -07:00
|
|
|
|
2018-04-10 02:15:34 -07:00
|
|
|
// read chain ID from genesis file, if present
|
|
|
|
func defaultChainID() (string, error) {
|
|
|
|
cfg, err := tcmd.ParseConfig()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-04-11 08:47:56 -07:00
|
|
|
doc, err := tmtypes.GenesisDocFromFile(cfg.GenesisFile())
|
2018-04-10 02:15:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return doc.ChainID, nil
|
|
|
|
}
|
|
|
|
|
2018-06-28 12:23:47 -07:00
|
|
|
// EnsureAccount - automatically set account number if none provided
|
2018-06-11 19:30:54 -07:00
|
|
|
func EnsureAccountNumber(ctx CoreContext) (CoreContext, error) {
|
|
|
|
// Should be viper.IsSet, but this does not work - https://github.com/spf13/viper/pull/331
|
|
|
|
if viper.GetInt64(client.FlagAccountNumber) != 0 {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
from, err := ctx.GetFromAddress()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
accnum, err := ctx.GetAccountNumber(from)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
fmt.Printf("Defaulting to account number: %d\n", accnum)
|
|
|
|
ctx = ctx.WithAccountNumber(accnum)
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 02:15:34 -07:00
|
|
|
// EnsureSequence - automatically set sequence number if none provided
|
2018-04-18 12:39:32 -07:00
|
|
|
func EnsureSequence(ctx CoreContext) (CoreContext, error) {
|
2018-05-03 09:35:12 -07:00
|
|
|
// Should be viper.IsSet, but this does not work - https://github.com/spf13/viper/pull/331
|
|
|
|
if viper.GetInt64(client.FlagSequence) != 0 {
|
2018-04-10 02:15:34 -07:00
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
from, err := ctx.GetFromAddress()
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
seq, err := ctx.NextSequence(from)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, err
|
2018-04-09 08:59:01 -07:00
|
|
|
}
|
2018-04-10 02:15:34 -07:00
|
|
|
fmt.Printf("Defaulting to next sequence number: %d\n", seq)
|
|
|
|
ctx = ctx.WithSequence(seq)
|
2018-04-09 08:59:01 -07:00
|
|
|
return ctx, nil
|
|
|
|
}
|