github.com/Finschia/finschia-sdk@v0.48.1/x/bank/keeper/genesis_test.go (about)

     1  package keeper_test
     2  
     3  import (
     4  	sdk "github.com/Finschia/finschia-sdk/types"
     5  	"github.com/Finschia/finschia-sdk/types/query"
     6  	"github.com/Finschia/finschia-sdk/x/bank/types"
     7  	minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
     8  )
     9  
    10  func (suite *IntegrationTestSuite) TestExportGenesis() {
    11  	app, ctx := suite.app, suite.ctx
    12  
    13  	expectedMetadata := suite.getTestMetadata()
    14  	expectedBalances, totalSupply := suite.getTestBalancesAndSupply()
    15  	for i := range []int{1, 2} {
    16  		app.BankKeeper.SetDenomMetaData(ctx, expectedMetadata[i])
    17  		accAddr, err1 := sdk.AccAddressFromBech32(expectedBalances[i].Address)
    18  		if err1 != nil {
    19  			panic(err1)
    20  		}
    21  		// set balances via mint and send
    22  		suite.
    23  			Require().
    24  			NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, expectedBalances[i].Coins))
    25  		suite.
    26  			Require().
    27  			NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, accAddr, expectedBalances[i].Coins))
    28  	}
    29  	app.BankKeeper.SetParams(ctx, types.DefaultParams())
    30  
    31  	exportGenesis := app.BankKeeper.ExportGenesis(ctx)
    32  
    33  	suite.Require().Len(exportGenesis.Params.SendEnabled, 0)
    34  	suite.Require().Equal(types.DefaultParams().DefaultSendEnabled, exportGenesis.Params.DefaultSendEnabled)
    35  	suite.Require().Equal(totalSupply, exportGenesis.Supply)
    36  	suite.Require().Equal(expectedBalances, exportGenesis.Balances)
    37  	suite.Require().Equal(expectedMetadata, exportGenesis.DenomMetadata)
    38  }
    39  
    40  func (suite *IntegrationTestSuite) getTestBalancesAndSupply() ([]types.Balance, sdk.Coins) {
    41  	addr2, _ := sdk.AccAddressFromBech32("link1f9xjhxm0plzrh9cskf4qee4pc2xwp0n0p662v8")
    42  	addr1, _ := sdk.AccAddressFromBech32("link1t5u0jfg3ljsjrh2m9e47d4ny2hea7eehndz0n9")
    43  	addr1Balance := sdk.Coins{sdk.NewInt64Coin("testcoin3", 10)}
    44  	addr2Balance := sdk.Coins{sdk.NewInt64Coin("testcoin1", 32), sdk.NewInt64Coin("testcoin2", 34)}
    45  
    46  	totalSupply := addr1Balance
    47  	totalSupply = totalSupply.Add(addr2Balance...)
    48  
    49  	return []types.Balance{
    50  		{Address: addr2.String(), Coins: addr2Balance},
    51  		{Address: addr1.String(), Coins: addr1Balance},
    52  	}, totalSupply
    53  }
    54  
    55  func (suite *IntegrationTestSuite) TestInitGenesis() {
    56  	m := types.Metadata{Description: sdk.DefaultBondDenom, Base: sdk.DefaultBondDenom, Display: sdk.DefaultBondDenom}
    57  	g := types.DefaultGenesisState()
    58  	g.DenomMetadata = []types.Metadata{m}
    59  	bk := suite.app.BankKeeper
    60  	bk.InitGenesis(suite.ctx, g)
    61  
    62  	m2, found := bk.GetDenomMetaData(suite.ctx, m.Base)
    63  	suite.Require().True(found)
    64  	suite.Require().Equal(m, m2)
    65  }
    66  
    67  func (suite *IntegrationTestSuite) TestTotalSupply() {
    68  	// Prepare some test data.
    69  	defaultGenesis := types.DefaultGenesisState()
    70  	balances := []types.Balance{
    71  		{Coins: sdk.NewCoins(sdk.NewCoin("foocoin", sdk.NewInt(1))), Address: "link1f9xjhxm0plzrh9cskf4qee4pc2xwp0n0p662v8"},
    72  		{Coins: sdk.NewCoins(sdk.NewCoin("barcoin", sdk.NewInt(1))), Address: "link1t5u0jfg3ljsjrh2m9e47d4ny2hea7eehndz0n9"},
    73  		{Coins: sdk.NewCoins(sdk.NewCoin("foocoin", sdk.NewInt(10)), sdk.NewCoin("barcoin", sdk.NewInt(20))), Address: "link1m3h30wlvsf8llruxtpukdvsy0km2kum8al86ug"},
    74  	}
    75  	totalSupply := sdk.NewCoins(sdk.NewCoin("foocoin", sdk.NewInt(11)), sdk.NewCoin("barcoin", sdk.NewInt(21)))
    76  
    77  	testcases := []struct {
    78  		name        string
    79  		genesis     *types.GenesisState
    80  		expSupply   sdk.Coins
    81  		expPanic    bool
    82  		expPanicMsg string
    83  	}{
    84  		{
    85  			"calculation NOT matching genesis Supply field",
    86  			types.NewGenesisState(defaultGenesis.Params, balances, sdk.NewCoins(sdk.NewCoin("wrongcoin", sdk.NewInt(1))), defaultGenesis.DenomMetadata),
    87  			nil, true, "genesis supply is incorrect, expected 1wrongcoin, got 21barcoin,11foocoin",
    88  		},
    89  		{
    90  			"calculation matches genesis Supply field",
    91  			types.NewGenesisState(defaultGenesis.Params, balances, totalSupply, defaultGenesis.DenomMetadata),
    92  			totalSupply, false, "",
    93  		},
    94  		{
    95  			"calculation is correct, empty genesis Supply field",
    96  			types.NewGenesisState(defaultGenesis.Params, balances, nil, defaultGenesis.DenomMetadata),
    97  			totalSupply, false, "",
    98  		},
    99  	}
   100  
   101  	for _, tc := range testcases {
   102  		tc := tc
   103  		suite.Run(tc.name, func() {
   104  			if tc.expPanic {
   105  				suite.PanicsWithError(tc.expPanicMsg, func() { suite.app.BankKeeper.InitGenesis(suite.ctx, tc.genesis) })
   106  			} else {
   107  				suite.app.BankKeeper.InitGenesis(suite.ctx, tc.genesis)
   108  				totalSupply, _, err := suite.app.BankKeeper.GetPaginatedTotalSupply(suite.ctx, &query.PageRequest{Limit: query.MaxLimit})
   109  				suite.Require().NoError(err)
   110  				suite.Require().Equal(tc.expSupply, totalSupply)
   111  			}
   112  		})
   113  	}
   114  }