Skip to content

Commit 67b8fa0

Browse files
authored
Get swapInfo from single-sided join pool and emit event (#1392)
* add virtual swap events utils * calculate and add event for virtual swaps at join pool * update tests * fix swapped tokenIn calculation for single sided join pool * update struct name from virtual swap to swapinfo * calculate and add event for swaps at exit pool * update tests * update swapinfo event fn name * fix fees during join and exit pool swaps * reviewed changes
1 parent d9cadee commit 67b8fa0

16 files changed

+177
-77
lines changed

x/amm/keeper/apply_exit_pool_state_change.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func (k Keeper) ApplyExitPoolStateChange(
1313
exitCoins sdk.Coins, isLiquidation bool,
1414
weightBalanceBonus osmomath.BigDec, takerFees osmomath.BigDec,
1515
swapFee osmomath.BigDec, slippageCoins sdk.Coins,
16+
swapInfos []types.SwapInfo,
1617
) error {
1718
// Withdraw exit amount of token from commitment module to exiter's wallet.
1819
poolShareDenom := types.GetPoolShareDenom(pool.GetPoolId())
@@ -104,6 +105,10 @@ func (k Keeper) ApplyExitPoolStateChange(
104105
)
105106
}
106107

108+
if exitCoins.Len() == 1 {
109+
types.EmitSwapsInfoEvent(ctx, pool.PoolId, exiter.String(), swapInfos)
110+
}
111+
107112
types.EmitRemoveLiquidityEvent(ctx, exiter, pool.GetPoolId(), exitCoins)
108113
if k.hooks != nil {
109114
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
@@ -82,7 +82,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
8282
suite.Require().True(lpTokenBalance.Amount.Equal(sdkmath.ZeroInt()))
8383

8484
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
85-
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
85+
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addrs[0], pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
8686
suite.Require().NoError(err)
8787
},
8888
func() {},
@@ -100,7 +100,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
100100

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

103-
err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
103+
err := suite.app.AmmKeeper.ApplyExitPoolStateChange(suite.ctx, pool, addr, pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
104104
suite.Require().Error(err)
105105
},
106106
func() {},
@@ -171,7 +171,7 @@ func (suite *AmmKeeperTestSuite) TestApplyExitPoolStateChange() {
171171
coins = sdk.NewCoins(sdk.NewCoin("invalid_denom", sdkmath.NewInt(100000000)))
172172

173173
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
174-
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
174+
err = app.AmmKeeper.ApplyExitPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, coins, false, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
175175
suite.Require().Error(err)
176176
},
177177
func() {},

x/amm/keeper/apply_join_pool_state_change.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (k Keeper) ApplyJoinPoolStateChange(
1919
takerFees osmomath.BigDec,
2020
swapFee osmomath.BigDec,
2121
slippageCoins sdk.Coins,
22+
swapInfos []types.SwapInfo,
2223
) error {
2324
if err := k.bankKeeper.SendCoins(ctx, joiner, sdk.MustAccAddressFromBech32(pool.GetAddress()), joinCoins); err != nil {
2425
return err
@@ -173,6 +174,11 @@ func (k Keeper) ApplyJoinPoolStateChange(
173174
}
174175

175176
types.EmitAddLiquidityEvent(ctx, joiner, pool.GetPoolId(), joinCoins)
177+
178+
if joinCoins.Len() == 1 {
179+
types.EmitSwapsInfoEvent(ctx, pool.PoolId, joiner.String(), swapInfos)
180+
}
181+
176182
if k.hooks != nil {
177183
err := k.hooks.AfterJoinPool(ctx, joiner, pool, joinCoins, numShares)
178184
if err != nil {

x/amm/keeper/apply_join_pool_state_change_test.go

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

7676
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
77-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
77+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
7878
suite.Require().NoError(err)
7979
},
8080
},
@@ -138,7 +138,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
138138
)
139139

140140
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
141-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
141+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
142142
suite.Require().Error(err)
143143
},
144144
},
@@ -205,7 +205,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
205205

206206
// must panic
207207
suite.Require().Panics(func() {
208-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, sdkmath.NewInt(-1000), joinCoins, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
208+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, sdkmath.NewInt(-1000), joinCoins, osmomath.ZeroBigDec(), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
209209
suite.Require().Error(err)
210210
})
211211
},
@@ -283,7 +283,7 @@ func (suite *AmmKeeperTestSuite) TestApplyJoinPoolStateChange() {
283283
)
284284

285285
ctx = ctx.WithBlockTime(ctx.BlockTime().Add(time.Hour))
286-
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.NewBigDecWithPrec(10, 2), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{})
286+
err = app.AmmKeeper.ApplyJoinPoolStateChange(ctx, pool, addr, pool.TotalShares.Amount, joinCoins, osmomath.NewBigDecWithPrec(10, 2), osmomath.OneBigDec(), osmomath.ZeroBigDec(), sdk.Coins{}, []types.SwapInfo{})
287287
suite.Require().NoError(err)
288288
},
289289
},

x/amm/keeper/keeper_exit_pool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (k Keeper) ExitPool(
3131
params := k.GetParams(ctx)
3232
takersFees := k.parameterKeeper.GetParams(ctx).GetBigDecTakerFees()
3333
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
34-
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, slippageCoins, err := pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, snapshot, shareInAmount, tokenOutDenom, params, takersFees, applyWeightBreakingFee)
34+
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, slippageCoins, swapInfos, err := pool.ExitPool(ctx, k.oracleKeeper, k.accountedPoolKeeper, snapshot, shareInAmount, tokenOutDenom, params, takersFees, applyWeightBreakingFee)
3535
if err != nil {
3636
return sdk.Coins{}, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), err
3737
}
@@ -59,7 +59,7 @@ func (k Keeper) ExitPool(
5959
exitCoins, tokenOutMins)
6060
}
6161

62-
err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
62+
err = k.ApplyExitPoolStateChange(ctx, pool, sender, shareInAmount, exitCoins, isLiquidation, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins, swapInfos)
6363
if err != nil {
6464
return sdk.Coins{}, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), err
6565
}

x/amm/keeper/keeper_join_pool_no_swap.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (k Keeper) JoinPoolNoSwap(
7070
params := k.GetParams(ctx)
7171
takerFees := k.parameterKeeper.GetParams(ctx).GetBigDecTakerFees()
7272
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
73-
tokensJoined, sharesOut, _, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(ctx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
73+
tokensJoined, sharesOut, _, weightBalanceBonus, swapFee, takerFeesFinal, virtualSwaps, err := pool.JoinPool(ctx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
7474
if err != nil {
7575
return nil, sdkmath.ZeroInt(), err
7676
}
@@ -83,7 +83,7 @@ func (k Keeper) JoinPoolNoSwap(
8383
// slippage will be 0 as tokensIn.Len() != 1
8484
slippageCoins := sdk.Coins{}
8585

86-
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
86+
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins, virtualSwaps)
8787
if err != nil {
8888
return nil, sdkmath.Int{}, err
8989
}
@@ -100,7 +100,7 @@ func (k Keeper) JoinPoolNoSwap(
100100
takerFees := k.parameterKeeper.GetParams(ctx).GetBigDecTakerFees()
101101
// on oracle pool, full tokenInMaxs are used regardless shareOutAmount
102102
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
103-
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(ctx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
103+
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, virtualSwaps, err := pool.JoinPool(ctx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
104104
if err != nil {
105105
return nil, sdkmath.ZeroInt(), err
106106
}
@@ -148,7 +148,7 @@ func (k Keeper) JoinPoolNoSwap(
148148
shareOutAmount, sharesOut))
149149
}
150150

151-
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins)
151+
err = k.ApplyJoinPoolStateChange(ctx, pool, sender, sharesOut, tokensJoined, weightBalanceBonus, takerFeesFinal, swapFee, slippageCoins, virtualSwaps)
152152
if err != nil {
153153
return nil, sdkmath.Int{}, err
154154
}

x/amm/keeper/query_exit_pool_estimation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (k Keeper) ExitPoolEst(
5454
params := k.GetParams(ctx)
5555
takerFees := k.parameterKeeper.GetParams(ctx).GetBigDecTakerFees()
5656
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
57-
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, _, err = pool.CalcExitPoolCoinsFromShares(ctx, k.oracleKeeper, k.accountedPoolKeeper, snapshot, shareInAmount, tokenOutDenom, params, takerFees, true)
57+
exitCoins, weightBalanceBonus, slippage, swapFee, takerFeesFinal, _, _, err = pool.CalcExitPoolCoinsFromShares(ctx, k.oracleKeeper, k.accountedPoolKeeper, snapshot, shareInAmount, tokenOutDenom, params, takerFees, true)
5858
if err != nil {
5959
return sdk.Coins{}, osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), err
6060
}

x/amm/keeper/query_join_pool_estimation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (k Keeper) JoinPoolEst(
6464
takerFees := k.parameterKeeper.GetParams(ctx).GetBigDecTakerFees()
6565
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
6666
cacheCtx, _ := ctx.CacheContext()
67-
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, err := pool.JoinPool(cacheCtx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
67+
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, _, err := pool.JoinPool(cacheCtx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokensIn, params, takerFees)
6868
if err != nil {
6969
return nil, math.ZeroInt(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coin{}, err
7070
}
@@ -78,7 +78,7 @@ func (k Keeper) JoinPoolEst(
7878
snapshot := k.GetPoolWithAccountedBalance(ctx, pool.PoolId)
7979
cacheCtx, _ := ctx.CacheContext()
8080
var tokensJoined sdk.Coins
81-
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, err = pool.JoinPool(cacheCtx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
81+
tokensJoined, sharesOut, slippage, weightBalanceBonus, swapFee, takerFeesFinal, _, err = pool.JoinPool(cacheCtx, snapshot, k.oracleKeeper, k.accountedPoolKeeper, tokenInMaxs, params, takerFees)
8282
if err != nil {
8383
return nil, math.ZeroInt(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), osmomath.ZeroBigDec(), sdk.Coin{}, err
8484
}

0 commit comments

Comments
 (0)