Skip to content

Commit

Permalink
cleanup bank
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Oct 22, 2024
1 parent 2b9ff60 commit fad5a66
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 77 deletions.
3 changes: 3 additions & 0 deletions x/bank/keeper/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ func TestBankStateCompatibility(t *testing.T) {
authority, err := ac.BytesToString(authtypes.NewModuleAddress(banktypes.GovModuleName))
require.NoError(t, err)

modaccs := runtime.NewModuleAccountsService(testModuleAccounts...)

k := keeper.NewBaseKeeper(
env,
encCfg.Codec,
authKeeper,
map[string]bool{addr: true},
authority,
modaccs,
)

// test we can decode balances without problems
Expand Down
1 change: 0 additions & 1 deletion x/bank/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func (suite *KeeperTestSuite) TestExportGenesis() {
panic(err1)
}
// set balances via mint and send
suite.mockMintCoins(mintAcc)
suite.
Require().
NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, expectedBalances[i].Coins))
Expand Down
5 changes: 0 additions & 5 deletions x/bank/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ func (suite *KeeperTestSuite) TestQueryTotalSupply() {
genesisSupply := res.Supply
testCoins := sdk.NewCoins(sdk.NewInt64Coin("test", 400000000))

suite.mockMintCoins(mintAcc)
suite.Require().NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, testCoins))

res, err = queryClient.TotalSupply(gocontext.Background(), &types.QueryTotalSupplyRequest{})
Expand All @@ -315,7 +314,6 @@ func (suite *KeeperTestSuite) TestQueryTotalSupplyOf() {
test2Supply := sdk.NewInt64Coin("test2", 700000000)
expectedTotalSupply := sdk.NewCoins(test1Supply, test2Supply)

suite.mockMintCoins(mintAcc)
suite.Require().NoError(suite.bankKeeper.MintCoins(ctx, types.MintModuleName, expectedTotalSupply))

_, err := queryClient.SupplyOf(gocontext.Background(), &types.QuerySupplyOfRequest{})
Expand Down Expand Up @@ -695,7 +693,6 @@ func (suite *KeeperTestSuite) TestGRPCDenomOwners() {

keeper := suite.bankKeeper

suite.mockMintCoins(mintAcc)
suite.Require().NoError(keeper.MintCoins(ctx, types.MintModuleName, initCoins))

for i := 0; i < 10; i++ {
Expand Down Expand Up @@ -916,11 +913,9 @@ func (suite *KeeperTestSuite) TestGRPCDenomOwnersByQuery() {

keeper := suite.bankKeeper

suite.mockMintCoins(mintAcc)
suite.Require().NoError(keeper.MintCoins(ctx, types.MintModuleName, initCoins))
denom := "ibc/123123213123"
newCoins := sdk.NewCoins(sdk.NewCoin(denom, initTokens))
suite.mockMintCoins(mintAcc)
suite.Require().NoError(keeper.MintCoins(ctx, types.MintModuleName, newCoins))

for i := 0; i < 10; i++ {
Expand Down
26 changes: 7 additions & 19 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ func (k BaseKeeper) WithMintCoinsRestriction(check types.MintingRestrictionFn) B
// address to a ModuleAccount address. If any of the delegation amounts are negative,
// an error is returned.
func (k BaseKeeper) DelegateCoins(ctx context.Context, delegatorAddr, moduleAccAddr sdk.AccAddress, amt sdk.Coins) error {
moduleAcc := k.ak.GetAccount(ctx, moduleAccAddr)
if moduleAcc == nil {
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleAccAddr)
}

if !amt.IsValid() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}
Expand Down Expand Up @@ -172,11 +167,6 @@ func (k BaseKeeper) DelegateCoins(ctx context.Context, delegatorAddr, moduleAccA
// address to the delegator address. If any of the undelegation amounts are
// negative, an error is returned.
func (k BaseKeeper) UndelegateCoins(ctx context.Context, moduleAccAddr, delegatorAddr sdk.AccAddress, amt sdk.Coins) error {
moduleAcc := k.ak.GetAccount(ctx, moduleAccAddr)
if moduleAcc == nil {
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleAccAddr)
}

if !amt.IsValid() {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, amt.String())
}
Expand Down Expand Up @@ -334,8 +324,11 @@ func (k BaseKeeper) MintCoins(ctx context.Context, moduleName string, amounts sd
}

moduleAddr := k.moduleAccountsService.Address(moduleName)
if moduleAddr == nil {
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName)
}

if !k.moduleAccountsService.HasPermission(moduleName, "mint") {
if !k.moduleAccountsService.HasPermission(moduleName, "minter") {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "module account %s does not have permissions to mint tokens", moduleName)
}

Expand Down Expand Up @@ -372,14 +365,9 @@ func (k BaseKeeper) MintCoins(ctx context.Context, moduleName string, amounts sd
// BurnCoins burns coins deletes coins from the balance of an account.
// An error is returned if the module account does not exist or is unauthorized.
func (k BaseKeeper) BurnCoins(ctx context.Context, address []byte, amounts sdk.Coins) error {
acc := k.ak.GetAccount(ctx, address)
if acc == nil {
return errorsmod.Wrapf(sdkerrors.ErrUnknownAddress, "account %x does not exist", address)
}

moduleName := k.moduleAccountsService.IsModuleAccount(address)
if moduleName != "" {
if !k.moduleAccountsService.HasPermission(moduleName, "burn") {
if !k.moduleAccountsService.HasPermission(moduleName, "burner") {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "account %x does not have permissions to burn tokens", address)
}
}
Expand All @@ -388,7 +376,7 @@ func (k BaseKeeper) BurnCoins(ctx context.Context, address []byte, amounts sdk.C
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, amounts.String())
}

err := k.subUnlockedCoins(ctx, acc.GetAddress(), amounts)
err := k.subUnlockedCoins(ctx, address, amounts)
if err != nil {
return err
}
Expand All @@ -399,7 +387,7 @@ func (k BaseKeeper) BurnCoins(ctx context.Context, address []byte, amounts sdk.C
k.setSupply(ctx, supply)
}

addrStr, err := k.addrCdc.BytesToString(acc.GetAddress())
addrStr, err := k.addrCdc.BytesToString(address)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit fad5a66

Please sign in to comment.