Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions tests/integration/x/precisebank/test_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,6 @@ func (s *GenesisTestSuite) SetupTestWithChainID(chainID testconstants.ChainID) {
})
}

func (s *GenesisTestSuite) adjustModuleBalance(expectedAmt sdkmath.Int) {
moduleAddr := s.network.App.GetAccountKeeper().GetModuleAddress(types.ModuleName)
balance := s.network.App.GetBankKeeper().GetBalance(s.network.GetContext(), moduleAddr, types.IntegerCoinDenom())

if balance.Amount.GT(expectedAmt) {
// Burn excess
err := s.network.App.GetBankKeeper().BurnCoins(
s.network.GetContext(),
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), balance.Amount.Sub(expectedAmt))),
)
s.Require().NoError(err)
} else if balance.Amount.LT(expectedAmt) {
// Mint deficit
err := s.network.App.GetBankKeeper().MintCoins(
s.network.GetContext(),
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), expectedAmt.Sub(balance.Amount))),
)
s.Require().NoError(err)
}
}

func (s *GenesisTestSuite) TestInitGenesis() {
tests := []struct {
name string
Expand All @@ -95,7 +72,8 @@ func (s *GenesisTestSuite) TestInitGenesis() {
func() {
// The network setup creates an initial balance of 1, so we need to mint 1 more
// to get to the expected amount of 2 for this test case
s.adjustModuleBalance(sdkmath.NewInt(2))
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, (sdkmath.NewInt(2)))
s.Require().NoError(err)
},
types.NewGenesisState(
types.FractionalBalances{
Expand Down Expand Up @@ -125,7 +103,8 @@ func (s *GenesisTestSuite) TestInitGenesis() {
func() {
// The network setup creates an initial balance of 1, so we need to burn that
// to get to 0 balance for this test case
s.adjustModuleBalance(sdkmath.ZeroInt())
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, (sdkmath.ZeroInt()))
s.Require().NoError(err)
},
types.NewGenesisState(
types.FractionalBalances{
Expand All @@ -143,7 +122,8 @@ func (s *GenesisTestSuite) TestInitGenesis() {
func() {
// The network setup creates an initial balance of 1, so we need to mint 99 more
// to get to 100 total balance for this test case
s.adjustModuleBalance(sdkmath.NewInt(100))
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, (sdkmath.NewInt(100)))
s.Require().NoError(err)
},
types.NewGenesisState(
types.FractionalBalances{
Expand Down Expand Up @@ -243,7 +223,8 @@ func (s *GenesisTestSuite) TestExportGenesis() {
"balances, no remainder",
func() *types.GenesisState {
// Burn the initial balance created by network setup, then mint the expected amount
s.adjustModuleBalance(sdkmath.NewInt(1))
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, (sdkmath.NewInt(1)))
s.Require().NoError(err)

return types.NewGenesisState(
types.FractionalBalances{
Expand All @@ -258,7 +239,8 @@ func (s *GenesisTestSuite) TestExportGenesis() {
"balances, remainder",
func() *types.GenesisState {
// Burn the initial balance created by network setup, then mint the expected amount
s.adjustModuleBalance(sdkmath.NewInt(1))
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, (sdkmath.NewInt(1)))
s.Require().NoError(err)

return types.NewGenesisState(
types.FractionalBalances{
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/x/precisebank/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package precisebank

import (
"github.com/cosmos/evm"
"github.com/cosmos/evm/x/precisebank/types"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -91,6 +92,31 @@ func ConvertCoinsToExtendedCoinDenom(coins sdk.Coins) sdk.Coins {
return coins.Sub(integerCoin).Add(extendedCoin)
}

// AdjustModuleBalance adjusts the precisebank module account balance to the expected amount.
// TODO: This is a test utility function and should ideally not be needed.
// Consider refactoring tests to avoid needing this.
func AdjustModuleBalance(sdkCtx sdk.Context, evmApp evm.EvmApp, expectedAmt sdkmath.Int) error {
moduleAddr := evmApp.GetAccountKeeper().GetModuleAddress(types.ModuleName)
balance := evmApp.GetBankKeeper().GetBalance(sdkCtx, moduleAddr, types.IntegerCoinDenom())

if balance.Amount.GT(expectedAmt) {
// Burn excess
return evmApp.GetBankKeeper().BurnCoins(
sdkCtx,
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), balance.Amount.Sub(expectedAmt))),
)
} else if balance.Amount.LT(expectedAmt) {
// Mint deficit
return evmApp.GetBankKeeper().MintCoins(
sdkCtx,
types.ModuleName,
sdk.NewCoins(sdk.NewCoin(types.IntegerCoinDenom(), expectedAmt.Sub(balance.Amount))),
)
}
return nil
}

func c(denom string, amount int64) sdk.Coin { return sdk.NewInt64Coin(denom, amount) }
func ci(denom string, amount sdkmath.Int) sdk.Coin { return sdk.NewCoin(denom, amount) }
func cs(coins ...sdk.Coin) sdk.Coins { return sdk.NewCoins(coins...) }
10 changes: 7 additions & 3 deletions tests/integration/x/precisebank/test_view_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,16 @@ func (s *KeeperIntegrationTestSuite) TestKeeperHiddenReserve() {
),
)

// Adjust the module balance to be exactly 1 integer coin
err := AdjustModuleBalance(s.network.GetContext(), s.network.App, sdkmath.NewInt(1))
s.Require().NoError(err)

// Check underlying x/bank balance for reserve
reserveIntCoin := s.network.App.GetBankKeeper().GetBalance(s.network.GetContext(), moduleAddr, types.IntegerCoinDenom())
s.Require().Equal(
sdkmath.NewInt(2), // Network setup creates 1, test mints 1 more = 2 total
sdkmath.NewInt(1),
reserveIntCoin.Amount,
"reserve should hold 2 integer coins (1 from network setup + 1 from test mint)",
"reserve should hold 1 integer coin",
)

tests := []struct {
Expand All @@ -160,7 +164,7 @@ func (s *KeeperIntegrationTestSuite) TestKeeperHiddenReserve() {
"reserve account - visible integer denom",
moduleAddr,
types.IntegerCoinDenom(),
sdkmath.NewInt(2), // Network setup creates 1, test mints 1 more = 2 total
sdkmath.OneInt(),
},
{
"user account - visible extended denom",
Expand Down
Loading