Skip to content

Commit

Permalink
Amm params improvements (#981)
Browse files Browse the repository at this point in the history
* changing amm module params structure

* fixing test cases

* fixing test cases

* merging with main

* merging with main

* fixing migration issues

* adding more validation for amm msgs

* fixing unit test cases

* fixing unit test cases
  • Loading branch information
avkr003 authored Nov 21, 2024
1 parent 6215e5d commit e59c9f2
Show file tree
Hide file tree
Showing 122 changed files with 3,771 additions and 2,908 deletions.
1,569 changes: 1,384 additions & 185 deletions api/elys/amm/params.pulsar.go

Large diffs are not rendered by default.

176 changes: 87 additions & 89 deletions api/elys/amm/pool.pulsar.go

Large diffs are not rendered by default.

676 changes: 62 additions & 614 deletions api/elys/amm/pool_params.pulsar.go

Large diffs are not rendered by default.

637 changes: 319 additions & 318 deletions api/elys/amm/tx.pulsar.go

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion proto/elys/amm/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "cosmos_proto/cosmos.proto";
option go_package = "github.com/elys-network/elys/x/amm/types";

// Params defines the parameters for the module.
message Params {
message LegacyParams {
string pool_creation_fee = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
Expand All @@ -16,3 +16,39 @@ message Params {
uint64 slippage_track_duration = 2; // default 1 week: 604,800
bool enable_base_currency_paired_pool_only = 3;
}

message Params {
string pool_creation_fee = 1 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
(gogoproto.nullable) = false
];
uint64 slippage_track_duration = 2; // default 1 week: 604,800
repeated string base_assets = 3;
string weight_breaking_fee_exponent = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_multiplier = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_portion = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_recovery_fee_portion = 7 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string threshold_weight_difference = 8 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
repeated string allowed_pool_creators = 9;
}
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 LegacyPoolAsset pool_assets = 5 [ (gogoproto.nullable) = false ];
repeated PoolAsset pool_assets = 5 [ (gogoproto.nullable) = false ];
string total_weight = 6 [
(cosmos_proto.scalar) = "cosmos.Int",
(gogoproto.customtype) = "cosmossdk.io/math.Int",
Expand Down
47 changes: 6 additions & 41 deletions proto/elys/amm/pool_params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,22 @@ message LegacyPoolParams {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string external_liquidity_ratio = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_recovery_fee_portion = 7 [
string weight_recovery_fee_portion = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string threshold_weight_difference = 8 [
string threshold_weight_difference = 7 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_portion = 9 [
string weight_breaking_fee_portion = 8 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string fee_denom = 10; // denom for fee collection
string fee_denom = 9; // denom for fee collection
}

message PoolParams {
Expand All @@ -57,36 +52,6 @@ message PoolParams {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string exit_fee = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
bool use_oracle = 3;
string weight_breaking_fee_multiplier = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_exponent = 5 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_recovery_fee_portion = 6 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string threshold_weight_difference = 7 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string weight_breaking_fee_portion = 8 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string fee_denom = 9; // denom for fee collection
bool use_oracle = 2;
string fee_denom = 3; // denom for fee collection
}
4 changes: 2 additions & 2 deletions proto/elys/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ message MsgCreatePool {
option (cosmos.msg.v1.signer) = "sender";
option (amino.name) = "amm/MsgCreatePool";
string sender = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
PoolParams pool_params = 2;
PoolParams pool_params = 2 [ (gogoproto.nullable) = false ];
repeated PoolAsset pool_assets = 3 [ (gogoproto.nullable) = false ];
}

Expand Down Expand Up @@ -224,7 +224,7 @@ message MsgUpdatePoolParams {
option (amino.name) = "amm/MsgUpdatePoolParams";
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
uint64 pool_id = 2;
PoolParams pool_params = 3;
PoolParams pool_params = 3 [ (gogoproto.nullable) = false ];
}

message MsgUpdatePoolParamsResponse {
Expand Down
12 changes: 3 additions & 9 deletions x/amm/client/cli/query_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@ func networkWithPoolObjects(t *testing.T, n int) (*network.Network, []types.Pool
TotalWeight: sdkmath.NewInt(100),
Address: types.NewPoolAddress(uint64(i)).String(),
PoolParams: types.PoolParams{
SwapFee: sdkmath.LegacyZeroDec(),
ExitFee: sdkmath.LegacyZeroDec(),
UseOracle: false,
WeightBreakingFeeMultiplier: sdkmath.LegacyZeroDec(),
WeightBreakingFeeExponent: sdkmath.LegacyNewDecWithPrec(25, 1), // 2.5
WeightRecoveryFeePortion: sdkmath.LegacyNewDecWithPrec(10, 2), // 10%
ThresholdWeightDifference: sdkmath.LegacyZeroDec(),
WeightBreakingFeePortion: sdkmath.LegacyNewDecWithPrec(50, 2), // 50%
FeeDenom: ptypes.BaseCurrency,
SwapFee: sdkmath.LegacyZeroDec(),
UseOracle: false,
FeeDenom: ptypes.BaseCurrency,
},
}
nullify.Fill(&pool)
Expand Down
62 changes: 7 additions & 55 deletions x/amm/client/cli/tx_create_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ import (
)

const (
FlagSwapFee = "swap-fee"
FlagExitFee = "exit-fee"
FlagUseOracle = "use-oracle"
FlagWeightBreakingFeeMultiplier = "weight-breaking-fee-multiplier"
FlagWeightBreakingFeeExponent = "weight-breaking-fee-exponent"
FlagWeightRecoveryFeePortion = "weight-recovery-fee-portion"
FlagWeightBreakingFeePortion = "weight-breaking-fee-portion"
FlagThresholdWeightDifference = "threshold-weight-diff"
FlagFeeDenom = "fee-denom"
FlagSwapFee = "swap-fee"
FlagUseOracle = "use-oracle"
FlagFeeDenom = "fee-denom"
)

func CmdCreatePool() *cobra.Command {
Expand Down Expand Up @@ -69,56 +63,20 @@ func CmdCreatePool() *cobra.Command {
return err
}

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

useOracle, err := cmd.Flags().GetBool(FlagUseOracle)
if err != nil {
return err
}

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

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

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

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

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

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

poolParams := &types.PoolParams{
SwapFee: sdkmath.LegacyMustNewDecFromStr(swapFeeStr),
ExitFee: sdkmath.LegacyMustNewDecFromStr(exitFeeStr),
UseOracle: useOracle,
WeightBreakingFeeMultiplier: sdkmath.LegacyMustNewDecFromStr(weightBreakingFeeMultiplierStr),
WeightBreakingFeeExponent: sdkmath.LegacyMustNewDecFromStr(weightBreakingFeeExponentStr),
WeightRecoveryFeePortion: sdkmath.LegacyMustNewDecFromStr(weightRecoveryFeePortionStr),
WeightBreakingFeePortion: sdkmath.LegacyMustNewDecFromStr(weightBreakingFeePortionStr),
ThresholdWeightDifference: sdkmath.LegacyMustNewDecFromStr(thresholdWeightDifferenceStr),
FeeDenom: feeDenom,
poolParams := types.PoolParams{
SwapFee: sdkmath.LegacyMustNewDecFromStr(swapFeeStr),
UseOracle: useOracle,
FeeDenom: feeDenom,
}

msg := types.NewMsgCreatePool(
Expand All @@ -136,13 +94,7 @@ func CmdCreatePool() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)

cmd.Flags().String(FlagSwapFee, "0.00", "swap fee")
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(FlagWeightRecoveryFeePortion, "0.10", "weight recovery fee portion")
cmd.Flags().String(FlagWeightBreakingFeePortion, "0.10", "weight breaking fee portion")
cmd.Flags().String(FlagThresholdWeightDifference, "0.00", "threshold weight difference - valid for oracle pool")
cmd.Flags().String(FlagFeeDenom, "", "fee denom")

return cmd
Expand Down
43 changes: 4 additions & 39 deletions x/amm/client/cli/tx_update_pool_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,20 @@ func CmdUpdatePoolParams() *cobra.Command {
return err
}

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

useOracle, err := cmd.Flags().GetBool(FlagUseOracle)
if err != nil {
return err
}

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

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

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

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

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

poolParams := &types.PoolParams{
SwapFee: sdkmath.LegacyMustNewDecFromStr(swapFeeStr),
ExitFee: sdkmath.LegacyMustNewDecFromStr(exitFeeStr),
UseOracle: useOracle,
WeightBreakingFeeMultiplier: sdkmath.LegacyMustNewDecFromStr(weightBreakingFeeMultiplierStr),
WeightBreakingFeeExponent: sdkmath.LegacyMustNewDecFromStr(weightBreakingFeeExponentStr),
WeightRecoveryFeePortion: sdkmath.LegacyMustNewDecFromStr(weightRecoveryFeePortionStr),
ThresholdWeightDifference: sdkmath.LegacyMustNewDecFromStr(thresholdWeightDifferenceStr),
FeeDenom: feeDenom,
poolParams := types.PoolParams{
SwapFee: sdkmath.LegacyMustNewDecFromStr(swapFeeStr),
UseOracle: useOracle,
FeeDenom: feeDenom,
}

clientCtx, err := client.GetClientTxContext(cmd)
Expand All @@ -93,12 +63,7 @@ func CmdUpdatePoolParams() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)

cmd.Flags().String(FlagSwapFee, "0.00", "swap fee")
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(FlagWeightBreakingFeeExponent, "0.00", "weight breaking fee exponent")
cmd.Flags().String(FlagWeightBreakingFeeMultiplier, "0.00", "weight breaking fee multiplier")
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")

return cmd
Expand Down
22 changes: 6 additions & 16 deletions x/amm/keeper/apply_exit_pool_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,22 @@ import (
"github.com/elys-network/elys/x/amm/types"
)

func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exiter sdk.AccAddress, numShares sdkmath.Int, exitCoins sdk.Coins, isLiquidation bool) (sdk.Coins, error) {
func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exiter sdk.AccAddress, numShares sdkmath.Int, exitCoins sdk.Coins, isLiquidation bool) error {
// Withdraw exit amount of token from commitment module to exiter's wallet.
poolShareDenom := types.GetPoolShareDenom(pool.GetPoolId())

// Withdraw committed LP tokens
err := k.commitmentKeeper.UncommitTokens(ctx, exiter, poolShareDenom, numShares, isLiquidation)
if err != nil {
return sdk.Coins{}, err
return err
}

if err = k.bankKeeper.SendCoins(ctx, sdk.MustAccAddressFromBech32(pool.GetAddress()), exiter, exitCoins); err != nil {
return sdk.Coins{}, err
}

exitFeeCoins := PortionCoins(exitCoins, pool.PoolParams.ExitFee)
rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
if err = k.bankKeeper.SendCoins(ctx, exiter, rebalanceTreasury, exitFeeCoins); err != nil {
return sdk.Coins{}, err
}

if err = k.OnCollectFee(ctx, pool, exitFeeCoins); err != nil {
return sdk.Coins{}, err
return err
}

if err = k.BurnPoolShareFromAccount(ctx, pool, exiter, numShares); err != nil {
return sdk.Coins{}, err
return err
}

k.SetPool(ctx, pool)
Expand All @@ -40,8 +30,8 @@ func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exite
if k.hooks != nil {
err = k.hooks.AfterExitPool(ctx, exiter, pool, numShares, exitCoins)
if err != nil {
return sdk.Coins{}, err
return err
}
}
return exitCoins.Sub(exitFeeCoins...), nil
return nil
}
Loading

0 comments on commit e59c9f2

Please sign in to comment.