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

Commit

Permalink
Emit events related to asset mutations (#14099) (#14102)
Browse files Browse the repository at this point in the history
* Emit events related to asset mutations

* Fixes

* Improve unit tests

* cargo fmt

Co-authored-by: Keith Yeung <[email protected]>
  • Loading branch information
chevdor and KiChjang authored May 10, 2023
1 parent bca8a29 commit 3bb3882
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
33 changes: 32 additions & 1 deletion frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,38 @@ impl<T: Config<I>, I: 'static> fungibles::Inspect<<T as SystemConfig>::AccountId
}
}

impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {}
impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
fn done_mint_into(
asset_id: Self::AssetId,
beneficiary: &<T as SystemConfig>::AccountId,
amount: Self::Balance,
) {
Self::deposit_event(Event::Issued { asset_id, owner: beneficiary.clone(), amount })
}

fn done_burn_from(
asset_id: Self::AssetId,
target: &<T as SystemConfig>::AccountId,
balance: Self::Balance,
) {
Self::deposit_event(Event::Burned { asset_id, owner: target.clone(), balance });
}

fn done_transfer(
asset_id: Self::AssetId,
source: &<T as SystemConfig>::AccountId,
dest: &<T as SystemConfig>::AccountId,
amount: Self::Balance,
) {
Self::deposit_event(Event::Transferred {
asset_id,
from: source.clone(),
to: dest.clone(),
amount,
});
}
}

impl<T: Config<I>, I: 'static> fungibles::Balanced<<T as SystemConfig>::AccountId>
for Pallet<T, I>
{
Expand Down
29 changes: 29 additions & 0 deletions frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ fn transfer_should_never_burn() {

while System::inc_consumers(&2).is_ok() {}
let _ = System::dec_consumers(&2);
let _ = System::dec_consumers(&2);
// Exactly one consumer ref remaining.
assert_eq!(System::consumers(&2), 1);

let _ = <Assets as fungibles::Mutate<_>>::transfer(0, &1, &2, 50, Protect);
System::assert_has_event(RuntimeEvent::Assets(crate::Event::Transferred {
asset_id: 0,
from: 1,
to: 2,
amount: 50,
}));
assert_eq!(Assets::balance(0, 1), 50);
assert_eq!(Assets::balance(0, 1) + Assets::balance(0, 2), 100);
});
}
Expand All @@ -59,11 +68,26 @@ fn basic_minting_should_work() {
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, true, 1));
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 1, 1, true, 1));
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
asset_id: 0,
owner: 1,
amount: 100,
}));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 2, 100));
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
asset_id: 0,
owner: 2,
amount: 100,
}));
assert_eq!(Assets::balance(0, 2), 100);
assert_eq!(asset_ids(), vec![0, 1, 999]);
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 1, 1, 100));
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
asset_id: 1,
owner: 1,
amount: 100,
}));
assert_eq!(Assets::account_balances(1), vec![(0, 100), (999, 100), (1, 100)]);
});
}
Expand Down Expand Up @@ -786,6 +810,11 @@ fn burning_asset_balance_with_positive_balance_should_work() {
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
assert_eq!(Assets::balance(0, 1), 100);
assert_ok!(Assets::burn(RuntimeOrigin::signed(1), 0, 1, u64::MAX));
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Burned {
asset_id: 0,
owner: 1,
balance: 100,
}));
assert_eq!(Assets::balance(0, 1), 0);
});
}
Expand Down

0 comments on commit 3bb3882

Please sign in to comment.