Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 911795a

Browse files
committed
Builds & tests.
1 parent 944c129 commit 911795a

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

frame/assets/src/lib.rs

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@
133133
// Ensure we're `no_std` when compiling for Wasm.
134134
#![cfg_attr(not(feature = "std"), no_std)]
135135

136-
use sp_std::{fmt::Debug, ops::Add, iter::once};
136+
use sp_std::{fmt::Debug};
137137
use sp_runtime::{RuntimeDebug, traits::{
138-
Member, AtLeast32Bit, AtLeast32BitUnsigned, Zero, StaticLookup, One, Saturating,
138+
Member, AtLeast32Bit, AtLeast32BitUnsigned, Zero, StaticLookup, One, Saturating, CheckedSub
139139
}};
140140
use codec::{Encode, Decode};
141141
use frame_support::{Parameter, decl_module, decl_event, decl_storage, decl_error, ensure,
@@ -234,15 +234,15 @@ decl_event! {
234234
Transferred(AssetId, AccountId, AccountId, Balance),
235235
/// Some assets were destroyed. \[asset_id, owner, balance\]
236236
Burned(AssetId, AccountId, Balance),
237-
/// The management team changed \[issuer, admin, freezer\]
238-
TeamChanged(AssetId, AccountId, AccountId, AccountId)
239-
/// The owner changed \[owner\]
240-
OwnerChanged(AssetId, AccountId)
237+
/// The management team changed \[asset_id, issuer, admin, freezer\]
238+
TeamChanged(AssetId, AccountId, AccountId, AccountId),
239+
/// The owner changed \[asset_id, owner\]
240+
OwnerChanged(AssetId, AccountId),
241241
/// Some assets was transferred by an admin. \[asset_id, from, to, amount\]
242242
ForceTransferred(AssetId, AccountId, AccountId, Balance),
243-
/// Some account `who` was frozen. \[who\]
243+
/// Some account `who` was frozen. \[asset_id, who\]
244244
Frozen(AssetId, AccountId),
245-
/// Some account `who` was thawed. \[who\]
245+
/// Some account `who` was thawed. \[asset_id, who\]
246246
Thawed(AssetId, AccountId),
247247
}
248248
}
@@ -315,7 +315,7 @@ decl_module! {
315315
ensure!(&origin == &d.issuer, Error::<T>::NoPermission);
316316
d.supply = d.supply.saturating_add(amount);
317317
Account::<T>::mutate((id, &beneficiary), |t|
318-
t.balance = t.balance.satuating_add(amount)
318+
t.balance = t.balance.saturating_add(amount)
319319
);
320320

321321
Self::deposit_event(RawEvent::Issued(id, beneficiary, amount));
@@ -340,18 +340,19 @@ decl_module! {
340340
let origin = (id, ensure_signed(origin)?);
341341
ensure!(!amount.is_zero(), Error::<T>::AmountZero);
342342

343-
let origin_account = Accounts::<T>::get(&origin);
344-
ensure!(origin_account.balance >= amount, Error::<T>::BalanceLow);
343+
let mut origin_account = Account::<T>::get(&origin);
345344
ensure!(!origin_account.is_frozen, Error::<T>::Frozen);
345+
origin_account.balance = origin_account.balance.checked_sub(&amount)
346+
.ok_or(Error::<T>::BalanceLow)?;
346347

347348
let dest = (id, T::Lookup::lookup(target)?);
348349

349-
if origin_account.balance == amount {
350-
Balance::<T>::remove(&origin);
350+
if origin_account.balance.is_zero() {
351+
Account::<T>::remove(&origin);
351352
} else {
352-
Balances::<T>::insert(&origin, origin_account.balance - amount);
353+
Account::<T>::insert(&origin, origin_account);
353354
}
354-
Balances::<T>::mutate(&dest, |a| a.balance = a.balance.saturating_add(amount));
355+
Account::<T>::mutate(&dest, |a| a.balance = a.balance.saturating_add(amount));
355356

356357
Self::deposit_event(RawEvent::Transferred(id, origin.1, dest.1, amount));
357358
}
@@ -364,7 +365,7 @@ decl_module! {
364365
#[weight = 0]
365366
fn burn(origin,
366367
#[compact] id: T::AssetId,
367-
who: T::<T::Lookup as StaticLookup>::Source,
368+
who: <T::Lookup as StaticLookup>::Source,
368369
#[compact] amount: T::Balance
369370
) -> DispatchResult {
370371
let origin = ensure_signed(origin)?;
@@ -385,7 +386,7 @@ decl_module! {
385386
};
386387
Ok(burned)
387388
} else {
388-
Err(Error::<T>::BalanceZero)?;
389+
Err(Error::<T>::BalanceZero)
389390
}
390391
})?;
391392

@@ -397,7 +398,10 @@ decl_module! {
397398
}
398399

399400
#[weight = 0]
400-
fn set_owner(origin, owner: <T::Lookup as StaticLookup>::Source) {
401+
fn set_owner(origin,
402+
#[compact] id: T::AssetId,
403+
owner: <T::Lookup as StaticLookup>::Source,
404+
) -> DispatchResult {
401405
let origin = ensure_signed(origin)?;
402406
let owner = T::Lookup::lookup(owner)?;
403407

@@ -407,13 +411,14 @@ decl_module! {
407411

408412
d.owner = owner.clone();
409413

410-
Self::deposit_event(OwnerChanged(id, owner))
414+
Self::deposit_event(RawEvent::OwnerChanged(id, owner));
415+
Ok(())
411416
})
412417
}
413418

414-
415419
#[weight = 0]
416420
fn set_team(origin,
421+
#[compact] id: T::AssetId,
417422
issuer: <T::Lookup as StaticLookup>::Source,
418423
admin: <T::Lookup as StaticLookup>::Source,
419424
freezer: <T::Lookup as StaticLookup>::Source,
@@ -431,12 +436,13 @@ decl_module! {
431436
d.admin = admin.clone();
432437
d.freezer = freezer.clone();
433438

434-
Self::deposit_event(TeamChanged(id, issuer, admin, freezer))
439+
Self::deposit_event(RawEvent::TeamChanged(id, issuer, admin, freezer));
440+
Ok(())
435441
})
436442
}
437443

438444
#[weight = 0]
439-
fn freeze(origin, who: <T::Lookup as StaticLookup>::Source) {
445+
fn freeze(origin, #[compact] id: T::AssetId, who: <T::Lookup as StaticLookup>::Source) {
440446
let origin = ensure_signed(origin)?;
441447

442448
let d = Details::<T>::get(id).ok_or(Error::<T>::Unknown)?;
@@ -445,11 +451,11 @@ decl_module! {
445451
let who = (id, T::Lookup::lookup(who)?);
446452
Account::<T>::mutate(&who, |a| a.is_frozen = true);
447453

448-
Self::deposit_event(Event::<T>::Frozen(id, who));
454+
Self::deposit_event(Event::<T>::Frozen(id, who.1));
449455
}
450456

451457
#[weight = 0]
452-
fn thaw(origin, who: <T::Lookup as StaticLookup>::Source) {
458+
fn thaw(origin, #[compact] id: T::AssetId, who: <T::Lookup as StaticLookup>::Source) {
453459
let origin = ensure_signed(origin)?;
454460

455461
let d = Details::<T>::get(id).ok_or(Error::<T>::Unknown)?;
@@ -458,7 +464,7 @@ decl_module! {
458464
let who = (id, T::Lookup::lookup(who)?);
459465
Account::<T>::mutate(&who, |a| a.is_frozen = false);
460466

461-
Self::deposit_event(Event::<T>::Thawed(id, who));
467+
Self::deposit_event(Event::<T>::Thawed(id, who.1));
462468
}
463469

464470
#[weight = 0]
@@ -474,7 +480,7 @@ decl_module! {
474480
ensure!(&origin == &d.admin, Error::<T>::NoPermission);
475481

476482
let source = (id, T::Lookup::lookup(source)?);
477-
let mut source_account = Accounts::<T>::get(&source);
483+
let mut source_account = Account::<T>::get(&source);
478484
let amount = amount.min(source_account.balance);
479485

480486
ensure!(!amount.is_zero(), Error::<T>::AmountZero);
@@ -488,7 +494,7 @@ decl_module! {
488494
Account::<T>::insert(&source, source_account);
489495
}
490496

491-
Balances::<T>::mutate(&dest, |a| a.balance = a.balance.saturating_add(amount));
497+
Account::<T>::mutate(&dest, |a| a.balance = a.balance.saturating_add(amount));
492498

493499
Self::deposit_event(RawEvent::ForceTransferred(id, source.1, dest.1, amount));
494500
}
@@ -501,7 +507,7 @@ impl<T: Trait> Module<T> {
501507

502508
/// Get the asset `id` balance of `who`.
503509
pub fn balance(id: T::AssetId, who: T::AccountId) -> T::Balance {
504-
Balances::<T>::get((id, who))
510+
Account::<T>::get((id, who)).balance
505511
}
506512

507513
/// Get the total supply of an asset `id`.
@@ -608,7 +614,7 @@ mod tests {
608614
assert_eq!(Assets::balance(0, 1), 50);
609615
assert_eq!(Assets::balance(0, 2), 19);
610616
assert_eq!(Assets::balance(0, 3), 31);
611-
assert_ok!(Assets::destroy(Origin::signed(3), 0, u64::max_value()));
617+
assert_ok!(Assets::burn(Origin::signed(1), 0, 3, u64::max_value()));
612618
assert_eq!(Assets::total_supply(0), 69);
613619
});
614620
}
@@ -634,7 +640,7 @@ mod tests {
634640
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 50));
635641
assert_eq!(Assets::balance(0, 1), 50);
636642
assert_eq!(Assets::balance(0, 2), 50);
637-
assert_ok!(Assets::destroy(Origin::signed(1), 0));
643+
assert_ok!(Assets::burn(Origin::signed(1), 0, 1, u64::max_value()));
638644
assert_eq!(Assets::balance(0, 1), 0);
639645
assert_noop!(Assets::transfer(Origin::signed(1), 0, 1, 50), Error::<Test>::BalanceLow);
640646
});
@@ -666,7 +672,7 @@ mod tests {
666672
assert_ok!(Assets::create(Origin::signed(1), 1));
667673
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
668674
assert_eq!(Assets::balance(0, 1), 100);
669-
assert_ok!(Assets::burn(Origin::signed(1), 0, u64::max_value()));
675+
assert_ok!(Assets::burn(Origin::signed(1), 0, 1, u64::max_value()));
670676
});
671677
}
672678

@@ -676,7 +682,7 @@ mod tests {
676682
assert_ok!(Assets::create(Origin::signed(1), 1));
677683
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
678684
assert_eq!(Assets::balance(0, 2), 0);
679-
assert_noop!(Assets::burn(Origin::signed(2), 0, u64::max_value()), Error::<Test>::BalanceZero);
685+
assert_noop!(Assets::burn(Origin::signed(1), 0, 2, u64::max_value()), Error::<Test>::BalanceZero);
680686
});
681687
}
682688
}

0 commit comments

Comments
 (0)