@@ -258,7 +258,9 @@ fn lock_should_work_reserve() {
258
258
<Balances as Currency <_>>:: transfer( & 1 , & 2 , 1 , AllowDeath ) ,
259
259
TokenError :: Frozen
260
260
) ;
261
- assert_noop ! ( Balances :: reserve( & 1 , 1 ) , Error :: <Test >:: LiquidityRestrictions , ) ;
261
+ // We can use frozen balance for reserves
262
+ assert_ok ! ( Balances :: reserve( & 1 , 9 ) ) ;
263
+ assert_noop ! ( Balances :: reserve( & 1 , 1 ) , DispatchError :: ConsumerRemaining , ) ;
262
264
assert ! ( ChargeTransactionPayment :: <Test >:: validate_and_prepare(
263
265
ChargeTransactionPayment :: from( 1 ) ,
264
266
Some ( 1 ) . into( ) ,
@@ -280,6 +282,47 @@ fn lock_should_work_reserve() {
280
282
} ) ;
281
283
}
282
284
285
+ #[ test]
286
+ fn reserve_should_work_for_frozen_balance ( ) {
287
+ ExtBuilder :: default ( )
288
+ . existential_deposit ( 1 )
289
+ . monied ( true )
290
+ . build_and_execute_with ( || {
291
+ // Check balances
292
+ let account = Balances :: account ( & 1 ) ;
293
+ assert_eq ! ( account. free, 10 ) ;
294
+ assert_eq ! ( account. frozen, 0 ) ;
295
+ assert_eq ! ( account. reserved, 0 ) ;
296
+
297
+ Balances :: set_lock ( ID_1 , & 1 , 9 , WithdrawReasons :: RESERVE ) ;
298
+
299
+ let account = Balances :: account ( & 1 ) ;
300
+ assert_eq ! ( account. free, 10 ) ;
301
+ assert_eq ! ( account. frozen, 9 ) ;
302
+ assert_eq ! ( account. reserved, 0 ) ;
303
+
304
+ assert_ok ! ( Balances :: reserve( & 1 , 5 ) ) ;
305
+
306
+ let account = Balances :: account ( & 1 ) ;
307
+ assert_eq ! ( account. free, 5 ) ;
308
+ assert_eq ! ( account. frozen, 9 ) ;
309
+ assert_eq ! ( account. reserved, 5 ) ;
310
+
311
+ assert_ok ! ( Balances :: reserve( & 1 , 4 ) ) ;
312
+
313
+ let account = Balances :: account ( & 1 ) ;
314
+ assert_eq ! ( account. free, 1 ) ;
315
+ assert_eq ! ( account. frozen, 9 ) ;
316
+ assert_eq ! ( account. reserved, 9 ) ;
317
+
318
+ // Check usable balance
319
+ // usable_balance = free - max(frozen - reserved, ExistentialDeposit)
320
+ // 0 = 1 - max(9 - 9, 1)
321
+ let usable_balance = Balances :: usable_balance ( & 1 ) ;
322
+ assert_eq ! ( usable_balance, 0 ) ;
323
+ } ) ;
324
+ }
325
+
283
326
#[ test]
284
327
fn lock_should_work_tx_fee ( ) {
285
328
ExtBuilder :: default ( )
@@ -291,7 +334,9 @@ fn lock_should_work_tx_fee() {
291
334
<Balances as Currency <_>>:: transfer( & 1 , & 2 , 1 , AllowDeath ) ,
292
335
TokenError :: Frozen
293
336
) ;
294
- assert_noop ! ( Balances :: reserve( & 1 , 1 ) , Error :: <Test >:: LiquidityRestrictions , ) ;
337
+ // We can use frozen balance for reserves
338
+ assert_ok ! ( Balances :: reserve( & 1 , 9 ) ) ;
339
+ assert_noop ! ( Balances :: reserve( & 1 , 1 ) , DispatchError :: ConsumerRemaining , ) ;
295
340
assert ! ( ChargeTransactionPayment :: <Test >:: validate_and_prepare(
296
341
ChargeTransactionPayment :: from( 1 ) ,
297
342
Some ( 1 ) . into( ) ,
@@ -1273,8 +1318,14 @@ fn reserve_must_succeed_if_can_reserve_does() {
1273
1318
ExtBuilder :: default ( ) . build_and_execute_with ( || {
1274
1319
let _ = Balances :: deposit_creating ( & 1 , 1 ) ;
1275
1320
let _ = Balances :: deposit_creating ( & 2 , 2 ) ;
1321
+ let _ = Balances :: deposit_creating ( & 3 , 3 ) ;
1276
1322
assert ! ( Balances :: can_reserve( & 1 , 1 ) == Balances :: reserve( & 1 , 1 ) . is_ok( ) ) ;
1277
1323
assert ! ( Balances :: can_reserve( & 2 , 1 ) == Balances :: reserve( & 2 , 1 ) . is_ok( ) ) ;
1324
+
1325
+ // Ensure that we can reserve as long (free + reserved) remains above
1326
+ // the maximum of frozen balance.
1327
+ Balances :: set_lock ( ID_1 , & 3 , 2 , WithdrawReasons :: RESERVE ) ;
1328
+ assert_eq ! ( Balances :: can_reserve( & 3 , 2 ) , Balances :: reserve( & 3 , 2 ) . is_ok( ) ) ;
1278
1329
} ) ;
1279
1330
}
1280
1331
0 commit comments