Skip to content

Commit

Permalink
[1789]: Unit tests on the tx commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon committed Feb 2, 2024
1 parent 71f07cf commit 3e7d943
Show file tree
Hide file tree
Showing 3 changed files with 374 additions and 23 deletions.
72 changes: 66 additions & 6 deletions x/exchange/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type CmdTestSuite struct {
keyring keyring.Keyring
keyringDir string
accountAddrs []sdk.AccAddress
feeDenom string

addr0 sdk.AccAddress
addr1 sdk.AccAddress
Expand All @@ -79,6 +80,7 @@ func (s *CmdTestSuite) SetupSuite() {
s.cfg.NumValidators = 1
s.cfg.ChainID = antewrapper.SimAppChainID
s.cfg.TimeoutCommit = 500 * time.Millisecond
s.feeDenom = pioconfig.GetProvenanceConfig().FeeDenom

s.generateAccountsWithKeyring(10)
s.addr0 = s.accountAddrs[0]
Expand Down Expand Up @@ -170,7 +172,6 @@ func (s *CmdTestSuite) SetupSuite() {
AcceptingCommitments: true,
CommitmentSettlementBips: 50,
IntermediaryDenom: "cherry",
FeeCreateCommitmentFlat: []sdk.Coin{sdk.NewInt64Coin("peach", 15)},
},
exchange.Market{
MarketId: 5,
Expand Down Expand Up @@ -229,7 +230,7 @@ func (s *CmdTestSuite) SetupSuite() {
ReqAttrCreateCommitment: []string{"committer.kyc"},
},
exchange.Market{
// This market has an invalid setup. Don't mess with it.
// This market has an invalid setup. Don't use it for anything else.
MarketId: 421,
MarketDetails: exchange.MarketDetails{Name: "Broken"},
FeeSellerSettlementRatios: []exchange.FeeRatio{
Expand All @@ -239,6 +240,9 @@ func (s *CmdTestSuite) SetupSuite() {
{Price: sdk.NewInt64Coin("peach", 56), Fee: sdk.NewInt64Coin("peach", 1)},
{Price: sdk.NewInt64Coin("plum", 57), Fee: sdk.NewInt64Coin("plum", 1)},
},
AccessGrants: []exchange.AccessGrant{
{Address: s.addr1.String(), Permissions: exchange.AllPermissions()},
},
},
)

Expand All @@ -255,7 +259,6 @@ func (s *CmdTestSuite) SetupSuite() {
exchangeGen.Commitments = append(exchangeGen.Commitments, *com)
}
}

exchangeGen.Commitments = append(exchangeGen.Commitments, exchange.Commitment{
Account: s.addr1.String(),
MarketId: 421,
Expand Down Expand Up @@ -292,7 +295,7 @@ func (s *CmdTestSuite) SetupSuite() {
markerGen.NetAssetValues = append(markerGen.NetAssetValues, []markertypes.MarkerNetAssetValues{
{
Address: cherryMarker.Address,
NetAssetValues: []markertypes.NetAssetValue{{Price: sdk.NewInt64Coin(pioconfig.GetProvenanceConfig().FeeDenom, 100), Volume: 1}},
NetAssetValues: []markertypes.NetAssetValue{{Price: s.feeCoin(100), Volume: 1}},
},
{
Address: appleMarker.Address,
Expand All @@ -314,7 +317,8 @@ func (s *CmdTestSuite) SetupSuite() {
// Any initial holds for an account are added to this so that
// this is what's available to each at the start of the unit tests.
balance := sdk.NewCoins(
sdk.NewInt64Coin(s.cfg.BondDenom, 1_000_000_000),
s.bondCoin(1_000_000_000),
s.feeCoin(1_000_000_000),
sdk.NewInt64Coin("acorn", 1_000_000_000),
sdk.NewInt64Coin("apple", 1_000_000_000),
sdk.NewInt64Coin("peach", 1_000_000_000),
Expand Down Expand Up @@ -464,6 +468,8 @@ type txCmdTestCase struct {
preRun func() ([]string, func(*sdk.TxResponse))
// args are the arguments to provide to the command.
args []string
// addedFees is any fees to add to the default 10<bond> amount.
addedFees sdk.Coins
// expInErr are strings to expect in an error from the cmd.
// Errors that come from the endpoint will not be here; use expInRawLog for those.
expInErr []string
Expand All @@ -489,10 +495,15 @@ func (s *CmdTestSuite) runTxCmdTestCase(tc txCmdTestCase) {

cmd := cli.CmdTx()

fees := s.bondCoins(10)
if !tc.addedFees.IsZero() {
fees = fees.Add(tc.addedFees...)
}

args := append(tc.args, extraArgs...)
args = append(args,
"--"+flags.FlagGas, "250000",
"--"+flags.FlagFees, s.bondCoins(10).String(),
"--"+flags.FlagFees, fees.String(),
"--"+flags.FlagBroadcastMode, flags.BroadcastBlock,
"--"+flags.FlagSkipConfirmation,
)
Expand Down Expand Up @@ -836,6 +847,16 @@ func (s *CmdTestSuite) bondCoins(amt int64) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(s.cfg.BondDenom, amt))
}

// feeCoin returns a Coin with the fee denom and the provided amount.
func (s *CmdTestSuite) feeCoin(amt int64) sdk.Coin {
return sdk.NewInt64Coin(s.feeDenom, amt)
}

// feeCoins returns a Coins with just an entry with the fee denom and the provided amount.
func (s *CmdTestSuite) feeCoins(amt int64) sdk.Coins {
return sdk.NewCoins(sdk.NewInt64Coin(s.feeDenom, amt))
}

// adjustBalance creates a new Balance with the order owner's Address and a Coins that's
// the result of the order and fees applied to the provided current balance.
func (s *CmdTestSuite) adjustBalance(curBal sdk.Coins, order *exchange.Order, creationFees ...sdk.Coin) banktypes.Balance {
Expand Down Expand Up @@ -943,6 +964,45 @@ func (s *CmdTestSuite) createOrder(order *exchange.Order, creationFee *sdk.Coin)
return s.asOrderID(orderIDStr)
}

// commitFunds issues a command to commit funds.
func (s *CmdTestSuite) commitFunds(addr sdk.AccAddress, marketID uint32, amount sdk.Coins, creationFee sdk.Coins) {
cmd := cli.CmdTx()
args := []string{
"commit",
"--from", addr.String(),
"--market", fmt.Sprintf("%d", marketID),
"--amount", amount.String(),
}
if !creationFee.IsZero() {
args = append(args, "--creation-fee", creationFee.String())
}

args = append(args,
"--"+flags.FlagFees, s.bondCoins(10).String(),
"--"+flags.FlagBroadcastMode, flags.BroadcastBlock,
"--"+flags.FlagSkipConfirmation,
)

cmdName := cmd.Name()
var outBz []byte
defer func() {
if s.T().Failed() {
s.T().Logf("Command: %s\nArgs: %q\nOutput\n%s", cmdName, args, string(outBz))
}
}()

clientCtx := s.getClientCtx()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
outBz = out.Bytes()

s.Require().NoError(err, "ExecTestCLICmd error")

var resp sdk.TxResponse
err = clientCtx.Codec.UnmarshalJSON(outBz, &resp)
s.Require().NoError(err, "UnmarshalJSON(command output) error")
s.Require().Equal(int(0), int(resp.Code), "response code:\n%v", resp)
}

// queryBankBalances executes a bank query to get an account's balances.
func (s *CmdTestSuite) queryBankBalances(addr string) sdk.Coins {
clientCtx := s.getClientCtx()
Expand Down
19 changes: 8 additions & 11 deletions x/exchange/client/cli/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/provenance-io/provenance/internal/pioconfig"
"github.com/provenance-io/provenance/x/exchange"
)

Expand Down Expand Up @@ -717,10 +716,8 @@ func (s *CmdTestSuite) TestCmdQueryCommitmentSettlementFeeCalc() {
tx := newTx(s.T(), fileMsg)
writeFileAsJson(s.T(), filename, tx)

feeDenom := pioconfig.GetProvenanceConfig().FeeDenom

// existing navs:
// 1cherry => 100<bond denom>
// 1cherry => 100<fee denom>
// 1apple => 8cherry
// 17acorn => 3cherry
// 3peach => 778cherry
Expand Down Expand Up @@ -757,13 +754,13 @@ func (s *CmdTestSuite) TestCmdQueryCommitmentSettlementFeeCalc() {
// 30peach * 778cherry/3peach = 7780cherry
// 1945apple * 4cherry/1apple = 7780cherry
// total = 15560cherry
// 15560cherry * 100nhash / 1 cherry = 1556000nhash
// 1556000nhash * 50 / 20000 = 3890nhash
// 15560cherry * 100<fee> / 1 cherry = 1556000<fee>
// 1556000<fee> * 50 / 20000 = 3890<fee>
expOut: `conversion_navs: []
converted_total: []
exchange_fees:
- amount: "3890"
denom: ` + feeDenom + `
denom: ` + s.feeDenom + `
input_total: []
to_fee_nav: null
`,
Expand All @@ -775,17 +772,17 @@ to_fee_nav: null
// 15banana * 35cherry/15banana = 35cherry
// 50peach * 778cherry/3peach = 12966.67cherry
// sum = 13081.67 => 13082cherry
// 13082cherry * 100nhash/1cherry = 1308200cherry
// 1308200 * 50 / 20000 = 3270.5 => 3271nhash
// 13082cherry * 100<fee>/1cherry = 1308200cherry
// 1308200 * 50 / 20000 = 3270.5 => 3271<fee>
expInOut: []string{
`"exchange_fees":[{"denom":"nhash","amount":"3271"}]`,
`"exchange_fees":[{"denom":"` + s.feeDenom + `","amount":"3271"}]`,
`"input_total":[{"denom":"apple","amount":"10"},{"denom":"banana","amount":"15"},{"denom":"peach","amount":"50"}]`,
`"converted_total":[{"denom":"cherry","amount":"13082"}]`,
`"conversion_navs":[`,
`{"assets":{"denom":"apple","amount":"1"},"price":{"denom":"cherry","amount":"8"}}`,
`{"assets":{"denom":"banana","amount":"15"},"price":{"denom":"cherry","amount":"35"}}`,
`{"assets":{"denom":"peach","amount":"3"},"price":{"denom":"cherry","amount":"778"}}`,
`"to_fee_nav":{"assets":{"denom":"cherry","amount":"1"},"price":{"denom":"nhash","amount":"100"}}`,
`"to_fee_nav":{"assets":{"denom":"cherry","amount":"1"},"price":{"denom":"` + s.feeDenom + `","amount":"100"}}`,
},
},
}
Expand Down
Loading

0 comments on commit 3e7d943

Please sign in to comment.