Skip to content

Commit

Permalink
Improve TestSpendableBalanceQuery test and fix some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil committed Oct 25, 2023
1 parent acc7151 commit e5dd4ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
63 changes: 38 additions & 25 deletions integration-tests/modules/assetft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ func TestSpendableBalanceQuery(t *testing.T) {
issueFee := chain.QueryAssetFTParams(ctx, t).IssueFee.Amount

issuer := chain.GenAccount()
recipient := chain.GenAccount()
recipient1 := chain.GenAccount()

chain.FundAccountWithOptions(ctx, t, issuer, integration.BalancesOptions{
Messages: []sdk.Msg{
Expand Down Expand Up @@ -469,15 +469,15 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgIssue)),
msgIssue,
)
require.NoError(t, err)
requireT.NoError(err)

denom1 := assetfttypes.BuildDenom(msgIssue.Subunit, issuer)
frozenCoin1 := sdk.NewInt64Coin(denom1, 20)
sendCoin1 := sdk.NewInt64Coin(denom1, 100)

msgSend := &banktypes.MsgSend{
FromAddress: issuer.String(),
ToAddress: recipient.String(),
ToAddress: recipient1.String(),
Amount: sdk.NewCoins(sendCoin1),
}
_, err = client.BroadcastTx(
Expand All @@ -486,18 +486,18 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgSend)),
msgSend,
)
require.NoError(t, err)
requireT.NoError(err)

recipientSpendableBalanceBeforeFreesRes, err := bankClient.SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: recipient.String(),
recipientSpendableBalanceBeforeFreezeRes, err := bankClient.SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: recipient1.String(),
Denom: denom1,
})
require.NoError(t, err)
requireT.Equal(sendCoin1.Amount.String(), recipientSpendableBalanceBeforeFreesRes.Balance.Amount.String())
requireT.NoError(err)
requireT.Equal(sendCoin1.Amount.String(), recipientSpendableBalanceBeforeFreezeRes.Balance.Amount.String())

msgFreeze := &assetfttypes.MsgFreeze{
Sender: issuer.String(),
Account: recipient.String(),
Account: recipient1.String(),
Coin: frozenCoin1,
}
_, err = client.BroadcastTx(
Expand All @@ -506,13 +506,13 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgFreeze)),
msgFreeze,
)
require.NoError(t, err)
requireT.NoError(err)

recipientSpendableBalanceAfterFreezeRes, err := bankClient.SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: recipient.String(),
Address: recipient1.String(),
Denom: denom1,
})
require.NoError(t, err)
requireT.NoError(err)
requireT.Equal(sendCoin1.Amount.Sub(frozenCoin1.Amount).String(), recipientSpendableBalanceAfterFreezeRes.Balance.Amount.String())

// freeze globally now
Expand All @@ -526,13 +526,13 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgGloballyFreeze)),
msgGloballyFreeze,
)
require.NoError(t, err)
requireT.NoError(err)

recipientSpendableBalanceAfterGlobalFreezeRes, err := bankClient.SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: recipient.String(),
Address: recipient1.String(),
Denom: denom1,
})
require.NoError(t, err)
requireT.NoError(err)
requireT.Equal(sdkmath.ZeroInt().String(), recipientSpendableBalanceAfterGlobalFreezeRes.Balance.Amount.String())

// issue one more token
Expand All @@ -552,15 +552,15 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgIssue)),
msgIssue,
)
require.NoError(t, err)
requireT.NoError(err)

denom2 := assetfttypes.BuildDenom(msgIssue.Subunit, issuer)
frozenCoin2 := sdk.NewInt64Coin(denom2, 20)
sendCoin2 := sdk.NewInt64Coin(denom2, 100)

msgSend = &banktypes.MsgSend{
FromAddress: issuer.String(),
ToAddress: recipient.String(),
ToAddress: recipient1.String(),
Amount: sdk.NewCoins(sendCoin2),
}

Expand All @@ -570,18 +570,18 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgSend)),
msgSend,
)
require.NoError(t, err)
requireT.NoError(err)

recipientSpendableBalancesBeforeFreezeRes, err := bankClient.SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{
Address: recipient.String(),
Address: recipient1.String(),
})
require.NoError(t, err)
require.Len(t, recipientSpendableBalancesBeforeFreezeRes.Balances, 2)
requireT.NoError(err)
requireT.Len(recipientSpendableBalancesBeforeFreezeRes.Balances, 2)
requireT.Equal(sendCoin2.Amount.String(), recipientSpendableBalancesBeforeFreezeRes.Balances.AmountOf(denom2).String())

msgFreeze = &assetfttypes.MsgFreeze{
Sender: issuer.String(),
Account: recipient.String(),
Account: recipient1.String(),
Coin: frozenCoin2,
}
_, err = client.BroadcastTx(
Expand All @@ -590,13 +590,26 @@ func TestSpendableBalanceQuery(t *testing.T) {
chain.TxFactory().WithGas(chain.GasLimitByMsgs(msgFreeze)),
msgFreeze,
)
require.NoError(t, err)
requireT.NoError(err)

recipientSpendableBalancesBeforeFreezeRes, err = bankClient.SpendableBalances(ctx, &banktypes.QuerySpendableBalancesRequest{
Address: recipient.String(),
Address: recipient1.String(),
})
require.NoError(t, err)
requireT.NoError(err)
requireT.Equal(sendCoin2.Amount.Sub(frozenCoin2.Amount).String(), recipientSpendableBalancesBeforeFreezeRes.Balances.AmountOf(denom2).String())

// check the native denom
recipient2 := chain.GenAccount()
amountToFund := sdkmath.NewInt(100)
chain.FundAccountWithOptions(ctx, t, recipient2, integration.BalancesOptions{
Amount: amountToFund,
})
recipient2SpendableBalance, err := bankClient.SpendableBalanceByDenom(ctx, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: recipient2.String(),
Denom: chain.Chain.ChainSettings.Denom,
})
requireT.NoError(err)
requireT.Equal(amountToFund.String(), recipient2SpendableBalance.Balance.Amount.String())
}

// TestEmptyBalanceQuery tests balance query.
Expand Down
3 changes: 1 addition & 2 deletions x/wbank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ func (k BaseKeeperWrapper) SpendableBalances(ctx context.Context, req *banktypes
return nil, sdkerrors.Wrapf(cosmoserrors.ErrInvalidAddress, "invalid address %s", req.Address)
}
for i := range res.Balances {
spendableCoin := k.getSpendableCoin(sdk.UnwrapSDKContext(ctx), addr, res.Balances[i])
res.Balances[i] = spendableCoin
res.Balances[i] = k.getSpendableCoin(sdk.UnwrapSDKContext(ctx), addr, res.Balances[i])
}

return res, nil
Expand Down
10 changes: 5 additions & 5 deletions x/wbank/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestBaseKeeperWrapper_SpendableBalances(t *testing.T) {
recipient := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())

totalTokens := 10
amountToSemd := sdkmath.NewInt(100)
amountToSend := sdkmath.NewInt(100)
denoms := make([]string, 0, totalTokens)
for i := 0; i < totalTokens; i++ {
settings := types.IssueSettings{
Expand All @@ -44,7 +44,7 @@ func TestBaseKeeperWrapper_SpendableBalances(t *testing.T) {
requireT.NoError(err)
denoms = append(denoms, denom)

coinToSend := sdk.NewCoin(denom, amountToSemd)
coinToSend := sdk.NewCoin(denom, amountToSend)
err = bankKeeper.SendCoins(ctx, issuer, recipient, sdk.NewCoins(
coinToSend,
))
Expand All @@ -70,8 +70,8 @@ func TestBaseKeeperWrapper_SpendableBalances(t *testing.T) {
})
requireT.NoError(err)
requireT.Equal(
balances.AmountOfNoDenomValidation(denom).Sub(coinToFreeze.Amount).String(),
spendableBalancesRes.Balances.AmountOfNoDenomValidation(denom).String(),
balances.AmountOf(denom).Sub(coinToFreeze.Amount).String(),
spendableBalancesRes.Balances.AmountOf(denom).String(),
)

// check with global freeze
Expand All @@ -83,7 +83,7 @@ func TestBaseKeeperWrapper_SpendableBalances(t *testing.T) {
requireT.NoError(err)
requireT.Equal(
sdk.ZeroInt().String(),
spendableBalancesRes.Balances.AmountOfNoDenomValidation(denom).String(),
spendableBalancesRes.Balances.AmountOf(denom).String(),
)
}

Expand Down

0 comments on commit e5dd4ce

Please sign in to comment.