Skip to content

Commit

Permalink
feat: events to all features + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Aug 6, 2024
1 parent 95bf679 commit 3a777d5
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 131 deletions.
145 changes: 91 additions & 54 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Event emitted when allowance by `owner` to `spender` changes.
Approval {
/// Token ID.
id: AssetIdOf<T>,
/// Account providing allowance.
owner: AccountIdOf<T>,
/// Allowance beneficiary.
Expand All @@ -109,6 +111,8 @@ pub mod pallet {
},
/// Event emitted when transfer of tokens occurs.
Transfer {
/// Token ID.
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 All @@ -125,7 +129,9 @@ pub mod pallet {
/// Admin of the token created.
admin: AccountIdOf<T>,
},
/// Event emitted when new token metadata is set.
/// Event emitted when a token is in the process of being destroyed.
StartDestroy { id: AssetIdOf<T> },
/// Event emitted when new metadata is set for a token.
SetMetadata {
/// Token ID.
id: AssetIdOf<T>,
Expand All @@ -136,6 +142,8 @@ pub mod pallet {
/// Token decimals.
decimals: u8,
},
/// Event emitted when metadata is cleared for a token.
ClearMetadata { id: AssetIdOf<T> },
}

#[pallet::call]
Expand All @@ -157,12 +165,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 @@ -185,12 +193,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 All @@ -208,40 +216,45 @@ pub mod pallet {
spender: AccountIdOf<T>,
value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
let current_allowance = AssetsOf::<T>::allowance(id.clone(), &owner, &spender);
let spender_unlookup = T::Lookup::unlookup(spender.clone());
let id: AssetIdParameterOf<T> = id.into();
let owner = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
let current_allowance = AssetsOf::<T>::allowance(id.clone(), &owner, &spender);
let spender_unlookup = T::Lookup::unlookup(spender.clone());
let id_param: AssetIdParameterOf<T> = id.clone().into();

let return_weight = match value.cmp(&current_allowance) {
// If the new value is equal to the current allowance, do nothing.
Equal => Self::weight_approve(0, 0),
// If the new value is greater than the current allowance, approve the difference
// because `approve_transfer` works additively (see `pallet-assets`).
Greater => {
AssetsOf::<T>::approve_transfer(
origin,
id,
spender_unlookup,
value.saturating_sub(current_allowance),
)
.map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?;
Self::weight_approve(1, 0)
},
// If the new value is less than the current allowance, cancel the approval and
// set the new value.
Less => {
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender_unlookup.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 1)).into());
}
AssetsOf::<T>::approve_transfer(origin, id, spender_unlookup, value)?;
Self::weight_approve(1, 1)
},
}
Self::deposit_event(Event::Approval { owner, spender, value });
let return_weight = match value.cmp(&current_allowance) {
// If the new value is equal to the current allowance, do nothing.
Equal => Self::weight_approve(0, 0),
// If the new value is greater than the current allowance, approve the difference
// because `approve_transfer` works additively (see `pallet-assets`).
Greater => {
AssetsOf::<T>::approve_transfer(
origin,
id_param,
spender_unlookup,
value.saturating_sub(current_allowance),
)
.map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?;
Self::weight_approve(1, 0)
},
// If the new value is less than the current allowance, cancel the approval and
// set the new value.
Less => {
AssetsOf::<T>::cancel_approval(
origin.clone(),
id_param.clone(),
spender_unlookup.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_param, spender_unlookup, value)?;
Self::weight_approve(1, 1)
}
},
};
Self::deposit_event(Event::Approval { id, owner, spender, value });
Ok(Some(return_weight).into())
}

Expand All @@ -266,8 +279,8 @@ pub mod pallet {
T::Lookup::unlookup(spender.clone()),
value,
)?;
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(())
}

Expand All @@ -289,22 +302,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_unlookup = 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 aproval and set the new value if `new_allowance` is more than zero.
AssetsOf::<T>::cancel_approval(origin.clone(), id.clone(), spender_unlookup.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
// Cancel the approval and set the new value if `new_allowance` is more than zero.
AssetsOf::<T>::cancel_approval(
origin.clone(),
id_param.clone(),
spender_unlookup.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_unlookup, new_allowance)?;
AssetsOf::<T>::approve_transfer(origin, id_param, spender_unlookup, new_allowance)?;
Self::weight_approve(1, 1)
};
Self::deposit_event(Event::Approval { owner, spender, value });
Self::deposit_event(Event::Approval { id, owner, spender, value: new_allowance });
Ok(Some(weight).into())
}

Expand All @@ -324,7 +341,7 @@ pub mod pallet {
) -> DispatchResult {
AssetsOf::<T>::create(
origin.clone(),
id.into(),
id.clone().into(),
T::Lookup::unlookup(admin.clone()),
min_balance,
)?;
Expand All @@ -340,7 +357,9 @@ pub mod pallet {
#[pallet::call_index(12)]
#[pallet::weight(AssetsWeightInfoOf::<T>::start_destroy())]
pub fn start_destroy(origin: OriginFor<T>, id: AssetIdOf<T>) -> DispatchResult {
AssetsOf::<T>::start_destroy(origin, id.into())
AssetsOf::<T>::start_destroy(origin, id.clone().into())?;
Self::deposit_event(Event::StartDestroy { id });
Ok(())
}

/// Set the metadata for a token with a given asset ID.
Expand All @@ -361,7 +380,13 @@ pub mod pallet {
symbol: Vec<u8>,
decimals: u8,
) -> DispatchResult {
AssetsOf::<T>::set_metadata(origin, id.into(), name.clone(), symbol.clone(), decimals)?;
AssetsOf::<T>::set_metadata(
origin,
id.clone().into(),
name.clone(),
symbol.clone(),
decimals,
)?;
Self::deposit_event(Event::SetMetadata { id, name, symbol, decimals });
Ok(())
}
Expand All @@ -373,7 +398,9 @@ pub mod pallet {
#[pallet::call_index(17)]
#[pallet::weight(AssetsWeightInfoOf::<T>::clear_metadata())]
pub fn clear_metadata(origin: OriginFor<T>, id: AssetIdOf<T>) -> DispatchResult {
AssetsOf::<T>::clear_metadata(origin, id.into())
AssetsOf::<T>::clear_metadata(origin, id.clone().into())?;
Self::deposit_event(Event::ClearMetadata { id });
Ok(())
}

/// Creates `value` amount of tokens and assigns them to `account`, increasing the total supply.
Expand All @@ -390,8 +417,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 @@ -409,8 +441,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
Loading

0 comments on commit 3a777d5

Please sign in to comment.