From 8b61c909e7525f0426159ae8649aa30fed279076 Mon Sep 17 00:00:00 2001 From: Amit Date: Thu, 23 Jan 2025 19:40:31 +0530 Subject: [PATCH 1/2] pay to tresury for exit pool --- x/amm/keeper/apply_exit_pool_state_change.go | 34 ++++++++++++++++ x/amm/keeper/apply_join_pool_state_change.go | 42 ++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/x/amm/keeper/apply_exit_pool_state_change.go b/x/amm/keeper/apply_exit_pool_state_change.go index f1a24bd4d..39c6cb126 100644 --- a/x/amm/keeper/apply_exit_pool_state_change.go +++ b/x/amm/keeper/apply_exit_pool_state_change.go @@ -27,6 +27,40 @@ func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exite k.SetPool(ctx, pool) rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + weightRecoveryFeeAmount := sdkmath.ZeroInt() + poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress()) + // send half (weight breaking fee portion) of weight breaking fee to rebalance treasury + if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() { + params := k.GetParams(ctx) + rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee + weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) + + var weightRecoveryFeeAmounts sdk.Coins + for _, coin := range exitCoins { + weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() + weightRecoveryFeeAmounts = append(weightRecoveryFeeAmounts, sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)) + + if weightRecoveryFeeAmount.IsPositive() { + // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ + netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)} + + err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) + if err != nil { + return err + } + + err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) + if err != nil { + return err + } + + // Track amount in pool + weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) + k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(sdkmath.LegacyDec(weightRecoveryFeeAmount))))) + } + } + } if weightBalanceBonus.IsPositive() { // calculate treasury amounts to send as bonus diff --git a/x/amm/keeper/apply_join_pool_state_change.go b/x/amm/keeper/apply_join_pool_state_change.go index 9fa8e132f..53bee4558 100644 --- a/x/amm/keeper/apply_join_pool_state_change.go +++ b/x/amm/keeper/apply_join_pool_state_change.go @@ -2,6 +2,7 @@ package keeper import ( "cosmossdk.io/math" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/elys-network/elys/x/amm/types" ) @@ -26,6 +27,47 @@ func (k Keeper) ApplyJoinPoolStateChange( rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + weightRecoveryFeeAmount := sdkmath.ZeroInt() + poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress()) + // send half (weight breaking fee portion) of weight breaking fee to rebalance treasury + if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() { + params := k.GetParams(ctx) + rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) + // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee + weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) + + if givenOut { + weightRecoveryFeeAmount = oracleInAmount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() + } else { + weightRecoveryFeeAmount = tokenIn.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() + } + + if weightRecoveryFeeAmount.IsPositive() { + // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ + netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(tokenIn.Denom, weightRecoveryFeeAmount)} + + err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) + if err != nil { + return err + } + + err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) + if err != nil { + return err + } + + // Track amount in pool + weightRecoveryFeeAmountForPool := sdkmath.ZeroInt() + weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) + if givenOut { + weightRecoveryFeeAmountForPool = oracleInAmount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt() + } else { + weightRecoveryFeeAmountForPool = tokenIn.Amount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt() + } + k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenIn.Denom, weightRecoveryFeeAmountForPool)) + } + } + if weightBalanceBonus.IsPositive() { // calculate treasury amounts to send as bonus weightBalanceBonusCoins := PortionCoins(joinCoins, weightBalanceBonus) From 4bbb331a2878624fcad77c3cdf353cd0f6a4d263 Mon Sep 17 00:00:00 2001 From: Amit Date: Fri, 24 Jan 2025 11:35:57 +0530 Subject: [PATCH 2/2] collect in join and exit --- x/amm/keeper/apply_exit_pool_state_change.go | 2 - x/amm/keeper/apply_join_pool_state_change.go | 41 ++++++++------------ 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/x/amm/keeper/apply_exit_pool_state_change.go b/x/amm/keeper/apply_exit_pool_state_change.go index 39c6cb126..54d7dbcb3 100644 --- a/x/amm/keeper/apply_exit_pool_state_change.go +++ b/x/amm/keeper/apply_exit_pool_state_change.go @@ -36,10 +36,8 @@ func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exite // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) - var weightRecoveryFeeAmounts sdk.Coins for _, coin := range exitCoins { weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() - weightRecoveryFeeAmounts = append(weightRecoveryFeeAmounts, sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)) if weightRecoveryFeeAmount.IsPositive() { // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ diff --git a/x/amm/keeper/apply_join_pool_state_change.go b/x/amm/keeper/apply_join_pool_state_change.go index 53bee4558..06d65fe2f 100644 --- a/x/amm/keeper/apply_join_pool_state_change.go +++ b/x/amm/keeper/apply_join_pool_state_change.go @@ -35,36 +35,27 @@ func (k Keeper) ApplyJoinPoolStateChange( rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury()) // we are multiplying here by params.WeightBreakingFeePortion as we didn't multiply in pool.Join/Exit for weight breaking fee weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion) + for _, coin := range joinCoins { + weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() - if givenOut { - weightRecoveryFeeAmount = oracleInAmount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() - } else { - weightRecoveryFeeAmount = tokenIn.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt() - } - - if weightRecoveryFeeAmount.IsPositive() { - // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ - netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(tokenIn.Denom, weightRecoveryFeeAmount)} + if weightRecoveryFeeAmount.IsPositive() { + // send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬ + netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)} - err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) - if err != nil { - return err - } + err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins) + if err != nil { + return err + } - err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) - if err != nil { - return err - } + err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins) + if err != nil { + return err + } - // Track amount in pool - weightRecoveryFeeAmountForPool := sdkmath.ZeroInt() - weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) - if givenOut { - weightRecoveryFeeAmountForPool = oracleInAmount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt() - } else { - weightRecoveryFeeAmountForPool = tokenIn.Amount.ToLegacyDec().Mul(weightRecoveryFeeForPool).RoundInt() + // Track amount in pool + weightRecoveryFeeForPool := weightBalanceBonus.Abs().Mul(sdkmath.LegacyOneDec().Sub(params.WeightBreakingFeePortion)) + k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(coin.Denom, sdkmath.Int(weightRecoveryFeeForPool.Mul(sdkmath.LegacyDec(weightRecoveryFeeAmount))))) } - k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenIn.Denom, weightRecoveryFeeAmountForPool)) } }