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

refactor: PSP22Error conversion & assertion #324

Merged
70 changes: 28 additions & 42 deletions pop-api/examples/fungibles/tests.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use drink::{
assert_ok,
devnet::{account_id_from_slice, AccountId, Balance, Runtime},
assert_ok, assert_runtime_err,
devnet::{
account_id_from_slice, AccountId, Balance, DrinkError, Runtime, RuntimeError::Assets,
},
error::{ArithmeticError, AssetsError::*},
session::Session,
utils::{call, last_contract_event},
AssetsAPI, TestExternalities, NO_SALT,
};
use ink::scale::Encode;
use pop_api::{
primitives::{
ArithmeticError::Overflow,
Error::{Arithmetic, Module},
TokenId,
},
primitives::TokenId,
v0::fungibles::events::{Approval, Created, Transfer},
};

Expand Down Expand Up @@ -166,9 +165,9 @@ fn transfer_fails_with_no_account() {
deploy(&mut session, "new", vec![TOKEN.to_string(), MIN_BALANCE.to_string()]).unwrap();
session.set_actor(contract.clone());
// `pallet-assets` returns `NoAccount` error.
assert_eq!(
assert_runtime_err!(
transfer(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [1, 0] }))
DrinkError::Module(Assets(NoAccount))
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down Expand Up @@ -212,9 +211,9 @@ fn transfer_fails_with_token_not_live() {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
transfer(&mut session, BOB, AMOUNT / 2),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -307,9 +306,9 @@ fn transfer_from_fails_with_token_not_live() {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
transfer_from(&mut session, ALICE, BOB, AMOUNT / 2),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -365,9 +364,9 @@ fn approve_fails_with_token_not_live(mut session: Session) {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
approve(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -437,9 +436,9 @@ fn increase_allowance_fails_with_token_not_live(mut session: Session) {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
increase_allowance(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -524,9 +523,9 @@ fn decrease_allowance_fails_with_token_not_live(mut session: Session) {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
decrease_allowance(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -615,10 +614,7 @@ fn mint_fails_with_no_permission(mut session: Session) {
assert_ok!(session.sandbox().create(&(TOKEN + 1), &BOB, MIN_BALANCE));
assert_ok!(deploy(&mut session, "existing", vec![(TOKEN + 1).to_string()]));
// `pallet-assets` returns `NoPermission` error.
assert_eq!(
mint(&mut session, BOB, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [2, 0] }))
);
assert_runtime_err!(mint(&mut session, BOB, AMOUNT), DrinkError::Module(Assets(NoPermission)));
}

#[drink::test(sandbox = Pop)]
Expand All @@ -642,7 +638,10 @@ fn mint_fails_with_arithmetic_overflow(mut session: Session) {
session.set_actor(contract.clone());
assert_ok!(mint(&mut session, ALICE, AMOUNT));
// Total supply increased by `value` exceeds maximal value of `u128` type.
assert_eq!(mint(&mut session, ALICE, u128::MAX), Err(into_psp22_custom(Arithmetic(Overflow))));
assert_runtime_err!(
mint(&mut session, ALICE, u128::MAX),
DrinkError::Raw(ArithmeticError::Overflow.into())
);
}

#[drink::test(sandbox = Pop)]
Expand All @@ -655,9 +654,9 @@ fn mint_fails_with_token_not_live(mut session: Session) {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `AssetNotLive` error.
assert_eq!(
assert_runtime_err!(
mint(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [16, 0] }))
DrinkError::Module(Assets(AssetNotLive))
);
}

Expand Down Expand Up @@ -694,10 +693,7 @@ fn burn_fails_with_no_permission(mut session: Session) {
assert_ok!(session.sandbox().mint_into(&(TOKEN + 1), &BOB, AMOUNT));
assert_ok!(deploy(&mut session, "existing", vec![(TOKEN + 1).to_string()]));
// `pallet-assets` returns `NoPermission` error.
assert_eq!(
burn(&mut session, BOB, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [2, 0] }))
);
assert_runtime_err!(burn(&mut session, BOB, AMOUNT), DrinkError::Module(Assets(NoPermission)));
}

#[drink::test(sandbox = Pop)]
Expand Down Expand Up @@ -734,9 +730,9 @@ fn burn_fails_with_token_not_live(mut session: Session) {
// Token is not live, i.e. frozen or being destroyed.
assert_ok!(session.sandbox().start_destroy(&TOKEN));
// `pallet-assets` returns `IncorrectStatus` error.
assert_eq!(
assert_runtime_err!(
burn(&mut session, ALICE, AMOUNT),
Err(into_psp22_custom(Module { index: 52, error: [17, 0] }))
DrinkError::Module(Assets(IncorrectStatus))
);
}

Expand Down Expand Up @@ -899,13 +895,3 @@ fn burn(session: &mut Session<Pop>, account: AccountId, amount: Balance) -> Resu
None,
)
}

// Convert into `PSP22Error::Custom` error type.
fn into_psp22_custom<T: Encode>(err: T) -> PSP22Error {
let mut padded_vec = err.encode().to_vec();
padded_vec.resize(4, 0);
// Convert the `Vec<u8>` to value of `StatusCode`.
let array: [u8; 4] = padded_vec.try_into().map_err(|_| "Invalid length").unwrap();
let status_code = u32::from_le_bytes(array);
PSP22Error::Custom(status_code.to_string())
}
9 changes: 9 additions & 0 deletions pop-api/src/v0/fungibles/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ pub enum PSP22Error {
SafeTransferCheckFailed(String),
}

impl From<PSP22Error> for u32 {
chungquantin marked this conversation as resolved.
Show resolved Hide resolved
fn from(value: PSP22Error) -> u32 {
match value {
PSP22Error::Custom(value) => value.parse::<u32>().unwrap(),
_ => unimplemented!(),
}
}
}

impl From<StatusCode> for PSP22Error {
/// Converts a `StatusCode` to a `PSP22Error`.
fn from(value: StatusCode) -> Self {
Expand Down
Loading