Skip to content

Commit

Permalink
weight balance test fixes1
Browse files Browse the repository at this point in the history
  • Loading branch information
jelysn committed Dec 15, 2023
1 parent 17dd5d3 commit 6429707
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion x/amm/client/wasm/query_amm_price_by_denom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions x/amm/keeper/msg_server_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
},
{
Expand All @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions x/amm/keeper/msg_server_join_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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(),
Expand Down
5 changes: 0 additions & 5 deletions x/amm/types/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions x/amm/types/pow_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 3 additions & 1 deletion x/amm/types/solve_constant_function_invariant.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/amm/types/swap_in_amt_given_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/margin/keeper/query_open_estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions x/margin/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type AmmKeeper interface {
swapFee sdk.Dec,
discountOut sdk.Dec,
availableLiquidity sdk.Coin,
weightBonus sdk.Dec,
err error,
)
}
Expand Down

0 comments on commit 6429707

Please sign in to comment.