2019-08-19 06:29:17 -07:00
|
|
|
package keeper_test
|
2019-06-18 06:44:09 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-02-17 10:20:33 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
|
|
|
|
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
|
|
|
|
2019-06-18 06:44:09 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2020-07-20 05:30:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2019-06-18 06:44:09 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-20 06:24:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
2019-06-18 06:44:09 -07:00
|
|
|
)
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
func (suite *IntegrationTestSuite) TestQuerier_QueryBalance() {
|
|
|
|
app, ctx := suite.app, suite.ctx
|
2020-08-26 02:39:38 -07:00
|
|
|
legacyAmino := app.LegacyAmino()
|
2020-07-20 05:30:12 -07:00
|
|
|
_, _, addr := testdata.KeyTestPubAddr()
|
2019-06-18 06:44:09 -07:00
|
|
|
req := abci.RequestQuery{
|
2020-01-30 13:31:16 -08:00
|
|
|
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryBalance),
|
2019-06-18 06:44:09 -07:00
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QueryBalance}, req)
|
|
|
|
suite.Require().NotNil(err)
|
|
|
|
suite.Require().Nil(res)
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
req.Data = legacyAmino.MustMarshalJSON(types.NewQueryBalanceRequest(addr, fooDenom))
|
2020-01-30 13:31:16 -08:00
|
|
|
res, err = querier(ctx, []string{types.QueryBalance}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
var balance sdk.Coin
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balance))
|
2020-01-30 13:31:16 -08:00
|
|
|
suite.True(balance.IsZero())
|
|
|
|
|
|
|
|
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30))
|
|
|
|
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
|
|
|
|
|
|
|
app.AccountKeeper.SetAccount(ctx, acc)
|
2021-02-17 10:20:33 -08:00
|
|
|
suite.Require().NoError(simapp.FundAccount(app, ctx, acc.GetAddress(), origCoins))
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
res, err = querier(ctx, []string{types.QueryBalance}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balance))
|
2020-01-30 13:31:16 -08:00
|
|
|
suite.True(balance.IsEqual(newFooCoin(50)))
|
|
|
|
}
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
func (suite *IntegrationTestSuite) TestQuerier_QueryAllBalances() {
|
|
|
|
app, ctx := suite.app, suite.ctx
|
2020-08-26 02:39:38 -07:00
|
|
|
legacyAmino := app.LegacyAmino()
|
2020-07-20 05:30:12 -07:00
|
|
|
_, _, addr := testdata.KeyTestPubAddr()
|
2020-01-30 13:31:16 -08:00
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryAllBalances),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
res, err := querier(ctx, []string{types.QueryAllBalances}, req)
|
|
|
|
suite.Require().NotNil(err)
|
|
|
|
suite.Require().Nil(res)
|
2019-06-18 06:44:09 -07:00
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
req.Data = legacyAmino.MustMarshalJSON(types.NewQueryAllBalancesRequest(addr, nil))
|
2020-01-30 13:31:16 -08:00
|
|
|
res, err = querier(ctx, []string{types.QueryAllBalances}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
var balances sdk.Coins
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balances))
|
2020-01-30 13:31:16 -08:00
|
|
|
suite.True(balances.IsZero())
|
|
|
|
|
|
|
|
origCoins := sdk.NewCoins(newFooCoin(50), newBarCoin(30))
|
2019-08-19 06:29:17 -07:00
|
|
|
acc := app.AccountKeeper.NewAccountWithAddress(ctx, addr)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2019-08-19 06:29:17 -07:00
|
|
|
app.AccountKeeper.SetAccount(ctx, acc)
|
2021-02-17 10:20:33 -08:00
|
|
|
suite.Require().NoError(simapp.FundAccount(app, ctx, acc.GetAddress(), origCoins))
|
2020-01-30 13:31:16 -08:00
|
|
|
res, err = querier(ctx, []string{types.QueryAllBalances}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &balances))
|
2020-01-30 13:31:16 -08:00
|
|
|
suite.True(balances.IsEqual(origCoins))
|
2019-06-18 06:44:09 -07:00
|
|
|
}
|
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupply() {
|
|
|
|
app, ctx := suite.app, suite.ctx
|
2020-08-26 02:39:38 -07:00
|
|
|
legacyAmino := app.LegacyAmino()
|
2020-06-14 16:06:16 -07:00
|
|
|
expectedTotalSupply := types.NewSupply(sdk.NewCoins(sdk.NewInt64Coin("test", 400000000)))
|
2021-02-17 10:20:33 -08:00
|
|
|
suite.
|
|
|
|
Require().
|
|
|
|
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QueryTotalSupply),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QueryTotalSupply}, req)
|
|
|
|
suite.Require().NotNil(err)
|
|
|
|
suite.Require().Nil(res)
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
req.Data = legacyAmino.MustMarshalJSON(types.NewQueryTotalSupplyParams(1, 100))
|
2020-04-20 08:22:12 -07:00
|
|
|
res, err = querier(ctx, []string{types.QueryTotalSupply}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
|
|
|
var resp sdk.Coins
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &resp))
|
2020-04-20 08:22:12 -07:00
|
|
|
suite.Require().Equal(expectedTotalSupply.Total, resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *IntegrationTestSuite) TestQuerier_QueryTotalSupplyOf() {
|
|
|
|
app, ctx := suite.app, suite.ctx
|
2020-08-26 02:39:38 -07:00
|
|
|
legacyAmino := app.LegacyAmino()
|
2020-04-20 08:22:12 -07:00
|
|
|
test1Supply := sdk.NewInt64Coin("test1", 4000000)
|
|
|
|
test2Supply := sdk.NewInt64Coin("test2", 700000000)
|
2020-06-14 16:06:16 -07:00
|
|
|
expectedTotalSupply := types.NewSupply(sdk.NewCoins(test1Supply, test2Supply))
|
2021-02-17 10:20:33 -08:00
|
|
|
suite.
|
|
|
|
Require().
|
|
|
|
NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedTotalSupply.Total))
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
req := abci.RequestQuery{
|
|
|
|
Path: fmt.Sprintf("custom/%s/%s", types.ModuleName, types.QuerySupplyOf),
|
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
|
2020-04-20 08:22:12 -07:00
|
|
|
|
|
|
|
res, err := querier(ctx, []string{types.QuerySupplyOf}, req)
|
|
|
|
suite.Require().NotNil(err)
|
|
|
|
suite.Require().Nil(res)
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
req.Data = legacyAmino.MustMarshalJSON(types.NewQuerySupplyOfParams(test1Supply.Denom))
|
2020-04-20 08:22:12 -07:00
|
|
|
res, err = querier(ctx, []string{types.QuerySupplyOf}, req)
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(res)
|
|
|
|
|
2020-06-26 09:30:49 -07:00
|
|
|
var resp sdk.Coin
|
2020-08-26 02:39:38 -07:00
|
|
|
suite.Require().NoError(legacyAmino.UnmarshalJSON(res, &resp))
|
2020-06-26 09:30:49 -07:00
|
|
|
suite.Require().Equal(test1Supply, resp)
|
2020-04-20 08:22:12 -07:00
|
|
|
}
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
func (suite *IntegrationTestSuite) TestQuerierRouteNotFound() {
|
|
|
|
app, ctx := suite.app, suite.ctx
|
2020-08-26 02:39:38 -07:00
|
|
|
legacyAmino := app.LegacyAmino()
|
2019-06-18 06:44:09 -07:00
|
|
|
req := abci.RequestQuery{
|
2020-01-30 13:31:16 -08:00
|
|
|
Path: fmt.Sprintf("custom/%s/invalid", types.ModuleName),
|
2019-06-18 06:44:09 -07:00
|
|
|
Data: []byte{},
|
|
|
|
}
|
|
|
|
|
2020-08-26 02:39:38 -07:00
|
|
|
querier := keeper.NewQuerier(app.BankKeeper, legacyAmino)
|
2020-01-30 13:31:16 -08:00
|
|
|
_, err := querier(ctx, []string{"invalid"}, req)
|
|
|
|
suite.Error(err)
|
2019-06-18 06:44:09 -07:00
|
|
|
}
|