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

fix: stop loss triggering logic #788

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions x/perpetual/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func SetupStableCoinPrices(ctx sdk.Context, oracle oraclekeeper.Keeper) {
Display: "ATOM",
Decimal: 6,
})
oracle.SetAssetInfo(ctx, oracletypes.AssetInfo{
Denom: "uatom",
Display: "uatom",
Decimal: 6,
})

oracle.SetPrice(ctx, oracletypes.Price{
Asset: "USDC",
Expand Down Expand Up @@ -134,4 +139,12 @@ func SetupStableCoinPrices(ctx sdk.Context, oracle oraclekeeper.Keeper) {
Provider: provider.String(),
Timestamp: uint64(ctx.BlockTime().Unix()),
})
oracle.SetPrice(ctx, oracletypes.Price{
Asset: "uatom",
Price: sdk.NewDec(1),
Source: "uatom",
Provider: provider.String(),
Timestamp: uint64(ctx.BlockTime().Unix()),
})

}
25 changes: 18 additions & 7 deletions x/perpetual/keeper/process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,28 @@
}
}()

lpTokenPrice, err := ammPool.LpTokenPrice(ctx, k.oracleKeeper)
if err != nil {
return err
tradingAssetPrice, found := k.oracleKeeper.GetAssetPrice(ctx, mtp.TradingAsset)
if !found {
return fmt.Errorf("asset price not found")

Check warning on line 132 in x/perpetual/keeper/process_mtp.go

View check run for this annotation

Codecov / codecov/patch

x/perpetual/keeper/process_mtp.go#L132

Added line #L132 was not covered by tests
}

underStopLossPrice := !mtp.StopLossPrice.IsNil() && lpTokenPrice.LTE(mtp.StopLossPrice)
if !underStopLossPrice {
return fmt.Errorf("mtp stop loss price is not <= lp token price")
if mtp.Position == types.Position_LONG {
underStopLossPrice := !mtp.StopLossPrice.IsNil() && tradingAssetPrice.Price.LTE(mtp.StopLossPrice)
if !underStopLossPrice {
return fmt.Errorf("mtp stop loss price is not <= token price")

Check warning on line 138 in x/perpetual/keeper/process_mtp.go

View check run for this annotation

Codecov / codecov/patch

x/perpetual/keeper/process_mtp.go#L138

Added line #L138 was not covered by tests
}
}else {
underStopLossPrice := !mtp.StopLossPrice.IsNil() && tradingAssetPrice.Price.GTE(mtp.StopLossPrice)
if !underStopLossPrice {
return fmt.Errorf("mtp stop loss price is not => token price")

Check warning on line 143 in x/perpetual/keeper/process_mtp.go

View check run for this annotation

Codecov / codecov/patch

x/perpetual/keeper/process_mtp.go#L140-L143

Added lines #L140 - L143 were not covered by tests
}
}


var repayAmount math.Int
var (
repayAmount math.Int
err error
)
switch mtp.Position {
case types.Position_LONG:
repayAmount, err = k.ForceCloseLong(ctx, mtp, &pool, true, baseCurrency)
Expand Down
Loading