Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fee collection in amm #1121

Merged
merged 3 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions x/amm/keeper/apply_exit_pool_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@
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)

for _, coin := range exitCoins {
weightRecoveryFeeAmount = coin.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(coin.Denom, weightRecoveryFeeAmount)}

err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins)
if err != nil {
return err
}

Check warning on line 49 in x/amm/keeper/apply_exit_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_exit_pool_state_change.go#L34-L49

Added lines #L34 - L49 were not covered by tests

err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins)
if err != nil {
return err
}

Check warning on line 54 in x/amm/keeper/apply_exit_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_exit_pool_state_change.go#L51-L54

Added lines #L51 - L54 were not covered by tests

// 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)))))

Check warning on line 58 in x/amm/keeper/apply_exit_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_exit_pool_state_change.go#L57-L58

Added lines #L57 - L58 were not covered by tests
}
}
}

if weightBalanceBonus.IsPositive() {
// calculate treasury amounts to send as bonus
Expand Down
33 changes: 33 additions & 0 deletions x/amm/keeper/apply_join_pool_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"cosmossdk.io/math"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/amm/types"
)
Expand All @@ -26,6 +27,38 @@

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)
for _, coin := range joinCoins {
weightRecoveryFeeAmount = coin.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(coin.Denom, weightRecoveryFeeAmount)}

err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins)
if err != nil {
return err
}

Check warning on line 48 in x/amm/keeper/apply_join_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_join_pool_state_change.go#L34-L48

Added lines #L34 - L48 were not covered by tests

err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), netWeightBreakingFeeCoins)
if err != nil {
return err
}

Check warning on line 53 in x/amm/keeper/apply_join_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_join_pool_state_change.go#L50-L53

Added lines #L50 - L53 were not covered by tests

// 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)))))

Check warning on line 57 in x/amm/keeper/apply_join_pool_state_change.go

View check run for this annotation

Codecov / codecov/patch

x/amm/keeper/apply_join_pool_state_change.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}
}
}

if weightBalanceBonus.IsPositive() {
// calculate treasury amounts to send as bonus
weightBalanceBonusCoins := PortionCoins(joinCoins, weightBalanceBonus)
Expand Down
Loading