Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/avkr003/exitLeverageLp' into avk…
Browse files Browse the repository at this point in the history
…r003/exitLeverageLp
  • Loading branch information
avkr003 committed Jan 24, 2025
2 parents 6a5ff22 + 33636b4 commit 50aa849
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 50 deletions.
108 changes: 93 additions & 15 deletions api/elys/stablestake/params.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions proto/elys/stablestake/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ message Params {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string max_withdraw_ratio = 12 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}
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 @@ 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)

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
}

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
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 @@ 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"
)
Expand All @@ -26,6 +27,38 @@ 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)
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
}

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
weightBalanceBonusCoins := PortionCoins(joinCoins, weightBalanceBonus)
Expand Down
2 changes: 1 addition & 1 deletion x/stablestake/keeper/msg_server_unbond.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (k msgServer) Unbond(goCtx context.Context, msg *types.MsgUnbond) (*types.M
redemptionAmount := shareCoin.Amount.ToLegacyDec().Mul(redemptionRate).RoundInt()

amountAfterRedemption := params.TotalValue.Sub(redemptionAmount)
maxAllowed := (params.TotalValue.ToLegacyDec().Mul(params.MaxLeverageRatio)).TruncateInt()
maxAllowed := (params.TotalValue.ToLegacyDec().Mul(params.MaxWithdrawRatio)).TruncateInt()
if amountAfterRedemption.LT(maxAllowed) {
return nil, types.ErrInvalidWithdraw
}
Expand Down
13 changes: 13 additions & 0 deletions x/stablestake/migrations/v8_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package migrations

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (m Migrator) V8Migration(ctx sdk.Context) error {
params := m.keeper.GetParams(ctx)
params.MaxWithdrawRatio = math.LegacyMustNewDecFromStr("0.9")
m.keeper.SetParams(ctx, params)
return nil
}
4 changes: 2 additions & 2 deletions x/stablestake/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 6, m.V7Migration)
err := cfg.RegisterMigration(types.ModuleName, 7, m.V8Migration)
if err != nil {
panic(err)
}
Expand All @@ -147,7 +147,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 7 }
func (AppModule) ConsensusVersion() uint64 { return 8 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(goCtx context.Context) error {
Expand Down
2 changes: 2 additions & 0 deletions x/stablestake/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

"cosmossdk.io/math"
Expand All @@ -21,6 +22,7 @@ func DefaultParams() Params {
HealthGainFactor: math.LegacyOneDec(),
TotalValue: math.ZeroInt(),
MaxLeverageRatio: math.LegacyMustNewDecFromStr("0.7"),
MaxWithdrawRatio: math.LegacyMustNewDecFromStr("0.7"),
}
}

Expand Down
Loading

0 comments on commit 50aa849

Please sign in to comment.