Skip to content

Commit

Permalink
Update the MsgMarketCommitmentSettle cli stuff use the keyring if a f…
Browse files Browse the repository at this point in the history
…rom is provided. (#2001) (#2002)

* Update the MsgMarketCommitmentSettle cli stuff use the keyring if a from is provided.

* Add changelog entry.

* Add test case for unknown keyring name.
  • Loading branch information
SpicyLemon authored May 24, 2024
1 parent affb7fd commit bf5275c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* Update the exchange `commitment-settlement-fee-calc` cli query to utilize the keyring [#2001](https://github.com/provenance-io/provenance/pull/2001).

### Bug Fixes

* The `add-net-asset-values` command now correctly uses the from `flag`'s `AccAddress` [#1995](https://github.com/provenance-io/provenance/issues/1995).
Expand Down
30 changes: 26 additions & 4 deletions x/exchange/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1409,9 +1409,11 @@ func addOneReqAnnotations(tc *setupTestCase, oneReqFlags ...string) {
}
}

// newClientContextWithCodec returns a new client.Context that has a useful Codec.
func newClientContextWithCodec() client.Context {
return clientContextWithCodec(client.Context{})
// newClientContext returns a new client.Context that has a Codec and keyring.
func newClientContext(t *testing.T) client.Context {
ctx := client.Context{}
ctx = clientContextWithCodec(ctx)
return clientContextWithKeyring(t, ctx)
}

// clientContextWithCodec adds a useful Codec to the provided client context.
Expand All @@ -1423,6 +1425,26 @@ func clientContextWithCodec(clientCtx client.Context) client.Context {
WithTxConfig(encCfg.TxConfig)
}

const (
keyringName = "keyringaddr"
keyringMnemonic = "pave kit dust loop forest symptom lobster tape note attitude aim cloth cat welcome basic head wet pistol hair funny library dove oak drift"
keyringAddr = "cosmos177zjzs7mh79j0cx6wcxq8dayetkftkx2crt4u6"
)

// clientContextWithKeyring returns a client context that has a keyring and an entry for keyringName=>keyringAddr.
func clientContextWithKeyring(t *testing.T, clientCtx client.Context) client.Context {
kr, err := client.NewKeyringFromBackend(clientCtx, keyring.BackendMemory)
require.NoError(t, err, "NewKeyringFromBackend")
clientCtx = clientCtx.WithKeyring(kr)

// This mnemonic was generated by using Keyring.NewAccount().
// I then logged it (and the address) with t.Logf(...) and copy/pasted it into the constants above.
_, err = clientCtx.Keyring.NewAccount(keyringName, keyringMnemonic, keyring.DefaultBIP39Passphrase, hd.CreateHDPath(118, 0, 0).String(), hd.Secp256k1)
require.NoError(t, err, "adding %s to the keyring using NewAccount(...)", keyringName)

return clientCtx
}

// newGovProp creates a new MsgSubmitProposal containing the provided messages, requiring it to not error.
func newGovProp(t *testing.T, msgs ...sdk.Msg) *govv1.MsgSubmitProposal {
rv := &govv1.MsgSubmitProposal{}
Expand Down Expand Up @@ -1451,7 +1473,7 @@ func newTx(t *testing.T, msgs ...sdk.Msg) *txtypes.Tx {

// writeFileAsJson writes the provided proto message as a json file, requiring it to not error.
func writeFileAsJson(t *testing.T, filename string, content proto.Message) {
clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)
bz, err := clientCtx.Codec.MarshalJSON(content)
require.NoError(t, err, "MarshalJSON(%T)", content)
writeFile(t, filename, bz)
Expand Down
12 changes: 6 additions & 6 deletions x/exchange/client/cli/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2541,7 +2541,7 @@ func TestReadTxFileFlag(t *testing.T) {
err := tc.flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actFilename string
var actTx *txtypes.Tx
Expand Down Expand Up @@ -2765,7 +2765,7 @@ func TestReadProposalFlag(t *testing.T) {
err := tc.flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actPropFN string
var actAnys []*codectypes.Any
Expand Down Expand Up @@ -2893,7 +2893,7 @@ func TestReadMsgGovCreateMarketRequestFromProposalFlag(t *testing.T) {
err := flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actual *exchange.MsgGovCreateMarketRequest
testFunc := func() {
Expand Down Expand Up @@ -3010,7 +3010,7 @@ func TestReadMsgGovManageFeesRequestFromProposalFlag(t *testing.T) {
err := flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actual *exchange.MsgGovManageFeesRequest
testFunc := func() {
Expand Down Expand Up @@ -3173,7 +3173,7 @@ func TestReadMsgMarketCommitmentSettleFromFileFlag(t *testing.T) {
err := flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actual *exchange.MsgMarketCommitmentSettleRequest
testFunc := func() {
Expand Down Expand Up @@ -3376,7 +3376,7 @@ func TestReadPaymentFromFileFlag(t *testing.T) {
err := flagSet.Parse(args)
require.NoError(t, err, "flagSet.Parse(%q)", args)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var actual exchange.Payment
testFunc := func() {
Expand Down
6 changes: 5 additions & 1 deletion x/exchange/client/cli/query_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,11 @@ func MakeQueryCommitmentSettlementFeeCalc(clientCtx client.Context, flagSet *pfl
errs := make([]error, 4)
clientCtx.From, errs[0] = flagSet.GetString(flags.FlagFrom)
if len(clientCtx.From) > 0 {
clientCtx.FromAddress, errs[1] = sdk.AccAddressFromBech32(clientCtx.From)
if addr, err := sdk.AccAddressFromBech32(clientCtx.From); err == nil {
clientCtx.FromAddress = addr
} else {
clientCtx.FromAddress, clientCtx.From, _, errs[1] = client.GetFromFields(clientCtx, clientCtx.Keyring, clientCtx.From)
}
}
rv.Settlement, errs[2] = MakeMsgMarketCommitmentSettle(clientCtx, flagSet, args)
rv.IncludeBreakdownFields, errs[3] = flagSet.GetBool(FlagDetails)
Expand Down
19 changes: 18 additions & 1 deletion x/exchange/client/cli/query_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runQueryMakerTest[R any](t *testing.T, td queryMakerTestDef[R], tc queryMak
err := cmd.Flags().Parse(tc.flags)
require.NoError(t, err, "cmd.Flags().Parse(%q)", tc.flags)

clientCtx := newClientContextWithCodec()
clientCtx := newClientContext(t)

var req *R
testFunc := func() {
Expand Down Expand Up @@ -1197,6 +1197,23 @@ func TestMakeQueryCommitmentSettlementFeeCalc(t *testing.T) {
},
},
},
{
name: "admin from keyring",
flags: []string{"--from", keyringName},
expReq: &exchange.QueryCommitmentSettlementFeeCalcRequest{
Settlement: &exchange.MsgMarketCommitmentSettleRequest{
Admin: keyringAddr,
},
},
},
{
name: "from flag is unknown name",
flags: []string{"--from", "notknown"},
expReq: &exchange.QueryCommitmentSettlementFeeCalcRequest{
Settlement: &exchange.MsgMarketCommitmentSettleRequest{},
},
expErr: joinErrs("notknown.info: key not found", "no <admin> provided"),
},
{
name: "admin as authority",
flags: []string{"--authority"},
Expand Down
4 changes: 2 additions & 2 deletions x/exchange/client/cli/tx_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ func TestMakeMsgMarketCommitmentSettle(t *testing.T) {
},
{
name: "from file",
clientCtx: newClientContextWithCodec(),
clientCtx: newClientContext(t),
flags: []string{"--file", filename},
expMsg: fileMsg,
},
Expand All @@ -743,7 +743,7 @@ func TestMakeMsgMarketCommitmentSettle(t *testing.T) {
"--file", filename, "--tag", "new-thang", "--authority",
"--outputs", "monroe:87plum",
},
clientCtx: newClientContextWithCodec(),
clientCtx: newClientContext(t),
expMsg: &exchange.MsgMarketCommitmentSettleRequest{
Admin: cli.AuthorityAddr.String(),
MarketId: 4,
Expand Down

0 comments on commit bf5275c

Please sign in to comment.