diff --git a/x/amm/client/wasm/query_amm_price_by_denom.go b/x/amm/client/wasm/query_amm_price_by_denom.go index 76f6d5f3f..ff406895a 100644 --- a/x/amm/client/wasm/query_amm_price_by_denom.go +++ b/x/amm/client/wasm/query_amm_price_by_denom.go @@ -38,7 +38,7 @@ func (oq *Querier) queryAmmPriceByDenom(ctx sdk.Context, query *ammtypes.QueryAM tokenIn := query.TokenIn discount := query.Discount - spotPrice, _, _, _, _, err := oq.keeper.CalcInRouteSpotPrice(ctx, tokenIn, routes, discount, sdk.ZeroDec()) + spotPrice, _, _, _, _, _, err := oq.keeper.CalcInRouteSpotPrice(ctx, tokenIn, routes, discount, sdk.ZeroDec()) if err != nil { return nil, errorsmod.Wrap(err, "failed to get in route by denom") } diff --git a/x/amm/keeper/msg_server_exit_pool_test.go b/x/amm/keeper/msg_server_exit_pool_test.go index 7abe32d58..96d684a3a 100644 --- a/x/amm/keeper/msg_server_exit_pool_test.go +++ b/x/amm/keeper/msg_server_exit_pool_test.go @@ -74,7 +74,7 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01 WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), @@ -85,9 +85,9 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { }, shareInAmount: types.OneShare.Quo(sdk.NewInt(10)), tokenOutDenom: "uusdt", - minAmountsOut: sdk.Coins{sdk.NewInt64Coin("uusdt", 97368)}, + minAmountsOut: sdk.Coins{sdk.NewInt64Coin("uusdt", 98699)}, // expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 95114)}, // slippage enabled - expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 97368)}, // slippage disabled + expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 98699)}, // slippage disabled expPass: true, }, { @@ -97,7 +97,7 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01 WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/msg_server_join_pool_test.go b/x/amm/keeper/msg_server_join_pool_test.go index 2617a207c..e0bb3e580 100644 --- a/x/amm/keeper/msg_server_join_pool_test.go +++ b/x/amm/keeper/msg_server_join_pool_test.go @@ -73,7 +73,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01 WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), @@ -83,7 +83,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { FeeDenom: ptypes.BaseCurrency, }, // shareOutAmount: sdk.NewInt(694444166666666666), // weight breaking fee - slippage enable - shareOutAmount: sdk.NewInt(833333333333333333), // weight breaking fee - slippage disable + shareOutAmount: sdk.NewInt(943431457505076198), // weight breaking fee - slippage disable expSenderBalance: sdk.Coins{}, expTokenIn: sdk.Coins{sdk.NewInt64Coin("uusdt", 1000000)}, expPass: true, @@ -96,7 +96,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01 WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), @@ -106,7 +106,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { FeeDenom: ptypes.BaseCurrency, }, // shareOutAmount: sdk.NewInt(805987500000000000), // weight recovery direction - slippage enable - shareOutAmount: sdk.NewInt(1250000000000000000), // weight recovery direction - slippage disable + shareOutAmount: sdk.NewInt(1002500000000000000), // weight recovery direction - slippage disable expSenderBalance: sdk.Coins{}, expTokenIn: sdk.Coins{sdk.NewInt64Coin("uusdt", 1000000)}, expPass: true, @@ -119,7 +119,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01 WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), diff --git a/x/amm/types/pow.go b/x/amm/types/pow.go index 3576b195f..7b6ca1a88 100644 --- a/x/amm/types/pow.go +++ b/x/amm/types/pow.go @@ -32,11 +32,6 @@ func Pow(base sdk.Dec, exp sdk.Dec) sdk.Dec { if !base.IsPositive() { panic(fmt.Errorf("base must be greater than 0")) } - // TODO: Remove this if we want to generalize the function, - // we can adjust the algorithm in this setting. - if base.GTE(two) { - panic(fmt.Errorf("base must be lesser than two")) - } // We will use an approximation algorithm to compute the power. // Since computing an integer power is easy, we split up the exponent into diff --git a/x/amm/types/pow_test.go b/x/amm/types/pow_test.go new file mode 100644 index 000000000..0c63ef36e --- /dev/null +++ b/x/amm/types/pow_test.go @@ -0,0 +1,16 @@ +package types_test + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/amm/types" + "github.com/stretchr/testify/require" +) + +func TestPow(t *testing.T) { + pow := types.Pow(sdk.NewDec(2), sdk.NewDecWithPrec(25, 1)) // 2^2.5 + require.Equal(t, pow.String(), "5.656854249492380196") + pow = types.Pow(sdk.NewDec(10), sdk.NewDecWithPrec(25, 1)) // 10^2.5 + require.Equal(t, pow.String(), "316.227766016837933200") +} diff --git a/x/amm/types/solve_constant_function_invariant.go b/x/amm/types/solve_constant_function_invariant.go index 6509d35a4..6d99c0dca 100644 --- a/x/amm/types/solve_constant_function_invariant.go +++ b/x/amm/types/solve_constant_function_invariant.go @@ -1,6 +1,8 @@ package types -import sdk "github.com/cosmos/cosmos-sdk/types" +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) // solveConstantFunctionInvariant solves the constant function of an AMM // that determines the relationship between the differences of two sides diff --git a/x/amm/types/swap_in_amt_given_out.go b/x/amm/types/swap_in_amt_given_out.go index 88a1becc6..abbec8658 100644 --- a/x/amm/types/swap_in_amt_given_out.go +++ b/x/amm/types/swap_in_amt_given_out.go @@ -124,11 +124,11 @@ func (p *Pool) SwapInAmtGivenOut( // weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) // target weight - targetWeightIn := NormalizedWeight(ctx, p.PoolAssets, tokenIn.Denom) + targetWeightIn := NormalizedWeight(ctx, p.PoolAssets, tokenInDenom) targetWeightOut := NormalizedWeight(ctx, p.PoolAssets, tokenOut.Denom) // weight breaking fee as in Plasma pool - weightIn := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenIn.Denom) + weightIn := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenInDenom) weightOut := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenOut.Denom) // (45/55*60/40) ^ 2.5 diff --git a/x/margin/keeper/query_open_estimation.go b/x/margin/keeper/query_open_estimation.go index 489459f48..b5c9a23c6 100644 --- a/x/margin/keeper/query_open_estimation.go +++ b/x/margin/keeper/query_open_estimation.go @@ -37,7 +37,7 @@ func (k Keeper) OpenEstimation(goCtx context.Context, req *types.QueryOpenEstima leveragedAmount := sdk.NewDecFromBigInt(req.Collateral.Amount.BigInt()).Mul(req.Leverage).TruncateInt() leveragedCoin := sdk.NewCoin(req.Collateral.Denom, leveragedAmount) - _, _, positionSize, openPrice, swapFee, discount, availableLiquidity, err := k.amm.CalcSwapEstimationByDenom(ctx, leveragedCoin, req.Collateral.Denom, req.TradingAsset, baseCurrency, req.Discount, swapFee) + _, _, positionSize, openPrice, swapFee, discount, availableLiquidity, _, err := k.amm.CalcSwapEstimationByDenom(ctx, leveragedCoin, req.Collateral.Denom, req.TradingAsset, baseCurrency, req.Discount, swapFee) if err != nil { return nil, err } diff --git a/x/margin/types/expected_keepers.go b/x/margin/types/expected_keepers.go index 8004dc7ef..254b4d542 100644 --- a/x/margin/types/expected_keepers.go +++ b/x/margin/types/expected_keepers.go @@ -152,6 +152,7 @@ type AmmKeeper interface { swapFee sdk.Dec, discountOut sdk.Dec, availableLiquidity sdk.Coin, + weightBonus sdk.Dec, err error, ) }