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

Commit 87f65b5

Browse files
KiChjangchevdor
authored andcommitted
Emit events related to asset mutations (#14099)
* Emit events related to asset mutations * Fixes * Improve unit tests * cargo fmt
1 parent bca8a29 commit 87f65b5

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

frame/assets/src/impl_fungibles.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,38 @@ impl<T: Config<I>, I: 'static> fungibles::Inspect<<T as SystemConfig>::AccountId
8181
}
8282
}
8383

84-
impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {}
84+
impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
85+
fn done_mint_into(
86+
asset_id: Self::AssetId,
87+
beneficiary: &<T as SystemConfig>::AccountId,
88+
amount: Self::Balance,
89+
) {
90+
Self::deposit_event(Event::Issued { asset_id, owner: beneficiary.clone(), amount })
91+
}
92+
93+
fn done_burn_from(
94+
asset_id: Self::AssetId,
95+
target: &<T as SystemConfig>::AccountId,
96+
balance: Self::Balance,
97+
) {
98+
Self::deposit_event(Event::Burned { asset_id, owner: target.clone(), balance });
99+
}
100+
101+
fn done_transfer(
102+
asset_id: Self::AssetId,
103+
source: &<T as SystemConfig>::AccountId,
104+
dest: &<T as SystemConfig>::AccountId,
105+
amount: Self::Balance,
106+
) {
107+
Self::deposit_event(Event::Transferred {
108+
asset_id,
109+
from: source.clone(),
110+
to: dest.clone(),
111+
amount,
112+
});
113+
}
114+
}
115+
85116
impl<T: Config<I>, I: 'static> fungibles::Balanced<<T as SystemConfig>::AccountId>
86117
for Pallet<T, I>
87118
{

frame/assets/src/tests.rs

+29
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,18 @@ fn transfer_should_never_burn() {
4646

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

5153
let _ = <Assets as fungibles::Mutate<_>>::transfer(0, &1, &2, 50, Protect);
54+
System::assert_has_event(RuntimeEvent::Assets(crate::Event::Transferred {
55+
asset_id: 0,
56+
from: 1,
57+
to: 2,
58+
amount: 50,
59+
}));
60+
assert_eq!(Assets::balance(0, 1), 50);
5261
assert_eq!(Assets::balance(0, 1) + Assets::balance(0, 2), 100);
5362
});
5463
}
@@ -59,11 +68,26 @@ fn basic_minting_should_work() {
5968
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, true, 1));
6069
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 1, 1, true, 1));
6170
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
71+
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
72+
asset_id: 0,
73+
owner: 1,
74+
amount: 100,
75+
}));
6276
assert_eq!(Assets::balance(0, 1), 100);
6377
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 2, 100));
78+
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
79+
asset_id: 0,
80+
owner: 2,
81+
amount: 100,
82+
}));
6483
assert_eq!(Assets::balance(0, 2), 100);
6584
assert_eq!(asset_ids(), vec![0, 1, 999]);
6685
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 1, 1, 100));
86+
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Issued {
87+
asset_id: 1,
88+
owner: 1,
89+
amount: 100,
90+
}));
6791
assert_eq!(Assets::account_balances(1), vec![(0, 100), (999, 100), (1, 100)]);
6892
});
6993
}
@@ -786,6 +810,11 @@ fn burning_asset_balance_with_positive_balance_should_work() {
786810
assert_ok!(Assets::mint(RuntimeOrigin::signed(1), 0, 1, 100));
787811
assert_eq!(Assets::balance(0, 1), 100);
788812
assert_ok!(Assets::burn(RuntimeOrigin::signed(1), 0, 1, u64::MAX));
813+
System::assert_last_event(RuntimeEvent::Assets(crate::Event::Burned {
814+
asset_id: 0,
815+
owner: 1,
816+
balance: 100,
817+
}));
789818
assert_eq!(Assets::balance(0, 1), 0);
790819
});
791820
}

0 commit comments

Comments
 (0)