From 687a44cfa8bdd29578ee67d1f615673db10b9136 Mon Sep 17 00:00:00 2001 From: jelysn Date: Mon, 20 Nov 2023 21:06:35 +0800 Subject: [PATCH] add weighted average of consolidated position --- x/leveragelp/keeper/position_open.go | 7 +++++-- x/leveragelp/keeper/position_open_test.go | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/x/leveragelp/keeper/position_open.go b/x/leveragelp/keeper/position_open.go index baa3289f3..daea6f8d1 100644 --- a/x/leveragelp/keeper/position_open.go +++ b/x/leveragelp/keeper/position_open.go @@ -45,12 +45,15 @@ func (k Keeper) OpenConsolidate(ctx sdk.Context, position *types.Position, msg * return nil, err } - collateralAmountDec := sdk.NewDecFromBigInt(msg.CollateralAmount.BigInt()) + collateralAmountDec := sdk.NewDecFromInt(msg.CollateralAmount) + originCollateral := sdk.NewDecFromInt(position.Collateral.Amount) position.Collateral = position.Collateral.Add(sdk.NewCoin(msg.CollateralAsset, msg.CollateralAmount)) maxLeverage := k.GetMaxLeverageParam(ctx) leverage := sdk.MinDec(msg.Leverage, maxLeverage) position.Leverage = leverage - position.StopLossPrice = msg.StopLossPrice + position.StopLossPrice = collateralAmountDec.Mul(msg.StopLossPrice). + Add(originCollateral.Mul(position.StopLossPrice)). + Quo(originCollateral.Add(collateralAmountDec)) position, err = k.ProcessOpenLong(ctx, position, leverage, collateralAmountDec, poolId, msg) if err != nil { diff --git a/x/leveragelp/keeper/position_open_test.go b/x/leveragelp/keeper/position_open_test.go index eb0e7a5d7..cbfe1be15 100644 --- a/x/leveragelp/keeper/position_open_test.go +++ b/x/leveragelp/keeper/position_open_test.go @@ -87,6 +87,7 @@ func (suite KeeperTestSuite) TestOpenLong() { CollateralAmount: sdk.NewInt(1000), AmmPoolId: 1, Leverage: sdk.NewDec(5), + StopLossPrice: sdk.ZeroDec(), }) suite.Require().NoError(err) suite.Require().Equal(position.Address, addr.String()) @@ -108,6 +109,7 @@ func (suite KeeperTestSuite) TestOpenLong() { CollateralAmount: sdk.NewInt(1000), AmmPoolId: 1, Leverage: sdk.NewDec(5), + StopLossPrice: sdk.ZeroDec(), }) suite.Require().NoError(err) position2, err := k.GetPosition(suite.ctx, position.Address, position.Id)