Skip to content

Commit

Permalink
fix: open estimation edge cases (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond authored Feb 27, 2024
1 parent 8e1f1b7 commit 5597784
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
4 changes: 1 addition & 3 deletions x/perpetual/keeper/query_open_estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func (k Keeper) OpenEstimation(goCtx context.Context, req *types.QueryOpenEstima
}

// invert openPrice if collateral is not in base currency
if req.Collateral.Denom != baseCurrency {
openPrice = sdk.OneDec().Quo(openPrice)
}
openPrice = sdk.OneDec().Quo(openPrice)

// calculate min collateral
minCollateral, err := k.CalcMinCollateral(ctx, req.Leverage, openPrice, decimals)
Expand Down
50 changes: 25 additions & 25 deletions x/perpetual/keeper/query_open_estimation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestOpenEstimation_Long5XAtom100Usdc(t *testing.T) {
poolAssets := []ammtypes.PoolAsset{
{
Weight: sdk.NewInt(50),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
},
{
Weight: sdk.NewInt(50),
Expand Down Expand Up @@ -84,25 +84,25 @@ func TestOpenEstimation_Long5XAtom100Usdc(t *testing.T) {
TradingAsset: ptypes.ATOM,
Collateral: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000000)),
Discount: sdk.MustNewDecFromStr("0.0"),
TakeProfitPrice: sdk.MustNewDecFromStr("2.0"),
TakeProfitPrice: sdk.MustNewDecFromStr("20.0"),
})
require.NoError(t, err)
require.Equal(t, &types.QueryOpenEstimationResponse{
Position: types.Position_LONG,
Leverage: sdk.MustNewDecFromStr("5.0"),
TradingAsset: ptypes.ATOM,
Collateral: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000000)),
MinCollateral: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(8333334)),
MinCollateral: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(83333334)),
ValidCollateral: true,
PositionSize: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(497017398)),
PositionSize: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(49701739)),
SwapFee: sdk.MustNewDecFromStr("0.001000000000000000"),
Discount: sdk.MustNewDecFromStr("0.000000000000000000"),
OpenPrice: sdk.MustNewDecFromStr("1.000000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("2.000000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("0.798799799760731917"),
EstimatedPnl: sdk.NewInt(247017398),
OpenPrice: sdk.MustNewDecFromStr("10.00000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("20.00000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("7.987997965222102188"),
EstimatedPnl: sdk.NewInt(24701739),
EstimatedPnlDenom: ptypes.ATOM,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
WeightBalanceRatio: sdk.MustNewDecFromStr("0.000000000000000000"),
BorrowInterestRate: sdk.MustNewDecFromStr("0.000000000000000000"),
FundingRate: sdk.MustNewDecFromStr("0.000000000000000000"),
Expand Down Expand Up @@ -153,11 +153,11 @@ func TestOpenEstimation_Long5XAtom10Atom(t *testing.T) {
poolAssets := []ammtypes.PoolAsset{
{
Weight: sdk.NewInt(50),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
},
{
Weight: sdk.NewInt(50),
Token: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(10000000000)),
Token: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000000000)),
},
}

Expand Down Expand Up @@ -192,29 +192,29 @@ func TestOpenEstimation_Long5XAtom10Atom(t *testing.T) {
TradingAsset: ptypes.ATOM,
Collateral: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000)),
Discount: sdk.MustNewDecFromStr("0.0"),
TakeProfitPrice: sdk.MustNewDecFromStr("2.0"),
TakeProfitPrice: sdk.MustNewDecFromStr("20.0"),
})
require.NoError(t, err)
require.Equal(t, &types.QueryOpenEstimationResponse{
Position: types.Position_LONG,
Leverage: sdk.MustNewDecFromStr("5.0"),
TradingAsset: ptypes.ATOM,
Collateral: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000)),
MinCollateral: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(9333333)),
ValidCollateral: true,
PositionSize: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(49870172)),
MinCollateral: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(84333333)),
ValidCollateral: false,
PositionSize: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(49602977)),
SwapFee: sdk.MustNewDecFromStr("0.001000000000000000"),
Discount: sdk.MustNewDecFromStr("0.000000000000000000"),
OpenPrice: sdk.MustNewDecFromStr("0.100000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("2.000000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("0.079969990879518122"),
EstimatedPnl: sdk.NewInt(42872372),
OpenPrice: sdk.MustNewDecFromStr("10.00000000000000000"),
TakeProfitPrice: sdk.MustNewDecFromStr("20.00000000000000000"),
LiquidationPrice: sdk.MustNewDecFromStr("7.988017957067375210"),
EstimatedPnl: sdk.NewInt(29142917),
EstimatedPnlDenom: ptypes.ATOM,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
WeightBalanceRatio: sdk.MustNewDecFromStr("0.000000000000000000"),
BorrowInterestRate: sdk.MustNewDecFromStr("0.000000000000000000"),
FundingRate: sdk.MustNewDecFromStr("0.000000000000000000"),
PriceImpact: sdk.MustNewDecFromStr("0.001498208028831715"),
PriceImpact: sdk.MustNewDecFromStr("0.005955342879283360"),
}, res)
}

Expand Down Expand Up @@ -261,11 +261,11 @@ func TestOpenEstimation_Short5XAtom10Usdc(t *testing.T) {
poolAssets := []ammtypes.PoolAsset{
{
Weight: sdk.NewInt(50),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
Token: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
},
{
Weight: sdk.NewInt(50),
Token: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(10000000000)),
Token: sdk.NewCoin(ptypes.BaseCurrency, sdk.NewInt(100000000000)),
},
}

Expand Down Expand Up @@ -318,10 +318,10 @@ func TestOpenEstimation_Short5XAtom10Usdc(t *testing.T) {
LiquidationPrice: sdk.MustNewDecFromStr("12.000000000000000000"),
EstimatedPnl: sdk.NewInt(200000000),
EstimatedPnlDenom: ptypes.BaseCurrency,
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(100000000000)),
AvailableLiquidity: sdk.NewCoin(ptypes.ATOM, sdk.NewInt(10000000000)),
WeightBalanceRatio: sdk.MustNewDecFromStr("0.000000000000000000"),
BorrowInterestRate: sdk.MustNewDecFromStr("0.000000000000000000"),
FundingRate: sdk.MustNewDecFromStr("0.000000000000000000"),
PriceImpact: sdk.MustNewDecFromStr("0.048526120400000000"),
PriceImpact: sdk.MustNewDecFromStr("0.005965220000000000"),
}, res)
}

0 comments on commit 5597784

Please sign in to comment.