Skip to content

Commit

Permalink
fix: add asset ID to event
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Aug 13, 2024
1 parent 221f63d commit 3848faa
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
58 changes: 38 additions & 20 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Event emitted when allowance by `owner` to `spender` changes.
Approval {
/// The ID of the asset.
id: AssetIdOf<T>,
/// Account providing allowance.
owner: AccountIdOf<T>,
/// Allowance beneficiary.
Expand All @@ -110,6 +112,8 @@ pub mod pallet {
},
/// Event emitted when transfer of tokens occurs.
Transfer {
/// The ID of the asset.
id: AssetIdOf<T>,
/// Transfer sender. `None` in case of minting new tokens.
from: Option<AccountIdOf<T>>,
/// Transfer recipient. `None` in case of burning tokens.
Expand Down Expand Up @@ -138,12 +142,12 @@ pub mod pallet {
) -> DispatchResult {
AssetsOf::<T>::transfer_keep_alive(
origin.clone(),
id.into(),
id.clone().into(),
T::Lookup::unlookup(to.clone()),
value,
)?;
let from = ensure_signed(origin)?;
Self::deposit_event(Event::Transfer { from: Some(from), to: Some(to), value });
Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value });
Ok(())
}

Expand All @@ -166,12 +170,12 @@ pub mod pallet {
) -> DispatchResult {
AssetsOf::<T>::transfer_approved(
origin,
id.into(),
id.clone().into(),
T::Lookup::unlookup(from.clone()),
T::Lookup::unlookup(to.clone()),
value,
)?;
Self::deposit_event(Event::Transfer { from: Some(from), to: Some(to), value });
Self::deposit_event(Event::Transfer { id, from: Some(from), to: Some(to), value });
Ok(())
}

Expand Down Expand Up @@ -201,7 +205,7 @@ pub mod pallet {
Greater => {
AssetsOf::<T>::approve_transfer(
origin,
id.into(),
id.clone().into(),
T::Lookup::unlookup(spender.clone()),
value.saturating_sub(current_allowance),
)
Expand All @@ -211,23 +215,23 @@ pub mod pallet {
// If the new value is less than the current allowance, cancel the approval and
// set the new value.
Less => {
let id: AssetIdParameterOf<T> = id.into();
let id_param: AssetIdParameterOf<T> = id.clone().into();
let spender_source = T::Lookup::unlookup(spender.clone());
AssetsOf::<T>::cancel_approval(
origin.clone(),
id.clone(),
id_param.clone(),
spender_source.clone(),
)
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
if value.is_zero() {
Self::weight_approve(0, 1)
} else {
AssetsOf::<T>::approve_transfer(origin, id, spender_source, value)?;
AssetsOf::<T>::approve_transfer(origin, id_param, spender_source, value)?;
Self::weight_approve(1, 1)
}
},
};
Self::deposit_event(Event::Approval { owner, spender, value });
Self::deposit_event(Event::Approval { id, owner, spender, value });
Ok(Some(return_weight).into())
}

Expand All @@ -254,8 +258,8 @@ pub mod pallet {
value,
)
.map_err(|e| e.with_weight(AssetsWeightInfoOf::<T>::approve_transfer()))?;
let value = AssetsOf::<T>::allowance(id, &owner, &spender);
Self::deposit_event(Event::Approval { owner, spender, value });
let value = AssetsOf::<T>::allowance(id.clone(), &owner, &spender);
Self::deposit_event(Event::Approval { id, owner, spender, value });
Ok(().into())
}

Expand All @@ -277,22 +281,26 @@ pub mod pallet {
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
let current_allowance = AssetsOf::<T>::allowance(id.clone(), &owner, &spender);
let spender_source = T::Lookup::unlookup(spender.clone());
let id: AssetIdParameterOf<T> = id.into();
let id_param: AssetIdParameterOf<T> = id.clone().into();

if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 0)).into());
}
// Cancel the approval and set the new value if `new_allowance` is more than zero.
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender_source.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
AssetsOf::<T>::cancel_approval(
origin.clone(),
id_param.clone(),
spender_source.clone(),
)
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
let new_allowance = current_allowance.saturating_sub(value);
let weight = if new_allowance.is_zero() {
Self::weight_approve(0, 1)
} else {
AssetsOf::<T>::approve_transfer(origin, id, spender_source, new_allowance)?;
AssetsOf::<T>::approve_transfer(origin, id_param, spender_source, new_allowance)?;
Self::weight_approve(1, 1)
};
Self::deposit_event(Event::Approval { owner, spender, value: new_allowance });
Self::deposit_event(Event::Approval { id, owner, spender, value: new_allowance });
Ok(Some(weight).into())
}

Expand Down Expand Up @@ -368,8 +376,13 @@ pub mod pallet {
account: AccountIdOf<T>,
value: BalanceOf<T>,
) -> DispatchResult {
AssetsOf::<T>::mint(origin, id.into(), T::Lookup::unlookup(account.clone()), value)?;
Self::deposit_event(Event::Transfer { from: None, to: Some(account), value });
AssetsOf::<T>::mint(
origin,
id.clone().into(),
T::Lookup::unlookup(account.clone()),
value,
)?;
Self::deposit_event(Event::Transfer { id, from: None, to: Some(account), value });
Ok(())
}

Expand All @@ -387,8 +400,13 @@ pub mod pallet {
account: AccountIdOf<T>,
value: BalanceOf<T>,
) -> DispatchResult {
AssetsOf::<T>::burn(origin, id.into(), T::Lookup::unlookup(account.clone()), value)?;
Self::deposit_event(Event::Transfer { from: Some(account), to: None, value });
AssetsOf::<T>::burn(
origin,
id.clone().into(),
T::Lookup::unlookup(account.clone()),
value,
)?;
Self::deposit_event(Event::Transfer { id, from: Some(account), to: None, value });
Ok(())
}
}
Expand Down
26 changes: 13 additions & 13 deletions pallets/api/src/fungibles/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn transfer_works() {
assert_ok!(Fungibles::transfer(signed(ALICE), id, BOB, value));
let balance_after_transfer = Assets::balance(id, &BOB);
assert_eq!(balance_after_transfer, balance_before_transfer + value);
System::assert_last_event(Event::Transfer { from, to, value }.into());
System::assert_last_event(Event::Transfer { id, from, to, value }.into());
});
}

Expand All @@ -47,7 +47,7 @@ fn transfer_from_works() {
// Check that BOB receives the `value` and ALICE `amount` is spent successfully by CHARLIE.
assert_eq!(bob_balance_after_transfer, bob_balance_before_transfer + value);
assert_eq!(alice_balance_after_transfer, alice_balance_before_transfer - value);
System::assert_last_event(Event::Transfer { from, to, value }.into());
System::assert_last_event(Event::Transfer { id, from, to, value }.into());
});
}

Expand All @@ -64,23 +64,23 @@ fn approve_works() {
assert_eq!(0, Assets::allowance(id, &ALICE, &BOB));
assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value);
System::assert_last_event(Event::Approval { owner, spender, value }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value }.into());
// Approves an value to spend that is lower than the current allowance.
assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value / 2));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2);
System::assert_last_event(Event::Approval { owner, spender, value: value / 2 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into());
// Approves an value to spend that is higher than the current allowance.
assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2);
System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into());
// Approves an value to spend that is equal to the current allowance.
assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, value * 2));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2);
System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into());
// Sets allowance to zero.
assert_ok!(Fungibles::approve(signed(ALICE), id, BOB, 0));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0);
System::assert_last_event(Event::Approval { owner, spender, value: 0 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into());
});
}

Expand All @@ -96,11 +96,11 @@ fn increase_allowance_works() {
assert_eq!(0, Assets::allowance(id, &ALICE, &BOB));
assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value);
System::assert_last_event(Event::Approval { owner, spender, value }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value }.into());
// Additive.
assert_ok!(Fungibles::increase_allowance(signed(ALICE), id, BOB, value));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value * 2);
System::assert_last_event(Event::Approval { owner, spender, value: value * 2 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: value * 2 }.into());
});
}

Expand All @@ -120,11 +120,11 @@ fn decrease_allowance_works() {
// Decrease allowance successfully.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value / 2));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), value / 2);
System::assert_last_event(Event::Approval { owner, spender, value: value / 2 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: value / 2 }.into());
// Saturating if current allowance is decreased more than the owner balance.
assert_ok!(Fungibles::decrease_allowance(signed(ALICE), id, BOB, value));
assert_eq!(Assets::allowance(id, &ALICE, &BOB), 0);
System::assert_last_event(Event::Approval { owner, spender, value: 0 }.into());
System::assert_last_event(Event::Approval { id, owner, spender, value: 0 }.into());
});
}

Expand Down Expand Up @@ -197,7 +197,7 @@ fn mint_works() {
assert_ok!(Fungibles::mint(signed(ALICE), id, BOB, value));
let balance_after_mint = Assets::balance(id, &BOB);
assert_eq!(balance_after_mint, balance_before_mint + value);
System::assert_last_event(Event::Transfer { from, to, value }.into());
System::assert_last_event(Event::Transfer { id, from, to, value }.into());
});
}

Expand All @@ -214,7 +214,7 @@ fn burn_works() {
assert_ok!(Fungibles::burn(signed(ALICE), id, BOB, value));
let balance_after_burn = Assets::balance(id, &BOB);
assert_eq!(balance_after_burn, balance_before_burn - value);
System::assert_last_event(Event::Transfer { from, to, value }.into());
System::assert_last_event(Event::Transfer { id, from, to, value }.into());
});
}

Expand Down

0 comments on commit 3848faa

Please sign in to comment.