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

test: pallet api fungibles #278

Merged
merged 26 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e8a3045
refactor: encoding scheme
Daanvdplas Sep 10, 2024
b16430b
tests: pallet api extension
Daanvdplas Sep 10, 2024
2179645
docs: tests
Daanvdplas Sep 10, 2024
dad4beb
refactor: remove unnecessary
Daanvdplas Sep 10, 2024
1c6f03a
test: primitives
Daanvdplas Sep 10, 2024
a51d619
style: naming variable
Daanvdplas Sep 10, 2024
9ff9915
test: devnet runtime versioning
Daanvdplas Sep 10, 2024
a2b9e76
test: runtime devnet mod (small tests)
Daanvdplas Sep 10, 2024
dc0f90a
refactor: fallible conversion
evilrobot-01 Sep 10, 2024
6f8205e
refactor: fallible conversion (#275)
evilrobot-01 Sep 11, 2024
1547dca
fix: tests after fallible conversion feat
Daanvdplas Sep 11, 2024
2b07130
rebase
Daanvdplas Sep 11, 2024
36e8735
fix: test after fallible conversion feat
Daanvdplas Sep 11, 2024
438de63
Merge branch 'daan/tests-pallet_api_extension' into daan/test-devnet_…
Daanvdplas Sep 11, 2024
a1ee377
test: config contracts
Daanvdplas Sep 11, 2024
8949e13
test: ensure signed test coverage
Daanvdplas Sep 11, 2024
d2eeb08
test: coverage fungibles pallet
Daanvdplas Sep 11, 2024
538b19e
fix: comment
Daanvdplas Sep 11, 2024
ca1ccca
test: error case test coverage
Daanvdplas Sep 12, 2024
1060a91
test: no state set test
Daanvdplas Sep 12, 2024
98e89dd
refactor: test variables
Daanvdplas Sep 12, 2024
0ad8b36
test: encoding read result
Daanvdplas Sep 12, 2024
682e8f3
refactor: pallet fungibles mod.rs
Daanvdplas Sep 13, 2024
db382a8
refactor: split tests and test badorigin first
Daanvdplas Sep 13, 2024
497b2f7
refactor: assets helper functions
Daanvdplas Sep 13, 2024
4e7fbaa
merge daan/api
Daanvdplas Sep 13, 2024
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
53 changes: 24 additions & 29 deletions pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ mod tests;
pub mod weights;

type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
type TokenIdOf<T> = <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<
<T as frame_system::Config>::AccountId,
>>::AssetId;
type TokenIdOf<T> = <AssetsOf<T> as Inspect<<T as frame_system::Config>::AccountId>>::AssetId;
type TokenIdParameterOf<T> = <T as pallet_assets::Config<AssetsInstanceOf<T>>>::AssetIdParameter;
type AssetsOf<T> = pallet_assets::Pallet<T, AssetsInstanceOf<T>>;
type AssetsInstanceOf<T> = <T as Config>::AssetsInstance;
type AssetsWeightInfoOf<T> = <T as pallet_assets::Config<AssetsInstanceOf<T>>>::WeightInfo;
type BalanceOf<T> = <pallet_assets::Pallet<T, AssetsInstanceOf<T>> as Inspect<
<T as frame_system::Config>::AccountId,
>>::Balance;
type BalanceOf<T> = <AssetsOf<T> as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
type WeightOf<T> = <T as Config>::WeightInfo;

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -79,7 +76,7 @@ pub mod pallet {
/// Decimals for the specified token.
#[codec(index = 10)]
TokenDecimals(TokenIdOf<T>),
/// Check if a specified token exists.
/// Whether a specified token exists.
#[codec(index = 18)]
TokenExists(TokenIdOf<T>),
}
Expand Down Expand Up @@ -125,7 +122,7 @@ pub mod pallet {
pub trait Config: frame_system::Config + pallet_assets::Config<Self::AssetsInstance> {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The instance of pallet assets it is tightly coupled to.
/// The instance of pallet-assets.
type AssetsInstance;
/// Weight information for dispatchables in this pallet.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -160,7 +157,7 @@ pub mod pallet {
/// The amount transferred (or minted/burned).
value: BalanceOf<T>,
},
/// Event emitted when an token is created.
/// Event emitted when a token is created.
Create {
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
/// The token identifier.
id: TokenIdOf<T>,
Expand Down Expand Up @@ -241,12 +238,12 @@ pub mod pallet {
value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
.map_err(|e| e.with_weight(WeightOf::<T>::approve(0, 0)))?;
let current_allowance = AssetsOf::<T>::allowance(token.clone(), &owner, &spender);

let weight = match value.cmp(&current_allowance) {
// If the new value is equal to the current allowance, do nothing.
Equal => Self::weight_approve(0, 0),
Equal => WeightOf::<T>::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 => {
Expand All @@ -256,8 +253,8 @@ pub mod pallet {
T::Lookup::unlookup(spender.clone()),
value.saturating_sub(current_allowance),
)
.map_err(|e| e.with_weight(Self::weight_approve(1, 0)))?;
Self::weight_approve(1, 0)
.map_err(|e| e.with_weight(WeightOf::<T>::approve(1, 0)))?;
WeightOf::<T>::approve(1, 0)
},
// If the new value is less than the current allowance, cancel the approval and
// set the new value.
Expand All @@ -269,17 +266,17 @@ pub mod pallet {
token_param.clone(),
spender_source.clone(),
)
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
.map_err(|e| e.with_weight(WeightOf::<T>::approve(0, 1)))?;
if value.is_zero() {
Self::weight_approve(0, 1)
WeightOf::<T>::approve(0, 1)
} else {
AssetsOf::<T>::approve_transfer(
origin,
token_param,
spender_source,
value,
)?;
Self::weight_approve(1, 1)
WeightOf::<T>::approve(1, 1)
}
},
};
Expand All @@ -302,7 +299,7 @@ pub mod pallet {
value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
.map_err(|e| e.with_weight(WeightOf::<T>::approve(0, 0)))?;
AssetsOf::<T>::approve_transfer(
origin,
token.clone().into(),
Expand Down Expand Up @@ -330,32 +327,32 @@ pub mod pallet {
value: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
let owner = ensure_signed(origin.clone())
.map_err(|e| e.with_weight(Self::weight_approve(0, 0)))?;
.map_err(|e| e.with_weight(WeightOf::<T>::approve(0, 0)))?;
if value.is_zero() {
return Ok(Some(Self::weight_approve(0, 0)).into());
return Ok(Some(WeightOf::<T>::approve(0, 0)).into());
}
let current_allowance = AssetsOf::<T>::allowance(token.clone(), &owner, &spender);
let spender_source = T::Lookup::unlookup(spender.clone());
let token_param: TokenIdParameterOf<T> = token.clone().into();

// Cancel the approval and set the new value if `new_allowance` is more than zero.
// Cancel the approval and approve `new_allowance` if difference is more than zero.
AssetsOf::<T>::cancel_approval(
origin.clone(),
token_param.clone(),
spender_source.clone(),
)
.map_err(|e| e.with_weight(Self::weight_approve(0, 1)))?;
.map_err(|e| e.with_weight(WeightOf::<T>::approve(0, 1)))?;
let new_allowance = current_allowance.saturating_sub(value);
let weight = if new_allowance.is_zero() {
Self::weight_approve(0, 1)
WeightOf::<T>::approve(0, 1)
} else {
AssetsOf::<T>::approve_transfer(
origin,
token_param,
spender_source,
new_allowance,
)?;
Self::weight_approve(1, 1)
WeightOf::<T>::approve(1, 1)
};
Self::deposit_event(Event::Approval { token, owner, spender, value: new_allowance });
Ok(Some(weight).into())
Expand Down Expand Up @@ -390,6 +387,10 @@ pub mod pallet {
///
/// # Parameters
/// - `token` - The token to be destroyed.
// See `pallet-assets` documentation for more information. Related dispatchables are:
// - `destroy_accounts`
// - `destroy_approvals`
// - `finish_destroy`
#[pallet::call_index(12)]
#[pallet::weight(AssetsWeightInfoOf::<T>::start_destroy())]
pub fn start_destroy(origin: OriginFor<T>, token: TokenIdOf<T>) -> DispatchResult {
Expand Down Expand Up @@ -475,12 +476,6 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T> {
fn weight_approve(approve: u32, cancel: u32) -> Weight {
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
<T as Config>::WeightInfo::approve(cancel, approve)
}
}

impl<T: Config> crate::Read for Pallet<T> {
/// The type of read requested.
type Read = Read<T>;
Expand Down
Loading
Loading