From a79a6dc6a72fe2bf734d5ac79c90a65fbfd1591b Mon Sep 17 00:00:00 2001 From: scorpioborn <97235353+scorpioborn@users.noreply.github.com> Date: Thu, 30 Nov 2023 17:55:50 +0300 Subject: [PATCH] fix: minimum amount between subaccount deduct and withdrawable --- x/subaccount/keeper/balance.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/x/subaccount/keeper/balance.go b/x/subaccount/keeper/balance.go index 2d886944..063f0069 100644 --- a/x/subaccount/keeper/balance.go +++ b/x/subaccount/keeper/balance.go @@ -167,23 +167,13 @@ func (k Keeper) withdrawLockedAndUnlocked(ctx sdk.Context, subAccAddr sdk.AccAdd accSummary, unlockedBalance, bankBalance := k.getAccountSummary(ctx, subAccAddr) withdrawableBalance := accSummary.WithdrawableBalance(unlockedBalance, bankBalance.Amount) - if withdrawableBalance.GT(sdkmath.ZeroInt()) { - if err := k.bankKeeper.SendCoins(ctx, - subAccAddr, - ownerAddr, - sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, withdrawableBalance))); err != nil { - return sdkerrors.Wrapf(types.ErrSendCoinError, "error sending coin from subaccount to main account %s", err) - } - } + // take the minimum of the withdrawable balance and the amount that need to be transferred from sub account + withdrawableToSend := sdkmath.MinInt(withdrawableBalance, subAmountDeduct) - lockedAmountToWithdraw := subAmountDeduct.Sub(withdrawableBalance) + // calculate the amount that need to be deducted from locked balances + lockedAmountToWithdraw := subAmountDeduct.Sub(withdrawableToSend) if lockedAmountToWithdraw.GT(sdkmath.ZeroInt()) { - if err := k.bankKeeper.SendCoins(ctx, subAccAddr, ownerAddr, - sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, lockedAmountToWithdraw))); err != nil { - return sdkerrors.Wrapf(types.ErrSendCoinError, "error sending coin from subaccount to main account %s", err) - } - // calculate locked balances lockedBalances := k.GetLockedBalances(ctx, subAccAddr) updatedLockedBalances := []types.LockedBalance{} @@ -211,6 +201,15 @@ func (k Keeper) withdrawLockedAndUnlocked(ctx sdk.Context, subAccAddr sdk.AccAdd k.SetLockedBalances(ctx, subAccAddr, updatedLockedBalances) } + // send the total calculated amount to the owner + if err := k.bankKeeper.SendCoins(ctx, + subAccAddr, + ownerAddr, + sdk.NewCoins(sdk.NewCoin(params.DefaultBondDenom, subAmountDeduct))); err != nil { + return sdkerrors.Wrapf(types.ErrSendCoinError, "error sending coin from subaccount to main account %s", err) + } + + // update account summary withdrawn amount if err := accSummary.Withdraw(subAmountDeduct); err != nil { return sdkerrors.Wrapf(types.ErrWithdrawLocked, "%s", err) }