Skip to content

Commit

Permalink
refactor: easier review
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jun 20, 2024
1 parent e5fd747 commit e1f67a8
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 27 deletions.
15 changes: 8 additions & 7 deletions pop-api/examples/fungibles/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mod fungibles {
impl Fungibles {
#[ink(constructor, payable)]
pub fn new() -> Self {
ink::env::debug_println!("PopApiAssetsExample::new");
ink::env::debug_println!("PopApiFungiblesExample::new");
Default::default()
}

Expand Down Expand Up @@ -62,7 +62,7 @@ mod fungibles {
#[ink(message)]
pub fn transfer(&self, id: AssetId, to: AccountId32, value: Balance) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::transfer: id: {:?}, to: {:?} value: {:?}",
"PopApiFungiblesExample::transfer: id: {:?}, to: {:?} value: {:?}",
id,
to,
value,
Expand All @@ -80,11 +80,12 @@ mod fungibles {
from: Option<AccountId32>,
to: Option<AccountId32>,
value: Balance,
// Size needs to be known at compile time or ink's `Vec`
// In the standard a `[u8]`, but the size needs to be known at compile time ink's `Vec`
// has to be used.
data: Vec<u8>,
) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}",
"PopApiFungiblesExample::transfer_from: id: {:?}, from: {:?}, to: {:?} value: {:?}",
id,
from,
to,
Expand Down Expand Up @@ -113,7 +114,7 @@ mod fungibles {
#[ink(message)]
pub fn create(&self, id: AssetId, admin: AccountId32, min_balance: Balance) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::create: id: {:?} admin: {:?} min_balance: {:?}",
"PopApiFungiblesExample::create: id: {:?} admin: {:?} min_balance: {:?}",
id,
admin,
min_balance,
Expand All @@ -132,7 +133,7 @@ mod fungibles {
decimals: u8,
) -> Result<()> {
ink::env::debug_println!(
"PopApiAssetsExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
"PopApiFungiblesExample::set_metadata: id: {:?} name: {:?} symbol: {:?}, decimals: {:?}",
id,
name,
symbol,
Expand All @@ -155,7 +156,7 @@ mod fungibles {

#[ink::test]
fn default_works() {
PopApiAssetsExample::new();
PopApiFungiblesExample::new();
}
}
}
2 changes: 1 addition & 1 deletion pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub mod error;
pub mod primitives;
pub mod v0;

// type AccountId = <Environment as ink::env::Environment>::AccountId;
type AccountId = AccountId32;
// TODO: do the same as above and check expanded code.
type Balance = <Environment as ink::env::Environment>::Balance;
type BlockNumber = <Environment as ink::env::Environment>::BlockNumber;
type StringLimit = u32;
Expand Down
42 changes: 42 additions & 0 deletions pop-api/src/v0/nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,4 +828,46 @@ mod types {
/// Tokens will be received.
Receive,
}

macro_rules! impl_codec_bitflags {
($wrapper:ty, $size:ty, $bitflag_enum:ty) => {
impl MaxEncodedLen for $wrapper {
fn max_encoded_len() -> usize {
<$size>::max_encoded_len()
}
}
impl Encode for $wrapper {
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
self.0.bits().using_encoded(f)
}
}
impl EncodeLike for $wrapper {}
impl Decode for $wrapper {
fn decode<I: scale::Input>(
input: &mut I,
) -> core::result::Result<Self, scale::Error> {
let field = <$size>::decode(input)?;
Ok(Self(BitFlags::from_bits(field as $size).map_err(|_| "invalid value")?))
}
}

impl TypeInfo for $wrapper {
type Identity = Self;

fn type_info() -> Type {
Type::builder()
.path(Path::new("BitFlags", module_path!()))
.type_params(vec![TypeParameter::new(
"T",
Some(meta_type::<$bitflag_enum>()),
)])
.composite(
Fields::unnamed()
.field(|f| f.ty::<$size>().type_name(stringify!($bitflag_enum))),
)
}
}
};
}
pub(crate) use impl_codec_bitflags;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ use crate::{
RuntimeOrigin, UNIT,
};

#[cfg(test)]
mod tests;

const LOG_TARGET: &str = "pop-api::extension";

type ContractSchedule<T> = <T as pallet_contracts::Config>::Schedule;

#[derive(Default)]
pub struct PopApiExtension;

// TODO: check removal or simplification of trait bounds.
impl<T> ChainExtension<T> for PopApiExtension
where
T: pallet_contracts::Config
Expand Down Expand Up @@ -75,7 +73,7 @@ where
}
}

fn convert_to_status_code(error: DispatchError) -> u32 {
pub(crate) fn convert_to_status_code(error: DispatchError) -> u32 {
match error {
_ => {
let mut encoded_error = error.encode();
Expand Down
2 changes: 2 additions & 0 deletions runtime/devnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod extensions;
mod weights;
// Public due to integration tests crate.
pub mod config;
#[cfg(test)]
mod tests;

use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases;
use polkadot_runtime_common::xcm_sender::NoPriceForMessageDelivery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
use super::*;

use pop_api::{
v0::{
assets::use_cases::fungibles::FungiblesError::*,
dispatch_error::{ArithmeticError::*, TokenError::*},
},
PopApiError::*,
error::{ArithmeticError::*, PopApiError::*, TokenError::*},
v0::assets::use_cases::fungibles::FungiblesError::*,
};

const ASSET_ID: AssetId = 1;
Expand Down Expand Up @@ -260,6 +257,10 @@ fn asset_exists_works() {
// - reserve(): Overflow, LiquidityRestrictions; frozen
// - Callback
// - StorageDepositLimitExhausted
// todo: errors:
// - TokenErrors
// - Arithmetic
// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125
#[test]
#[ignore]
fn create_works() {
Expand Down Expand Up @@ -322,10 +323,6 @@ fn set_metadata_works() {
});
}

// todo: errors:
// - TokenErrors
// - Arithmetic
// - https://github.com/paritytech/polkadot-sdk/blob/3977f389cce4a00fd7100f95262e0563622b9aa4/substrate/frame/assets/src/functions.rs#L125
#[test]
#[ignore]
fn transfer_from_mint_works() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#![cfg(test)]

use super::*;
use crate::{config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, System};
use crate::{
config::assets::TrustBackedAssetsInstance, Assets, Contracts, Runtime, RuntimeOrigin, System,
Weight, UNIT,
};
use codec::{Decode, Encode};
use frame_support::traits::fungibles::{approvals::Inspect as ApprovalInspect, Inspect};
use frame_system::Config;
use pallet_contracts::{Code, CollectEvents, Determinism, ExecReturnValue};
use pop_api::error::{ArithmeticError, PopApiError, TokenError, TransactionalError};
use sp_runtime::{traits::Hash, AccountId32, BuildStorage};
use sp_runtime::{traits::Hash, AccountId32, BuildStorage, DispatchError};

mod local_fungibles;

Expand Down Expand Up @@ -124,7 +128,7 @@ fn encoding_decoding_dispatch_error() {
let index = get_pallet_index::<Runtime>();
let mut error =
pallet_assets::Error::NotFrozen::<Runtime, TrustBackedAssetsInstance>.encode();
error.resize(MAX_MODULE_ERROR_ENCODED_SIZE, 0);
error.resize(sp_runtime::MAX_MODULE_ERROR_ENCODED_SIZE, 0);
let message = None;
let error = DispatchError::Module(ModuleError {
index,
Expand Down Expand Up @@ -251,8 +255,8 @@ fn dispatch_error_to_status_code_to_pop_api_error_works() {
];
for (error, pop_api_error) in test_cases {
// Show that the encoding and decoding of the PopApiError <> u32 (status code) works.
let status_code = convert_to_status_code(error);
let error = pop_api::convert_to_pop_api_error(status_code);
let status_code = crate::extensions::convert_to_status_code(error);
let error = pop_api::error::convert_to_pop_api_error(status_code);
assert_eq!(pop_api_error, error,);
}
}

0 comments on commit e1f67a8

Please sign in to comment.