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: fees does not require additional funds #1718

Merged
merged 12 commits into from
Dec 18, 2023
14 changes: 11 additions & 3 deletions wasmbinding/querier_perp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,17 @@ func (s *TestSuitePerpQuerier) TestPosition() {
freshCwResp := new(bindings.PositionResponse)
err = json.Unmarshal(jsonBz, freshCwResp)
s.NoErrorf(err, "freshCwResp: %s", freshCwResp)
s.Assert().EqualValues(resp.ExchangedNotionalValue, leverage.MulInt(margin))
s.Assert().EqualValues(cwResp.Position.OpenNotional, leverage.MulInt(margin))
s.Assert().EqualValues(cwResp.Position.Margin, sdk.NewDecFromInt(margin))

market, err := s.nibiru.PerpKeeperV2.GetMarket(s.ctx, pair)
s.NoError(err)
defaultFee := market.ExchangeFeeRatio.Add(market.EcosystemFundFeeRatio)

feeFromNotionalValue := leverage.MulInt(margin).Mul(defaultFee)
expectedExchangedNotionalValue := margin.ToLegacyDec().Sub(feeFromNotionalValue).Mul(leverage)

s.Assert().EqualValues(expectedExchangedNotionalValue, resp.ExchangedNotionalValue)
s.Assert().EqualValues(expectedExchangedNotionalValue, cwResp.Position.OpenNotional)
s.Assert().EqualValues(margin.ToLegacyDec().Sub(feeFromNotionalValue), cwResp.Position.Margin)

s.T().Log("fail due to invalid pair")
cwReq = &bindings.PositionRequest{
Expand Down
94 changes: 48 additions & 46 deletions x/perp/v2/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,20 +244,20 @@ func (s *IntegrationTestSuite) TestMarketOrdersAndCloseCmd() {
ammMarketDuo, err = testutilcli.QueryMarketV2(val.ClientCtx, pair)
s.Require().NoError(err)
s.T().Logf("ammMarketDuo: %s", ammMarketDuo.String())
s.EqualValues(sdk.MustNewDecFromStr("9999666.677777407419752675"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10000333.333333333333333333"), ammMarketDuo.Amm.QuoteReserve)
s.EqualValues(sdk.MustNewDecFromStr("9999667.344399676304101617"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10000332.666666666666666667"), ammMarketDuo.Amm.QuoteReserve)

s.T().Log("B. check trader position")
queryResp, err := testutilcli.QueryPositionV2(val.ClientCtx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), user)
s.NoError(err)
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(asset.Registry.Pair(denoms.BTC, denoms.NUSD), queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("333.322222592580247325"), queryResp.Position.Size_)
s.EqualValues(sdk.NewDec(2*common.TO_MICRO), queryResp.Position.Margin)
s.EqualValues(sdk.NewDec(2*common.TO_MICRO), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("1999999.999999999999998000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("332.655600323695898383"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("1996000.000000000000000000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("1996000.000000000000000000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("1996000.000000000000002000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.OneDec(), queryResp.MarginRatio)

s.T().Log("C. open position with 2x leverage and zero baseAmtLimit")
Expand All @@ -278,12 +278,12 @@ func (s *IntegrationTestSuite) TestMarketOrdersAndCloseCmd() {
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(asset.Registry.Pair(denoms.BTC, denoms.NUSD), queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("999.900009999000099990"), queryResp.Position.Size_)
s.EqualValues(sdk.NewDec(4*common.TO_MICRO), queryResp.Position.Margin)
s.EqualValues(sdk.NewDec(6*common.TO_MICRO), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("6000000.000000000000000000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("0.000000000000000000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.666666666666666667"), queryResp.MarginRatio)
s.EqualValues(sdk.MustNewDecFromStr("996.567342121568550334"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("3988000.000000000000000000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("5980000.0"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("5979999.999999999999996000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000004000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.666889632107023411"), queryResp.MarginRatio)

s.T().Log("D. Open a reverse position smaller than the existing position")
txResp, err = s.network.ExecTxCmd(cli.MarketOrderCmd(), user, []string{
Expand All @@ -302,21 +302,23 @@ func (s *IntegrationTestSuite) TestMarketOrdersAndCloseCmd() {
ammMarketDuo, err = testutilcli.QueryMarketV2(val.ClientCtx, pair)
s.Require().NoError(err)
s.T().Logf("ammMarketDuo: %s", ammMarketDuo.String())
s.EqualValues(sdk.MustNewDecFromStr("9999166.736105324556286976"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10000833.333333333333333333"), ammMarketDuo.Amm.QuoteReserve)
s.EqualValues(sdk.MustNewDecFromStr("9999169.735606286824650660"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10000830.333333333333333334"), ammMarketDuo.Amm.QuoteReserve)

s.T().Log("D. Check trader position")
queryResp, err = testutilcli.QueryPositionV2(val.ClientCtx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), user)
s.NoError(err)
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(asset.Registry.Pair(denoms.BTC, denoms.NUSD), queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("833.263894675443713024"), queryResp.Position.Size_)
s.EqualValues(sdk.NewDec(4*common.TO_MICRO), queryResp.Position.Margin)
s.EqualValues(sdk.NewDec(5_000_000), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("4999999.999999999999998000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.800000000000000000"), queryResp.MarginRatio)
s.EqualValues(sdk.MustNewDecFromStr("830.264393713175349340"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("3987999.999999999999999332"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("3987999.999999999999999332"), queryResp.Position.Margin)

s.EqualValues(sdk.MustNewDecFromStr("4981999.999999999999999332"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("4981999.999999999999998000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000001332"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("0.800481734243275793"), queryResp.MarginRatio)

s.T().Log("E. Open a reverse position larger than the existing position")
txResp, err = s.network.ExecTxCmd(cli.MarketOrderCmd(), user, []string{
Expand All @@ -336,11 +338,11 @@ func (s *IntegrationTestSuite) TestMarketOrdersAndCloseCmd() {
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(asset.Registry.Pair(denoms.BTC, denoms.NUSD), queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("-500.025001250062503125"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("3000000.000000000000002000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("3000000.000000000000002000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("3000000.000000000000000000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("-500.358367930342114784"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("3002000.000000000000002000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("3002000.000000000000002000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("3002000.000000000000004000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000002000"), queryResp.UnrealizedPnl)
// there is a random delta due to twap margin ratio calculation and random block times in the in-process network
s.InDelta(1, queryResp.MarginRatio.MustFloat64(), 0.008)

Expand Down Expand Up @@ -382,19 +384,19 @@ func (s *IntegrationTestSuite) TestPartialCloseCmd() {
ammMarketDuo, err := testutilcli.QueryMarketV2(val.ClientCtx, pair)
s.Require().NoError(err)
s.T().Logf("ammMarketDuo: %s", ammMarketDuo.String())
s.EqualValues(sdk.MustNewDecFromStr("9998000.399920015996800640"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10002000.000000000000000000"), ammMarketDuo.Amm.QuoteReserve)
s.EqualValues(sdk.MustNewDecFromStr("9998004.398322094909855993"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10001996.000000000000000001"), ammMarketDuo.Amm.QuoteReserve)

s.T().Log("Check trader position")
queryResp, err := testutilcli.QueryPositionV2(val.ClientCtx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), user)
s.NoError(err)
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(pair, queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("1999.600079984003199360"), queryResp.Position.Size_)
s.EqualValues(sdk.NewDec(12e6), queryResp.Position.Margin)
s.EqualValues(sdk.NewDec(12e6), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("12000000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("1995.601677905090144007"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("11976000.000000000000000000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("11976000.000000000000000000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("11976000.000000000000000000"), queryResp.PositionNotional)
s.EqualValues(sdk.ZeroDec(), queryResp.UnrealizedPnl)
s.EqualValues(sdk.OneDec(), queryResp.MarginRatio)

Expand Down Expand Up @@ -428,21 +430,21 @@ func (s *IntegrationTestSuite) TestPartialCloseCmd() {
ammMarketDuo, err = testutilcli.QueryMarketV2(val.ClientCtx, pair)
s.Require().NoError(err)
s.T().Logf("ammMarketDuo: %s", ammMarketDuo.String())
s.EqualValues(sdk.MustNewDecFromStr("9998500.399920015996800640"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10001499.824993752062459356"), ammMarketDuo.Amm.QuoteReserve)
s.EqualValues(sdk.MustNewDecFromStr("9998503.398288059264958855"), ammMarketDuo.Amm.BaseReserve)
s.EqualValues(sdk.MustNewDecFromStr("10001496.825727135305804567"), ammMarketDuo.Amm.QuoteReserve)

s.T().Log("Check trader position")
queryResp, err = testutilcli.QueryPositionV2(val.ClientCtx, asset.Registry.Pair(denoms.BTC, denoms.NUSD), user)
s.NoError(err)
s.T().Logf("query response: %+v", queryResp)
s.EqualValues(user.String(), queryResp.Position.TraderAddress)
s.EqualValues(asset.Registry.Pair(denoms.BTC, denoms.NUSD), queryResp.Position.Pair)
s.EqualValues(sdk.MustNewDecFromStr("1499.600079984003199360"), queryResp.Position.Size_)
s.EqualValues(sdk.NewDec(12e6), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("8998949.962512374756136000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("8998949.962512374756136000"), queryResp.PositionNotional)
s.EqualValues(sdk.ZeroDec(), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("1.333488912594172945"), queryResp.MarginRatio)
s.EqualValues(sdk.MustNewDecFromStr("1496.601711940735041145"), queryResp.Position.Size_)
s.EqualValues(sdk.MustNewDecFromStr("11976000.000000000000000000"), queryResp.Position.Margin)
s.EqualValues(sdk.MustNewDecFromStr("8980954.362811834827398000"), queryResp.Position.OpenNotional)
s.EqualValues(sdk.MustNewDecFromStr("8980954.362811834827396000"), queryResp.PositionNotional)
s.EqualValues(sdk.MustNewDecFromStr("-0.000000000000002000"), queryResp.UnrealizedPnl)
s.EqualValues(sdk.MustNewDecFromStr("1.333488571057658776"), queryResp.MarginRatio)
}

func (s *IntegrationTestSuite) TestPositionEmptyAndClose() {
Expand Down Expand Up @@ -519,7 +521,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
fmt.Sprintf("10000%s", denoms.USDT),
},
expectFail: false,
expectedMargin: sdk.NewDec(1_000_000),
expectedMargin: sdk.NewDec(980000),
expectedCode: 1,
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
},
{
Expand All @@ -529,7 +531,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
fmt.Sprintf("10000%s", types.TestingCollateralDenomNUSD),
},
expectedCode: types.ErrPositionNotFound.ABCICode(),
expectedMargin: sdk.NewDec(1_000_000),
expectedMargin: sdk.NewDec(980000),
expectFail: false,
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
},
{
Expand All @@ -539,7 +541,7 @@ func (s *IntegrationTestSuite) TestX_AddMargin() {
fmt.Sprintf("10000%s", types.TestingCollateralDenomNUSD),
},
expectedCode: 0,
expectedMargin: sdk.NewDec(1_010_000),
expectedMargin: sdk.NewDec(990_000),
expectFail: false,
},
{
Expand Down Expand Up @@ -623,7 +625,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
},
expectFail: false,
expectedCode: 1,
expectedMargin: sdk.NewDec(1_000_000),
expectedMargin: sdk.NewDec(980_000),
},
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
{
name: "fail: position not found",
Expand All @@ -632,7 +634,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
fmt.Sprintf("10000%s", types.TestingCollateralDenomNUSD),
},
expectedCode: types.ErrPositionNotFound.ABCICode(),
expectedMargin: sdk.NewDec(1_000_000),
expectedMargin: sdk.NewDec(980_000),
expectFail: false,
jgimeno marked this conversation as resolved.
Show resolved Hide resolved
},
{
Expand All @@ -642,7 +644,7 @@ func (s *IntegrationTestSuite) TestX_RemoveMargin() {
fmt.Sprintf("10000%s", types.TestingCollateralDenomNUSD),
},
expectedCode: 0,
expectedMargin: sdk.NewDec(990_000),
expectedMargin: sdk.NewDec(970_000),
expectFail: false,
},
{
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestCloseMarket(t *testing.T) {
),
).When(
CloseMarket(pairBtcUsdc),
AMMShouldBeEqual(pairBtcUsdc, AMM_SettlementPriceShoulBeEqual(sdk.MustNewDecFromStr("1.1"))),
AMMShouldBeEqual(pairBtcUsdc, AMM_SettlementPriceShoulBeEqual(sdk.MustNewDecFromStr("1.099800000000000000"))),
).Then(
PartialCloseFails(alice, pairBtcUsdc, sdk.NewDec(5_000), types.ErrMarketNotEnabled),
),
Expand Down
6 changes: 3 additions & 3 deletions x/perp/v2/keeper/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (k Keeper) EditPriceMultiplier(
return err
}

err = k.handleMarketUpdateCost(ctx, pair, cost)
err = k.handleMarketUpdateCost(ctx, cost)
if err != nil {
return err
}
Expand Down Expand Up @@ -63,7 +63,7 @@ func (k Keeper) EditSwapInvariant(ctx sdk.Context, pair asset.Pair, newSwapInvar
return err
}

err = k.handleMarketUpdateCost(ctx, pair, cost)
err = k.handleMarketUpdateCost(ctx, cost)
if err != nil {
return err
}
Expand All @@ -78,7 +78,7 @@ func (k Keeper) EditSwapInvariant(ctx sdk.Context, pair asset.Pair, newSwapInvar
return nil
}

func (k Keeper) handleMarketUpdateCost(ctx sdk.Context, pair asset.Pair, costAmt sdkmath.Int) (err error) {
func (k Keeper) handleMarketUpdateCost(ctx sdk.Context, costAmt sdkmath.Int) (err error) {
collateral, err := k.Collateral.Get(ctx)
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handleMarketUpdateCost function's error handling for insufficient funds is appropriate, but consider reviewing the use of nolint:staticcheck to ensure that it's justified and that no potential issues are being suppressed.

Expand Down
50 changes: 35 additions & 15 deletions x/perp/v2/keeper/clearing_house.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ func (k Keeper) MarketOrder(
sameSideLong := position.Size_.IsPositive() && dir == types.Direction_LONG
sameSideShort := position.Size_.IsNegative() && dir == types.Direction_SHORT

openNotionalPreFees := leverage.MulInt(quoteAssetAmt)
transferredFee, err := k.transferFee(ctx, market.Pair, traderAddr, openNotionalPreFees,
market.ExchangeFeeRatio, market.EcosystemFundFeeRatio,
)
if err != nil {
return nil, err
}
quoteAssetAmtMinusFees := quoteAssetAmt.Sub(transferredFee)

var updatedAMM *types.AMM
openSideMatchesPosition := sameSideLong || sameSideShort
if isNewPosition || openSideMatchesPosition {
Expand All @@ -73,14 +82,14 @@ func (k Keeper) MarketOrder(
amm,
position,
dir,
/* openNotional */ leverage.MulInt(quoteAssetAmt),
/* openNotional */ leverage.MulInt(quoteAssetAmtMinusFees),
/* minPositionSize */ baseAmtLimit,
/* leverage */ leverage)
if err != nil {
return nil, err
}
} else {
quoteAssetAmtToDec := sdk.NewDecFromInt(quoteAssetAmt)
quoteAssetAmtToDec := sdk.NewDecFromInt(quoteAssetAmtMinusFees)
updatedAMM, positionResp, err = k.openReversePosition(
ctx,
market,
Expand Down Expand Up @@ -108,7 +117,7 @@ func (k Keeper) MarketOrder(
}

if err = k.afterPositionUpdate(
ctx, market, *updatedAMM, traderAddr, *positionResp, types.ChangeReason_MarketOrder, position,
ctx, market, traderAddr, *positionResp, types.ChangeReason_MarketOrder, transferredFee, position,
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -531,10 +540,10 @@ func checkMarketOrderRequirements(market types.Market, quoteAssetAmt sdkmath.Int
func (k Keeper) afterPositionUpdate(
ctx sdk.Context,
market types.Market,
amm types.AMM,
traderAddr sdk.AccAddress,
positionResp types.PositionResp,
changeType types.ChangeReason,
transferredFee sdkmath.Int,
existingPosition types.Position,
) (err error) {
// transfer trader <=> vault
Expand All @@ -558,13 +567,6 @@ func (k Keeper) afterPositionUpdate(
}
}

transferredFee, err := k.transferFee(ctx, market.Pair, traderAddr, positionResp.ExchangedNotionalValue,
market.ExchangeFeeRatio, market.EcosystemFundFeeRatio,
)
if err != nil {
return err
}

if !positionResp.Position.Size_.IsZero() {
k.SavePosition(ctx, market.Pair, market.Version, traderAddr, positionResp.Position)
}
Expand All @@ -579,6 +581,17 @@ func (k Keeper) afterPositionUpdate(
positionNotional = positionResp.Position.OpenNotional.Sub(positionResp.UnrealizedPnlAfter)
}

// This means that the position has been closed entirely.
if transferredFee.IsZero() {
// If the position has margin to be transferred to the user, we get the fees from it.
if positionResp.MarginToVault.IsNegative() {
transferredFee, err = k.transferFee(ctx, market.Pair, traderAddr, positionResp.MarginToVault.Abs(), market.ExchangeFeeRatio, market.EcosystemFundFeeRatio)
if err != nil {
return err
}
}
}

_ = ctx.EventManager().EmitTypedEvents(
&types.PositionChangedEvent{
FinalPosition: positionResp.Position,
Expand Down Expand Up @@ -718,7 +731,7 @@ func (k Keeper) ClosePosition(ctx sdk.Context, pair asset.Pair, traderAddr sdk.A
return nil, err
}

updatedAMM, positionResp, err := k.closePositionEntirely(
_, positionResp, err := k.closePositionEntirely(
ctx,
market,
amm,
Expand All @@ -742,10 +755,10 @@ func (k Keeper) ClosePosition(ctx sdk.Context, pair asset.Pair, traderAddr sdk.A
if err = k.afterPositionUpdate(
ctx,
market,
*updatedAMM,
traderAddr,
*positionResp,
types.ChangeReason_ClosePosition,
sdk.ZeroInt(),
position,
); err != nil {
return nil, err
Expand Down Expand Up @@ -889,7 +902,14 @@ func (k Keeper) PartialClose(
}
reverseNotionalAmt = amm.QuoteReserveToAsset(reverseNotionalAmt)

updatedAMM, positionResp, err := k.decreasePosition(ctx, market, amm, position, reverseNotionalAmt, sdk.ZeroDec())
feesTransferred, err := k.transferFee(ctx, market.Pair, traderAddr, reverseNotionalAmt, market.ExchangeFeeRatio, market.EcosystemFundFeeRatio)
if err != nil {
return nil, err
}

reverseNotionalAmtWithoutFees := reverseNotionalAmt.Sub(feesTransferred.ToLegacyDec())

_, positionResp, err := k.decreasePosition(ctx, market, amm, position, reverseNotionalAmtWithoutFees, sdk.ZeroDec())
if err != nil {
return nil, err
}
Expand All @@ -907,10 +927,10 @@ func (k Keeper) PartialClose(
err = k.afterPositionUpdate(
ctx,
market,
*updatedAMM,
traderAddr,
*positionResp,
types.ChangeReason_PartialClose,
feesTransferred,
position,
)
if err != nil {
Expand Down
Loading