@@ -10,6 +10,8 @@ import (
1010
1111const (
1212 doesNotExistDenom = "nodenom"
13+ // Max positive int64.
14+ int64Max = int64 (^ uint64 (0 ) >> 1 )
1315)
1416
1517var _ = suite .TestingSuite (nil )
@@ -46,7 +48,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn_Events() {
4648 tokenIn : sdk .NewCoin ("foo" , sdk .NewInt (tokenIn )),
4749 tokenOutMinAmount : sdk .NewInt (tokenInMinAmount ),
4850 expectedSwapEvents : 1 ,
49- expectedMessageEvents : 3 , // 1 gamm + 2 tendermint .
51+ expectedMessageEvents : 3 , // 1 gamm + 2 events emitted by other keeper methods .
5052 },
5153 "two hops" : {
5254 routes : []types.SwapAmountInRoute {
@@ -62,7 +64,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn_Events() {
6264 tokenIn : sdk .NewCoin ("foo" , sdk .NewInt (tokenIn )),
6365 tokenOutMinAmount : sdk .NewInt (tokenInMinAmount ),
6466 expectedSwapEvents : 2 ,
65- expectedMessageEvents : 5 , // 1 gamm + 4 tendermint .
67+ expectedMessageEvents : 5 , // 1 gamm + 4 events emitted by other keeper methods .
6668 },
6769 "invalid - two hops, denom does not exist" : {
6870 routes : []types.SwapAmountInRoute {
@@ -107,8 +109,8 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn_Events() {
107109 suite .NotNil (response )
108110 }
109111
110- assertEventEmitted ( suite , ctx , types .TypeEvtTokenSwapped , tc .expectedSwapEvents )
111- assertEventEmitted ( suite , ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
112+ suite . AssertEventEmitted ( ctx , types .TypeEvtTokenSwapped , tc .expectedSwapEvents )
113+ suite . AssertEventEmitted ( ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
112114 })
113115 }
114116}
@@ -117,8 +119,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn_Events() {
117119// when calling SwapExactAmountOut.
118120func (suite * KeeperTestSuite ) TestSwapExactAmountOut_Events () {
119121 const (
120- // Max positive int64.
121- tokenInMaxAmount = int64 (^ uint64 (0 ) >> 1 )
122+ tokenInMaxAmount = int64Max
122123 tokenOut = 5
123124 )
124125
@@ -146,7 +147,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut_Events() {
146147 tokenOut : sdk .NewCoin ("foo" , sdk .NewInt (tokenOut )),
147148 tokenInMaxAmount : sdk .NewInt (tokenInMaxAmount ),
148149 expectedSwapEvents : 1 ,
149- expectedMessageEvents : 3 , // 1 gamm + 2 tendermint .
150+ expectedMessageEvents : 3 , // 1 gamm + 2 events emitted by other keeper methods .
150151 },
151152 "two hops" : {
152153 routes : []types.SwapAmountOutRoute {
@@ -162,7 +163,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut_Events() {
162163 tokenOut : sdk .NewCoin ("foo" , sdk .NewInt (tokenOut )),
163164 tokenInMaxAmount : sdk .NewInt (tokenInMaxAmount ),
164165 expectedSwapEvents : 2 ,
165- expectedMessageEvents : 5 , // 1 gamm + 4 tendermint .
166+ expectedMessageEvents : 5 , // 1 gamm + 4 events emitted by other keeper methods .
166167 },
167168 "invalid - two hops, denom does not exist" : {
168169 routes : []types.SwapAmountOutRoute {
@@ -207,8 +208,8 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut_Events() {
207208 suite .NotNil (response )
208209 }
209210
210- assertEventEmitted ( suite , ctx , types .TypeEvtTokenSwapped , tc .expectedSwapEvents )
211- assertEventEmitted ( suite , ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
211+ suite . AssertEventEmitted ( ctx , types .TypeEvtTokenSwapped , tc .expectedSwapEvents )
212+ suite . AssertEventEmitted ( ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
212213 })
213214 }
214215}
@@ -217,8 +218,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountOut_Events() {
217218// when calling JoinPool.
218219func (suite * KeeperTestSuite ) TestJoinPool_Events () {
219220 const (
220- // Max positive int64.
221- tokenInMaxAmount = int64 (^ uint64 (0 ) >> 1 )
221+ tokenInMaxAmount = int64Max
222222 shareOut = 110
223223 )
224224
@@ -239,7 +239,7 @@ func (suite *KeeperTestSuite) TestJoinPool_Events() {
239239 sdk .NewCoin ("baz" , sdk .NewInt (tokenInMaxAmount )),
240240 ),
241241 expectedAddLiquidityEvents : 1 ,
242- expectedMessageEvents : 3 , // 1 gamm + 2 tendermint .
242+ expectedMessageEvents : 3 , // 1 gamm + 2 events emitted by other keeper methods .
243243 },
244244 "tokenInMaxs do not match all tokens in pool - invalid join" : {
245245 poolId : 1 ,
@@ -254,7 +254,6 @@ func (suite *KeeperTestSuite) TestJoinPool_Events() {
254254 suite .Setup ()
255255 ctx := suite .Ctx
256256
257- suite .PrepareBalancerPool ()
258257 suite .PrepareBalancerPool ()
259258
260259 msgServer := keeper .NewMsgServerImpl (suite .App .GAMMKeeper )
@@ -275,20 +274,85 @@ func (suite *KeeperTestSuite) TestJoinPool_Events() {
275274 suite .Require ().NotNil (response )
276275 }
277276
278- assertEventEmitted ( suite , ctx , types .TypeEvtPoolJoined , tc .expectedAddLiquidityEvents )
279- assertEventEmitted ( suite , ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
277+ suite . AssertEventEmitted ( ctx , types .TypeEvtPoolJoined , tc .expectedAddLiquidityEvents )
278+ suite . AssertEventEmitted ( ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
280279 })
281280 }
282281}
283282
284- func assertEventEmitted (suite * KeeperTestSuite , ctx sdk.Context , eventTypeExpected string , numEventsExpected int ) {
285- allEvents := ctx .EventManager ().Events ()
286- // filter out other events
287- actualEvents := make ([]sdk.Event , 0 , 1 )
288- for _ , event := range allEvents {
289- if event .Type == eventTypeExpected {
290- actualEvents = append (actualEvents , event )
291- }
283+ // TestExitPool_Events tests that events are correctly emitted
284+ // when calling ExitPool.
285+ func (suite * KeeperTestSuite ) TestExitPool_Events () {
286+ const (
287+ tokenOutMinAmount = 1
288+ shareIn = 110
289+ )
290+
291+ testcases := map [string ]struct {
292+ poolId uint64
293+ shareInAmount sdk.Int
294+ tokenOutMins sdk.Coins
295+ expectError bool
296+ expectedRemoveLiquidityEvents int
297+ expectedMessageEvents int
298+ }{
299+ "successful exit" : {
300+ poolId : 1 ,
301+ shareInAmount : sdk .NewInt (shareIn ),
302+ tokenOutMins : sdk .NewCoins (),
303+ expectedRemoveLiquidityEvents : 1 ,
304+ expectedMessageEvents : 3 , // 1 gamm + 2 events emitted by other keeper methods.
305+ },
306+ "invalid tokenOutMins" : {
307+ poolId : 1 ,
308+ shareInAmount : sdk .NewInt (shareIn ),
309+ tokenOutMins : sdk .NewCoins (sdk .NewCoin ("foo" , sdk .NewInt (tokenOutMinAmount ))),
310+ expectError : true ,
311+ },
312+ }
313+
314+ for name , tc := range testcases {
315+ suite .Run (name , func () {
316+ suite .Setup ()
317+ ctx := suite .Ctx
318+
319+ suite .PrepareBalancerPool ()
320+ msgServer := keeper .NewMsgServerImpl (suite .App .GAMMKeeper )
321+
322+ sender := suite .TestAccs [0 ].String ()
323+
324+ // Pre-join pool to be able to ExitPool.
325+ joinPoolResponse , err := msgServer .JoinPool (sdk .WrapSDKContext (ctx ), & types.MsgJoinPool {
326+ Sender : sender ,
327+ PoolId : tc .poolId ,
328+ ShareOutAmount : sdk .NewInt (shareIn ),
329+ TokenInMaxs : sdk .NewCoins (
330+ sdk .NewCoin ("foo" , sdk .NewInt (int64Max )),
331+ sdk .NewCoin ("bar" , sdk .NewInt (int64Max )),
332+ sdk .NewCoin ("baz" , sdk .NewInt (int64Max )),
333+ ),
334+ })
335+ suite .Require ().NoError (err )
336+
337+ // Reset event counts to 0 by creating a new manager.
338+ ctx = ctx .WithEventManager (sdk .NewEventManager ())
339+ suite .Require ().Equal (0 , len (ctx .EventManager ().Events ()))
340+
341+ // System under test.
342+ response , err := msgServer .ExitPool (sdk .WrapSDKContext (ctx ), & types.MsgExitPool {
343+ Sender : sender ,
344+ PoolId : tc .poolId ,
345+ ShareInAmount : joinPoolResponse .ShareOutAmount ,
346+ TokenOutMins : tc .tokenOutMins ,
347+ })
348+
349+ if ! tc .expectError {
350+ suite .Require ().NoError (err )
351+ suite .Require ().NotNil (response )
352+ }
353+
354+ suite .AssertEventEmitted (ctx , types .TypeEvtPoolExited , tc .expectedRemoveLiquidityEvents )
355+ suite .AssertEventEmitted (ctx , sdk .EventTypeMessage , tc .expectedMessageEvents )
356+ })
292357 }
293- suite .Require ().Equal (numEventsExpected , len (actualEvents ))
294358}
0 commit comments