Skip to content

Commit

Permalink
Merge pull request #753 from neutron-org/chore/add_min_avg_limit_attr
Browse files Browse the repository at this point in the history
Chore: Add minAvgSellPrice attr to PlaceLimitOrderEvent
  • Loading branch information
pr0n00gler authored Oct 26, 2024
2 parents 6724550 + cb465ca commit 3ce1b5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion x/dex/keeper/grpc_query_simulate_place_limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (k Keeper) SimulatePlaceLimitOrder(
return nil, errors.Wrapf(err, "invalid LimitSellPrice %s", msg.LimitSellPrice.String())
}
}
trancheKey, totalIn, takerCoinIn, takerCoinOut, _, err := k.ExecutePlaceLimitOrder(
trancheKey, totalIn, takerCoinIn, takerCoinOut, _, _, err := k.ExecutePlaceLimitOrder(
cacheCtx,
takerTradePairID,
msg.AmountIn,
Expand Down
32 changes: 20 additions & 12 deletions x/dex/keeper/place_limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (k Keeper) PlaceLimitOrderCore(
orderType types.LimitOrderType,
goodTil *time.Time,
maxAmountOut *math.Int,
minAvgSellPrice *math_utils.PrecDec,
minAvgSellPriceP *math_utils.PrecDec,
callerAddr sdk.AccAddress,
receiverAddr sdk.AccAddress,
) (trancheKey string, totalInCoin, swapInCoin, swapOutCoin sdk.Coin, err error) {
Expand All @@ -31,15 +31,15 @@ func (k Keeper) PlaceLimitOrderCore(
if err != nil {
return trancheKey, totalInCoin, swapInCoin, swapOutCoin, err
}
trancheKey, totalIn, swapInCoin, swapOutCoin, sharesIssued, err := k.ExecutePlaceLimitOrder(
trancheKey, totalIn, swapInCoin, swapOutCoin, sharesIssued, minAvgSellPrice, err := k.ExecutePlaceLimitOrder(
ctx,
takerTradePairID,
amountIn,
tickIndexInToOut,
orderType,
goodTil,
maxAmountOut,
minAvgSellPrice,
minAvgSellPriceP,
receiverAddr,
)
if err != nil {
Expand Down Expand Up @@ -84,6 +84,7 @@ func (k Keeper) PlaceLimitOrderCore(
totalIn,
tickIndexInToOut,
orderType.String(),
minAvgSellPrice,
sharesIssued,
trancheKey,
swapInCoin.Amount,
Expand All @@ -106,16 +107,23 @@ func (k Keeper) ExecutePlaceLimitOrder(
maxAmountOut *math.Int,
minAvgSellPriceP *math_utils.PrecDec,
receiverAddr sdk.AccAddress,
) (trancheKey string, totalIn math.Int, swapInCoin, swapOutCoin sdk.Coin, sharesIssued math.Int, err error) {
) (
trancheKey string,
totalIn math.Int,
swapInCoin, swapOutCoin sdk.Coin,
sharesIssued math.Int,
minAvgSellPrice math_utils.PrecDec,
err error,
) {
amountLeft := amountIn

limitBuyPrice, err := types.CalcPrice(tickIndexInToOut)
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), math_utils.ZeroPrecDec(), err
}

// Use limitPrice for minAvgSellPrice if it has not been specified
minAvgSellPrice := math_utils.OnePrecDec().Quo(limitBuyPrice)
minAvgSellPrice = math_utils.OnePrecDec().Quo(limitBuyPrice)

if minAvgSellPriceP != nil {
minAvgSellPrice = *minAvgSellPriceP
Expand All @@ -124,7 +132,7 @@ func (k Keeper) ExecutePlaceLimitOrder(
// Ensure that after rounding user will get at least 1 token out.
err = types.ValidateFairOutput(amountIn, limitBuyPrice)
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), minAvgSellPrice, err
}

var orderFilled bool
Expand All @@ -134,7 +142,7 @@ func (k Keeper) ExecutePlaceLimitOrder(
swapInCoin, swapOutCoin, orderFilled, err = k.MakerLimitOrderSwap(ctx, *takerTradePairID, amountIn, limitBuyPrice, minAvgSellPrice)
}
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), minAvgSellPrice, err
}

totalIn = swapInCoin.Amount
Expand All @@ -151,7 +159,7 @@ func (k Keeper) ExecutePlaceLimitOrder(
orderType,
)
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), minAvgSellPrice, err
}

trancheKey = placeTranche.Key.TrancheKey
Expand All @@ -174,7 +182,7 @@ func (k Keeper) ExecutePlaceLimitOrder(
// order with the remaining liquidity.
err = types.ValidateFairOutput(amountLeft, limitBuyPrice)
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), minAvgSellPrice, err
}
placeTranche.PlaceMakerLimitOrder(amountLeft)
trancheUser.SharesOwned = trancheUser.SharesOwned.Add(amountLeft)
Expand All @@ -200,10 +208,10 @@ func (k Keeper) ExecutePlaceLimitOrder(
if orderType.IsJIT() {
err = k.AssertCanPlaceJIT(ctx)
if err != nil {
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), err
return trancheKey, totalIn, swapInCoin, swapOutCoin, math.ZeroInt(), minAvgSellPrice, err
}
k.IncrementJITsInBlock(ctx)
}

return trancheKey, totalIn, swapInCoin, swapOutCoin, sharesIssued, nil
return trancheKey, totalIn, swapInCoin, swapOutCoin, sharesIssued, minAvgSellPrice, nil
}
5 changes: 5 additions & 0 deletions x/dex/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"cosmossdk.io/math"
"cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

math_utils "github.com/neutron-org/neutron/v5/utils/math"
)

// Shared Attributes
Expand Down Expand Up @@ -53,6 +55,7 @@ const (
AttributeTakerDenom = "TakerDenom"
AttributeSharesOwned = "SharesOwned"
AttributeSharesWithdrawn = "SharesWithdrawn"
AttributeMinAvgSellPrice = "MinAvgSellPrice"
)

// Event Keys
Expand Down Expand Up @@ -168,6 +171,7 @@ func CreatePlaceLimitOrderEvent(
amountIn math.Int,
limitTick int64,
orderType string,
minAvgSellPrice math_utils.PrecDec,
shares math.Int,
trancheKey string,
swapAmountIn math.Int,
Expand All @@ -189,6 +193,7 @@ func CreatePlaceLimitOrderEvent(
sdk.NewAttribute(AttributeTrancheKey, trancheKey),
sdk.NewAttribute(AttributeSwapAmountIn, swapAmountIn.String()),
sdk.NewAttribute(AttributeSwapAmountOut, swapAmountOut.String()),
sdk.NewAttribute(AttributeMinAvgSellPrice, minAvgSellPrice.String()),
}

return sdk.NewEvent(sdk.EventTypeMessage, attrs...)
Expand Down

0 comments on commit 3ce1b5b

Please sign in to comment.