Skip to content

Commit

Permalink
[1789]: Unit tests on the query commands. Alter the commitment settle…
Browse files Browse the repository at this point in the history
…ment fee calc setup to not require an admin arg when --file is provided.
  • Loading branch information
SpicyLemon committed Feb 1, 2024
1 parent 3a4c6bf commit 71f07cf
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 58 deletions.
73 changes: 68 additions & 5 deletions x/exchange/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *CmdTestSuite) SetupSuite() {
newMarker := func(denom string) *markertypes.MarkerAccount {
return &markertypes.MarkerAccount{
BaseAccount: &authtypes.BaseAccount{
Address: markertypes.MustGetMarkerAddress("cherry").String(),
Address: markertypes.MustGetMarkerAddress(denom).String(),
},
Status: markertypes.StatusActive,
Denom: "cherry",
Expand Down Expand Up @@ -196,7 +196,7 @@ func (s *CmdTestSuite) SetupSuite() {
},
// Do not make a market 419, lots of tests expect it to not exist.
exchange.Market{
// The orders in this market are for the orders queries.
// The stuff in this market is for the query tests.
// Don't use it in other unit tests (e.g. order creation or settlement).
MarketId: 420,
MarketDetails: exchange.MarketDetails{
Expand Down Expand Up @@ -241,14 +241,35 @@ func (s *CmdTestSuite) SetupSuite() {
},
},
)
toHold := make(map[string]sdk.Coins)

exchangeGen.Orders = make([]exchange.Order, 60)
for i := range exchangeGen.Orders {
order := s.makeInitialOrder(uint64(i + 1))
exchangeGen.Orders[i] = *order
toHold[order.GetOwner()] = toHold[order.GetOwner()].Add(order.GetHoldAmount()...)
}
exchangeGen.LastOrderId = uint64(100)

for i := range s.accountAddrs {
com := s.makeInitialCommitment(i)
if !com.Amount.IsZero() {
exchangeGen.Commitments = append(exchangeGen.Commitments, *com)
}
}

exchangeGen.Commitments = append(exchangeGen.Commitments, exchange.Commitment{
Account: s.addr1.String(),
MarketId: 421,
Amount: sdk.NewCoins(sdk.NewInt64Coin("apple", 4210), sdk.NewInt64Coin("peach", 421)),
})

toHold := make(map[string]sdk.Coins)
for _, order := range exchangeGen.Orders {
toHold[order.GetOwner()] = toHold[order.GetOwner()].Add(order.GetHoldAmount()...)
}
for _, com := range exchangeGen.Commitments {
toHold[com.Account] = toHold[com.Account].Add(com.Amount...)
}

s.cfg.GenesisState[exchange.ModuleName], err = s.cfg.Codec.MarshalJSON(&exchangeGen)
s.Require().NoError(err, "MarshalJSON exchange gen state")

Expand All @@ -271,7 +292,7 @@ func (s *CmdTestSuite) SetupSuite() {
markerGen.NetAssetValues = append(markerGen.NetAssetValues, []markertypes.MarkerNetAssetValues{
{
Address: cherryMarker.Address,
NetAssetValues: []markertypes.NetAssetValue{{Price: sdk.NewInt64Coin("nhash", 100), Volume: 1}},
NetAssetValues: []markertypes.NetAssetValue{{Price: sdk.NewInt64Coin(pioconfig.GetProvenanceConfig().FeeDenom, 100), Volume: 1}},
},
{
Address: appleMarker.Address,
Expand Down Expand Up @@ -376,6 +397,48 @@ func (s *CmdTestSuite) makeInitialOrder(orderID uint64) *exchange.Order {
return order
}

// makeInitialCommitment makes a commitment for the s.accountAddrs with the provided addrI.
// The amount is based off the addrI and might be zero.
func (s *CmdTestSuite) makeInitialCommitment(addrI int) *exchange.Commitment {
var addr sdk.AccAddress
s.Require().NotPanics(func() {
addr = s.accountAddrs[addrI]
}, "s.accountAddrs[%d]", addrI)
rv := &exchange.Commitment{
Account: addr.String(),
MarketId: 420,
}

i := addrI % 10 // in case the number of addresses changes later.
// One denom (3): 0 = apple, 1 = acorn, 2 = peach
// Two denoms (3): 3 = apple+acorn, 4 = apple+peach, 5 = acorn+peach
// All denoms (2): 6, 7
// Nothing (2): 8, 9
// apple <= 0, 3, 4, 6, 7
// acorn <= 1, 3, 5, 6, 7
// peach <= 2, 4, 5, 6, 7
if contains([]int{0, 3, 4, 6, 7}, i) {
rv.Amount = rv.Amount.Add(sdk.NewInt64Coin("apple", int64(1000+addrI*200)))
}
if contains([]int{1, 3, 5, 6, 7}, i) {
rv.Amount = rv.Amount.Add(sdk.NewInt64Coin("acorn", int64(10_000+addrI*100)))
}
if contains([]int{2, 4, 5, 6, 7}, i) {
rv.Amount = rv.Amount.Add(sdk.NewInt64Coin("peach", int64(500+addrI*addrI*100)))
}
return rv
}

// contains reports whether v is present in s.
func contains[S ~[]E, E comparable](s S, v E) bool {
for i := range s {
if v == s[i] {
return true
}
}
return false
}

// getClientCtx get a client context that knows about the suite's keyring.
func (s *CmdTestSuite) getClientCtx() client.Context {
return s.testnet.Validators[0].ClientCtx.
Expand Down
9 changes: 8 additions & 1 deletion x/exchange/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,18 @@ func MarkFlagsRequired(cmd *cobra.Command, names ...string) {
//
// Use ReadFlagsAdminOrFrom to read these flags.
func AddFlagsAdmin(cmd *cobra.Command) {
AddFlagsAdminOpt(cmd)
cmd.MarkFlagsOneRequired(flags.FlagFrom, FlagAdmin, FlagAuthority)
}

// AddFlagsAdminOpt adds the --admin and --authority flags to a command and makes them mutually exclusive.
//
// Use ReadFlagsAdminOrFrom to read these flags.
func AddFlagsAdminOpt(cmd *cobra.Command) {
cmd.Flags().String(FlagAdmin, "", "The admin (defaults to --from account)")
cmd.Flags().Bool(FlagAuthority, false, "Use the governance module account for the admin")

cmd.MarkFlagsMutuallyExclusive(FlagAdmin, FlagAuthority)
cmd.MarkFlagsOneRequired(flags.FlagFrom, FlagAdmin, FlagAuthority)
}

// ReadFlagsAdminOrFrom reads the --admin flag if provided.
Expand Down
7 changes: 4 additions & 3 deletions x/exchange/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,10 @@ func CmdQueryParams() *cobra.Command {
// CmdQueryCommitmentSettlementFeeCalc creates the commitment-settlement-fee-calc sub-command for the exchange query command.
func CmdQueryCommitmentSettlementFeeCalc() *cobra.Command {
cmd := &cobra.Command{
Use: "commitment-settlement-fee-calc",
Short: "Calculate the fee required for a commitment settlement",
RunE: genericQueryRunE(MakeQueryCommitmentSettlementFeeCalc, exchange.QueryClient.CommitmentSettlementFeeCalc),
Use: "commitment-settlement-fee-calc",
Aliases: []string{"settle-commitments-fee-calc", "fee-calc-commitment-settlement", "fee-calc-settle-commitments"},
Short: "Calculate the fee required for a commitment settlement",
RunE: genericQueryRunE(MakeQueryCommitmentSettlementFeeCalc, exchange.QueryClient.CommitmentSettlementFeeCalc),
}

flags.AddQueryFlagsToCmd(cmd)
Expand Down
31 changes: 10 additions & 21 deletions x/exchange/client/cli/query_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ func TestMakeQueryParams(t *testing.T) {
}

func TestSetupCmdQueryCommitmentSettlementFeeCalc(t *testing.T) {
tc := setupTestCase{
runSetupTestCase(t, setupTestCase{
name: "SetupCmdQueryCommitmentSettlementFeeCalc",
setup: cli.SetupCmdQueryCommitmentSettlementFeeCalc,
expFlags: []string{
Expand All @@ -1121,15 +1121,20 @@ func TestSetupCmdQueryCommitmentSettlementFeeCalc(t *testing.T) {
cli.FlagSettlementFees, cli.FlagNavs, cli.FlagTag, cli.FlagFile,
},
expAnnotations: map[string]map[string][]string{
flags.FlagFrom: {oneReq: {flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority}},
flags.FlagFrom: {oneReq: {cli.FlagFile + " " + flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority}},
cli.FlagAdmin: {
mutExc: {cli.FlagAdmin + " " + cli.FlagAuthority},
oneReq: {flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority},
oneReq: {cli.FlagFile + " " + flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority},
},
cli.FlagAuthority: {
mutExc: {cli.FlagAdmin + " " + cli.FlagAuthority},
oneReq: {flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority},
oneReq: {cli.FlagFile + " " + flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority},
},
cli.FlagFile: {oneReq: {
cli.FlagFile + " " + flags.FlagFrom + " " + cli.FlagAdmin + " " + cli.FlagAuthority,
cli.FlagFile + " " + cli.FlagMarket,
}},
cli.FlagMarket: {oneReq: {cli.FlagFile + " " + cli.FlagMarket}},
},
expInUse: []string{
"[--details]",
Expand All @@ -1141,23 +1146,7 @@ func TestSetupCmdQueryCommitmentSettlementFeeCalc(t *testing.T) {
cli.MsgFileDesc(&exchange.MsgMarketCommitmentSettleRequest{}),
},
skipAddingFromFlag: true,
}

oneReqFlags := []string{
cli.FlagMarket, cli.FlagInputs, cli.FlagOutputs, cli.FlagFile,
}
oneReqVal := strings.Join(oneReqFlags, " ")
if tc.expAnnotations == nil {
tc.expAnnotations = make(map[string]map[string][]string)
}
for _, name := range oneReqFlags {
if tc.expAnnotations[name] == nil {
tc.expAnnotations[name] = make(map[string][]string)
}
tc.expAnnotations[name][oneReq] = []string{oneReqVal}
}

runSetupTestCase(t, tc)
})
}

func TestMakeQueryCommitmentSettlementFeeCalc(t *testing.T) {
Expand Down
Loading

0 comments on commit 71f07cf

Please sign in to comment.