-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #611 from neutron-org/chore/split-dex-calc-and-exe…
…cution
- Loading branch information
Showing
8 changed files
with
816 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.