Skip to content

Commit

Permalink
Merge branch 'main' into track-eden
Browse files Browse the repository at this point in the history
  • Loading branch information
amityadav0 authored Jan 23, 2025
2 parents 846f0cb + b3f318b commit 110f9c9
Show file tree
Hide file tree
Showing 73 changed files with 1,485 additions and 1,104 deletions.
612 changes: 345 additions & 267 deletions api/elys/amm/tx.pulsar.go

Large diffs are not rendered by default.

666 changes: 370 additions & 296 deletions api/elys/leveragelp/tx.pulsar.go

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions api/elys/leveragelp/tx_grpc.pb.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/amm/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ message MsgExitPool {
message MsgExitPoolResponse {
repeated cosmos.base.v1beta1.Coin token_out = 1
[ (gogoproto.nullable) = false ];
string weight_balance_ratio = 2 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

message MsgSwapExactAmountIn {
Expand Down
14 changes: 9 additions & 5 deletions proto/elys/leveragelp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ service Msg {
rpc Dewhitelist(MsgDewhitelist) returns (MsgDewhitelistResponse);
rpc UpdateStopLoss(MsgUpdateStopLoss) returns (MsgUpdateStopLossResponse);
rpc ClosePositions(MsgClosePositions) returns (MsgClosePositionsResponse);
rpc UpdateMaxLeverageForPool(MsgUpdateMaxLeverageForPool)
returns (MsgUpdateMaxLeverageForPoolResponse);
rpc UpdatePool(MsgUpdatePool) returns (MsgUpdatePoolResponse);
rpc UpdateEnabledPools(MsgUpdateEnabledPools)
returns (MsgUpdateEnabledPoolsResponse);
}
Expand Down Expand Up @@ -161,9 +160,9 @@ message MsgClosePositions {

message MsgClosePositionsResponse {}

message MsgUpdateMaxLeverageForPool {
message MsgUpdatePool {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "leveragelp/MsgUpdateMaxLeverageForPool";
option (amino.name) = "leveragelp/MsgUpdatePool";
// authority is the address that controls the module (defaults to x/gov unless
// overwritten).
string authority = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
Expand All @@ -174,9 +173,14 @@ message MsgUpdateMaxLeverageForPool {
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
string max_leveragelp_ratio = 4 [
(cosmos_proto.scalar) = "cosmos.Dec",
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
(gogoproto.nullable) = false
];
}

message MsgUpdateMaxLeverageForPoolResponse {}
message MsgUpdatePoolResponse {}
message MsgUpdateEnabledPools {
option (cosmos.msg.v1.signer) = "authority";
option (amino.name) = "leveragelp/MsgUpdateEnabledPools";
Expand Down
1 change: 0 additions & 1 deletion x/accountedpool/types/query.pb.go

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

1 change: 0 additions & 1 deletion x/accountedpool/types/tx.pb.go

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

25 changes: 24 additions & 1 deletion x/amm/keeper/apply_exit_pool_state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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) error {
func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exiter sdk.AccAddress, numShares sdkmath.Int, exitCoins sdk.Coins, isLiquidation bool, weightBalanceBonus sdkmath.LegacyDec) error {
// Withdraw exit amount of token from commitment module to exiter's wallet.
poolShareDenom := types.GetPoolShareDenom(pool.GetPoolId())

Expand All @@ -26,6 +26,29 @@ func (k Keeper) ApplyExitPoolStateChange(ctx sdk.Context, pool types.Pool, exite

k.SetPool(ctx, pool)

rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())

if weightBalanceBonus.IsPositive() {
// calculate treasury amounts to send as bonus
weightBalanceBonusCoins := PortionCoins(exitCoins, weightBalanceBonus)
for _, coin := range weightBalanceBonusCoins {
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, coin.Denom).Amount
if treasuryTokenAmount.LT(coin.Amount) {
// override coin amount by treasuryTokenAmount
weightBalanceBonusCoins = weightBalanceBonusCoins.
Sub(coin). // remove the original coin
Add(sdk.NewCoin(coin.Denom, treasuryTokenAmount)) // add the treasuryTokenAmount
}
}

// send bonus tokens to recipient if positive
if weightBalanceBonusCoins.IsAllPositive() {
if err := k.bankKeeper.SendCoins(ctx, rebalanceTreasuryAddr, exiter, weightBalanceBonusCoins); err != nil {
return err
}
}
}

types.EmitRemoveLiquidityEvent(ctx, exiter, pool.GetPoolId(), exitCoins)
if k.hooks != nil {
err = k.hooks.AfterExitPool(ctx, exiter, pool, numShares, exitCoins)
Expand Down
6 changes: 3 additions & 3 deletions x/amm/keeper/apply_exit_pool_state_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt()))

ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false)
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec())
suite.Require().NoError(err)
},
func() {},
Expand All @@ -99,7 +99,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {

coins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin("uusdt", sdkmath.NewInt(100000)))

err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false)
err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec())
suite.Require().Error(err)
},
func() {},
Expand Down Expand Up @@ -170,7 +170,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
coins = sdk.NewCoins(sdk.NewCoin("invalid_denom", sdkmath.NewInt(100000000)))

ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false)
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec())
suite.Require().Error(err)
},
func() {},
Expand Down
22 changes: 11 additions & 11 deletions x/amm/keeper/keeper_exit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ func (k Keeper) ExitPool(
tokenOutMins sdk.Coins,
tokenOutDenom string,
isLiquidation bool,
) (exitCoins sdk.Coins, err error) {
) (exitCoins sdk.Coins, weightBalanceBonus math.LegacyDec, err error) {
pool, poolExists := k.GetPool(ctx, poolId)
if !poolExists {
return sdk.Coins{}, types.ErrInvalidPoolId
return sdk.Coins{}, math.LegacyZeroDec(), types.ErrInvalidPoolId
}

totalSharesAmount := pool.GetTotalShares()
if shareInAmount.GTE(totalSharesAmount.Amount) {
return sdk.Coins{}, errorsmod.Wrapf(types.ErrInvalidMathApprox, "Trying to exit >= the number of shares contained in the pool.")
return sdk.Coins{}, math.LegacyZeroDec(), errorsmod.Wrapf(types.ErrInvalidMathApprox, "Trying to exit >= the number of shares contained in the pool.")
} else if shareInAmount.LTE(math.ZeroInt()) {
return sdk.Coins{}, errorsmod.Wrapf(types.ErrInvalidMathApprox, "Trying to exit a negative amount of shares")
return sdk.Coins{}, math.LegacyZeroDec(), errorsmod.Wrapf(types.ErrInvalidMathApprox, "Trying to exit a negative amount of shares")
}
params := k.GetParams(ctx)
exitCoins, err = pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, shareInAmount, tokenOutDenom, params)
exitCoins, weightBalanceBonus, err = pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, shareInAmount, tokenOutDenom, params)
if err != nil {
return sdk.Coins{}, err
return sdk.Coins{}, math.LegacyZeroDec(), err
}
if !tokenOutMins.DenomsSubsetOf(exitCoins) || tokenOutMins.IsAnyGT(exitCoins) {
return sdk.Coins{}, errorsmod.Wrapf(types.ErrLimitMinAmount,
return sdk.Coins{}, math.LegacyZeroDec(), errorsmod.Wrapf(types.ErrLimitMinAmount,
"Exit pool returned %s , minimum tokens out specified as %s",
exitCoins, tokenOutMins)
}

err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation)
err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation, weightBalanceBonus)
if err != nil {
return sdk.Coins{}, err
return sdk.Coins{}, math.LegacyZeroDec(), err
}

err = k.RecordTotalLiquidityDecrease(ctx, exitCoins)
if err != nil {
return sdk.Coins{}, err
return sdk.Coins{}, math.LegacyZeroDec(), err
}

return exitCoins, nil
return exitCoins, weightBalanceBonus, nil
}
2 changes: 1 addition & 1 deletion x/amm/keeper/keeper_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (suite *AmmKeeperTestSuite) TestExitPool() {
for _, tc := range testCases {
suite.Run(tc.name, func() {
exiter, poolId, inShares, minTokensOut, tokenOutDenom, isLiq := tc.setup()
_, err := suite.app.AmmKeeper.ExitPool(suite.ctx, exiter, poolId, inShares, minTokensOut, tokenOutDenom, isLiq)
_, _, err := suite.app.AmmKeeper.ExitPool(suite.ctx, exiter, poolId, inShares, minTokensOut, tokenOutDenom, isLiq)
if tc.expectedErrMsg != "" {
suite.Require().Error(err)
suite.Require().Contains(err.Error(), tc.expectedErrMsg)
Expand Down
8 changes: 4 additions & 4 deletions x/amm/keeper/keeper_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ func (suite *AmmKeeperTestSuite) TestSwapExactAmountIn() {
swapFeeOut: sdkmath.LegacyZeroDec(),
tokenIn: sdk.NewInt64Coin("uusda", 10000),
tokenOutMin: sdkmath.ZeroInt(),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9944),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9950),
weightBalanceBonus: sdkmath.LegacyZeroDec(),
isOraclePool: true,
useNewRecipient: false,
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 990000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1009944)},
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 990000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1009950)},
expRecipientBalance: sdk.Coins{},
expPoolBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1009997), sdk.NewInt64Coin(ptypes.BaseCurrency, 990056)},
expTreasuryBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000003), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
expPoolBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1010000), sdk.NewInt64Coin(ptypes.BaseCurrency, 990050)},
expTreasuryBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
expPass: true,
errMsg: "",
},
Expand Down
8 changes: 4 additions & 4 deletions x/amm/keeper/keeper_swap_exact_amount_out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,16 @@ func (suite *AmmKeeperTestSuite) TestSwapExactAmountOut() {
poolInitBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
treasuryInitBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
swapFeeOut: sdkmath.LegacyZeroDec(),
tokenIn: sdk.NewInt64Coin("uusda", 222546),
tokenIn: sdk.NewInt64Coin("uusda", 222224),
tokenInMax: sdkmath.NewInt(10000000),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 200000),
weightBalanceBonus: sdkmath.LegacyZeroDec(),
isOraclePool: true,
useNewRecipient: false,
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 777454), sdk.NewInt64Coin(ptypes.BaseCurrency, 1200000)},
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 777776), sdk.NewInt64Coin(ptypes.BaseCurrency, 1200000)},
expRecipientBalance: sdk.Coins{},
expPoolBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1222402), sdk.NewInt64Coin(ptypes.BaseCurrency, 800000)},
expTreasuryBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000144), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
expPoolBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1222224), sdk.NewInt64Coin(ptypes.BaseCurrency, 800000)},
expTreasuryBalance: sdk.Coins{sdk.NewInt64Coin("uusda", 1000000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1000000)},
expPass: true,
},
{
Expand Down
5 changes: 3 additions & 2 deletions x/amm/keeper/msg_server_exit_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (k msgServer) ExitPool(goCtx context.Context, msg *types.MsgExitPool) (*typ
if err != nil {
return nil, err
}
exitCoins, err := k.Keeper.ExitPool(ctx, sender, msg.PoolId, msg.ShareAmountIn, msg.MinAmountsOut, msg.TokenOutDenom, false)
exitCoins, weightBalanceBonus, err := k.Keeper.ExitPool(ctx, sender, msg.PoolId, msg.ShareAmountIn, msg.MinAmountsOut, msg.TokenOutDenom, false)
if err != nil {
return nil, err
}
Expand All @@ -28,6 +28,7 @@ func (k msgServer) ExitPool(goCtx context.Context, msg *types.MsgExitPool) (*typ
})

return &types.MsgExitPoolResponse{
TokenOut: exitCoins,
TokenOut: exitCoins,
WeightBalanceRatio: weightBalanceBonus,
}, nil
}
8 changes: 4 additions & 4 deletions x/amm/keeper/msg_server_exit_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func (suite *AmmKeeperTestSuite) TestMsgServerExitPool() {
},
shareInAmount: types.OneShare.Quo(sdkmath.NewInt(10)),
tokenOutDenom: "uusdt",
minAmountsOut: sdk.Coins{sdk.NewInt64Coin("uusdt", 99935)},
minAmountsOut: sdk.Coins{sdk.NewInt64Coin("uusdt", 100000)},
// expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 95114)}, // slippage enabled
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 99935)}, // slippage disabled
expSenderBalance: sdk.Coins{sdk.NewInt64Coin("uusdt", 100000)}, // slippage disabled
expPass: true,
},
{
Expand All @@ -79,9 +79,9 @@ func (suite *AmmKeeperTestSuite) TestMsgServerExitPool() {
},
shareInAmount: types.OneShare.Quo(sdkmath.NewInt(10)),
tokenOutDenom: ptypes.BaseCurrency,
minAmountsOut: sdk.Coins{sdk.NewInt64Coin(ptypes.BaseCurrency, 99221)},
minAmountsOut: sdk.Coins{sdk.NewInt64Coin(ptypes.BaseCurrency, 100000)},
// expSenderBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.BaseCurrency, 99197)}, // slippage enabled
expSenderBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.BaseCurrency, 99221)}, // slippage disabled
expSenderBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.BaseCurrency, 100000)}, // slippage disabled
expPass: true,
},
} {
Expand Down
4 changes: 2 additions & 2 deletions x/amm/keeper/msg_server_join_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerJoinPool() {
FeeDenom: ptypes.BaseCurrency,
},
// shareOutAmount: math.NewInt(694444166666666666), // weight breaking fee - slippage enable
shareOutAmount: math.NewInt(999057190958417937), // weight breaking fee - slippage disable
shareOutAmount: math.NewInt(1000000000000000000), // weight breaking fee - slippage disable
expSenderBalance: sdk.Coins{},
expTokenIn: sdk.Coins{sdk.NewInt64Coin("uusdt", 1000000)},
expPass: true,
Expand All @@ -75,7 +75,7 @@ func (suite *AmmKeeperTestSuite) TestMsgServerJoinPool() {
FeeDenom: ptypes.BaseCurrency,
},
// shareOutAmount: math.NewInt(805987500000000000), // weight recovery direction - slippage enable
shareOutAmount: math.NewInt(996102885682970026), // weight recovery direction - slippage disable
shareOutAmount: math.NewInt(1000000000000000000), // weight recovery direction - slippage disable
expSenderBalance: sdk.Coins{},
expTokenIn: sdk.Coins{sdk.NewInt64Coin("uusdt", 1000000)},
expPass: true,
Expand Down
Loading

0 comments on commit 110f9c9

Please sign in to comment.