Skip to content

Commit

Permalink
add weighted average of consolidated position
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Nov 20, 2023
1 parent b63ab1f commit 687a44c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions x/leveragelp/keeper/position_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions x/leveragelp/keeper/position_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down

0 comments on commit 687a44c

Please sign in to comment.