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

[LeverageLp]: CloseEst bug fix #812

Merged
merged 3 commits into from
Sep 24, 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
4 changes: 2 additions & 2 deletions x/leveragelp/keeper/query_estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func (k Keeper) CloseEst(goCtx context.Context, req *types.QueryCloseEstRequest)
return nil, types.ErrAmountTooLow
}

repayAmount := debt.GetTotalLiablities()
repayAmount := debt.GetTotalLiablities().Mul(req.LpAmount).Quo(position.LeveragedLpAmount)
userAmount := exitCoins[0].Amount.Sub(repayAmount)

return &types.QueryCloseEstResponse{
Liability: position.Liabilities,
Liability: repayAmount,
WeightBalanceRatio: weightBalanceBonus,
AmountReturned: userAmount,
}, nil
Expand Down
105 changes: 105 additions & 0 deletions x/leveragelp/keeper/query_estimation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package keeper_test

import (
"github.com/cometbft/cometbft/crypto/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/leveragelp/types"
stablestakekeeper "github.com/elys-network/elys/x/stablestake/keeper"
stablestaketypes "github.com/elys-network/elys/x/stablestake/types"
)

func (suite KeeperTestSuite) TestQueryEstimation() {
k := suite.app.LeveragelpKeeper
suite.SetupCoinPrices(suite.ctx)
addr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
poolAddr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
treasuryAddr := sdk.AccAddress(ed25519.GenPrivKey().PubKey().Address())
params := stablestaketypes.DefaultParams()
suite.app.StablestakeKeeper.SetParams(suite.ctx, params)

pool := types.Pool{
AmmPoolId: 1,
Enabled: true,
Closed: false,
Health: sdk.ZeroDec(),
LeveragedLpAmount: sdk.ZeroInt(),
LeverageMax: sdk.OneDec().MulInt64(10),
}
poolInit := sdk.Coins{sdk.NewInt64Coin("uusdc", 100000), sdk.NewInt64Coin("uusdt", 100000)}

err := suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, poolInit)
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, poolAddr, poolInit)
suite.Require().NoError(err)

suite.app.AmmKeeper.SetPool(suite.ctx, ammtypes.Pool{
PoolId: 1,
Address: poolAddr.String(),
RebalanceTreasury: treasuryAddr.String(),
PoolParams: ammtypes.PoolParams{
SwapFee: sdk.ZeroDec(),
ExitFee: sdk.ZeroDec(),
UseOracle: true,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
FeeDenom: "uusdc",
},
TotalShares: sdk.NewCoin("amm/pool/1", sdk.NewInt(2).Mul(ammtypes.OneShare)),
PoolAssets: []ammtypes.PoolAsset{
{
Token: poolInit[0],
Weight: sdk.NewInt(10),
},
{
Token: poolInit[1],
Weight: sdk.NewInt(10),
},
},
TotalWeight: sdk.NewInt(20),
})
k.SetPool(suite.ctx, pool)
suite.app.AmmKeeper.SetDenomLiquidity(suite.ctx, ammtypes.DenomLiquidity{
Denom: "uusdc",
Liquidity: sdk.NewInt(100000),
})
suite.app.AmmKeeper.SetDenomLiquidity(suite.ctx, ammtypes.DenomLiquidity{
Denom: "uusdt",
Liquidity: sdk.NewInt(100000),
})

usdcToken := sdk.NewInt64Coin("uusdc", 100000)
err = suite.app.BankKeeper.MintCoins(suite.ctx, minttypes.ModuleName, sdk.Coins{usdcToken})
suite.Require().NoError(err)
err = suite.app.BankKeeper.SendCoinsFromModuleToAccount(suite.ctx, minttypes.ModuleName, addr, sdk.Coins{usdcToken})
suite.Require().NoError(err)

stableMsgServer := stablestakekeeper.NewMsgServerImpl(suite.app.StablestakeKeeper)
_, err = stableMsgServer.Bond(sdk.WrapSDKContext(suite.ctx), &stablestaketypes.MsgBond{
Creator: addr.String(),
Amount: sdk.NewInt(10000),
})
suite.Require().NoError(err)

// open a position
position, _ := k.OpenLong(suite.ctx, &types.MsgOpen{
Creator: addr.String(),
CollateralAsset: "uusdc",
CollateralAmount: sdk.NewInt(1000),
AmmPoolId: 1,
Leverage: sdk.NewDec(5),
StopLossPrice: sdk.ZeroDec(),
})

estimation, _ := k.CloseEst(suite.ctx, &types.QueryCloseEstRequest{
Owner: addr.String(),
Id: position.Id,
LpAmount: sdk.NewInt(10000000000000000),
})
// Total liability is 4000, so 800 is the liability for 10000000000000000 Lp amount
suite.Require().Equal(estimation.Liability.String(), "800")
}
Loading