Skip to content

Commit 23af03e

Browse files
99adarshamityadav0avkr003
authored
Add swap fees event (#1169)
* add swap fees event * update swap fees denom in event * cleanup * refactor and cleanup * update function name * update fn name * collect fees * fixes * fix * fix cli (#1172) * fix cli * add migration * fix * stablestake fixes (#1171) * stablestake fixes * add tests * Fix supply calculation (#1163) * fix supply migration * fix * Add fee events and fixes * fix swap_fee nil error --------- Co-authored-by: Amit <amy29981@gmail.com> Co-authored-by: Abhinav Kumar <57705190+avkr003@users.noreply.github.com>
1 parent 6735a6b commit 23af03e

18 files changed

+199
-65
lines changed

x/amm/keeper/apply_exit_pool_state_change.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func (k Keeper) ApplyExitPoolStateChange(
1111
exiter sdk.AccAddress, numShares sdkmath.Int,
1212
exitCoins sdk.Coins, isLiquidation bool,
1313
weightBalanceBonus sdkmath.LegacyDec, takerFees sdkmath.LegacyDec,
14+
swapFee sdkmath.LegacyDec, slippageCoins sdk.Coins,
1415
) error {
1516
// Withdraw exit amount of token from commitment module to exiter's wallet.
1617
poolShareDenom := types.GetPoolShareDenom(pool.GetPoolId())
@@ -32,8 +33,36 @@ func (k Keeper) ApplyExitPoolStateChange(
3233
k.SetPool(ctx, pool)
3334

3435
rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
35-
weightRecoveryFeeAmount := sdkmath.ZeroInt()
3636
poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress())
37+
38+
swapFeeInCoins := sdk.Coins{}
39+
// As swapfee will always be positive only if there is single asset in exitCoins
40+
if swapFee.IsPositive() {
41+
swapFeeAmount := (exitCoins[0].Amount.ToLegacyDec().Mul(swapFee)).Quo(sdkmath.LegacyOneDec().Sub(swapFee))
42+
swapFeeInCoins = sdk.Coins{sdk.NewCoin(exitCoins[0].Denom, swapFeeAmount.RoundInt())}
43+
}
44+
45+
// send swap fee to rebalance treasury
46+
if swapFeeInCoins.IsAllPositive() {
47+
rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
48+
err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, swapFeeInCoins)
49+
if err != nil {
50+
return err
51+
}
52+
53+
err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), swapFeeInCoins)
54+
if err != nil {
55+
return err
56+
}
57+
58+
err = k.OnCollectFee(ctx, pool, swapFeeInCoins)
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
64+
var weightRecoveryFeeAmount sdkmath.LegacyDec
65+
weightRecoveryFeeCoins := sdk.Coins{}
3766
// send half (weight breaking fee portion) of weight breaking fee to rebalance treasury
3867
if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() {
3968
params := k.GetParams(ctx)
@@ -42,11 +71,12 @@ func (k Keeper) ApplyExitPoolStateChange(
4271
weightRecoveryFee := weightBalanceBonus.Abs().Mul(params.WeightBreakingFeePortion)
4372

4473
for _, coin := range exitCoins {
45-
weightRecoveryFeeAmount = coin.Amount.ToLegacyDec().Mul(weightRecoveryFee).RoundInt()
74+
weightRecoveryFeeAmount = (coin.Amount.ToLegacyDec().Mul(weightRecoveryFee)).Quo(sdkmath.LegacyOneDec().Sub(weightRecoveryFee))
4675

4776
if weightRecoveryFeeAmount.IsPositive() {
4877
// send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬
49-
netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)}
78+
netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount.RoundInt())}
79+
weightRecoveryFeeCoins = weightRecoveryFeeCoins.Add(netWeightBreakingFeeCoins...)
5080

5181
err = k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins)
5282
if err != nil {
@@ -65,9 +95,10 @@ func (k Keeper) ApplyExitPoolStateChange(
6595
}
6696
}
6797

98+
weightBalanceBonusCoins := sdk.Coins{}
6899
if weightBalanceBonus.IsPositive() {
69100
// calculate treasury amounts to send as bonus
70-
weightBalanceBonusCoins := PortionCoins(exitCoins, weightBalanceBonus)
101+
weightBalanceBonusCoins = PortionCoins(exitCoins, weightBalanceBonus)
71102
for _, coin := range weightBalanceBonusCoins {
72103
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, coin.Denom).Amount
73104
if treasuryTokenAmount.LT(coin.Amount) {
@@ -109,6 +140,15 @@ func (k Keeper) ApplyExitPoolStateChange(
109140
}
110141
}
111142

143+
// convert the fees into USD
144+
swapFeeValueInUSD := k.CalculateCoinsUSDValue(ctx, swapFeeInCoins).String()
145+
slippageAmountInUSD := k.CalculateCoinsUSDValue(ctx, slippageCoins).String()
146+
weightRecoveryFeeAmountInUSD := k.CalculateCoinsUSDValue(ctx, weightRecoveryFeeCoins).String()
147+
bonusTokenAmountInUSD := k.CalculateCoinsUSDValue(ctx, weightBalanceBonusCoins).String()
148+
149+
// emit swap fees event
150+
types.EmitSwapFeesCollectedEvent(ctx, swapFeeValueInUSD, slippageAmountInUSD, weightRecoveryFeeAmountInUSD, bonusTokenAmountInUSD)
151+
112152
types.EmitRemoveLiquidityEvent(ctx, exiter, pool.GetPoolId(), exitCoins)
113153
if k.hooks != nil {
114154
err = k.hooks.AfterExitPool(ctx, exiter, pool, numShares, exitCoins)

x/amm/keeper/apply_exit_pool_state_change_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
8181
suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt()))
8282

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

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

102-
err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec())
102+
err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
103103
suite.Require().Error(err)
104104
},
105105
func() {},
@@ -170,7 +170,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
170170
coins = sdk.NewCoins(sdk.NewCoin("invalid_denom", sdkmath.NewInt(100000000)))
171171

172172
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
173-
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec())
173+
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
174174
suite.Require().Error(err)
175175
},
176176
func() {},

x/amm/keeper/apply_join_pool_state_change.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ func (k Keeper) ApplyJoinPoolStateChange(
1515
joinCoins sdk.Coins,
1616
weightBalanceBonus math.LegacyDec,
1717
takerFees math.LegacyDec,
18+
swapFee sdkmath.LegacyDec,
19+
slippageCoins sdk.Coins,
1820
) error {
1921
if err := k.bankKeeper.SendCoins(ctx, joiner, sdk.MustAccAddressFromBech32(pool.GetAddress()), joinCoins); err != nil {
2022
return err
@@ -27,9 +29,34 @@ func (k Keeper) ApplyJoinPoolStateChange(
2729
k.SetPool(ctx, pool)
2830

2931
rebalanceTreasuryAddr := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
32+
poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress())
33+
34+
swapFeeInCoins := sdk.Coins{}
35+
if swapFee.IsPositive() {
36+
swapFeeInCoins = PortionCoins(joinCoins, swapFee)
37+
}
38+
39+
// send swap fee to rebalance treasury
40+
if swapFeeInCoins.IsAllPositive() {
41+
rebalanceTreasury := sdk.MustAccAddressFromBech32(pool.GetRebalanceTreasury())
42+
err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, swapFeeInCoins)
43+
if err != nil {
44+
return err
45+
}
46+
47+
err = k.RemoveFromPoolBalanceAndUpdateLiquidity(ctx, &pool, sdkmath.ZeroInt(), swapFeeInCoins)
48+
if err != nil {
49+
return err
50+
}
51+
52+
err = k.OnCollectFee(ctx, pool, swapFeeInCoins)
53+
if err != nil {
54+
return err
55+
}
56+
}
3057

3158
weightRecoveryFeeAmount := sdkmath.ZeroInt()
32-
poolAddr := sdk.MustAccAddressFromBech32(pool.GetAddress())
59+
weightRecoveryFeeCoins := sdk.Coins{}
3360
// send half (weight breaking fee portion) of weight breaking fee to rebalance treasury
3461
if pool.PoolParams.UseOracle && weightBalanceBonus.IsNegative() {
3562
params := k.GetParams(ctx)
@@ -42,6 +69,7 @@ func (k Keeper) ApplyJoinPoolStateChange(
4269
if weightRecoveryFeeAmount.IsPositive() {
4370
// send weight recovery fee to rebalance treasury if weight recovery fee amount is positive¬
4471
netWeightBreakingFeeCoins := sdk.Coins{sdk.NewCoin(coin.Denom, weightRecoveryFeeAmount)}
72+
weightRecoveryFeeCoins = weightRecoveryFeeCoins.Add(netWeightBreakingFeeCoins...)
4573

4674
err := k.bankKeeper.SendCoins(ctx, poolAddr, rebalanceTreasury, netWeightBreakingFeeCoins)
4775
if err != nil {
@@ -60,9 +88,10 @@ func (k Keeper) ApplyJoinPoolStateChange(
6088
}
6189
}
6290

91+
weightBalanceBonusCoins := sdk.Coins{}
6392
if weightBalanceBonus.IsPositive() {
6493
// calculate treasury amounts to send as bonus
65-
weightBalanceBonusCoins := PortionCoins(joinCoins, weightBalanceBonus)
94+
weightBalanceBonusCoins = PortionCoins(joinCoins, weightBalanceBonus)
6695
for _, coin := range weightBalanceBonusCoins {
6796
treasuryTokenAmount := k.bankKeeper.GetBalance(ctx, rebalanceTreasuryAddr, coin.Denom).Amount
6897
if treasuryTokenAmount.LT(coin.Amount) {
@@ -104,6 +133,15 @@ func (k Keeper) ApplyJoinPoolStateChange(
104133
}
105134
}
106135

136+
// convert the fees into USD
137+
swapFeeValueInUSD := k.CalculateCoinsUSDValue(ctx, swapFeeInCoins).String()
138+
slippageAmountInUSD := k.CalculateCoinsUSDValue(ctx, slippageCoins).String()
139+
weightRecoveryFeeAmountInUSD := k.CalculateCoinsUSDValue(ctx, weightRecoveryFeeCoins).String()
140+
bonusTokenAmountInUSD := k.CalculateCoinsUSDValue(ctx, weightBalanceBonusCoins).String()
141+
142+
// emit swap fees event
143+
types.EmitSwapFeesCollectedEvent(ctx, swapFeeValueInUSD, slippageAmountInUSD, weightRecoveryFeeAmountInUSD, bonusTokenAmountInUSD)
144+
107145
types.EmitAddLiquidityEvent(ctx, joiner, pool.GetPoolId(), joinCoins)
108146
if k.hooks != nil {
109147
err := k.hooks.AfterJoinPool(ctx, joiner, pool, joinCoins, numShares)

x/amm/keeper/apply_join_pool_state_change_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
7373
joinCoins := sdk.NewCoins(sdk.NewCoin(ptypes.BaseCurrency, sdkmath.NewInt(100000)), sdk.NewCoin(ptypes.ATOM, sdkmath.NewInt(100000)))
7474

7575
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
76-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec())
76+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
7777
suite.Require().NoError(err)
7878
},
7979
},
@@ -137,7 +137,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
137137
)
138138

139139
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
140-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec())
140+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
141141
suite.Require().Error(err)
142142
},
143143
},
@@ -204,7 +204,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
204204

205205
// must panic
206206
suite.Require().Panics(func() {
207-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, sdkmath.NewInt(-1000), joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec())
207+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, sdkmath.NewInt(-1000), joinCoins, sdkmath.LegacyZeroDec(), sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
208208
suite.Require().Error(err)
209209
})
210210
},
@@ -282,7 +282,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
282282
)
283283

284284
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
285-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyNewDecWithPrec(10, 2), sdkmath.LegacyOneDec())
285+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, sdkmath.LegacyNewDecWithPrec(10, 2), sdkmath.LegacyOneDec(), sdkmath.LegacyZeroDec(), sdk.Coins{})
286286
suite.Require().NoError(err)
287287
},
288288
},

x/amm/keeper/estimate_price.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ func (k Keeper) GetTokenPrice(ctx sdk.Context, tokenInDenom, baseCurrency string
6666
return tokenUsdcRate.Mul(usdcDenomPrice)
6767
}
6868

69+
func (k Keeper) CalculateCoinsUSDValue(
70+
ctx sdk.Context,
71+
coins sdk.Coins,
72+
) sdkmath.LegacyDec {
73+
totalValueInUSD := sdkmath.LegacyZeroDec()
74+
for _, coin := range coins {
75+
valueInUSD := k.CalculateUSDValue(ctx, coin.Denom, coin.Amount)
76+
totalValueInUSD = totalValueInUSD.Add(valueInUSD)
77+
}
78+
79+
return totalValueInUSD
80+
}
81+
6982
func (k Keeper) CalculateUSDValue(ctx sdk.Context, denom string, amount sdkmath.Int) sdkmath.LegacyDec {
7083
asset, found := k.assetProfileKeeper.GetEntryByDenom(ctx, denom)
7184
if !found {

x/amm/keeper/fee.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (k Keeper) SwapFeesToRevenueToken(ctx sdk.Context, pool types.Pool, fee sdk
5454
// Executes the swap in the pool and stores the output. Updates pool assets but
5555
// does not actually transfer any tokens to or from the pool.
5656
snapshot := k.GetAccountedPoolSnapshotOrSet(ctx, pool)
57-
tokenOutCoin, _, _, _, oracleOutAmount, _, err := pool.SwapOutAmtGivenIn(ctx, k.oracleKeeper, &snapshot, sdk.Coins{tokenIn}, pool.PoolParams.FeeDenom, sdkmath.LegacyZeroDec(), k.accountedPoolKeeper, sdkmath.LegacyOneDec(), params, takersFees)
57+
tokenOutCoin, _, slippageAmount, _, oracleOutAmount, _, err := pool.SwapOutAmtGivenIn(ctx, k.oracleKeeper, &snapshot, sdk.Coins{tokenIn}, pool.PoolParams.FeeDenom, sdkmath.LegacyZeroDec(), k.accountedPoolKeeper, sdkmath.LegacyOneDec(), params, takersFees)
5858
if err != nil {
5959
return err
6060
}
@@ -67,7 +67,7 @@ func (k Keeper) SwapFeesToRevenueToken(ctx sdk.Context, pool types.Pool, fee sdk
6767

6868
// Settles balances between the tx sender and the pool to match the swap that was executed earlier.
6969
// Also emits a swap event and updates related liquidity metrics.
70-
err = k.UpdatePoolForSwap(ctx, pool, poolRevenueAddress, poolRevenueAddress, tokenIn, tokenOutCoin, sdkmath.LegacyZeroDec(), sdkmath.ZeroInt(), oracleOutAmount.TruncateInt(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), false)
70+
err = k.UpdatePoolForSwap(ctx, pool, poolRevenueAddress, poolRevenueAddress, tokenIn, tokenOutCoin, sdkmath.LegacyZeroDec(), slippageAmount, sdkmath.ZeroInt(), oracleOutAmount.TruncateInt(), sdkmath.LegacyZeroDec(), sdkmath.LegacyZeroDec(), false)
7171
if err != nil {
7272
return err
7373
}

x/amm/keeper/keeper_exit_pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (k Keeper) ExitPool(
2929
}
3030
params := k.GetParams(ctx)
3131
takersFees := k.parameterKeeper.GetParams(ctx).TakerFees
32-
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, err = pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, shareInAmount, tokenOutDenom, params, takersFees, applyWeightBreakingFee)
32+
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, slippageCoins, err := pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, shareInAmount, tokenOutDenom, params, takersFees, applyWeightBreakingFee)
3333
if err != nil {
3434
return sdk.Coins{}, math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), err
3535
}
@@ -39,7 +39,7 @@ func (k Keeper) ExitPool(
3939
exitCoins, tokenOutMins)
4040
}
4141

42-
err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation, weightBalanceBonus, takerFeesFinal)
42+
err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
4343
if err != nil {
4444
return sdk.Coins{}, math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec(), err
4545
}

x/amm/keeper/keeper_join_pool_no_swap.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ func (k Keeper) JoinPoolNoSwap(
6868
params := k.GetParams(ctx)
6969
takerFees := k.parameterKeeper.GetParams(ctx).TakerFees
7070
snapshot := k.GetAccountedPoolSnapshotOrSet(ctx, pool)
71-
tokensJoined, sharesOut, slippage, weightBalanceBonus, _, takerFeesFinal, err := pool.JoinPool(ctx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
71+
tokensJoined, sharesOut, _, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(ctx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
7272
if err != nil {
7373
return nil, sdkmath.ZeroInt(), err
7474
}
7575

76-
if pool.PoolParams.UseOracle && len(tokensIn) == 1 {
77-
slippageAmount := slippage.Mul(tokensIn[0].Amount.ToLegacyDec()).RoundInt()
78-
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokensIn[0].Denom, slippageAmount))
79-
}
80-
8176
// sanity check, don't return error as not worth halting the LP. We know its not too much.
8277
if sharesOut.LT(shareOutAmount) {
8378
ctx.Logger().Error(fmt.Sprintf("Expected to JoinPoolNoSwap >= %s shares, actually did %s shares",
8479
shareOutAmount, sharesOut))
8580
}
81+
// slippage will be 0 as tokensIn.Len() != 1
82+
slippageCoins := sdk.Coins{}
8683

87-
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal)
84+
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
8885
if err != nil {
8986
return nil, sdkmath.Int{}, err
9087
}
@@ -101,18 +98,27 @@ func (k Keeper) JoinPoolNoSwap(
10198
takerFees := k.parameterKeeper.GetParams(ctx).TakerFees
10299
// on oracle pool, full tokenInMaxs are used regardless shareOutAmount
103100
snapshot := k.GetAccountedPoolSnapshotOrSet(ctx, pool)
104-
tokensJoined, sharesOut, _, weightBalanceBonus, _, takerFeesFinal, err := pool.JoinPool(ctx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
101+
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(ctx, &snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
105102
if err != nil {
106103
return nil, sdkmath.ZeroInt(), err
107104
}
108105

106+
slippageCoins := sdk.Coins{}
107+
if pool.PoolParams.UseOracle && len(tokenInMaxs) == 1 {
108+
slippageAmount := slippage.Mul(tokenInMaxs[0].Amount.ToLegacyDec()).RoundInt()
109+
if slippageAmount.IsPositive() {
110+
slippageCoins = sdk.NewCoins(sdk.NewCoin(tokenInMaxs[0].Denom, slippageAmount))
111+
k.TrackWeightBreakingSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenInMaxs[0].Denom, slippageAmount))
112+
}
113+
}
114+
109115
// sanity check, don't return error as not worth halting the LP. We know its not too much.
110116
if sharesOut.LT(shareOutAmount) {
111117
ctx.Logger().Error(fmt.Sprintf("Expected to JoinPoolNoSwap >= %s shares, actually did %s shares",
112118
shareOutAmount, sharesOut))
113119
}
114120

115-
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal)
121+
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
116122
if err != nil {
117123
return nil, sdkmath.Int{}, err
118124
}

x/amm/keeper/keeper_swap_exact_amount_in.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (k Keeper) InternalSwapExactAmountIn(
6060

6161
// Settles balances between the tx sender and the pool to match the swap that was executed earlier.
6262
// Also emits a swap event and updates related liquidity metrics.
63-
err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOutCoin, swapFee, math.ZeroInt(), oracleOutAmount.TruncateInt(), weightBalanceBonus, takersFees, false)
63+
err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOutCoin, swapFee, slippageAmount, math.ZeroInt(), oracleOutAmount.TruncateInt(), weightBalanceBonus, takersFees, false)
6464
if err != nil {
6565
return math.Int{}, err
6666
}

x/amm/keeper/keeper_swap_exact_amount_out.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (k Keeper) InternalSwapExactAmountOut(
5757
return math.Int{}, errorsmod.Wrapf(types.ErrLimitMaxAmount, "swap requires %s, which is greater than the amount %s", tokenIn, tokenInMaxAmount)
5858
}
5959

60-
err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOut, swapFee, oracleInAmount.TruncateInt(), math.ZeroInt(), weightBalanceBonus, takersFees, true)
60+
err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOut, swapFee, slippageAmount, oracleInAmount.TruncateInt(), math.ZeroInt(), weightBalanceBonus, takersFees, true)
6161
if err != nil {
6262
return math.Int{}, err
6363
}

0 commit comments

Comments
 (0)