Skip to content

Commit

Permalink
Update CancelPerpetualOrder to return all available balances to the o… (
Browse files Browse the repository at this point in the history
#1053)

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.
  • Loading branch information
cosmic-vagabond authored Dec 9, 2024
1 parent 970f1b3 commit b7f0316
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions x/tradeshield/keeper/msg_server_perpetual_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b7f0316

Please sign in to comment.