Skip to content

Commit 486109c

Browse files
authored
Add bonus amount field (#1222)
* add field * add field * fix * fix
1 parent 7c83d9c commit 486109c

File tree

8 files changed

+1118
-660
lines changed

8 files changed

+1118
-660
lines changed

api/elys/amm/query.pulsar.go

Lines changed: 762 additions & 479 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/elys/amm/query.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ service Query {
4848
// Queries a list of SwapEstimation items, uses swap exact amount out route.
4949
rpc SwapEstimationExactAmountOut(QuerySwapEstimationExactAmountOutRequest)
5050
returns (QuerySwapEstimationExactAmountOutResponse) {
51-
option (google.api.http).get = "/elys-network/elys/amm/swap_estimation_exact_amount_out";
51+
option (google.api.http).get =
52+
"/elys-network/elys/amm/swap_estimation_exact_amount_out";
5253
}
5354
// Queries JoinPool estimation
5455
rpc JoinPoolEstimation(QueryJoinPoolEstimationRequest)
@@ -203,6 +204,8 @@ message QueryJoinPoolEstimationResponse {
203204
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
204205
(gogoproto.nullable) = false
205206
];
207+
cosmos.base.v1beta1.Coin weight_balance_reward_amount = 7
208+
[ (gogoproto.nullable) = false ];
206209
}
207210

208211
message QueryExitPoolEstimationRequest {
@@ -238,6 +241,8 @@ message QueryExitPoolEstimationResponse {
238241
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
239242
(gogoproto.nullable) = false
240243
];
244+
cosmos.base.v1beta1.Coin weight_balance_reward_amount = 6
245+
[ (gogoproto.nullable) = false ];
241246
}
242247

243248
message QuerySwapEstimationResponse {
@@ -382,6 +387,8 @@ message QuerySwapEstimationByDenomResponse {
382387
(gogoproto.customtype) = "cosmossdk.io/math.LegacyDec",
383388
(gogoproto.nullable) = false
384389
];
390+
cosmos.base.v1beta1.Coin weight_balance_reward_amount = 11
391+
[ (gogoproto.nullable) = false ];
385392
}
386393

387394
message QueryAMMPriceRequest {

x/amm/keeper/apply_join_pool_state_change.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ func (k Keeper) ApplyJoinPoolStateChange(
103103
bonusTokenAmount := joinCoins[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
104104

105105
if treasuryTokenAmount.LT(bonusTokenAmount) {
106-
weightBalanceBonus = treasuryTokenAmount.ToLegacyDec().Quo(joinCoins[0].Amount.ToLegacyDec())
106+
bonusTokenAmount = treasuryTokenAmount
107107
}
108-
109-
weightBalanceBonusCoins = sdk.Coins{sdk.NewCoin(otherAsset.Token.Denom, weightBalanceBonus.TruncateInt())}
108+
weightBalanceBonusCoins = sdk.Coins{sdk.NewCoin(otherAsset.Token.Denom, bonusTokenAmount)}
110109

111110
// send bonus tokens to recipient if positive
112111
if weightBalanceBonusCoins.IsAllPositive() {

x/amm/keeper/query_join_pool_estimation.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,40 @@ func (k Keeper) JoinPoolEstimation(goCtx context.Context, req *types.QueryJoinPo
1616
}
1717

1818
ctx := sdk.UnwrapSDKContext(goCtx)
19-
tokensIn, sharesOut, slippage, weightBalanceBonus, swapFee, takerFees, err := k.JoinPoolEst(ctx, req.PoolId, req.AmountsIn)
19+
tokensIn, sharesOut, slippage, weightBalanceBonus, swapFee, takerFees, weightRewardAmount, err := k.JoinPoolEst(ctx, req.PoolId, req.AmountsIn)
2020
if err != nil {
2121
return nil, err
2222
}
2323

2424
shareDenom := types.GetPoolShareDenom(req.PoolId)
2525
return &types.QueryJoinPoolEstimationResponse{
26-
ShareAmountOut: sdk.NewCoin(shareDenom, sharesOut),
27-
AmountsIn: tokensIn,
28-
Slippage: slippage,
29-
WeightBalanceRatio: weightBalanceBonus,
30-
SwapFee: swapFee,
31-
TakerFee: takerFees,
26+
ShareAmountOut: sdk.NewCoin(shareDenom, sharesOut),
27+
AmountsIn: tokensIn,
28+
Slippage: slippage,
29+
WeightBalanceRatio: weightBalanceBonus,
30+
SwapFee: swapFee,
31+
TakerFee: takerFees,
32+
WeightBalanceRewardAmount: weightRewardAmount,
3233
}, nil
3334
}
3435

3536
func (k Keeper) JoinPoolEst(
3637
ctx sdk.Context,
3738
poolId uint64,
3839
tokenInMaxs sdk.Coins,
39-
) (tokensIn sdk.Coins, sharesOut math.Int, slippage math.LegacyDec, weightBalanceBonus math.LegacyDec, swapFee math.LegacyDec, takerFeesFinal math.LegacyDec, err error) {
40+
) (tokensIn sdk.Coins, sharesOut math.Int, slippage math.LegacyDec, weightBalanceBonus math.LegacyDec, swapFee math.LegacyDec, takerFeesFinal math.LegacyDec, weightRewardAmount sdk.Coin, err error) {
4041
// all pools handled within this method are pointer references, `JoinPool` directly updates the pools
4142
pool, poolExists := k.GetPool(ctx, poolId)
4243
if !poolExists {
43-
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), types.ErrInvalidPoolId
44+
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, types.ErrInvalidPoolId
4445
}
4546

4647
if !pool.PoolParams.UseOracle {
4748
tokensIn := tokenInMaxs
4849
if len(tokensIn) != 1 {
4950
numShares, tokensIn, err := pool.CalcJoinPoolNoSwapShares(tokenInMaxs)
5051
if err != nil {
51-
return tokensIn, numShares, math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), err
52+
return tokensIn, numShares, math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, err
5253
}
5354
}
5455

@@ -58,10 +59,10 @@ func (k Keeper) JoinPoolEst(
5859
cacheCtx, _ := ctx.CacheContext()
5960
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(cacheCtx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
6061
if err != nil {
61-
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), err
62+
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, err
6263
}
6364

64-
return tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, nil
65+
return tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, sdk.Coin{}, nil
6566
}
6667

6768
params := k.GetParams(ctx)
@@ -71,10 +72,11 @@ func (k Keeper) JoinPoolEst(
7172
cacheCtx, _ := ctx.CacheContext()
7273
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, _, err := pool.JoinPool(cacheCtx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
7374
if err != nil {
74-
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), err
75+
return nil, math.ZeroInt(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), sdk.Coin{}, err
7576
}
7677

7778
var otherAsset types.PoolAsset
79+
bonusTokenAmount := math.ZeroInt()
7880
// Check treasury and update weightBalance
7981
if weightBalanceBonus.IsPositive() && tokensJoined.Len() == 1 {
8082
rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
@@ -85,12 +87,16 @@ func (k Keeper) JoinPoolEst(
8587
otherAsset = asset
8688
}
8789
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, otherAsset.Token.Denom).Amount
88-
89-
bonusTokenAmount := tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
90+
bonusTokenAmount = tokensJoined[0].Amount.ToLegacyDec().Mul(weightBalanceBonus).TruncateInt()
9091

9192
if treasuryTokenAmount.LT(bonusTokenAmount) {
92-
weightBalanceBonus = treasuryTokenAmount.ToLegacyDec().Quo(tokensJoined[0].Amount.ToLegacyDec())
93+
bonusTokenAmount = treasuryTokenAmount
9394
}
9495
}
95-
return tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, nil
96+
rewards := sdk.Coin{}
97+
if otherAsset.Token.Denom != "" && bonusTokenAmount.IsPositive() {
98+
rewards = sdk.NewCoin(otherAsset.Token.Denom, bonusTokenAmount)
99+
}
100+
101+
return tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, rewards, nil
96102
}

x/amm/keeper/query_swap_estimation_by_denom.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ func (k Keeper) SwapEstimationByDenom(goCtx context.Context, req *types.QuerySwa
4141
amount.Amount = amount.Amount.Add(((amount.Amount.ToLegacyDec()).Mul(weightBonus)).TruncateInt())
4242

4343
return &types.QuerySwapEstimationByDenomResponse{
44-
InRoute: inRoute,
45-
OutRoute: outRoute,
46-
Amount: amount,
47-
SpotPrice: spotPrice,
48-
SwapFee: swapFee,
49-
Discount: discount,
50-
AvailableLiquidity: availableLiquidity,
51-
Slippage: slippage,
52-
WeightBalanceRatio: weightBonus,
53-
PriceImpact: priceImpact,
44+
InRoute: inRoute,
45+
OutRoute: outRoute,
46+
Amount: amount,
47+
SpotPrice: spotPrice,
48+
SwapFee: swapFee,
49+
Discount: discount,
50+
AvailableLiquidity: availableLiquidity,
51+
Slippage: slippage,
52+
WeightBalanceRatio: weightBonus,
53+
PriceImpact: priceImpact,
54+
WeightBalanceRewardAmount: sdk.NewCoin(amount.Denom, ((amount.Amount.ToLegacyDec()).Mul(weightBonus)).TruncateInt()),
5455
}, nil
5556
}

0 commit comments

Comments
 (0)