From b7f03164485e612db6fedbd46fe2628887a5ed33 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Mon, 9 Dec 2024 18:33:07 +0100 Subject: [PATCH] =?UTF-8?q?Update=20CancelPerpetualOrder=20to=20return=20a?= =?UTF-8?q?ll=20available=20balances=20to=20the=20o=E2=80=A6=20(#1053)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update CancelPerpetualOrder to return all available balances to the owner - Updated the CancelPerpetualOrder function to retrieve and send all available balances from the order address back to the owner, enhancing the balance management process. - This change ensures that users receive the full amount of their collateral and any other balances associated with the order upon cancellation. --- .../keeper/msg_server_perpetual_order.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/x/tradeshield/keeper/msg_server_perpetual_order.go b/x/tradeshield/keeper/msg_server_perpetual_order.go index 6049536b5..4b0638a53 100644 --- a/x/tradeshield/keeper/msg_server_perpetual_order.go +++ b/x/tradeshield/keeper/msg_server_perpetual_order.go @@ -179,11 +179,17 @@ func (k msgServer) CancelPerpetualOrder(goCtx context.Context, msg *types.MsgCan return nil, errorsmod.Wrap(sdkerrors.ErrUnauthorized, "incorrect owner") } - // send the collateral amount back to the owner - ownerAddress := sdk.MustAccAddressFromBech32(order.OwnerAddress) - err := k.Keeper.bank.SendCoins(ctx, order.GetOrderAddress(), ownerAddress, sdk.NewCoins(order.Collateral)) - if err != nil { - return nil, err + // Get all balances from the spot order address + orderAddress := order.GetOrderAddress() + balances := k.Keeper.bank.GetAllBalances(ctx, orderAddress) + + // Send all available balances back to the owner if there are any + if !balances.IsZero() { + ownerAddress := sdk.MustAccAddressFromBech32(order.OwnerAddress) + err := k.Keeper.bank.SendCoins(ctx, orderAddress, ownerAddress, balances) + if err != nil { + return nil, err + } } k.RemovePendingPerpetualOrder(ctx, msg.OrderId)