Skip to content

Commit

Permalink
[Amm]: Improve external liquidity (#898)
Browse files Browse the repository at this point in the history
* add migration

* add logic for multiple external liq ratio

* update

* fix tests

* fix tests

* add tests

* more coverage

* remove external liq from pool params
  • Loading branch information
amityadav0 authored Nov 3, 2024
1 parent a2bfd93 commit 01e3fbf
Show file tree
Hide file tree
Showing 52 changed files with 945 additions and 436 deletions.
2 changes: 1 addition & 1 deletion proto/elys/amm/pool.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ message LegacyPool {
string address = 2;
LegacyPoolParams pool_params = 3 [(gogoproto.nullable) = false];
cosmos.base.v1beta1.Coin total_shares = 4 [(gogoproto.nullable) = false];
repeated PoolAsset pool_assets = 5 [(gogoproto.nullable) = false];
repeated LegacyPoolAsset pool_assets = 5 [(gogoproto.nullable) = false];
string total_weight = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
Expand Down
15 changes: 14 additions & 1 deletion proto/elys/amm/pool_asset.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,24 @@ option go_package = "github.com/elys-network/elys/x/amm/types";
import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

message LegacyPoolAsset {

cosmos.base.v1beta1.Coin token = 1 [(gogoproto.nullable) = false];
string weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
}

message PoolAsset {

cosmos.base.v1beta1.Coin token = 1 [(gogoproto.nullable) = false];
string weight = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = false
];
];
string external_liquidity_ratio = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}
26 changes: 13 additions & 13 deletions proto/elys/amm/pool_params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ message LegacyPoolParams {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_exponent = 11 [
string weight_breaking_fee_exponent = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string external_liquidity_ratio = 5 [
string external_liquidity_ratio = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string weight_recovery_fee_portion = 6 [
string weight_recovery_fee_portion = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string threshold_weight_difference = 7 [
string threshold_weight_difference = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string fee_denom = 8; // denom for fee collection
string weight_breaking_fee_portion = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string fee_denom = 10; // denom for fee collection
}

message PoolParams {
Expand All @@ -57,21 +61,17 @@ message PoolParams {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string external_liquidity_ratio = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string weight_recovery_fee_portion = 7 [
string weight_recovery_fee_portion = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string threshold_weight_difference = 8 [
string threshold_weight_difference = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_portion = 9 [
string weight_breaking_fee_portion = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
string fee_denom = 10; // denom for fee collection
string fee_denom = 9; // denom for fee collection
}
1 change: 0 additions & 1 deletion x/amm/client/cli/query_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func networkWithPoolObjects(t *testing.T, n int) (*network.Network, []types.Pool
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down
15 changes: 5 additions & 10 deletions x/amm/client/cli/tx_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const (
FlagUseOracle = "use-oracle"
FlagWeightBreakingFeeMultiplier = "weight-breaking-fee-multiplier"
FlagWeightBreakingFeeExponent = "weight-breaking-fee-exponent"
FlagExternalLiquidityRatio = "extern-liquidity-ratio"
FlagWeightRecoveryFeePortion = "weight-recovery-fee"
FlagThresholdWeightDifference = "threshold-weight-diff"
FlagFeeDenom = "fee-denom"
Expand Down Expand Up @@ -54,9 +53,12 @@ func CmdCreatePool() *cobra.Command {
return errors.New("deposit tokens and token weights should have same denom order")
}

// External liquidity ratio initially is set to 1, this value can be changed by feeder only with relevant orderbook liquidity
// Setting this ratio to 1 is equivalent to considering only amm liquidity when making swaps
poolAssets = append(poolAssets, types.PoolAsset{
Weight: argWeights[i].Amount,
Token: argInitialDeposit[i],
Weight: argWeights[i].Amount,
Token: argInitialDeposit[i],
ExternalLiquidityRatio: sdk.OneDec(),
})
}

Expand Down Expand Up @@ -85,11 +87,6 @@ func CmdCreatePool() *cobra.Command {
return err
}

externalLiquidityRatioStr, err := cmd.Flags().GetString(FlagExternalLiquidityRatio)
if err != nil {
return err
}

weightRecoveryFeePortionStr, err := cmd.Flags().GetString(FlagWeightRecoveryFeePortion)
if err != nil {
return err
Expand All @@ -111,7 +108,6 @@ func CmdCreatePool() *cobra.Command {
UseOracle: useOracle,
WeightBreakingFeeMultiplier: sdk.MustNewDecFromStr(weightBreakingFeeMultiplierStr),
WeightBreakingFeeExponent: sdk.MustNewDecFromStr(weightBreakingFeeExponentStr),
ExternalLiquidityRatio: sdk.MustNewDecFromStr(externalLiquidityRatioStr),
WeightRecoveryFeePortion: sdk.MustNewDecFromStr(weightRecoveryFeePortionStr),
ThresholdWeightDifference: sdk.MustNewDecFromStr(thresholdWeightDifferenceStr),
FeeDenom: feeDenom,
Expand All @@ -136,7 +132,6 @@ func CmdCreatePool() *cobra.Command {
cmd.Flags().Bool(FlagUseOracle, false, "flag to be an oracle pool or non-oracle pool")
cmd.Flags().String(FlagWeightBreakingFeeMultiplier, "0.00", "weight breaking fee multiplier")
cmd.Flags().String(FlagWeightBreakingFeeExponent, "2.50", "weight breaking fee exponent")
cmd.Flags().String(FlagExternalLiquidityRatio, "0.00", "external liquidity ratio - valid for oracle pools")
cmd.Flags().String(FlagWeightRecoveryFeePortion, "0.10", "weight recovery fee portion")
cmd.Flags().String(FlagThresholdWeightDifference, "0.00", "threshold weight difference - valid for oracle pool")
cmd.Flags().String(FlagFeeDenom, "", "fee denom")
Expand Down
7 changes: 0 additions & 7 deletions x/amm/client/cli/tx_update_pool_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ func CmdUpdatePoolParams() *cobra.Command {
return err
}

externalLiquidityRatioStr, err := cmd.Flags().GetString(FlagExternalLiquidityRatio)
if err != nil {
return err
}

weightRecoveryFeePortionStr, err := cmd.Flags().GetString(FlagWeightRecoveryFeePortion)
if err != nil {
return err
Expand All @@ -73,7 +68,6 @@ func CmdUpdatePoolParams() *cobra.Command {
UseOracle: useOracle,
WeightBreakingFeeMultiplier: sdk.MustNewDecFromStr(weightBreakingFeeMultiplierStr),
WeightBreakingFeeExponent: sdk.MustNewDecFromStr(weightBreakingFeeExponentStr),
ExternalLiquidityRatio: sdk.MustNewDecFromStr(externalLiquidityRatioStr),
WeightRecoveryFeePortion: sdk.MustNewDecFromStr(weightRecoveryFeePortionStr),
ThresholdWeightDifference: sdk.MustNewDecFromStr(thresholdWeightDifferenceStr),
FeeDenom: feeDenom,
Expand Down Expand Up @@ -103,7 +97,6 @@ func CmdUpdatePoolParams() *cobra.Command {
cmd.Flags().Bool(FlagUseOracle, false, "flag to be an oracle pool or non-oracle pool")
cmd.Flags().String(FlagWeightBreakingFeeExponent, "0.00", "weight breaking fee exponent")
cmd.Flags().String(FlagWeightBreakingFeeMultiplier, "0.00", "weight breaking fee multiplier")
cmd.Flags().String(FlagExternalLiquidityRatio, "0.00", "external liquidity ratio - valid for oracle pools")
cmd.Flags().String(FlagWeightRecoveryFeePortion, "0.00", "weight recovery fee portion")
cmd.Flags().String(FlagThresholdWeightDifference, "0.00", "threshold weight difference - valid for oracle pool")
cmd.Flags().String(FlagFeeDenom, "", "fee denom")
Expand Down
1 change: 0 additions & 1 deletion x/amm/keeper/apply_exit_pool_state_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func (suite *KeeperTestSuite) TestApplyExitPoolStateChange_WithdrawFromCommitmen
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: ptypes.BaseCurrency,
Expand Down
1 change: 0 additions & 1 deletion x/amm/keeper/calc_in_route_spot_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (suite *KeeperTestSuite) TestCalcInRouteSpotPrice() {
RebalanceTreasury: treasuryAddr.String(),
PoolParams: types.PoolParams{
UseOracle: false,
ExternalLiquidityRatio: sdk.NewDec(2),
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
Expand Down
1 change: 0 additions & 1 deletion x/amm/keeper/calc_out_route_spot_price_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (suite *KeeperTestSuite) TestCalcOutRouteSpotPrice() {
RebalanceTreasury: treasuryAddr.String(),
PoolParams: types.PoolParams{
UseOracle: false,
ExternalLiquidityRatio: sdk.NewDec(2),
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
Expand Down
1 change: 0 additions & 1 deletion x/amm/keeper/calc_swap_estimation_by_denom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func (suite *KeeperTestSuite) TestCalcSwapEstimationByDenom() {
RebalanceTreasury: treasuryAddr.String(),
PoolParams: types.PoolParams{
UseOracle: false,
ExternalLiquidityRatio: sdk.NewDec(2),
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
Expand Down
2 changes: 0 additions & 2 deletions x/amm/keeper/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (suite *KeeperTestSuite) TestOnCollectFee() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
FeeDenom: ptypes.BaseCurrency,
Expand Down Expand Up @@ -186,7 +185,6 @@ func (suite *KeeperTestSuite) TestSwapFeesToRevenueToken() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
FeeDenom: ptypes.BaseCurrency,
Expand Down
11 changes: 6 additions & 5 deletions x/amm/keeper/keeper_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn() {
RebalanceTreasury: treasuryAddr.String(),
PoolParams: types.PoolParams{
UseOracle: tc.isOraclePool,
ExternalLiquidityRatio: sdk.NewDec(2),
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
Expand All @@ -233,12 +232,14 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn() {
TotalShares: sdk.Coin{},
PoolAssets: []types.PoolAsset{
{
Token: tc.poolInitBalance[0],
Weight: sdk.NewInt(10),
Token: tc.poolInitBalance[0],
Weight: sdk.NewInt(10),
ExternalLiquidityRatio: sdk.NewDec(2),
},
{
Token: tc.poolInitBalance[1],
Weight: sdk.NewInt(10),
Token: tc.poolInitBalance[1],
Weight: sdk.NewInt(10),
ExternalLiquidityRatio: sdk.NewDec(2),
},
},
TotalWeight: sdk.ZeroInt(),
Expand Down
11 changes: 6 additions & 5 deletions x/amm/keeper/keeper_swap_exact_amount_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut() {
RebalanceTreasury: treasuryAddr.String(),
PoolParams: types.PoolParams{
UseOracle: tc.isOraclePool,
ExternalLiquidityRatio: sdk.NewDec(2),
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
Expand All @@ -224,12 +223,14 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut() {
TotalShares: sdk.Coin{},
PoolAssets: []types.PoolAsset{
{
Token: tc.poolInitBalance[0],
Weight: sdk.NewInt(10),
Token: tc.poolInitBalance[0],
Weight: sdk.NewInt(10),
ExternalLiquidityRatio: sdk.NewDec(2),
},
{
Token: tc.poolInitBalance[1],
Weight: sdk.NewInt(10),
Token: tc.poolInitBalance[1],
Weight: sdk.NewInt(10),
ExternalLiquidityRatio: sdk.NewDec(2),
},
},
TotalWeight: sdk.ZeroInt(),
Expand Down
5 changes: 0 additions & 5 deletions x/amm/keeper/msg_server_create_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down Expand Up @@ -61,7 +60,6 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down Expand Up @@ -91,7 +89,6 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down Expand Up @@ -121,7 +118,6 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down Expand Up @@ -151,7 +147,6 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down
4 changes: 0 additions & 4 deletions x/amm/keeper/msg_server_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand All @@ -54,7 +53,6 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() {
UseOracle: false,
WeightBreakingFeeMultiplier: sdk.ZeroDec(),
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.ZeroDec(),
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand All @@ -75,7 +73,6 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() {
UseOracle: true,
WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.NewDecWithPrec(2, 1), // 20%
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand All @@ -97,7 +94,6 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() {
UseOracle: true,
WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 2), // 0.01
WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5
ExternalLiquidityRatio: sdk.NewDec(1),
WeightRecoveryFeePortion: sdk.NewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdk.NewDecWithPrec(2, 1), // 20%
WeightBreakingFeePortion: sdk.NewDecWithPrec(50, 2), // 50%
Expand Down
Loading

0 comments on commit 01e3fbf

Please sign in to comment.