33use frame_support:: { assert_noop, assert_ok, traits:: Currency } ;
44use mockall:: predicate;
55use sp_core:: H160 ;
6- use sp_runtime:: DispatchError ;
6+ use sp_runtime:: { DispatchError , TokenError } ;
77use sp_std:: str:: FromStr ;
88
99use crate :: { mock:: * , * } ;
@@ -29,6 +29,12 @@ fn swap_works() {
2929 System :: set_block_number ( 1 ) ;
3030
3131 // Set mock expectations.
32+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
33+ estimate_swapped_balance_ctx
34+ . expect ( )
35+ . once ( )
36+ . with ( predicate:: eq ( swap_balance) )
37+ . return_const ( swap_balance) ;
3238 let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
3339 swap_ctx
3440 . expect ( )
@@ -61,6 +67,7 @@ fn swap_works() {
6167 } ) ) ;
6268
6369 // Assert mock invocations.
70+ estimate_swapped_balance_ctx. checkpoint ( ) ;
6471 swap_ctx. checkpoint ( ) ;
6572 } ) ;
6673}
@@ -86,6 +93,12 @@ fn swap_works_kill_origin() {
8693 System :: set_block_number ( 1 ) ;
8794
8895 // Set mock expectations.
96+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
97+ estimate_swapped_balance_ctx
98+ . expect ( )
99+ . once ( )
100+ . with ( predicate:: eq ( swap_balance) )
101+ . return_const ( swap_balance) ;
89102 let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
90103 swap_ctx
91104 . expect ( )
@@ -115,6 +128,7 @@ fn swap_works_kill_origin() {
115128 } ) ) ;
116129
117130 // Assert mock invocations.
131+ estimate_swapped_balance_ctx. checkpoint ( ) ;
118132 swap_ctx. checkpoint ( ) ;
119133 } ) ;
120134}
@@ -139,6 +153,12 @@ fn swap_keep_alive_works() {
139153 System :: set_block_number ( 1 ) ;
140154
141155 // Set mock expectations.
156+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
157+ estimate_swapped_balance_ctx
158+ . expect ( )
159+ . once ( )
160+ . with ( predicate:: eq ( swap_balance) )
161+ . return_const ( swap_balance) ;
142162 let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
143163 swap_ctx
144164 . expect ( )
@@ -171,6 +191,7 @@ fn swap_keep_alive_works() {
171191 } ) ) ;
172192
173193 // Assert mock invocations.
194+ estimate_swapped_balance_ctx. checkpoint ( ) ;
174195 swap_ctx. checkpoint ( ) ;
175196 } ) ;
176197}
@@ -188,6 +209,12 @@ fn swap_fails() {
188209 Balances :: make_free_balance_be ( & alice, alice_balance) ;
189210
190211 // Set mock expectations.
212+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
213+ estimate_swapped_balance_ctx
214+ . expect ( )
215+ . once ( )
216+ . with ( predicate:: eq ( swap_balance) )
217+ . return_const ( swap_balance) ;
191218 let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
192219 swap_ctx
193220 . expect ( )
@@ -213,6 +240,46 @@ fn swap_fails() {
213240 assert_eq ! ( EvmBalances :: total_balance( & alice_evm) , 0 ) ;
214241
215242 // Assert mock invocations.
243+ estimate_swapped_balance_ctx. checkpoint ( ) ;
244+ swap_ctx. checkpoint ( ) ;
245+ } ) ;
246+ }
247+
248+ /// This test verifies that swap call fails in case estimated swapped balance less or equal
249+ /// than target currency existential deposit.
250+ #[ test]
251+ fn swap_below_ed_fails ( ) {
252+ new_test_ext ( ) . execute_with_ext ( |_| {
253+ let alice = 42 ;
254+ let alice_evm = H160 :: from_str ( "1000000000000000000000000000000000000001" ) . unwrap ( ) ;
255+ let alice_balance = 1000 ;
256+ let swap_balance = 100 ;
257+
258+ // Prepare the test state.
259+ Balances :: make_free_balance_be ( & alice, alice_balance) ;
260+
261+ // // Set mock expectations.
262+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
263+ estimate_swapped_balance_ctx
264+ . expect ( )
265+ . once ( )
266+ . with ( predicate:: eq ( swap_balance) )
267+ . return_const ( EXISTENTIAL_DEPOSIT - 1 ) ;
268+ let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
269+ swap_ctx. expect ( ) . never ( ) ;
270+
271+ // Invoke the function under test.
272+ assert_noop ! (
273+ CurrencySwap :: swap( RuntimeOrigin :: signed( alice) , alice_evm, swap_balance) ,
274+ DispatchError :: Token ( TokenError :: BelowMinimum )
275+ ) ;
276+
277+ // Assert state changes.
278+ assert_eq ! ( Balances :: total_balance( & alice) , alice_balance) ;
279+ assert_eq ! ( EvmBalances :: total_balance( & alice_evm) , 0 ) ;
280+
281+ // Assert mock invocations.
282+ estimate_swapped_balance_ctx. checkpoint ( ) ;
216283 swap_ctx. checkpoint ( ) ;
217284 } ) ;
218285}
@@ -231,6 +298,12 @@ fn swap_keep_alive_fails() {
231298 Balances :: make_free_balance_be ( & alice, alice_balance) ;
232299
233300 // Set mock expectations.
301+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
302+ estimate_swapped_balance_ctx
303+ . expect ( )
304+ . once ( )
305+ . with ( predicate:: eq ( swap_balance) )
306+ . return_const ( swap_balance) ;
234307 let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
235308 swap_ctx. expect ( ) . never ( ) ;
236309
@@ -245,6 +318,46 @@ fn swap_keep_alive_fails() {
245318 assert_eq ! ( EvmBalances :: total_balance( & alice_evm) , 0 ) ;
246319
247320 // Assert mock invocations.
321+ estimate_swapped_balance_ctx. checkpoint ( ) ;
322+ swap_ctx. checkpoint ( ) ;
323+ } ) ;
324+ }
325+
326+ /// This test verifies that `swap_keep_alive` call fails in case estimated swapped balance less or equal
327+ /// than target currency existential deposit.
328+ #[ test]
329+ fn swap_keep_alive_below_ed_fails ( ) {
330+ new_test_ext ( ) . execute_with_ext ( |_| {
331+ let alice = 42 ;
332+ let alice_evm = H160 :: from_str ( "1000000000000000000000000000000000000001" ) . unwrap ( ) ;
333+ let alice_balance = 1000 ;
334+ let swap_balance = 100 ;
335+
336+ // Prepare the test state.
337+ Balances :: make_free_balance_be ( & alice, alice_balance) ;
338+
339+ // // Set mock expectations.
340+ let estimate_swapped_balance_ctx = MockCurrencySwap :: estimate_swapped_balance_context ( ) ;
341+ estimate_swapped_balance_ctx
342+ . expect ( )
343+ . once ( )
344+ . with ( predicate:: eq ( swap_balance) )
345+ . return_const ( EXISTENTIAL_DEPOSIT - 1 ) ;
346+ let swap_ctx = MockCurrencySwap :: swap_context ( ) ;
347+ swap_ctx. expect ( ) . never ( ) ;
348+
349+ // Invoke the function under test.
350+ assert_noop ! (
351+ CurrencySwap :: swap_keep_alive( RuntimeOrigin :: signed( alice) , alice_evm, swap_balance) ,
352+ DispatchError :: Token ( TokenError :: BelowMinimum )
353+ ) ;
354+
355+ // Assert state changes.
356+ assert_eq ! ( Balances :: total_balance( & alice) , alice_balance) ;
357+ assert_eq ! ( EvmBalances :: total_balance( & alice_evm) , 0 ) ;
358+
359+ // Assert mock invocations.
360+ estimate_swapped_balance_ctx. checkpoint ( ) ;
248361 swap_ctx. checkpoint ( ) ;
249362 } ) ;
250363}
0 commit comments