From 149f9de7a263fc8667f6368faea7a302e6a6f44a Mon Sep 17 00:00:00 2001 From: codchen Date: Tue, 13 Feb 2024 16:17:22 +0800 Subject: [PATCH] Expose add/sub balance methods on bank keeper (#432) ## Describe your changes and provide context ## Testing performed to validate your change --- x/bank/keeper/keeper.go | 14 +++---- x/bank/keeper/send.go | 79 ++++++++++++++++++++++++++-------------- x/bank/spec/04_events.md | 4 +- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index e74d868a1..8bad207e8 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -214,7 +214,7 @@ func (k BaseKeeper) DelegateCoins(ctx sdk.Context, delegatorAddr, moduleAccAddr types.NewCoinSpentEvent(delegatorAddr, amt), ) - err := k.addCoins(ctx, moduleAccAddr, amt, true) + err := k.AddCoins(ctx, moduleAccAddr, amt, true) if err != nil { return err } @@ -237,7 +237,7 @@ func (k BaseKeeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAdd return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String()) } - err := k.subUnlockedCoins(ctx, moduleAccAddr, amt, true) + err := k.SubUnlockedCoins(ctx, moduleAccAddr, amt, true) if err != nil { return err } @@ -246,7 +246,7 @@ func (k BaseKeeper) UndelegateCoins(ctx sdk.Context, moduleAccAddr, delegatorAdd return sdkerrors.Wrap(err, "failed to track undelegation") } - err = k.addCoins(ctx, delegatorAddr, amt, true) + err = k.AddCoins(ctx, delegatorAddr, amt, true) if err != nil { return err } @@ -410,7 +410,7 @@ func (k BaseKeeper) DeferredSendCoinsFromAccountToModule( panic("bank keeper created without deferred cache") } // Deducts Fees from the Sender Account - err := k.subUnlockedCoins(ctx, senderAddr, amount, true) + err := k.SubUnlockedCoins(ctx, senderAddr, amount, true) if err != nil { return err } @@ -468,7 +468,7 @@ func (k BaseKeeper) WriteDeferredBalances(ctx sdk.Context) []abci.Event { ctx.Logger().Error(err.Error()) panic(err) } - err := k.addCoins(ctx, sdk.MustAccAddressFromBech32(moduleBech32Addr), amount, true) + err := k.AddCoins(ctx, sdk.MustAccAddressFromBech32(moduleBech32Addr), amount, true) if err != nil { ctx.Logger().Error(fmt.Sprintf("Failed to add coin=%s to module address=%s, error is: %s", amount, moduleBech32Addr, err)) panic(err) @@ -570,7 +570,7 @@ func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amounts sdk.Co if acc == nil { return errors.New(fmt.Sprintf("module account for %s not found", moduleName)) } - return k.addCoins(ctx, acc.GetAddress(), amounts, true) + return k.AddCoins(ctx, acc.GetAddress(), amounts, true) } err := k.createCoins(ctx, moduleName, amounts, addFn) @@ -616,7 +616,7 @@ func (k BaseKeeper) destroyCoins(ctx sdk.Context, moduleName string, amounts sdk func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error { subFn := func(ctx sdk.Context, moduleName string, amounts sdk.Coins) error { acc := k.ak.GetModuleAccount(ctx, moduleName) - return k.subUnlockedCoins(ctx, acc.GetAddress(), amounts, true) + return k.SubUnlockedCoins(ctx, acc.GetAddress(), amounts, true) } err := k.destroyCoins(ctx, moduleName, amounts, subFn) diff --git a/x/bank/keeper/send.go b/x/bank/keeper/send.go index 401a815ba..b08728e05 100644 --- a/x/bank/keeper/send.go +++ b/x/bank/keeper/send.go @@ -21,6 +21,10 @@ type SendKeeper interface { SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsWithoutAccCreation(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error SendCoinsAndWei(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int, wei sdk.Int) error + SubUnlockedCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error + AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error + SubWei(ctx sdk.Context, addr sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int) error + AddWei(ctx sdk.Context, addr sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int) error GetParams(ctx sdk.Context) types.Params SetParams(ctx sdk.Context, params types.Params) @@ -89,7 +93,7 @@ func (k BaseSendKeeper) InputOutputCoins(ctx sdk.Context, inputs []types.Input, return err } - err = k.subUnlockedCoins(ctx, inAddress, in.Coins, true) + err = k.SubUnlockedCoins(ctx, inAddress, in.Coins, true) if err != nil { return err } @@ -107,7 +111,7 @@ func (k BaseSendKeeper) InputOutputCoins(ctx sdk.Context, inputs []types.Input, if err != nil { return err } - err = k.addCoins(ctx, outAddress, out.Coins, true) + err = k.AddCoins(ctx, outAddress, out.Coins, true) if err != nil { return err } @@ -159,12 +163,12 @@ func (k BaseSendKeeper) SendCoinsWithoutAccCreation(ctx sdk.Context, fromAddr sd } func (k BaseSendKeeper) sendCoinsWithoutAccCreation(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error { - err := k.subUnlockedCoins(ctx, fromAddr, amt, checkNeg) + err := k.SubUnlockedCoins(ctx, fromAddr, amt, checkNeg) if err != nil { return err } - err = k.addCoins(ctx, toAddr, amt, checkNeg) + err = k.AddCoins(ctx, toAddr, amt, checkNeg) if err != nil { return err } @@ -185,10 +189,10 @@ func (k BaseSendKeeper) sendCoinsWithoutAccCreation(ctx sdk.Context, fromAddr sd return nil } -// subUnlockedCoins removes the unlocked amt coins of the given account. An error is +// SubUnlockedCoins removes the unlocked amt coins of the given account. An error is // returned if the resulting balance is negative or the initial amount is invalid. // A coin_spent event is emitted after. -func (k BaseSendKeeper) subUnlockedCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error { +func (k BaseSendKeeper) SubUnlockedCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error { if !amt.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String()) } @@ -227,9 +231,9 @@ func (k BaseSendKeeper) subUnlockedCoins(ctx sdk.Context, addr sdk.AccAddress, a return nil } -// addCoins increase the addr balance by the given amt. Fails if the provided amt is invalid. +// AddCoins increase the addr balance by the given amt. Fails if the provided amt is invalid. // It emits a coin received event. -func (k BaseSendKeeper) addCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error { +func (k BaseSendKeeper) AddCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins, checkNeg bool) error { if !amt.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, amt.String()) } @@ -328,52 +332,73 @@ func (k BaseSendKeeper) BlockedAddr(addr sdk.AccAddress) bool { return k.blockedAddrs[addr.String()] } -func (k BaseSendKeeper) SendCoinsAndWei(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int, wei sdk.Int) error { - if wei.Equal(sdk.ZeroInt()) { - if amt.Equal(sdk.ZeroInt()) { - return nil - } - return k.SendCoinsWithoutAccCreation(ctx, from, to, sdk.NewCoins(sdk.NewCoin(denom, amt))) +func (k BaseSendKeeper) SubWei(ctx sdk.Context, addr sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int) error { + if amt.Equal(sdk.ZeroInt()) { + return nil } - if wei.GTE(MaxWeiBalance) { + if amt.GTE(MaxWeiBalance) { return errors.New("cannot send more than 10^12 wei") } escrow := customEscrow if escrow == nil { escrow = k.ak.GetModuleAddress(types.WeiEscrowName) } - currentWeiBalanceFrom := k.GetWeiBalance(ctx, from) - postWeiBalanceFrom := currentWeiBalanceFrom.Sub(wei) - if postWeiBalanceFrom.GTE(sdk.ZeroInt()) { - if err := k.setWeiBalance(ctx, from, postWeiBalanceFrom); err != nil { + currentWeiBalance := k.GetWeiBalance(ctx, addr) + postWeiBalance := currentWeiBalance.Sub(amt) + if postWeiBalance.GTE(sdk.ZeroInt()) { + if err := k.setWeiBalance(ctx, addr, postWeiBalance); err != nil { return err } } else { - if err := k.setWeiBalance(ctx, from, MaxWeiBalance.Add(postWeiBalanceFrom)); err != nil { + if err := k.setWeiBalance(ctx, addr, MaxWeiBalance.Add(postWeiBalance)); err != nil { // postWeiBalanceFrom is negative return err } // need to send one sei to escrow because wei balance is insufficient - if err := k.sendCoinsWithoutAccCreation(ctx, from, escrow, sdk.NewCoins(sdk.NewCoin(denom, sdk.OneInt())), false); err != nil { + if err := k.sendCoinsWithoutAccCreation(ctx, addr, escrow, sdk.NewCoins(sdk.NewCoin(denom, sdk.OneInt())), false); err != nil { return err } } - currentWeiBalanceTo := k.GetWeiBalance(ctx, to) - postWeiBalanceTo := currentWeiBalanceTo.Add(wei) - if postWeiBalanceTo.LT(MaxWeiBalance) { - if err := k.setWeiBalance(ctx, to, postWeiBalanceTo); err != nil { + return nil +} + +func (k BaseSendKeeper) AddWei(ctx sdk.Context, addr sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int) error { + if amt.Equal(sdk.ZeroInt()) { + return nil + } + if amt.GTE(MaxWeiBalance) { + return errors.New("cannot send more than 10^12 wei") + } + escrow := customEscrow + if escrow == nil { + escrow = k.ak.GetModuleAddress(types.WeiEscrowName) + } + currentWeiBalance := k.GetWeiBalance(ctx, addr) + postWeiBalance := currentWeiBalance.Add(amt) + if postWeiBalance.LT(MaxWeiBalance) { + if err := k.setWeiBalance(ctx, addr, postWeiBalance); err != nil { return err } } else { - if err := k.setWeiBalance(ctx, to, postWeiBalanceTo.Sub(MaxWeiBalance)); err != nil { + if err := k.setWeiBalance(ctx, addr, postWeiBalance.Sub(MaxWeiBalance)); err != nil { return err } // need to redeem one sei from escrow because wei balance overflowed one := sdk.NewCoins(sdk.NewCoin(denom, sdk.OneInt())) - if err := k.sendCoinsWithoutAccCreation(ctx, escrow, to, one, false); err != nil { + if err := k.sendCoinsWithoutAccCreation(ctx, escrow, addr, one, false); err != nil { return err } } + return nil +} + +func (k BaseSendKeeper) SendCoinsAndWei(ctx sdk.Context, from sdk.AccAddress, to sdk.AccAddress, customEscrow sdk.AccAddress, denom string, amt sdk.Int, wei sdk.Int) error { + if err := k.SubWei(ctx, from, customEscrow, denom, wei); err != nil { + return err + } + if err := k.AddWei(ctx, to, customEscrow, denom, wei); err != nil { + return err + } if amt.GT(sdk.ZeroInt()) { return k.SendCoinsWithoutAccCreation(ctx, from, to, sdk.NewCoins(sdk.NewCoin(denom, amt))) } diff --git a/x/bank/spec/04_events.md b/x/bank/spec/04_events.md index e71b82057..92dde22c1 100644 --- a/x/bank/spec/04_events.md +++ b/x/bank/spec/04_events.md @@ -108,7 +108,7 @@ In addition to handlers events, the bank keeper will produce events when the fol } ``` -### addCoins +### AddCoins ```json { @@ -128,7 +128,7 @@ In addition to handlers events, the bank keeper will produce events when the fol } ``` -### subUnlockedCoins/DelegateCoins +### SubUnlockedCoins/DelegateCoins ```json {