Skip to content

Commit

Permalink
Expose add/sub balance methods on bank keeper (#432)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context

## Testing performed to validate your change
  • Loading branch information
codchen authored and udpatil committed Mar 27, 2024
1 parent 67787c9 commit 149f9de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 36 deletions.
14 changes: 7 additions & 7 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
79 changes: 52 additions & 27 deletions x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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())
}
Expand Down Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)))
}
Expand Down
4 changes: 2 additions & 2 deletions x/bank/spec/04_events.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ In addition to handlers events, the bank keeper will produce events when the fol
}
```

### addCoins
### AddCoins

```json
{
Expand All @@ -128,7 +128,7 @@ In addition to handlers events, the bank keeper will produce events when the fol
}
```

### subUnlockedCoins/DelegateCoins
### SubUnlockedCoins/DelegateCoins

```json
{
Expand Down

0 comments on commit 149f9de

Please sign in to comment.