diff --git a/proto/elys/amm/pool_params.proto b/proto/elys/amm/pool_params.proto index 75f547aaa..8ba1902cc 100644 --- a/proto/elys/amm/pool_params.proto +++ b/proto/elys/amm/pool_params.proto @@ -20,6 +20,10 @@ message PoolParams { (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false ]; + string weight_breaking_fee_exponent = 11 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false + ]; string external_liquidity_ratio = 5 [ (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", (gogoproto.nullable) = false diff --git a/x/amm/client/cli/query_pool_test.go b/x/amm/client/cli/query_pool_test.go index b800af22f..1ed48038d 100644 --- a/x/amm/client/cli/query_pool_test.go +++ b/x/amm/client/cli/query_pool_test.go @@ -35,6 +35,7 @@ func networkWithPoolObjects(t *testing.T, n int) (*network.Network, []types.Pool ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/client/cli/tx_create_pool.go b/x/amm/client/cli/tx_create_pool.go index c7accefff..33c9b6043 100644 --- a/x/amm/client/cli/tx_create_pool.go +++ b/x/amm/client/cli/tx_create_pool.go @@ -16,6 +16,7 @@ const ( FlagExitFee = "exit-fee" FlagUseOracle = "use-oracle" FlagWeightBreakingFeeMultiplier = "weight-breaking-fee-multiplier" + FlagWeightBreakingFeeExponent = "weight-breaking-fee-exponent" FlagExternalLiquidityRatio = "extern-liquidity-ratio" FlagLpFeePortion = "lp-fee" FlagStakingFeePortion = "staking-fee" @@ -81,6 +82,11 @@ func CmdCreatePool() *cobra.Command { return err } + weightBreakingFeeExponentStr, err := cmd.Flags().GetString(FlagWeightBreakingFeeExponent) + if err != nil { + return err + } + externalLiquidityRatioStr, err := cmd.Flags().GetString(FlagExternalLiquidityRatio) if err != nil { return err @@ -116,6 +122,7 @@ func CmdCreatePool() *cobra.Command { ExitFee: sdk.MustNewDecFromStr(exitFeeStr), UseOracle: useOracle, WeightBreakingFeeMultiplier: sdk.MustNewDecFromStr(weightBreakingFeeMultiplierStr), + WeightBreakingFeeExponent: sdk.MustNewDecFromStr(weightBreakingFeeExponentStr), ExternalLiquidityRatio: sdk.MustNewDecFromStr(externalLiquidityRatioStr), LpFeePortion: sdk.MustNewDecFromStr(lpFeePortionStr), StakingFeePortion: sdk.MustNewDecFromStr(stakingFeePortionStr), @@ -142,6 +149,7 @@ func CmdCreatePool() *cobra.Command { cmd.Flags().String(FlagExitFee, "0.00", "exit fee") 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(FlagLpFeePortion, "0.00", "lp fee portion") cmd.Flags().String(FlagStakingFeePortion, "0.00", "staking fee portion") diff --git a/x/amm/client/cli/tx_update_pool_params.go b/x/amm/client/cli/tx_update_pool_params.go index 2a293db13..5ad4833f9 100644 --- a/x/amm/client/cli/tx_update_pool_params.go +++ b/x/amm/client/cli/tx_update_pool_params.go @@ -42,6 +42,11 @@ func CmdUpdatePoolParams() *cobra.Command { return err } + weightBreakingFeeExponentStr, err := cmd.Flags().GetString(FlagWeightBreakingFeeExponent) + if err != nil { + return err + } + externalLiquidityRatioStr, err := cmd.Flags().GetString(FlagExternalLiquidityRatio) if err != nil { return err @@ -77,6 +82,7 @@ func CmdUpdatePoolParams() *cobra.Command { ExitFee: sdk.MustNewDecFromStr(exitFeeStr), UseOracle: useOracle, WeightBreakingFeeMultiplier: sdk.MustNewDecFromStr(weightBreakingFeeMultiplierStr), + WeightBreakingFeeExponent: sdk.MustNewDecFromStr(weightBreakingFeeExponentStr), ExternalLiquidityRatio: sdk.MustNewDecFromStr(externalLiquidityRatioStr), LpFeePortion: sdk.MustNewDecFromStr(lpFeePortionStr), StakingFeePortion: sdk.MustNewDecFromStr(stakingFeePortionStr), diff --git a/x/amm/keeper/apply_exit_pool_state_change_test.go b/x/amm/keeper/apply_exit_pool_state_change_test.go index 043dec035..231301c9d 100644 --- a/x/amm/keeper/apply_exit_pool_state_change_test.go +++ b/x/amm/keeper/apply_exit_pool_state_change_test.go @@ -49,6 +49,7 @@ func (suite *KeeperTestSuite) TestApplyExitPoolStateChange_WithdrawFromCommitmen ExitFee: exitFee, UseOracle: true, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/fee_test.go b/x/amm/keeper/fee_test.go index f8d730f74..3e741dd23 100644 --- a/x/amm/keeper/fee_test.go +++ b/x/amm/keeper/fee_test.go @@ -88,6 +88,7 @@ func (suite *KeeperTestSuite) TestOnCollectFee() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -186,6 +187,7 @@ func (suite *KeeperTestSuite) TestSwapFeesToRevenueToken() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), 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 951b1b294..a702ad6a6 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_in_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_in_test.go @@ -223,6 +223,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn() { UseOracle: tc.isOraclePool, ExternalLiquidityRatio: sdk.NewDec(2), WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), WeightRecoveryFeePortion: sdk.ZeroDec(), 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 86f33bef7..18c62b6ed 100644 --- a/x/amm/keeper/keeper_swap_exact_amount_out_test.go +++ b/x/amm/keeper/keeper_swap_exact_amount_out_test.go @@ -214,6 +214,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut() { UseOracle: tc.isOraclePool, ExternalLiquidityRatio: sdk.NewDec(2), WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), WeightRecoveryFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/msg_server_create_pool_test.go b/x/amm/keeper/msg_server_create_pool_test.go index eeb46d030..c10d8d1f2 100644 --- a/x/amm/keeper/msg_server_create_pool_test.go +++ b/x/amm/keeper/msg_server_create_pool_test.go @@ -28,6 +28,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -57,6 +58,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -86,6 +88,7 @@ func (suite *KeeperTestSuite) TestMsgServerCreatePool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/msg_server_exit_pool_test.go b/x/amm/keeper/msg_server_exit_pool_test.go index d6bd2d702..7abe32d58 100644 --- a/x/amm/keeper/msg_server_exit_pool_test.go +++ b/x/amm/keeper/msg_server_exit_pool_test.go @@ -31,6 +31,7 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -52,6 +53,7 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -72,7 +74,8 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -94,7 +97,8 @@ func (suite *KeeperTestSuite) TestMsgServerExitPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: 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 09fc7b9a8..2617a207c 100644 --- a/x/amm/keeper/msg_server_join_pool_test.go +++ b/x/amm/keeper/msg_server_join_pool_test.go @@ -30,6 +30,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -51,6 +52,7 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -71,7 +73,8 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -93,7 +96,8 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -115,7 +119,8 @@ func (suite *KeeperTestSuite) TestMsgServerJoinPool() { SwapFee: sdk.ZeroDec(), ExitFee: sdk.ZeroDec(), UseOracle: true, - WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeMultiplier: sdk.NewDecWithPrec(1, 0), // 1.00 + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/msg_server_update_pool_params_test.go b/x/amm/keeper/msg_server_update_pool_params_test.go index e6103656c..bc9c5a691 100644 --- a/x/amm/keeper/msg_server_update_pool_params_test.go +++ b/x/amm/keeper/msg_server_update_pool_params_test.go @@ -30,6 +30,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -42,6 +43,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { ExitFee: sdk.MustNewDecFromStr("0.02"), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -71,6 +73,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -83,6 +86,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), @@ -112,6 +116,7 @@ func (suite *KeeperTestSuite) TestMsgServerUpdatePoolParams() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/pool_test.go b/x/amm/keeper/pool_test.go index 0ba7bf67a..258e0ca3d 100644 --- a/x/amm/keeper/pool_test.go +++ b/x/amm/keeper/pool_test.go @@ -23,6 +23,7 @@ func createNPool(keeper *keeper.Keeper, ctx sdk.Context, n int) []types.Pool { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.ZeroDec(), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/keeper/update_pool_for_swap_test.go b/x/amm/keeper/update_pool_for_swap_test.go index 7fe29d157..2c420af6a 100644 --- a/x/amm/keeper/update_pool_for_swap_test.go +++ b/x/amm/keeper/update_pool_for_swap_test.go @@ -153,6 +153,7 @@ func (suite *KeeperTestSuite) TestUpdatePoolForSwap() { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/amm/types/calc_exit_pool.go b/x/amm/types/calc_exit_pool.go index c1bfeda15..e4ad1b45b 100644 --- a/x/amm/types/calc_exit_pool.go +++ b/x/amm/types/calc_exit_pool.go @@ -128,6 +128,7 @@ func CalcExitPool(ctx sdk.Context, oracleKeeper OracleKeeper, pool Pool, account weightBreakingFee := sdk.ZeroDec() if distanceDiff.IsPositive() { weightBreakingFee = pool.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + // TODO: weight breaking fee might need to be changed for exit pool } tokenOutAmount := oracleOutAmount.Mul(sdk.OneDec().Sub(weightBreakingFee)).RoundInt() diff --git a/x/amm/types/pool_join_pool_no_swap.go b/x/amm/types/pool_join_pool_no_swap.go index 331349aab..2977f9d6f 100644 --- a/x/amm/types/pool_join_pool_no_swap.go +++ b/x/amm/types/pool_join_pool_no_swap.go @@ -166,6 +166,7 @@ func (p *Pool) JoinPool(ctx sdk.Context, oracleKeeper OracleKeeper, accountedPoo weightBreakingFee := sdk.ZeroDec() if distanceDiff.IsPositive() { weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + // TODO: weight breaking fee might need to be changed for exit pool } weightBalanceBonus := sdk.ZeroDec() if initialWeightDistance.GT(p.PoolParams.ThresholdWeightDifference) && distanceDiff.IsNegative() { diff --git a/x/amm/types/pool_params.pb.go b/x/amm/types/pool_params.pb.go index 2e3e58587..8a105474a 100644 --- a/x/amm/types/pool_params.pb.go +++ b/x/amm/types/pool_params.pb.go @@ -30,6 +30,7 @@ type PoolParams struct { ExitFee github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,2,opt,name=exit_fee,json=exitFee,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"exit_fee"` UseOracle bool `protobuf:"varint,3,opt,name=use_oracle,json=useOracle,proto3" json:"use_oracle,omitempty"` WeightBreakingFeeMultiplier github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,4,opt,name=weight_breaking_fee_multiplier,json=weightBreakingFeeMultiplier,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"weight_breaking_fee_multiplier"` + WeightBreakingFeeExponent github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,11,opt,name=weight_breaking_fee_exponent,json=weightBreakingFeeExponent,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"weight_breaking_fee_exponent"` ExternalLiquidityRatio github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,5,opt,name=external_liquidity_ratio,json=externalLiquidityRatio,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"external_liquidity_ratio"` LpFeePortion github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,6,opt,name=lp_fee_portion,json=lpFeePortion,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"lp_fee_portion"` StakingFeePortion github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,7,opt,name=staking_fee_portion,json=stakingFeePortion,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"staking_fee_portion"` @@ -92,35 +93,37 @@ func init() { func init() { proto.RegisterFile("elys/amm/pool_params.proto", fileDescriptor_3500125990074bc9) } var fileDescriptor_3500125990074bc9 = []byte{ - // 447 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0xd3, 0xcd, 0x8a, 0xdb, 0x30, - 0x10, 0x07, 0xf0, 0xb8, 0x1f, 0xbb, 0x8e, 0x28, 0x85, 0xba, 0xa5, 0x78, 0x13, 0xea, 0x5d, 0x7a, - 0x28, 0xb9, 0xac, 0x7d, 0xe8, 0x1b, 0x84, 0xb0, 0x50, 0x68, 0x69, 0x30, 0x85, 0x42, 0x0f, 0x15, - 0x8a, 0x3d, 0xb1, 0x45, 0x24, 0x8f, 0x2b, 0xc9, 0x4d, 0xf2, 0x16, 0x7d, 0xac, 0x3d, 0x2e, 0x3d, - 0x95, 0x1e, 0x96, 0x92, 0xbc, 0x48, 0x91, 0xec, 0xa4, 0xe9, 0xd5, 0x27, 0x7b, 0x34, 0xe8, 0xf7, - 0x47, 0x12, 0x43, 0x46, 0x20, 0xb6, 0x3a, 0x61, 0x52, 0x26, 0x35, 0xa2, 0xa0, 0x35, 0x53, 0x4c, - 0xea, 0xb8, 0x56, 0x68, 0x30, 0xf0, 0x6d, 0x2f, 0x66, 0x52, 0x8e, 0x5e, 0x14, 0x58, 0xa0, 0x5b, - 0x4c, 0xec, 0x5f, 0xdb, 0x1f, 0x5d, 0x64, 0xa8, 0x25, 0x6a, 0xda, 0x36, 0xda, 0xa2, 0x6d, 0xbd, - 0xfe, 0x79, 0x46, 0xc8, 0x1c, 0x51, 0xcc, 0x9d, 0x17, 0x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0xeb, 0xa8, 0x13, - 0x5f, 0x92, 0xee, 0xfa, 0xa8, 0x82, 0x0c, 0xbf, 0x83, 0xda, 0xfe, 0x97, 0xe3, 0xf7, 0xca, 0x09, - 0x5b, 0x32, 0xed, 0xc4, 0x93, 0xb8, 0x8a, 0x8c, 0x4d, 0xa9, 0x40, 0x97, 0x28, 0x72, 0xda, 0x05, - 0xe7, 0x7c, 0xb9, 0x04, 0x05, 0x55, 0x06, 0xe1, 0xb0, 0x57, 0xdc, 0xc5, 0x91, 0xfc, 0xec, 0xc4, - 0xd9, 0x11, 0x0c, 0xc6, 0x64, 0x68, 0x8f, 0x93, 0x43, 0x85, 0x32, 0x24, 0x56, 0x4f, 0xfd, 0x25, - 0xc0, 0xcc, 0xd6, 0xd3, 0xe9, 0xed, 0x2e, 0xf2, 0xee, 0x76, 0x91, 0xf7, 0x67, 0x17, 0x79, 0x3f, - 0xf6, 0xd1, 0xe0, 0x6e, 0x1f, 0x0d, 0x7e, 0xed, 0xa3, 0xc1, 0x97, 0xc9, 0x49, 0xb2, 0x1d, 0xda, - 0xeb, 0x0a, 0xcc, 0x1a, 0xd5, 0xca, 0x15, 0xc9, 0xc6, 0xcd, 0xb7, 0xcb, 0x5f, 0x9c, 0xb9, 0xf9, - 0x7c, 0xfb, 0x37, 0x00, 0x00, 0xff, 0xff, 0x0b, 0xfb, 0x03, 0x55, 0xf8, 0x03, 0x00, 0x00, + // 471 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0xd4, 0xcf, 0x8b, 0xd3, 0x40, + 0x14, 0x07, 0xf0, 0xc6, 0x1f, 0xdb, 0x76, 0x14, 0xc1, 0x28, 0x32, 0xdb, 0x6a, 0x76, 0xf1, 0x20, + 0xbd, 0x6c, 0x73, 0xf0, 0x3f, 0x28, 0x75, 0x41, 0x50, 0x2c, 0x41, 0x10, 0x3c, 0x38, 0xa4, 0xc9, + 0x6b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xf0, 0xae, + 0xb7, 0x81, 0xf7, 0x7b, 0x1b, 0x78, 0xdf, 0x77, 0x41, 0xe7, 0x7a, 0x17, 0x74, 0x7e, 0xee, 0x82, + 0xce, 0xe7, 0xd1, 0x51, 0xb2, 0x9d, 0x12, 0x17, 0x05, 0x98, 0x15, 0xaa, 0xa5, 0x2b, 0xc2, 0xb5, + 0x1b, 0x28, 0x2e, 0x7f, 0x7e, 0xe2, 0x06, 0xc2, 0xeb, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x2d, + 0x0e, 0x59, 0x1a, 0x69, 0x04, 0x00, 0x00, } func (m *PoolParams) Marshal() (dAtA []byte, err error) { @@ -143,6 +146,16 @@ func (m *PoolParams) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + { + size := m.WeightBreakingFeeExponent.Size() + i -= size + if _, err := m.WeightBreakingFeeExponent.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintPoolParams(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x5a if len(m.FeeDenom) > 0 { i -= len(m.FeeDenom) copy(dAtA[i:], m.FeeDenom) @@ -283,6 +296,8 @@ func (m *PoolParams) Size() (n int) { if l > 0 { n += 1 + l + sovPoolParams(uint64(l)) } + l = m.WeightBreakingFeeExponent.Size() + n += 1 + l + sovPoolParams(uint64(l)) return n } @@ -645,6 +660,40 @@ func (m *PoolParams) Unmarshal(dAtA []byte) error { } m.FeeDenom = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex + case 11: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field WeightBreakingFeeExponent", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowPoolParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthPoolParams + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthPoolParams + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.WeightBreakingFeeExponent.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipPoolParams(dAtA[iNdEx:]) diff --git a/x/amm/types/swap_in_amt_given_out.go b/x/amm/types/swap_in_amt_given_out.go index 82b14213b..f0728432e 100644 --- a/x/amm/types/swap_in_amt_given_out.go +++ b/x/amm/types/swap_in_amt_given_out.go @@ -81,6 +81,9 @@ func (p *Pool) SwapInAmtGivenOut( initialWeightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, p.PoolAssets) + startWeightIn := OracleAssetWeight(ctx, oracleKeeper, p.PoolAssets, tokenIn.Denom) + startWeightOut := OracleAssetWeight(ctx, oracleKeeper, p.PoolAssets, tokenOut.Denom) + // in amount is calculated in this formula // balancer slippage amount = Max(oracleOutAmount-balancerOutAmount, 0) // resizedAmount = tokenIn / externalLiquidityRatio @@ -120,7 +123,16 @@ func (p *Pool) SwapInAmtGivenOut( // cut is valid when distance higher than original distance weightBreakingFee := sdk.ZeroDec() if distanceDiff.IsPositive() { - weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + // old weight breaking fee implementation + // weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + + // weight breaking fee as in Plasma pool + weightIn := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenIn.Denom) + weightOut := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenOut.Denom) + + // (45/55*60/40) ^ 2.5 + weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier. + Mul(Pow(weightIn.Mul(startWeightOut).Quo(weightOut).Quo(startWeightIn), p.PoolParams.WeightBreakingFeeExponent)) } // bonus is valid when distance is lower than original distance and when threshold weight reached diff --git a/x/amm/types/swap_in_amt_given_out_test.go b/x/amm/types/swap_in_amt_given_out_test.go index d2f001805..d7d2caca9 100644 --- a/x/amm/types/swap_in_amt_given_out_test.go +++ b/x/amm/types/swap_in_amt_given_out_test.go @@ -184,6 +184,7 @@ func (suite *TestSuite) TestSwapInAmtGivenOut() { ExternalLiquidityRatio: tc.externalLiquidityRatio, ThresholdWeightDifference: tc.thresholdWeightDiff, WeightBreakingFeeMultiplier: sdk.OneDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 }, TotalShares: sdk.Coin{}, PoolAssets: tc.poolAssets, diff --git a/x/amm/types/swap_out_amt_given_in.go b/x/amm/types/swap_out_amt_given_in.go index f2b8fa0a2..900fc074e 100644 --- a/x/amm/types/swap_out_amt_given_in.go +++ b/x/amm/types/swap_out_amt_given_in.go @@ -104,6 +104,19 @@ func (p Pool) WeightDistanceFromTarget(ctx sdk.Context, oracleKeeper OracleKeepe return distanceSum.Quo(sdk.NewDec(int64(len(p.PoolAssets)))) } +func OracleAssetWeight(ctx sdk.Context, oracleKeeper OracleKeeper, poolAssets []PoolAsset, denom string) sdk.Dec { + oracleWeights, err := OraclePoolNormalizedWeights(ctx, oracleKeeper, poolAssets) + if err != nil { + return sdk.ZeroDec() + } + for _, weight := range oracleWeights { + if weight.Asset == denom { + return weight.Weight + } + } + return sdk.ZeroDec() +} + func (p Pool) CalcGivenInSlippage( ctx sdk.Context, oracleKeeper OracleKeeper, @@ -182,6 +195,9 @@ func (p *Pool) SwapOutAmtGivenIn( initialWeightDistance := p.WeightDistanceFromTarget(ctx, oracleKeeper, p.PoolAssets) + startWeightIn := OracleAssetWeight(ctx, oracleKeeper, p.PoolAssets, tokenIn.Denom) + startWeightOut := OracleAssetWeight(ctx, oracleKeeper, p.PoolAssets, tokenOutDenom) + // out amount is calculated in this formula // balancer slippage amount = Max(oracleOutAmount-balancerOutAmount, 0) // resizedAmount = tokenIn / externalLiquidityRatio @@ -263,7 +279,16 @@ func (p *Pool) SwapOutAmtGivenIn( // cut is valid when distance higher than original distance weightBreakingFee := sdk.ZeroDec() if distanceDiff.IsPositive() { - weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + // old weight breaking fee implementation + // weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier.Mul(distanceDiff) + + // weight breaking fee as in Plasma pool + weightIn := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenIn.Denom) + weightOut := OracleAssetWeight(ctx, oracleKeeper, newAssetPools, tokenOutDenom) + + // (45/55*60/40) ^ 2.5 + weightBreakingFee = p.PoolParams.WeightBreakingFeeMultiplier. + Mul(Pow(weightIn.Mul(startWeightOut).Quo(weightOut).Quo(startWeightIn), p.PoolParams.WeightBreakingFeeExponent)) } // bonus is valid when distance is lower than original distance and when threshold weight reached diff --git a/x/amm/types/swap_out_amt_given_in_test.go b/x/amm/types/swap_out_amt_given_in_test.go index 2fe3a1bef..fa29b106e 100644 --- a/x/amm/types/swap_out_amt_given_in_test.go +++ b/x/amm/types/swap_out_amt_given_in_test.go @@ -594,6 +594,7 @@ func (suite *TestSuite) TestSwapOutAmtGivenIn() { ExternalLiquidityRatio: tc.externalLiquidityRatio, ThresholdWeightDifference: tc.thresholdWeightDiff, WeightBreakingFeeMultiplier: sdk.OneDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 }, TotalShares: sdk.Coin{}, PoolAssets: tc.poolAssets, diff --git a/x/incentive/keeper/keeper_lps_test.go b/x/incentive/keeper/keeper_lps_test.go index 7dacf6e2d..f3b686ff5 100644 --- a/x/incentive/keeper/keeper_lps_test.go +++ b/x/incentive/keeper/keeper_lps_test.go @@ -134,6 +134,7 @@ func TestCalculateRewardsForLPs(t *testing.T) { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.OneDec(), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/incentive/keeper/params_test.go b/x/incentive/keeper/params_test.go index 1d2387249..0c1cb41ce 100644 --- a/x/incentive/keeper/params_test.go +++ b/x/incentive/keeper/params_test.go @@ -64,6 +64,7 @@ func TestUpdatePoolMultiplierInfo(t *testing.T) { ExitFee: sdk.ZeroDec(), UseOracle: false, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.OneDec(), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/leveragelp/keeper/position_close_test.go b/x/leveragelp/keeper/position_close_test.go index 46ab3a9d9..a68307b80 100644 --- a/x/leveragelp/keeper/position_close_test.go +++ b/x/leveragelp/keeper/position_close_test.go @@ -42,6 +42,7 @@ func (suite KeeperTestSuite) OpenPosition(addr sdk.AccAddress) (*types.Position, ExitFee: sdk.ZeroDec(), UseOracle: true, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(), diff --git a/x/leveragelp/keeper/position_open_test.go b/x/leveragelp/keeper/position_open_test.go index cbfe1be15..00888d715 100644 --- a/x/leveragelp/keeper/position_open_test.go +++ b/x/leveragelp/keeper/position_open_test.go @@ -37,6 +37,7 @@ func (suite KeeperTestSuite) TestOpenLong() { ExitFee: sdk.ZeroDec(), UseOracle: true, WeightBreakingFeeMultiplier: sdk.ZeroDec(), + WeightBreakingFeeExponent: sdk.NewDecWithPrec(25, 1), // 2.5 ExternalLiquidityRatio: sdk.NewDec(1), LpFeePortion: sdk.ZeroDec(), StakingFeePortion: sdk.ZeroDec(),