Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: split dex execution from banking operations #NTRN-353 #611

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions x/dex/keeper/cancel_limit_order.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package keeper

import (
"context"

sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/neutron-org/neutron/v4/x/dex/types"
)

// CancelLimitOrderCore handles the logic for MsgCancelLimitOrder including bank operations and event emissions.
func (k Keeper) CancelLimitOrderCore(
goCtx context.Context,
trancheKey string,
callerAddr sdk.AccAddress,
) error {
ctx := sdk.UnwrapSDKContext(goCtx)

coinOut, tradePairID, err := k.ExecuteCancelLimitOrder(ctx, trancheKey, callerAddr)
if err != nil {
return err
}

err = k.bankKeeper.SendCoinsFromModuleToAccount(
ctx,
types.ModuleName,
callerAddr,
sdk.Coins{coinOut},
)
if err != nil {
return err
}

// This will never panic since TradePairID has already been successfully constructed by ExecuteCancelLimitOrder
pairID := tradePairID.MustPairID()
ctx.EventManager().EmitEvent(types.CancelLimitOrderEvent(
callerAddr,
pairID.Token0,
pairID.Token1,
tradePairID.MakerDenom,
tradePairID.TakerDenom,
coinOut.Amount,
trancheKey,
))

return nil
}

// ExecuteCancelLimitOrder handles the core logic for CancelLimitOrder -- removing remaining TokenIn from the
// LimitOrderTranche and returning it to the user, updating the number of canceled shares on the LimitOrderTrancheUser.
// IT DOES NOT PERFORM ANY BANKING OPERATIONS
func (k Keeper) ExecuteCancelLimitOrder(
ctx sdk.Context,
trancheKey string,
callerAddr sdk.AccAddress,
) (coinOut sdk.Coin, tradePairID *types.TradePairID, error error) {
trancheUser, found := k.GetLimitOrderTrancheUser(ctx, callerAddr.String(), trancheKey)
if !found {
return sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
}

tradePairID, tickIndex := trancheUser.TradePairId, trancheUser.TickIndexTakerToMaker
tranche := k.GetLimitOrderTranche(
ctx,
&types.LimitOrderTrancheKey{
TradePairId: tradePairID,
TickIndexTakerToMaker: tickIndex,
TrancheKey: trancheKey,
},
)
if tranche == nil {
return sdk.Coin{}, nil, types.ErrActiveLimitOrderNotFound
}

amountToCancel := tranche.RemoveTokenIn(trancheUser)
trancheUser.SharesCancelled = trancheUser.SharesCancelled.Add(amountToCancel)

if !amountToCancel.IsPositive() {
return sdk.Coin{}, nil, sdkerrors.Wrapf(types.ErrCancelEmptyLimitOrder, "%s", tranche.Key.TrancheKey)
}

k.SaveTrancheUser(ctx, trancheUser)
k.SaveTranche(ctx, tranche)

if trancheUser.OrderType.HasExpiration() {
k.RemoveLimitOrderExpiration(ctx, *tranche.ExpirationTime, tranche.Key.KeyMarshal())
}
coinOut = sdk.NewCoin(tradePairID.MakerDenom, amountToCancel)

return coinOut, tradePairID, nil
}
Loading
Loading