2020-03-24 12:55:52 -07:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
2020-06-30 13:59:21 -07:00
|
|
|
"github.com/spf13/pflag"
|
2020-03-24 13:36:12 -07:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2020-03-24 13:51:59 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-25 11:30:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-03-24 13:51:59 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-06-16 12:57:37 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
2020-03-24 12:55:52 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Factory defines a client transaction factory that facilitates generating and
|
|
|
|
// signing an application-specific transaction.
|
|
|
|
type Factory struct {
|
2020-04-08 02:38:28 -07:00
|
|
|
keybase keyring.Keyring
|
2020-07-20 05:30:12 -07:00
|
|
|
txConfig client.TxConfig
|
2020-06-01 05:46:03 -07:00
|
|
|
accountRetriever client.AccountRetriever
|
2020-03-24 12:55:52 -07:00
|
|
|
accountNumber uint64
|
|
|
|
sequence uint64
|
|
|
|
gas uint64
|
2020-08-07 16:32:22 -07:00
|
|
|
timeoutHeight uint64
|
2020-03-24 12:55:52 -07:00
|
|
|
gasAdjustment float64
|
|
|
|
chainID string
|
|
|
|
memo string
|
|
|
|
fees sdk.Coins
|
|
|
|
gasPrices sdk.DecCoins
|
2020-06-16 12:57:37 -07:00
|
|
|
signMode signing.SignMode
|
|
|
|
simulateAndExecute bool
|
2020-03-24 12:55:52 -07:00
|
|
|
}
|
|
|
|
|
2020-12-11 05:54:50 -08:00
|
|
|
// NewFactoryCLI creates a new Factory.
|
2020-06-30 13:59:21 -07:00
|
|
|
func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) Factory {
|
2020-12-11 05:54:50 -08:00
|
|
|
signModeStr := clientCtx.SignModeStr
|
2020-06-30 13:59:21 -07:00
|
|
|
|
|
|
|
signMode := signing.SignMode_SIGN_MODE_UNSPECIFIED
|
|
|
|
switch signModeStr {
|
2020-12-11 05:54:50 -08:00
|
|
|
case flags.SignModeDirect:
|
2020-06-30 13:59:21 -07:00
|
|
|
signMode = signing.SignMode_SIGN_MODE_DIRECT
|
2020-12-11 05:54:50 -08:00
|
|
|
case flags.SignModeLegacyAminoJSON:
|
2020-06-30 13:59:21 -07:00
|
|
|
signMode = signing.SignMode_SIGN_MODE_LEGACY_AMINO_JSON
|
|
|
|
}
|
|
|
|
|
|
|
|
accNum, _ := flagSet.GetUint64(flags.FlagAccountNumber)
|
|
|
|
accSeq, _ := flagSet.GetUint64(flags.FlagSequence)
|
|
|
|
gasAdj, _ := flagSet.GetFloat64(flags.FlagGasAdjustment)
|
|
|
|
memo, _ := flagSet.GetString(flags.FlagMemo)
|
2020-08-07 16:32:22 -07:00
|
|
|
timeoutHeight, _ := flagSet.GetUint64(flags.FlagTimeoutHeight)
|
2020-06-30 13:59:21 -07:00
|
|
|
|
2020-07-11 01:13:46 -07:00
|
|
|
gasStr, _ := flagSet.GetString(flags.FlagGas)
|
|
|
|
gasSetting, _ := flags.ParseGasSetting(gasStr)
|
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
f := Factory{
|
2020-07-20 05:30:12 -07:00
|
|
|
txConfig: clientCtx.TxConfig,
|
2020-06-30 13:59:21 -07:00
|
|
|
accountRetriever: clientCtx.AccountRetriever,
|
|
|
|
keybase: clientCtx.Keyring,
|
|
|
|
chainID: clientCtx.ChainID,
|
2020-07-11 01:13:46 -07:00
|
|
|
gas: gasSetting.Gas,
|
|
|
|
simulateAndExecute: gasSetting.Simulate,
|
2020-06-30 13:59:21 -07:00
|
|
|
accountNumber: accNum,
|
|
|
|
sequence: accSeq,
|
2020-08-07 16:32:22 -07:00
|
|
|
timeoutHeight: timeoutHeight,
|
2020-06-30 13:59:21 -07:00
|
|
|
gasAdjustment: gasAdj,
|
|
|
|
memo: memo,
|
|
|
|
signMode: signMode,
|
|
|
|
}
|
|
|
|
|
|
|
|
feesStr, _ := flagSet.GetString(flags.FlagFees)
|
|
|
|
f = f.WithFees(feesStr)
|
|
|
|
|
|
|
|
gasPricesStr, _ := flagSet.GetString(flags.FlagGasPrices)
|
|
|
|
f = f.WithGasPrices(gasPricesStr)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
func (f Factory) AccountNumber() uint64 { return f.accountNumber }
|
|
|
|
func (f Factory) Sequence() uint64 { return f.sequence }
|
|
|
|
func (f Factory) Gas() uint64 { return f.gas }
|
|
|
|
func (f Factory) GasAdjustment() float64 { return f.gasAdjustment }
|
|
|
|
func (f Factory) Keybase() keyring.Keyring { return f.keybase }
|
|
|
|
func (f Factory) ChainID() string { return f.chainID }
|
|
|
|
func (f Factory) Memo() string { return f.memo }
|
|
|
|
func (f Factory) Fees() sdk.Coins { return f.fees }
|
|
|
|
func (f Factory) GasPrices() sdk.DecCoins { return f.gasPrices }
|
|
|
|
func (f Factory) AccountRetriever() client.AccountRetriever { return f.accountRetriever }
|
2020-08-07 16:32:22 -07:00
|
|
|
func (f Factory) TimeoutHeight() uint64 { return f.timeoutHeight }
|
2020-03-24 12:55:52 -07:00
|
|
|
|
|
|
|
// SimulateAndExecute returns the option to simulate and then execute the transaction
|
|
|
|
// using the gas from the simulation results
|
|
|
|
func (f Factory) SimulateAndExecute() bool { return f.simulateAndExecute }
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
// WithTxConfig returns a copy of the Factory with an updated TxConfig.
|
|
|
|
func (f Factory) WithTxConfig(g client.TxConfig) Factory {
|
|
|
|
f.txConfig = g
|
2020-03-24 12:55:52 -07:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithAccountRetriever returns a copy of the Factory with an updated AccountRetriever.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (f Factory) WithAccountRetriever(ar client.AccountRetriever) Factory {
|
2020-03-24 12:55:52 -07:00
|
|
|
f.accountRetriever = ar
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithChainID returns a copy of the Factory with an updated chainID.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithChainID(chainID string) Factory {
|
|
|
|
f.chainID = chainID
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithGas returns a copy of the Factory with an updated gas value.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithGas(gas uint64) Factory {
|
|
|
|
f.gas = gas
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithFees returns a copy of the Factory with an updated fee.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithFees(fees string) Factory {
|
2020-11-16 03:34:54 -08:00
|
|
|
parsedFees, err := sdk.ParseCoinsNormalized(fees)
|
2020-03-24 12:55:52 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.fees = parsedFees
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithGasPrices returns a copy of the Factory with updated gas prices.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithGasPrices(gasPrices string) Factory {
|
|
|
|
parsedGasPrices, err := sdk.ParseDecCoins(gasPrices)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.gasPrices = parsedGasPrices
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithKeybase returns a copy of the Factory with updated Keybase.
|
2020-04-08 02:38:28 -07:00
|
|
|
func (f Factory) WithKeybase(keybase keyring.Keyring) Factory {
|
2020-03-24 12:55:52 -07:00
|
|
|
f.keybase = keybase
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithSequence returns a copy of the Factory with an updated sequence number.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithSequence(sequence uint64) Factory {
|
|
|
|
f.sequence = sequence
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithMemo returns a copy of the Factory with an updated memo.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithMemo(memo string) Factory {
|
2020-03-25 12:15:43 -07:00
|
|
|
f.memo = memo
|
2020-03-24 12:55:52 -07:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2020-03-25 11:23:34 -07:00
|
|
|
// WithAccountNumber returns a copy of the Factory with an updated account number.
|
2020-03-24 12:55:52 -07:00
|
|
|
func (f Factory) WithAccountNumber(accnum uint64) Factory {
|
|
|
|
f.accountNumber = accnum
|
|
|
|
return f
|
|
|
|
}
|
2020-03-25 11:23:34 -07:00
|
|
|
|
|
|
|
// WithGasAdjustment returns a copy of the Factory with an updated gas adjustment.
|
|
|
|
func (f Factory) WithGasAdjustment(gasAdj float64) Factory {
|
|
|
|
f.gasAdjustment = gasAdj
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSimulateAndExecute returns a copy of the Factory with an updated gas
|
|
|
|
// simulation value.
|
|
|
|
func (f Factory) WithSimulateAndExecute(sim bool) Factory {
|
|
|
|
f.simulateAndExecute = sim
|
|
|
|
return f
|
|
|
|
}
|
2020-07-29 15:33:42 -07:00
|
|
|
|
|
|
|
// SignMode returns the sign mode configured in the Factory
|
|
|
|
func (f Factory) SignMode() signing.SignMode {
|
|
|
|
return f.signMode
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSignMode returns a copy of the Factory with an updated sign mode value.
|
|
|
|
func (f Factory) WithSignMode(mode signing.SignMode) Factory {
|
|
|
|
f.signMode = mode
|
|
|
|
return f
|
|
|
|
}
|
2020-08-07 16:32:22 -07:00
|
|
|
|
|
|
|
// WithTimeoutHeight returns a copy of the Factory with an updated timeout height.
|
|
|
|
func (f Factory) WithTimeoutHeight(height uint64) Factory {
|
|
|
|
f.timeoutHeight = height
|
|
|
|
return f
|
|
|
|
}
|