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

Emit events related to asset mutations #14099

Merged
merged 4 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -52,9 +52,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 @@ -65,11 +74,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 @@ -1133,6 +1157,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