Skip to content

Commit de2c870

Browse files
authored
update LockableCurrency to return resut (#343)
* update LockableCurrency to return resut * update tests
1 parent 59129c2 commit de2c870

File tree

5 files changed

+86
-50
lines changed

5 files changed

+86
-50
lines changed

currencies/src/lib.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -352,27 +352,37 @@ impl<T: Config> MultiCurrencyExtended<T::AccountId> for Module<T> {
352352
impl<T: Config> MultiLockableCurrency<T::AccountId> for Module<T> {
353353
type Moment = T::BlockNumber;
354354

355-
fn set_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) {
355+
fn set_lock(
356+
lock_id: LockIdentifier,
357+
currency_id: Self::CurrencyId,
358+
who: &T::AccountId,
359+
amount: Self::Balance,
360+
) -> DispatchResult {
356361
if currency_id == T::GetNativeCurrencyId::get() {
357-
T::NativeCurrency::set_lock(lock_id, who, amount);
362+
T::NativeCurrency::set_lock(lock_id, who, amount)
358363
} else {
359-
T::MultiCurrency::set_lock(lock_id, currency_id, who, amount);
364+
T::MultiCurrency::set_lock(lock_id, currency_id, who, amount)
360365
}
361366
}
362367

363-
fn extend_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) {
368+
fn extend_lock(
369+
lock_id: LockIdentifier,
370+
currency_id: Self::CurrencyId,
371+
who: &T::AccountId,
372+
amount: Self::Balance,
373+
) -> DispatchResult {
364374
if currency_id == T::GetNativeCurrencyId::get() {
365-
T::NativeCurrency::extend_lock(lock_id, who, amount);
375+
T::NativeCurrency::extend_lock(lock_id, who, amount)
366376
} else {
367-
T::MultiCurrency::extend_lock(lock_id, currency_id, who, amount);
377+
T::MultiCurrency::extend_lock(lock_id, currency_id, who, amount)
368378
}
369379
}
370380

371-
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId) {
381+
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId) -> DispatchResult {
372382
if currency_id == T::GetNativeCurrencyId::get() {
373-
T::NativeCurrency::remove_lock(lock_id, who);
383+
T::NativeCurrency::remove_lock(lock_id, who)
374384
} else {
375-
T::MultiCurrency::remove_lock(lock_id, currency_id, who);
385+
T::MultiCurrency::remove_lock(lock_id, currency_id, who)
376386
}
377387
}
378388
}
@@ -502,16 +512,16 @@ where
502512
{
503513
type Moment = T::BlockNumber;
504514

505-
fn set_lock(lock_id: LockIdentifier, who: &T::AccountId, amount: Self::Balance) {
506-
<Module<T> as MultiLockableCurrency<T::AccountId>>::set_lock(lock_id, GetCurrencyId::get(), who, amount);
515+
fn set_lock(lock_id: LockIdentifier, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
516+
<Module<T> as MultiLockableCurrency<T::AccountId>>::set_lock(lock_id, GetCurrencyId::get(), who, amount)
507517
}
508518

509-
fn extend_lock(lock_id: LockIdentifier, who: &T::AccountId, amount: Self::Balance) {
510-
<Module<T> as MultiLockableCurrency<T::AccountId>>::extend_lock(lock_id, GetCurrencyId::get(), who, amount);
519+
fn extend_lock(lock_id: LockIdentifier, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
520+
<Module<T> as MultiLockableCurrency<T::AccountId>>::extend_lock(lock_id, GetCurrencyId::get(), who, amount)
511521
}
512522

513-
fn remove_lock(lock_id: LockIdentifier, who: &T::AccountId) {
514-
<Module<T> as MultiLockableCurrency<T::AccountId>>::remove_lock(lock_id, GetCurrencyId::get(), who);
523+
fn remove_lock(lock_id: LockIdentifier, who: &T::AccountId) -> DispatchResult {
524+
<Module<T> as MultiLockableCurrency<T::AccountId>>::remove_lock(lock_id, GetCurrencyId::get(), who)
515525
}
516526
}
517527

@@ -659,16 +669,19 @@ where
659669
{
660670
type Moment = Moment;
661671

662-
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
672+
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) -> DispatchResult {
663673
Currency::set_lock(lock_id, who, amount, WithdrawReasons::all());
674+
Ok(())
664675
}
665676

666-
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
677+
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) -> DispatchResult {
667678
Currency::extend_lock(lock_id, who, amount, WithdrawReasons::all());
679+
Ok(())
668680
}
669681

670-
fn remove_lock(lock_id: LockIdentifier, who: &AccountId) {
682+
fn remove_lock(lock_id: LockIdentifier, who: &AccountId) -> DispatchResult {
671683
Currency::remove_lock(lock_id, who);
684+
Ok(())
672685
}
673686
}
674687

currencies/src/tests.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ fn multi_lockable_currency_should_work() {
1616
.one_hundred_for_alice_n_bob()
1717
.build()
1818
.execute_with(|| {
19-
Currencies::set_lock(ID_1, X_TOKEN_ID, &ALICE, 50);
19+
assert_ok!(Currencies::set_lock(ID_1, X_TOKEN_ID, &ALICE, 50));
2020
assert_eq!(Tokens::locks(&ALICE, X_TOKEN_ID).len(), 1);
21-
Currencies::set_lock(ID_1, NATIVE_CURRENCY_ID, &ALICE, 50);
21+
assert_ok!(Currencies::set_lock(ID_1, NATIVE_CURRENCY_ID, &ALICE, 50));
2222
assert_eq!(PalletBalances::locks(&ALICE).len(), 1);
2323
});
2424
}
@@ -47,9 +47,9 @@ fn native_currency_lockable_should_work() {
4747
.one_hundred_for_alice_n_bob()
4848
.build()
4949
.execute_with(|| {
50-
NativeCurrency::set_lock(ID_1, &ALICE, 10);
50+
assert_ok!(NativeCurrency::set_lock(ID_1, &ALICE, 10));
5151
assert_eq!(PalletBalances::locks(&ALICE).len(), 1);
52-
NativeCurrency::remove_lock(ID_1, &ALICE);
52+
assert_ok!(NativeCurrency::remove_lock(ID_1, &ALICE));
5353
assert_eq!(PalletBalances::locks(&ALICE).len(), 0);
5454
});
5555
}
@@ -71,9 +71,9 @@ fn basic_currency_adapting_pallet_balances_lockable() {
7171
.one_hundred_for_alice_n_bob()
7272
.build()
7373
.execute_with(|| {
74-
AdaptedBasicCurrency::set_lock(ID_1, &ALICE, 10);
74+
assert_ok!(AdaptedBasicCurrency::set_lock(ID_1, &ALICE, 10));
7575
assert_eq!(PalletBalances::locks(&ALICE).len(), 1);
76-
AdaptedBasicCurrency::remove_lock(ID_1, &ALICE);
76+
assert_ok!(AdaptedBasicCurrency::remove_lock(ID_1, &ALICE));
7777
assert_eq!(PalletBalances::locks(&ALICE).len(), 0);
7878
});
7979
}

tokens/src/lib.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,14 @@ impl<T: Config> MultiLockableCurrency<T::AccountId> for Module<T> {
603603

604604
// Set a lock on the balance of `who` under `currency_id`.
605605
// Is a no-op if lock amount is zero.
606-
fn set_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) {
606+
fn set_lock(
607+
lock_id: LockIdentifier,
608+
currency_id: Self::CurrencyId,
609+
who: &T::AccountId,
610+
amount: Self::Balance,
611+
) -> DispatchResult {
607612
if amount.is_zero() {
608-
return;
613+
return Ok(());
609614
}
610615
let mut new_lock = Some(BalanceLock { id: lock_id, amount });
611616
let mut locks = Self::locks(who, currency_id)
@@ -622,13 +627,19 @@ impl<T: Config> MultiLockableCurrency<T::AccountId> for Module<T> {
622627
locks.push(lock)
623628
}
624629
Self::update_locks(currency_id, who, &locks[..]);
630+
Ok(())
625631
}
626632

627633
// Extend a lock on the balance of `who` under `currency_id`.
628634
// Is a no-op if lock amount is zero
629-
fn extend_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId, amount: Self::Balance) {
635+
fn extend_lock(
636+
lock_id: LockIdentifier,
637+
currency_id: Self::CurrencyId,
638+
who: &T::AccountId,
639+
amount: Self::Balance,
640+
) -> DispatchResult {
630641
if amount.is_zero() {
631-
return;
642+
return Ok(());
632643
}
633644
let mut new_lock = Some(BalanceLock { id: lock_id, amount });
634645
let mut locks = Self::locks(who, currency_id)
@@ -648,12 +659,14 @@ impl<T: Config> MultiLockableCurrency<T::AccountId> for Module<T> {
648659
locks.push(lock)
649660
}
650661
Self::update_locks(currency_id, who, &locks[..]);
662+
Ok(())
651663
}
652664

653-
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId) {
665+
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId) -> DispatchResult {
654666
let mut locks = Self::locks(who, currency_id);
655667
locks.retain(|lock| lock.id != lock_id);
656668
Self::update_locks(currency_id, who, &locks[..]);
669+
Ok(())
657670
}
658671
}
659672

@@ -980,15 +993,15 @@ where
980993
type MaxLocks = ();
981994

982995
fn set_lock(id: LockIdentifier, who: &T::AccountId, amount: Self::Balance, _reasons: WithdrawReasons) {
983-
Module::<T>::set_lock(id, GetCurrencyId::get(), who, amount)
996+
let _ = Module::<T>::set_lock(id, GetCurrencyId::get(), who, amount);
984997
}
985998

986999
fn extend_lock(id: LockIdentifier, who: &T::AccountId, amount: Self::Balance, _reasons: WithdrawReasons) {
987-
Module::<T>::extend_lock(id, GetCurrencyId::get(), who, amount)
1000+
let _ = Module::<T>::extend_lock(id, GetCurrencyId::get(), who, amount);
9881001
}
9891002

9901003
fn remove_lock(id: LockIdentifier, who: &T::AccountId) {
991-
Module::<T>::remove_lock(id, GetCurrencyId::get(), who)
1004+
let _ = Module::<T>::remove_lock(id, GetCurrencyId::get(), who);
9921005
}
9931006
}
9941007

tokens/src/tests.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ fn set_lock_should_work() {
7676
.one_hundred_for_alice_n_bob()
7777
.build()
7878
.execute_with(|| {
79-
Tokens::set_lock(ID_1, DOT, &ALICE, 10);
79+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 10));
8080
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen, 10);
8181
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen(), 10);
8282
assert_eq!(Tokens::locks(ALICE, DOT).len(), 1);
83-
Tokens::set_lock(ID_1, DOT, &ALICE, 50);
83+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 50));
8484
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen, 50);
8585
assert_eq!(Tokens::locks(ALICE, DOT).len(), 1);
86-
Tokens::set_lock(ID_2, DOT, &ALICE, 60);
86+
assert_ok!(Tokens::set_lock(ID_2, DOT, &ALICE, 60));
8787
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen, 60);
8888
assert_eq!(Tokens::locks(ALICE, DOT).len(), 2);
8989
});
@@ -95,14 +95,14 @@ fn extend_lock_should_work() {
9595
.one_hundred_for_alice_n_bob()
9696
.build()
9797
.execute_with(|| {
98-
Tokens::set_lock(ID_1, DOT, &ALICE, 10);
98+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 10));
9999
assert_eq!(Tokens::locks(ALICE, DOT).len(), 1);
100100
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen, 10);
101-
Tokens::extend_lock(ID_1, DOT, &ALICE, 20);
101+
assert_ok!(Tokens::extend_lock(ID_1, DOT, &ALICE, 20));
102102
assert_eq!(Tokens::locks(ALICE, DOT).len(), 1);
103103
assert_eq!(Tokens::accounts(&ALICE, DOT).frozen, 20);
104-
Tokens::extend_lock(ID_2, DOT, &ALICE, 10);
105-
Tokens::extend_lock(ID_1, DOT, &ALICE, 20);
104+
assert_ok!(Tokens::extend_lock(ID_2, DOT, &ALICE, 10));
105+
assert_ok!(Tokens::extend_lock(ID_1, DOT, &ALICE, 20));
106106
assert_eq!(Tokens::locks(ALICE, DOT).len(), 2);
107107
});
108108
}
@@ -113,10 +113,10 @@ fn remove_lock_should_work() {
113113
.one_hundred_for_alice_n_bob()
114114
.build()
115115
.execute_with(|| {
116-
Tokens::set_lock(ID_1, DOT, &ALICE, 10);
117-
Tokens::set_lock(ID_2, DOT, &ALICE, 20);
116+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 10));
117+
assert_ok!(Tokens::set_lock(ID_2, DOT, &ALICE, 20));
118118
assert_eq!(Tokens::locks(ALICE, DOT).len(), 2);
119-
Tokens::remove_lock(ID_2, DOT, &ALICE);
119+
assert_ok!(Tokens::remove_lock(ID_2, DOT, &ALICE));
120120
assert_eq!(Tokens::locks(ALICE, DOT).len(), 1);
121121
});
122122
}
@@ -127,12 +127,12 @@ fn frozen_can_limit_liquidity() {
127127
.one_hundred_for_alice_n_bob()
128128
.build()
129129
.execute_with(|| {
130-
Tokens::set_lock(ID_1, DOT, &ALICE, 90);
130+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 90));
131131
assert_noop!(
132132
<Tokens as MultiCurrency<_>>::transfer(DOT, &ALICE, &BOB, 11),
133133
Error::<Runtime>::LiquidityRestrictions,
134134
);
135-
Tokens::set_lock(ID_1, DOT, &ALICE, 10);
135+
assert_ok!(Tokens::set_lock(ID_1, DOT, &ALICE, 10));
136136
assert_ok!(<Tokens as MultiCurrency<_>>::transfer(DOT, &ALICE, &BOB, 11),);
137137
});
138138
}

traits/src/currency.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ pub trait MultiLockableCurrency<AccountId>: MultiCurrency<AccountId> {
9999
/// than a user has.
100100
///
101101
/// If the lock `lock_id` already exists, this will update it.
102-
fn set_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &AccountId, amount: Self::Balance);
102+
fn set_lock(
103+
lock_id: LockIdentifier,
104+
currency_id: Self::CurrencyId,
105+
who: &AccountId,
106+
amount: Self::Balance,
107+
) -> DispatchResult;
103108

104109
/// Changes a balance lock (selected by `lock_id`) so that it becomes less
105110
/// liquid in all parameters or creates a new one if it does not exist.
@@ -109,10 +114,15 @@ pub trait MultiLockableCurrency<AccountId>: MultiCurrency<AccountId> {
109114
/// while `set_lock` replaces the lock with the new parameters. As in,
110115
/// `extend_lock` will set:
111116
/// - maximum `amount`
112-
fn extend_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &AccountId, amount: Self::Balance);
117+
fn extend_lock(
118+
lock_id: LockIdentifier,
119+
currency_id: Self::CurrencyId,
120+
who: &AccountId,
121+
amount: Self::Balance,
122+
) -> DispatchResult;
113123

114124
/// Remove an existing lock.
115-
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &AccountId);
125+
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &AccountId) -> DispatchResult;
116126
}
117127

118128
/// A fungible multi-currency system where funds can be reserved from the user.
@@ -250,7 +260,7 @@ pub trait BasicLockableCurrency<AccountId>: BasicCurrency<AccountId> {
250260
/// than a user has.
251261
///
252262
/// If the lock `lock_id` already exists, this will update it.
253-
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance);
263+
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) -> DispatchResult;
254264

255265
/// Changes a balance lock (selected by `lock_id`) so that it becomes less
256266
/// liquid in all parameters or creates a new one if it does not exist.
@@ -260,10 +270,10 @@ pub trait BasicLockableCurrency<AccountId>: BasicCurrency<AccountId> {
260270
/// while `set_lock` replaces the lock with the new parameters. As in,
261271
/// `extend_lock` will set:
262272
/// - maximum `amount`
263-
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance);
273+
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) -> DispatchResult;
264274

265275
/// Remove an existing lock.
266-
fn remove_lock(lock_id: LockIdentifier, who: &AccountId);
276+
fn remove_lock(lock_id: LockIdentifier, who: &AccountId) -> DispatchResult;
267277
}
268278

269279
/// A fungible single currency system where funds can be reserved from the user.

0 commit comments

Comments
 (0)