From 85f92e4ffa638ba483ec500b49261eb3c59a21d7 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Sat, 20 Jul 2024 16:45:35 +0100 Subject: [PATCH] refactor(api): decoding example --- .../integration-tests/src/local_fungibles.rs | 7 --- primitives/src/storage_keys.rs | 10 ++++ runtime/devnet/src/extensions/mod.rs | 53 ++++++++----------- runtime/devnet/src/extensions/v0/assets.rs | 40 -------------- 4 files changed, 32 insertions(+), 78 deletions(-) diff --git a/pop-api/integration-tests/src/local_fungibles.rs b/pop-api/integration-tests/src/local_fungibles.rs index 8f0384c0e..a316f6ea1 100644 --- a/pop-api/integration-tests/src/local_fungibles.rs +++ b/pop-api/integration-tests/src/local_fungibles.rs @@ -263,7 +263,6 @@ fn token_decimals_asset(asset_id: AssetId) -> u8 { /// - decrease_allowance #[test] -#[ignore] fn total_supply_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -285,7 +284,6 @@ fn total_supply_works() { } #[test] -#[ignore] fn balance_of_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -304,7 +302,6 @@ fn balance_of_works() { } #[test] -#[ignore] fn allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -329,7 +326,6 @@ fn allowance_works() { } #[test] -#[ignore] fn transfer_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -382,7 +378,6 @@ fn transfer_works() { } #[test] -#[ignore] fn transfer_from_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -435,7 +430,6 @@ fn transfer_from_works() { } #[test] -#[ignore] fn increase_allowance_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); @@ -493,7 +487,6 @@ fn increase_allowance_works() { /// - token_decimals #[test] -#[ignore] fn token_metadata_works() { new_test_ext().execute_with(|| { let _ = env_logger::try_init(); diff --git a/primitives/src/storage_keys.rs b/primitives/src/storage_keys.rs index e42dbca01..807a08e46 100644 --- a/primitives/src/storage_keys.rs +++ b/primitives/src/storage_keys.rs @@ -2,6 +2,8 @@ use super::nfts::*; use super::*; +// This should be moved to the runtime, it is no longer required in primitives if we are just using +// a chainextensionmethod call where we encode the first four bytes to channel the request #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum RuntimeStateKeys { #[cfg(feature = "cross-chain")] @@ -42,15 +44,23 @@ pub enum NftsKeys { CollectionAttribute(CollectionId, BoundedVec), } +// This should be moved to the runtime, it is no longer required in primitives if we are just using +// a chainextensionmethod call where we encode the first four bytes to channel the request /// The required input for state queries in pallet assets. #[cfg(feature = "assets")] #[derive(Encode, Decode, Debug, MaxEncodedLen)] pub enum AssetsKeys { + #[codec(index = 0)] TotalSupply(AssetId), + #[codec(index = 1)] BalanceOf(AssetId, AccountId), + #[codec(index = 2)] Allowance(AssetId, AccountId, AccountId), + #[codec(index = 3)] TokenName(AssetId), + #[codec(index = 4)] TokenSymbol(AssetId), + #[codec(index = 5)] TokenDecimals(AssetId), // AssetExists(AssetId), } diff --git a/runtime/devnet/src/extensions/mod.rs b/runtime/devnet/src/extensions/mod.rs index d2bd63e77..b81fd00f8 100644 --- a/runtime/devnet/src/extensions/mod.rs +++ b/runtime/devnet/src/extensions/mod.rs @@ -180,21 +180,6 @@ fn construct_call( } } -fn construct_key( - version: u8, - pallet_index: u8, - call_index: u8, - params: Vec, -) -> Result { - match pallet_index { - 52 => { - let key = versioned_construct_assets_key(version, call_index, params)?; - Ok(RuntimeStateKeys::Assets(key)) - }, - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - fn versioned_construct_assets_call( version: u8, call_index: u8, @@ -206,23 +191,12 @@ fn versioned_construct_assets_call( } } -fn versioned_construct_assets_key( - version: u8, - call_index: u8, - params: Vec, -) -> Result { - match version { - V0 => v0::assets::construct_assets_key(call_index, params), - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - fn read_state( env: &mut Environment, version: u8, pallet_index: u8, call_index: u8, - params: Vec, + mut params: Vec, ) -> Result<(), DispatchError> where T: pallet_contracts::Config @@ -233,11 +207,21 @@ where E: Ext, { const LOG_PREFIX: &str = " read_state |"; - let key = construct_key(version, pallet_index, call_index, params)?; + + // Prefix params with version, pallet, index to simplify decoding + params.insert(0, version); + params.insert(1, pallet_index); + params.insert(2, call_index); + + let key = ::decode(&mut ¶ms[..]) + .map_err(|_| DispatchError::Other("DecodingFailed"))?; + let result = match key { - RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, env), - RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, env), - RuntimeStateKeys::Assets(key) => read_assets_state::(key, env), + VersionedRuntimeStateKeys::V0(key) => match key { + RuntimeStateKeys::Nfts(key) => read_nfts_state::(key, env), + RuntimeStateKeys::ParachainSystem(key) => read_parachain_system_state::(key, env), + RuntimeStateKeys::Assets(key) => read_assets_state::(key, env), + }, }? .encode(); log::trace!( @@ -247,6 +231,13 @@ where env.write(&result, false, None) } +// Example wrapper to enable versioning of state read keys +#[derive(Encode, Decode, Debug, MaxEncodedLen)] +enum VersionedRuntimeStateKeys { + #[codec(index = 0)] + V0(RuntimeStateKeys), +} + fn send_xcm(env: &mut Environment) -> Result<(), DispatchError> where T: pallet_contracts::Config diff --git a/runtime/devnet/src/extensions/v0/assets.rs b/runtime/devnet/src/extensions/v0/assets.rs index c6b15b4ee..033236eea 100644 --- a/runtime/devnet/src/extensions/v0/assets.rs +++ b/runtime/devnet/src/extensions/v0/assets.rs @@ -6,46 +6,6 @@ use crate::extensions::{ use pop_primitives::AccountId; use sp_std::vec::Vec; -pub(crate) fn construct_assets_key( - call_index: u8, - params: Vec, -) -> Result { - match call_index { - 0 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TotalSupply(id)) - }, - 1 => { - let (id, owner) = <(AssetId, AccountId)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(BalanceOf(id, owner)) - }, - 2 => { - let (id, owner, spender) = <(AssetId, AccountId, AccountId)>::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(Allowance(id, owner, spender)) - }, - 3 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenName(id)) - }, - 4 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenSymbol(id)) - }, - 5 => { - let id = ::decode(&mut ¶ms[..]) - .map_err(|_| DispatchError::Other("DecodingFailed"))?; - Ok(TokenDecimals(id)) - }, - // other calls - _ => Err(DispatchError::Other("UnknownFunctionId")), - } -} - pub(crate) fn construct_assets_call( call_index: u8, params: Vec,