2020-06-01 05:46:03 -07:00
|
|
|
package client
|
2018-08-06 11:11:30 -07:00
|
|
|
|
|
|
|
import (
|
2021-08-09 03:35:01 -07:00
|
|
|
"bufio"
|
2018-09-02 11:20:14 -07:00
|
|
|
"io"
|
2018-10-03 08:48:23 -07:00
|
|
|
"os"
|
2018-09-02 11:20:14 -07:00
|
|
|
|
2021-03-31 03:04:33 -07:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2021-09-24 07:37:34 -07:00
|
|
|
"sigs.k8s.io/yaml"
|
2020-08-11 00:19:49 -07:00
|
|
|
|
2020-09-25 01:41:16 -07:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2018-08-30 00:52:17 -07:00
|
|
|
rpcclient "github.com/tendermint/tendermint/rpc/client"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2020-07-21 08:28:43 -07:00
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2019-01-29 11:22:47 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-08-06 11:11:30 -07:00
|
|
|
)
|
|
|
|
|
2020-06-15 10:39:09 -07:00
|
|
|
// Context implements a typical context created in SDK modules for transaction
|
|
|
|
// handling and queries.
|
2020-06-01 05:46:03 -07:00
|
|
|
type Context struct {
|
2021-07-01 01:52:38 -07:00
|
|
|
FromAddress sdk.AccAddress
|
|
|
|
Client rpcclient.Client
|
|
|
|
ChainID string
|
2021-06-11 04:49:39 -07:00
|
|
|
Codec codec.Codec
|
2020-07-03 09:42:12 -07:00
|
|
|
InterfaceRegistry codectypes.InterfaceRegistry
|
|
|
|
Input io.Reader
|
|
|
|
Keyring keyring.Keyring
|
2021-03-18 01:27:03 -07:00
|
|
|
KeyringOptions []keyring.Option
|
2020-07-03 09:42:12 -07:00
|
|
|
Output io.Writer
|
|
|
|
OutputFormat string
|
|
|
|
Height int64
|
|
|
|
HomeDir string
|
2020-10-08 10:41:35 -07:00
|
|
|
KeyringDir string
|
2020-07-03 09:42:12 -07:00
|
|
|
From string
|
|
|
|
BroadcastMode string
|
|
|
|
FromName string
|
2020-12-11 05:54:50 -08:00
|
|
|
SignModeStr string
|
2020-07-03 09:42:12 -07:00
|
|
|
UseLedger bool
|
|
|
|
Simulate bool
|
|
|
|
GenerateOnly bool
|
|
|
|
Offline bool
|
|
|
|
SkipConfirm bool
|
2020-07-20 05:30:12 -07:00
|
|
|
TxConfig TxConfig
|
2020-07-03 09:42:12 -07:00
|
|
|
AccountRetriever AccountRetriever
|
|
|
|
NodeURI string
|
2021-12-01 03:47:40 -08:00
|
|
|
FeePayer sdk.AccAddress
|
Add fee grant module (#8061)
* Add docs
* Add BasicFeeAllowance implementation
* Add expiration structs and complete basic fee
* Add delegation messages, add validation logic
* Add keeper and helper structs
* Add alias and handler to top level
* Add delegation module
* Add basic querier
* Add types tests
* Add types tests
* More internal test coverage
* Solid internal test coverage
* Expose Querier to top level module
* Add FeeAccount to auth/types, like StdTx, SignDoc
* Fix all tests in x/auth
* All tests pass
* Appease the Golang Linter
* Add fee-account command line flag
* Start on DelegatedDeductFeeDecorator
* Cleanup the Decorator
* Wire up delegation module in simapp
* add basic test for decorator (no delegation)
* Table tests for deduct fees
* Table tests over all conditions of delegated fee decorator
* Build full ante handler stack and test it
* Start genesis
* Implement Genesis
* Rename package delegation to subkeys
* Clarify antes test cases, handle empty account w/o fees
* Allow paying delegated fees with no account
* Pull mempool into delegated ante, for control on StdFee
* Use custom DelegatedTx, DelegatedFee for subkeys
* Revert all changes to x/auth.StdTx
* Appease scopelint
* Register DelegatedTx with codec
* Address PR comments
* Remove unnecessary DelegatedMempoolFeeDecorator
* Cleaned up errors in querier
* Clean up message sign bytes
* Minor PR comments
* Replace GetAllFees... with Iterator variants
* PrepareForExport adjusts grant expiration height
* Panic on de/serialization error in keeper
* Move custom ante handler chain to tests, update docs
* More cleanup
* More doc cleanup
* Renamed subkeys module to fee_grant
* Rename subkeys/delegation to fee grant in all strings
* Modify Msg and Keeper methods to use Grant not Delegate
* Add PeriodicFeeAllowance
* Update aliases
* Cover all accept cases for PeriodicFeeAllowance
* Et tu scopelint?
* Update docs as requested
* Remove error return from GetFeeGrant
* Code cleanup as requested by PR
* Updated all errors to use new sdk/errors package
* Use test suite for keeper tests
* Clean up alias.go file
* Define expected interfaces in exported, rather than importing from account
* Remove dependency on auth/ante
* Improve godoc, Logger
* Cleaned up ExpiresAt
* Improve error reporting with UseGrantedFee
* Enforce period limit subset of basic limit
* Add events
* Rename fee_grant to feegrant
* Ensure KeeperTestSuite actually runs
* Move types/tx to types
* Update alias file, include ante
* I do need nolint in alias.go
* Properly emit events in the handler. Use cosmos-sdk in amino types
* Update godoc
* Linting...
* Update errors
* Update pkg doc and fix ante-handler order
* Merge PR #5782: Migrate x/feegrant to proto
* fix errors
* proto changes
* proto changes
* fix errors
* fix errors
* genesis state changed to proto
* fix keeper tests
* fix test
* fixed tests
* fix tests
* updated expected keepers
* updated ante tests
* lint
* deleted alias.go
* tx updated to proto tx
* remove explicit signmode
* tests
* Added `cli/query.go`
* Added tx.go in cli
* updated `module.go`
* resolve errors in tx.go
* Add fee payer gentx func
* updated tx
* fixed error
* WIP: cli tests
* fix query error
* fix tests
* Unused types and funcs
* fix tests
* rename helper func to create tx
* remove unused
* update tx cfg
* fix cli tests
* added simulations
* Add `decoder.go`
* fix build fail
* added init genesis code
* update tx.go
* fixed LGTM alert
* modified cli
* remove gogoproto extensions
* change acc address type to string
* lint
* fix simulations
* Add gen simulations
* remove legacy querier
* remove legacy code
* add grpc queries tests
* fix simulations
* update module.go
* lint
* register feegrant NewSimulationManager
* fix sims
* fix sims
* add genesis test
* add periodic grant
* updated cmd
* changed times
* updated flags
* removed days as period clock
* added condition for period and exp
* add periodic fee cli tests
* udpated tests
* fix lint
* fix tests
* fix sims
* renaming to `fee_grant`
* review changes
* fix test
* add condition for duplicate grants
* fix tests
* add `genTxWithFeeGranter` in tests
* fix simulation
* one of changes & test fixes
* fix test
* fix lint
* changed package name `feegrant` to `fee_grant`
* review comments
* review changes
* review change
* review changes
* added fee-account in flags
* address review changes
* read fee granter from cli
* updated create account with mnemonic
* Address review comments
* move `simapp/ante` file to `feegrant/ante`
* update keeper logic to create account
* update docs
* fix tests
* update `serviceMsgClientConn` from `msgservice`
* review changes
* add test case for using more fees than allowed
* eliminate panic checks from keeper
* fix lint
* change store keys string to bytes
* fix tests
* review changes
* review changes
* udpate docs
* make spend limit optional
* fix tests
* fix tests
* review changes
* add norace tag
* proto-docs
* add docs
Co-authored-by: Ethan Frey <ethanfrey@users.noreply.github.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
Co-authored-by: aleem1413 <aleem@vitwit.com>
Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-01-29 11:54:51 -08:00
|
|
|
FeeGranter sdk.AccAddress
|
2021-03-31 03:04:33 -07:00
|
|
|
Viper *viper.Viper
|
2021-12-07 10:02:21 -08:00
|
|
|
|
|
|
|
// IsAux is true when the signer is an auxiliary signer (e.g. the tipper).
|
|
|
|
IsAux bool
|
2020-06-15 10:39:09 -07:00
|
|
|
|
2020-03-13 10:59:14 -07:00
|
|
|
// TODO: Deprecated (remove).
|
2020-08-10 12:41:21 -07:00
|
|
|
LegacyAmino *codec.LegacyAmino
|
2018-08-06 11:11:30 -07:00
|
|
|
}
|
|
|
|
|
2020-04-08 02:38:28 -07:00
|
|
|
// WithKeyring returns a copy of the context with an updated keyring.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithKeyring(k keyring.Keyring) Context {
|
2020-04-08 02:38:28 -07:00
|
|
|
ctx.Keyring = k
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-03-18 01:27:03 -07:00
|
|
|
// WithKeyringOptions returns a copy of the context with an updated keyring.
|
|
|
|
func (ctx Context) WithKeyringOptions(opts ...keyring.Option) Context {
|
|
|
|
ctx.KeyringOptions = opts
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
// WithInput returns a copy of the context with an updated input.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithInput(r io.Reader) Context {
|
2021-08-09 03:35:01 -07:00
|
|
|
// convert to a bufio.Reader to have a shared buffer between the keyring and the
|
|
|
|
// the Commands, ensuring a read from one advance the read pointer for the other.
|
|
|
|
// see https://github.com/cosmos/cosmos-sdk/issues/9566.
|
|
|
|
ctx.Input = bufio.NewReader(r)
|
2019-11-14 06:17:21 -08:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-06-11 04:49:39 -07:00
|
|
|
// WithCodec returns a copy of the Context with an updated Codec.
|
|
|
|
func (ctx Context) WithCodec(m codec.Codec) Context {
|
|
|
|
ctx.Codec = m
|
2020-03-13 10:59:14 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
// WithLegacyAmino returns a copy of the context with an updated LegacyAmino codec.
|
2020-03-13 10:59:14 -07:00
|
|
|
// TODO: Deprecated (remove).
|
2020-08-10 12:41:21 -07:00
|
|
|
func (ctx Context) WithLegacyAmino(cdc *codec.LegacyAmino) Context {
|
|
|
|
ctx.LegacyAmino = cdc
|
2018-08-06 11:11:30 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-09-28 21:45:54 -07:00
|
|
|
// WithOutput returns a copy of the context with an updated output writer (e.g. stdout).
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithOutput(w io.Writer) Context {
|
2018-09-28 21:45:54 -07:00
|
|
|
ctx.Output = w
|
2018-08-06 11:11:30 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-09-25 13:48:38 -07:00
|
|
|
// WithFrom returns a copy of the context with an updated from address or name.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithFrom(from string) Context {
|
2018-09-25 13:48:38 -07:00
|
|
|
ctx.From = from
|
2018-08-06 11:11:30 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-07-10 07:45:46 -07:00
|
|
|
// WithOutputFormat returns a copy of the context with an updated OutputFormat field.
|
|
|
|
func (ctx Context) WithOutputFormat(format string) Context {
|
|
|
|
ctx.OutputFormat = format
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// WithNodeURI returns a copy of the context with an updated node URI.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithNodeURI(nodeURI string) Context {
|
2018-08-06 11:11:30 -07:00
|
|
|
ctx.NodeURI = nodeURI
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-06-07 06:02:07 -07:00
|
|
|
// WithHeight returns a copy of the context with an updated height.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithHeight(height int64) Context {
|
2019-06-07 06:02:07 -07:00
|
|
|
ctx.Height = height
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-08-06 11:11:30 -07:00
|
|
|
// WithClient returns a copy of the context with an updated RPC client
|
|
|
|
// instance.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithClient(client rpcclient.Client) Context {
|
2018-08-06 11:11:30 -07:00
|
|
|
ctx.Client = client
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithUseLedger returns a copy of the context with an updated UseLedger flag.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithUseLedger(useLedger bool) Context {
|
2018-08-06 11:11:30 -07:00
|
|
|
ctx.UseLedger = useLedger
|
|
|
|
return ctx
|
|
|
|
}
|
2018-08-29 21:50:41 -07:00
|
|
|
|
2019-10-02 17:29:28 -07:00
|
|
|
// WithChainID returns a copy of the context with an updated chain ID.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithChainID(chainID string) Context {
|
2019-10-02 17:29:28 -07:00
|
|
|
ctx.ChainID = chainID
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:39:09 -07:00
|
|
|
// WithHomeDir returns a copy of the Context with HomeDir set.
|
|
|
|
func (ctx Context) WithHomeDir(dir string) Context {
|
2021-03-31 03:04:33 -07:00
|
|
|
if dir != "" {
|
|
|
|
ctx.HomeDir = dir
|
|
|
|
}
|
2020-06-15 10:39:09 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-10-08 10:41:35 -07:00
|
|
|
// WithKeyringDir returns a copy of the Context with KeyringDir set.
|
|
|
|
func (ctx Context) WithKeyringDir(dir string) Context {
|
|
|
|
ctx.KeyringDir = dir
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2018-12-11 06:02:26 -08:00
|
|
|
// WithGenerateOnly returns a copy of the context with updated GenerateOnly value
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithGenerateOnly(generateOnly bool) Context {
|
2018-12-11 06:02:26 -08:00
|
|
|
ctx.GenerateOnly = generateOnly
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithSimulation returns a copy of the context with updated Simulate value
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithSimulation(simulate bool) Context {
|
2018-12-11 06:02:26 -08:00
|
|
|
ctx.Simulate = simulate
|
|
|
|
return ctx
|
|
|
|
}
|
2019-01-22 09:28:39 -08:00
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
// WithOffline returns a copy of the context with updated Offline value.
|
|
|
|
func (ctx Context) WithOffline(offline bool) Context {
|
|
|
|
ctx.Offline = offline
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-01-29 11:22:47 -08:00
|
|
|
// WithFromName returns a copy of the context with an updated from account name.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithFromName(name string) Context {
|
2019-01-29 11:22:47 -08:00
|
|
|
ctx.FromName = name
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFromAddress returns a copy of the context with an updated from account
|
|
|
|
// address.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithFromAddress(addr sdk.AccAddress) Context {
|
2019-01-29 11:22:47 -08:00
|
|
|
ctx.FromAddress = addr
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:47:40 -08:00
|
|
|
// WithFeePayerAddress returns a copy of the context with an updated fee payer account
|
|
|
|
// address.
|
|
|
|
func (ctx Context) WithFeePayerAddress(addr sdk.AccAddress) Context {
|
|
|
|
ctx.FeePayer = addr
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
Add fee grant module (#8061)
* Add docs
* Add BasicFeeAllowance implementation
* Add expiration structs and complete basic fee
* Add delegation messages, add validation logic
* Add keeper and helper structs
* Add alias and handler to top level
* Add delegation module
* Add basic querier
* Add types tests
* Add types tests
* More internal test coverage
* Solid internal test coverage
* Expose Querier to top level module
* Add FeeAccount to auth/types, like StdTx, SignDoc
* Fix all tests in x/auth
* All tests pass
* Appease the Golang Linter
* Add fee-account command line flag
* Start on DelegatedDeductFeeDecorator
* Cleanup the Decorator
* Wire up delegation module in simapp
* add basic test for decorator (no delegation)
* Table tests for deduct fees
* Table tests over all conditions of delegated fee decorator
* Build full ante handler stack and test it
* Start genesis
* Implement Genesis
* Rename package delegation to subkeys
* Clarify antes test cases, handle empty account w/o fees
* Allow paying delegated fees with no account
* Pull mempool into delegated ante, for control on StdFee
* Use custom DelegatedTx, DelegatedFee for subkeys
* Revert all changes to x/auth.StdTx
* Appease scopelint
* Register DelegatedTx with codec
* Address PR comments
* Remove unnecessary DelegatedMempoolFeeDecorator
* Cleaned up errors in querier
* Clean up message sign bytes
* Minor PR comments
* Replace GetAllFees... with Iterator variants
* PrepareForExport adjusts grant expiration height
* Panic on de/serialization error in keeper
* Move custom ante handler chain to tests, update docs
* More cleanup
* More doc cleanup
* Renamed subkeys module to fee_grant
* Rename subkeys/delegation to fee grant in all strings
* Modify Msg and Keeper methods to use Grant not Delegate
* Add PeriodicFeeAllowance
* Update aliases
* Cover all accept cases for PeriodicFeeAllowance
* Et tu scopelint?
* Update docs as requested
* Remove error return from GetFeeGrant
* Code cleanup as requested by PR
* Updated all errors to use new sdk/errors package
* Use test suite for keeper tests
* Clean up alias.go file
* Define expected interfaces in exported, rather than importing from account
* Remove dependency on auth/ante
* Improve godoc, Logger
* Cleaned up ExpiresAt
* Improve error reporting with UseGrantedFee
* Enforce period limit subset of basic limit
* Add events
* Rename fee_grant to feegrant
* Ensure KeeperTestSuite actually runs
* Move types/tx to types
* Update alias file, include ante
* I do need nolint in alias.go
* Properly emit events in the handler. Use cosmos-sdk in amino types
* Update godoc
* Linting...
* Update errors
* Update pkg doc and fix ante-handler order
* Merge PR #5782: Migrate x/feegrant to proto
* fix errors
* proto changes
* proto changes
* fix errors
* fix errors
* genesis state changed to proto
* fix keeper tests
* fix test
* fixed tests
* fix tests
* updated expected keepers
* updated ante tests
* lint
* deleted alias.go
* tx updated to proto tx
* remove explicit signmode
* tests
* Added `cli/query.go`
* Added tx.go in cli
* updated `module.go`
* resolve errors in tx.go
* Add fee payer gentx func
* updated tx
* fixed error
* WIP: cli tests
* fix query error
* fix tests
* Unused types and funcs
* fix tests
* rename helper func to create tx
* remove unused
* update tx cfg
* fix cli tests
* added simulations
* Add `decoder.go`
* fix build fail
* added init genesis code
* update tx.go
* fixed LGTM alert
* modified cli
* remove gogoproto extensions
* change acc address type to string
* lint
* fix simulations
* Add gen simulations
* remove legacy querier
* remove legacy code
* add grpc queries tests
* fix simulations
* update module.go
* lint
* register feegrant NewSimulationManager
* fix sims
* fix sims
* add genesis test
* add periodic grant
* updated cmd
* changed times
* updated flags
* removed days as period clock
* added condition for period and exp
* add periodic fee cli tests
* udpated tests
* fix lint
* fix tests
* fix sims
* renaming to `fee_grant`
* review changes
* fix test
* add condition for duplicate grants
* fix tests
* add `genTxWithFeeGranter` in tests
* fix simulation
* one of changes & test fixes
* fix test
* fix lint
* changed package name `feegrant` to `fee_grant`
* review comments
* review changes
* review change
* review changes
* added fee-account in flags
* address review changes
* read fee granter from cli
* updated create account with mnemonic
* Address review comments
* move `simapp/ante` file to `feegrant/ante`
* update keeper logic to create account
* update docs
* fix tests
* update `serviceMsgClientConn` from `msgservice`
* review changes
* add test case for using more fees than allowed
* eliminate panic checks from keeper
* fix lint
* change store keys string to bytes
* fix tests
* review changes
* review changes
* udpate docs
* make spend limit optional
* fix tests
* fix tests
* review changes
* add norace tag
* proto-docs
* add docs
Co-authored-by: Ethan Frey <ethanfrey@users.noreply.github.com>
Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <aleks.bezobchuk@gmail.com>
Co-authored-by: SaReN <sahithnarahari@gmail.com>
Co-authored-by: aleem1413 <aleem@vitwit.com>
Co-authored-by: MD Aleem <72057206+aleem1314@users.noreply.github.com>
Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-01-29 11:54:51 -08:00
|
|
|
// WithFeeGranterAddress returns a copy of the context with an updated fee granter account
|
|
|
|
// address.
|
|
|
|
func (ctx Context) WithFeeGranterAddress(addr sdk.AccAddress) Context {
|
|
|
|
ctx.FeeGranter = addr
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2019-03-25 17:54:23 -07:00
|
|
|
// WithBroadcastMode returns a copy of the context with an updated broadcast
|
|
|
|
// mode.
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithBroadcastMode(mode string) Context {
|
2019-03-25 17:54:23 -07:00
|
|
|
ctx.BroadcastMode = mode
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-12-11 05:54:50 -08:00
|
|
|
// WithSignModeStr returns a copy of the context with an updated SignMode
|
|
|
|
// value.
|
|
|
|
func (ctx Context) WithSignModeStr(signModeStr string) Context {
|
|
|
|
ctx.SignModeStr = signModeStr
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-06-30 13:59:21 -07:00
|
|
|
// WithSkipConfirmation returns a copy of the context with an updated SkipConfirm
|
|
|
|
// value.
|
|
|
|
func (ctx Context) WithSkipConfirmation(skip bool) Context {
|
|
|
|
ctx.SkipConfirm = skip
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
// WithTxConfig returns the context with an updated TxConfig
|
|
|
|
func (ctx Context) WithTxConfig(generator TxConfig) Context {
|
|
|
|
ctx.TxConfig = generator
|
2020-05-21 14:29:34 -07:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithAccountRetriever returns the context with an updated AccountRetriever
|
2020-06-01 05:46:03 -07:00
|
|
|
func (ctx Context) WithAccountRetriever(retriever AccountRetriever) Context {
|
2020-05-21 14:29:34 -07:00
|
|
|
ctx.AccountRetriever = retriever
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-07-03 09:42:12 -07:00
|
|
|
// WithInterfaceRegistry returns the context with an updated InterfaceRegistry
|
|
|
|
func (ctx Context) WithInterfaceRegistry(interfaceRegistry codectypes.InterfaceRegistry) Context {
|
|
|
|
ctx.InterfaceRegistry = interfaceRegistry
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-03-31 03:04:33 -07:00
|
|
|
// WithViper returns the context with Viper field. This Viper instance is used to read
|
|
|
|
// client-side config from the config file.
|
2021-04-13 10:32:08 -07:00
|
|
|
func (ctx Context) WithViper(prefix string) Context {
|
2021-03-31 03:04:33 -07:00
|
|
|
v := viper.New()
|
2021-04-13 10:32:08 -07:00
|
|
|
v.SetEnvPrefix(prefix)
|
|
|
|
v.AutomaticEnv()
|
2021-03-31 03:04:33 -07:00
|
|
|
ctx.Viper = v
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2021-12-07 10:02:21 -08:00
|
|
|
// WithAux returns a copy of the context with an updated IsAux value.
|
|
|
|
func (ctx Context) WithAux(isAux bool) Context {
|
|
|
|
ctx.IsAux = isAux
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:44:15 -08:00
|
|
|
// PrintString prints the raw string to ctx.Output if it's defined, otherwise to os.Stdout
|
2020-07-29 15:33:42 -07:00
|
|
|
func (ctx Context) PrintString(str string) error {
|
2020-12-14 13:44:15 -08:00
|
|
|
return ctx.PrintBytes([]byte(str))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrintBytes prints the raw bytes to ctx.Output if it's defined, otherwise to os.Stdout.
|
|
|
|
// NOTE: for printing a complex state object, you should use ctx.PrintOutput
|
|
|
|
func (ctx Context) PrintBytes(o []byte) error {
|
2020-07-29 15:33:42 -07:00
|
|
|
writer := ctx.Output
|
|
|
|
if writer == nil {
|
|
|
|
writer = os.Stdout
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:44:15 -08:00
|
|
|
_, err := writer.Write(o)
|
2020-07-29 15:33:42 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-08 12:17:40 -08:00
|
|
|
// PrintProto outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
|
2020-03-25 09:24:13 -07:00
|
|
|
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
|
2021-06-11 04:49:39 -07:00
|
|
|
// will be JSON encoded using ctx.Codec. An error is returned upon failure.
|
2020-12-08 12:17:40 -08:00
|
|
|
func (ctx Context) PrintProto(toPrint proto.Message) error {
|
2020-08-26 02:39:38 -07:00
|
|
|
// always serialize JSON initially because proto json can't be directly YAML encoded
|
2021-06-11 04:49:39 -07:00
|
|
|
out, err := ctx.Codec.MarshalJSON(toPrint)
|
2020-08-26 02:39:38 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ctx.printOutput(out)
|
2020-08-11 00:19:49 -07:00
|
|
|
}
|
|
|
|
|
2020-12-08 12:17:40 -08:00
|
|
|
// PrintObjectLegacy is a variant of PrintProto that doesn't require a proto.Message type
|
|
|
|
// and uses amino JSON encoding.
|
|
|
|
// Deprecated: It will be removed in the near future!
|
|
|
|
func (ctx Context) PrintObjectLegacy(toPrint interface{}) error {
|
2020-08-26 02:39:38 -07:00
|
|
|
out, err := ctx.LegacyAmino.MarshalJSON(toPrint)
|
2020-06-18 00:36:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-26 02:39:38 -07:00
|
|
|
return ctx.printOutput(out)
|
|
|
|
}
|
2020-03-25 09:24:13 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
func (ctx Context) printOutput(out []byte) error {
|
2021-09-24 07:37:34 -07:00
|
|
|
var err error
|
2020-06-18 00:36:25 -07:00
|
|
|
if ctx.OutputFormat == "text" {
|
2021-09-24 07:37:34 -07:00
|
|
|
out, err = yaml.JSONToYAML(out)
|
2020-06-18 00:36:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-25 09:24:13 -07:00
|
|
|
}
|
|
|
|
|
2020-06-18 00:36:25 -07:00
|
|
|
writer := ctx.Output
|
|
|
|
if writer == nil {
|
|
|
|
writer = os.Stdout
|
|
|
|
}
|
|
|
|
|
2021-09-24 07:37:34 -07:00
|
|
|
_, err = writer.Write(out)
|
2020-03-25 09:24:13 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 00:36:25 -07:00
|
|
|
if ctx.OutputFormat != "text" {
|
|
|
|
// append new-line for formats besides YAML
|
|
|
|
_, err = writer.Write([]byte("\n"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-22 09:28:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 06:21:38 -07:00
|
|
|
return nil
|
2019-01-22 09:28:39 -08:00
|
|
|
}
|
2019-01-29 11:22:47 -08:00
|
|
|
|
2020-12-11 05:54:50 -08:00
|
|
|
// GetFromFields returns a from account address, account name and keyring type, given either
|
2019-03-25 08:27:24 -07:00
|
|
|
// an address or key name. If genOnly is true, only a valid Bech32 cosmos
|
|
|
|
// address is returned.
|
2020-12-11 05:54:50 -08:00
|
|
|
func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddress, string, keyring.KeyType, error) {
|
2019-01-29 11:22:47 -08:00
|
|
|
if from == "" {
|
2020-12-11 05:54:50 -08:00
|
|
|
return nil, "", 0, nil
|
2019-01-29 11:22:47 -08:00
|
|
|
}
|
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
var k *keyring.Record
|
2019-01-29 11:22:47 -08:00
|
|
|
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
|
2021-09-20 05:02:15 -07:00
|
|
|
k, err = kr.KeyByAddress(addr)
|
2019-01-29 11:22:47 -08:00
|
|
|
if err != nil {
|
2020-12-11 05:54:50 -08:00
|
|
|
return nil, "", 0, err
|
2019-01-29 11:22:47 -08:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-20 05:02:15 -07:00
|
|
|
k, err = kr.Key(from)
|
2019-01-29 11:22:47 -08:00
|
|
|
if err != nil {
|
2020-12-11 05:54:50 -08:00
|
|
|
return nil, "", 0, err
|
2019-01-29 11:22:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
addr, err := k.GetAddress()
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, k.Name, k.GetType(), nil
|
2019-01-29 11:22:47 -08:00
|
|
|
}
|
2020-04-08 02:38:28 -07:00
|
|
|
|
2021-03-31 03:04:33 -07:00
|
|
|
// NewKeyringFromBackend gets a Keyring object from a backend
|
|
|
|
func NewKeyringFromBackend(ctx Context, backend string) (keyring.Keyring, error) {
|
2021-03-18 01:27:03 -07:00
|
|
|
if ctx.GenerateOnly || ctx.Simulate {
|
2021-09-20 05:02:15 -07:00
|
|
|
backend = keyring.BackendMemory
|
2020-04-08 02:38:28 -07:00
|
|
|
}
|
2020-06-30 13:59:21 -07:00
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input, ctx.Codec, ctx.KeyringOptions...)
|
2020-04-08 02:38:28 -07:00
|
|
|
}
|