Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assets Events for Deposited and Withdrawn #4312

Merged
merged 12 commits into from
Apr 30, 2024
24 changes: 24 additions & 0 deletions substrate/frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ impl<T: Config<I>, I: 'static> fungibles::Balanced<<T as SystemConfig>::AccountI
{
type OnDropCredit = fungibles::DecreaseIssuance<T::AccountId, Self>;
type OnDropDebt = fungibles::IncreaseIssuance<T::AccountId, Self>;

fn done_deposit(
asset_id: Self::AssetId,
who: &<T as SystemConfig>::AccountId,
amount: Self::Balance,
) {
Self::deposit_event(Event::Deposit {
asset_id,
who: who.clone(),
amount
})
}

fn done_withdraw(
asset_id: Self::AssetId,
who: &<T as SystemConfig>::AccountId,
amount: Self::Balance,
) {
Self::deposit_event(Event::Withdraw {
asset_id,
who: who.clone(),
amount
})
}
}

impl<T: Config<I>, I: 'static> fungibles::Unbalanced<T::AccountId> for Pallet<T, I> {
Expand Down
4 changes: 4 additions & 0 deletions substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,10 @@ pub mod pallet {
Created { asset_id: T::AssetId, creator: T::AccountId, owner: T::AccountId },
/// Some assets were issued.
Issued { asset_id: T::AssetId, owner: T::AccountId, amount: T::Balance },
/// Some assets were deposited (e.g. for transaction fees).
Deposit { asset_id: T::AssetId, who: T::AccountId, amount: T::Balance },
/// Some assets were withdrawn from the account (e.g. for transaction fees).
Withdraw { asset_id: T::AssetId, who: T::AccountId, amount: T::Balance },
pandres95 marked this conversation as resolved.
Show resolved Hide resolved
/// Some assets were transferred.
Transferred {
asset_id: T::AssetId,
Expand Down
12 changes: 12 additions & 0 deletions substrate/frame/assets/src/tests/sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ fn deposit_from_set_types_works() {
assert_eq!(First::<Assets>::balance((), &account2), 50);
assert_eq!(First::<Assets>::total_issuance(()), 100);

System::assert_has_event(RuntimeEvent::Assets(crate::Event::Deposit {
asset_id: asset1,
who: account2,
amount: 50
}));

assert_eq!(imb.peek(), 50);

let (imb1, imb2) = imb.split(30);
Expand Down Expand Up @@ -336,6 +342,12 @@ fn withdraw_from_set_types_works() {
assert_eq!(First::<Assets>::balance((), &account2), 50);
assert_eq!(First::<Assets>::total_issuance(()), 200);

System::assert_has_event(RuntimeEvent::Assets(crate::Event::Withdraw {
asset_id: asset1,
who: account2,
amount: 50
}));

assert_eq!(imb.peek(), 50);
drop(imb);
assert_eq!(First::<Assets>::total_issuance(()), 150);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ fn transaction_payment_in_asset_possible() {
.base_weight(Weight::from_parts(base_weight, 0))
.build()
.execute_with(|| {
System::set_block_number(1);

// create the asset
let asset_id = 1;
let min_balance = 2;
Expand Down Expand Up @@ -246,6 +248,12 @@ fn transaction_payment_in_asset_possible() {
// check that fee was charged in the given asset
assert_eq!(Assets::balance(asset_id, caller), balance - fee_in_asset);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Withdraw {
asset_id,
who: caller,
amount: fee_in_asset
}));

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(WEIGHT_5), // estimated tx weight
Expand Down Expand Up @@ -385,6 +393,8 @@ fn asset_transaction_payment_with_tip_and_refund() {
.base_weight(Weight::from_parts(base_weight, 0))
.build()
.execute_with(|| {
System::set_block_number(1);

// create the asset
let asset_id = 1;
let min_balance = 2;
Expand Down Expand Up @@ -434,6 +444,12 @@ fn asset_transaction_payment_with_tip_and_refund() {
)
.unwrap();

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Withdraw {
asset_id,
who: caller,
amount: fee_in_asset
}));

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(WEIGHT_100),
Expand All @@ -451,6 +467,12 @@ fn asset_transaction_payment_with_tip_and_refund() {
balance - fee_in_asset + expected_token_refund
);
assert_eq!(Balances::free_balance(caller), 20 * balance_factor);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Deposit {
asset_id,
who: caller,
amount: expected_token_refund
}));
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ fn transaction_payment_in_asset_possible() {
.base_weight(Weight::from_parts(base_weight, 0))
.build()
.execute_with(|| {
System::set_block_number(1);

// create the asset
let asset_id = 1;
let min_balance = 2;
Expand Down Expand Up @@ -188,6 +190,12 @@ fn transaction_payment_in_asset_possible() {
assert_eq!(Assets::balance(asset_id, caller), balance - fee);
assert_eq!(Assets::balance(asset_id, BLOCK_AUTHOR), 0);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Withdraw {
asset_id,
who: caller,
amount: fee
}));

assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(Weight::from_parts(weight, 0)),
Expand All @@ -198,6 +206,12 @@ fn transaction_payment_in_asset_possible() {
assert_eq!(Assets::balance(asset_id, caller), balance - fee);
// check that the block author gets rewarded
assert_eq!(Assets::balance(asset_id, BLOCK_AUTHOR), fee);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Deposit {
asset_id,
who: BLOCK_AUTHOR,
amount: fee
}));
});
}

Expand Down Expand Up @@ -263,6 +277,8 @@ fn asset_transaction_payment_with_tip_and_refund() {
.base_weight(Weight::from_parts(base_weight, 0))
.build()
.execute_with(|| {
System::set_block_number(1);

// create the asset
let asset_id = 1;
let min_balance = 2;
Expand Down Expand Up @@ -292,6 +308,12 @@ fn asset_transaction_payment_with_tip_and_refund() {
.unwrap();
assert_eq!(Assets::balance(asset_id, caller), balance - fee_with_tip);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Withdraw {
asset_id,
who: caller,
amount: fee_with_tip
}));

let final_weight = 50;
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
Expand All @@ -304,6 +326,12 @@ fn asset_transaction_payment_with_tip_and_refund() {
fee_with_tip - (weight - final_weight) * min_balance / ExistentialDeposit::get();
assert_eq!(Assets::balance(asset_id, caller), balance - (final_fee));
assert_eq!(Assets::balance(asset_id, BLOCK_AUTHOR), final_fee);

System::assert_has_event(RuntimeEvent::Assets(pallet_assets::Event::Deposit {
asset_id,
who: caller,
amount: fee_with_tip - final_fee
}));
});
}

Expand Down
Loading