From 72b5aa5828cfc521b4bd2ff0fd210d316614e975 Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Mon, 9 Dec 2024 13:13:49 +0530 Subject: [PATCH] Slippage amount and APR nan fixes (#1050) * add a check for weight worse * more changes * apr fixes * apr nan * fix --- x/amm/keeper/keeper_swap_exact_amount_in_test.go | 2 +- x/amm/keeper/keeper_swap_exact_amount_out_test.go | 2 +- x/amm/keeper/query_pool.go | 2 +- x/amm/types/swap_in_amt_given_out.go | 3 ++- x/amm/types/swap_out_amt_given_in.go | 3 ++- x/masterchef/keeper/apr_denom.go | 8 ++++++-- 6 files changed, 13 insertions(+), 7 deletions(-) diff --git a/x/amm/keeper/keeper_swap_exact_amount_in_test.go b/x/amm/keeper/keeper_swap_exact_amount_in_test.go index e92c33d1d..52cbf25a0 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_in_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_in_test.go @@ -283,7 +283,7 @@ func (suite *AmmKeeperTestSuite) TestSwapExactAmountIn() { track := suite.app.AmmKeeper.GetSlippageTrack(suite.ctx, 1, uint64(suite.ctx.BlockTime().Unix())) if tc.isOraclePool { - suite.Require().Equal(track.Tracked.String(), "25uusdc") + suite.Require().Equal(track.Tracked.String(), "50uusdc") } else { suite.Require().Equal(track.Tracked.String(), "") } diff --git a/x/amm/keeper/keeper_swap_exact_amount_out_test.go b/x/amm/keeper/keeper_swap_exact_amount_out_test.go index 2b456f76e..a3fb49378 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_out_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_out_test.go @@ -274,7 +274,7 @@ func (suite *AmmKeeperTestSuite) TestSwapExactAmountOut() { track := suite.app.AmmKeeper.GetSlippageTrack(suite.ctx, 1, uint64(suite.ctx.BlockTime().Unix())) if tc.isOraclePool { - suite.Require().Equal(track.Tracked.String(), "11112uusda") + suite.Require().Equal(track.Tracked.String(), "22224uusda") } else { suite.Require().Equal(track.Tracked.String(), "") } diff --git a/x/amm/keeper/query_pool.go b/x/amm/keeper/query_pool.go index 945730fc4..cb7db08d3 100644 --- a/x/amm/keeper/query_pool.go +++ b/x/amm/keeper/query_pool.go @@ -20,7 +20,7 @@ func (k Keeper) PoolExtraInfo(ctx sdk.Context, pool types.Pool) types.PoolExtraI avg := k.GetWeightBreakingSlippageAvg(ctx, pool.PoolId) apr := math.LegacyZeroDec() if tvl.IsPositive() { - apr = avg.Mul(math.LegacyNewDec(52)).Quo(tvl) + apr = avg.Mul(math.LegacyNewDec(365)).Quo(tvl) } return types.PoolExtraInfo{ Tvl: tvl, diff --git a/x/amm/types/swap_in_amt_given_out.go b/x/amm/types/swap_in_amt_given_out.go index 465352d6a..95be97a60 100644 --- a/x/amm/types/swap_in_amt_given_out.go +++ b/x/amm/types/swap_in_amt_given_out.go @@ -111,7 +111,8 @@ func (p *Pool) SwapInAmtGivenOut( return sdk.Coin{}, sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), err } inAmountAfterSlippage := oracleInAmount.Add(slippageAmount.Mul(externalLiquidityRatio)) - slippage = slippageAmount.Mul(externalLiquidityRatio).Quo(oracleInAmount) + slippageAmount = slippageAmount.Mul(externalLiquidityRatio) + slippage = slippageAmount.Quo(oracleInAmount) // calculate weight distance difference to calculate bonus/cut on the operation newAssetPools, err := p.NewPoolAssetsAfterSwap(ctx, diff --git a/x/amm/types/swap_out_amt_given_in.go b/x/amm/types/swap_out_amt_given_in.go index 454144dc2..3afd0d484 100644 --- a/x/amm/types/swap_out_amt_given_in.go +++ b/x/amm/types/swap_out_amt_given_in.go @@ -246,7 +246,8 @@ func (p *Pool) SwapOutAmtGivenIn( return sdk.Coin{}, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), err } outAmountAfterSlippage := oracleOutAmount.Sub(slippageAmount.Mul(externalLiquidityRatio)) - slippage = slippageAmount.Mul(externalLiquidityRatio).Quo(oracleOutAmount) + slippageAmount = slippageAmount.Mul(externalLiquidityRatio) + slippage = slippageAmount.Quo(oracleOutAmount) // oracleOutAmount = 100 ATOM // BalancerOutAmount = 95 ATOM diff --git a/x/masterchef/keeper/apr_denom.go b/x/masterchef/keeper/apr_denom.go index 5fcf05445..01bb147b8 100644 --- a/x/masterchef/keeper/apr_denom.go +++ b/x/masterchef/keeper/apr_denom.go @@ -103,8 +103,12 @@ func (k Keeper) CalculateApr(ctx sdk.Context, query *types.QueryAprRequest) (mat return math.LegacyZeroDec(), nil } - // Mutiply by 52 to get yearly rewards - yearlyDexRewardAmount := usdcAmount.Mul(math.LegacyNewDec(52)) + // Mutiply by 365 to get yearly rewards + entry, found := k.assetProfileKeeper.GetEntry(ctx, ptypes.BaseCurrency) + if !found { + return math.LegacyZeroDec(), assetprofiletypes.ErrAssetProfileNotFound + } + yearlyDexRewardAmount := usdcAmount.Mul(math.LegacyNewDec(365)).Quo(math.LegacyNewDec(int64(entry.Decimals))) apr := yearlyDexRewardAmount. Quo(edenDenomPrice).