From 5fa1e14469637104360be0113f81f956301231f1 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Mon, 01 Nov 2021 13:50:44 +0000 Subject: [PATCH] fix: serde rpc --- --- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -63,7 +63,7 @@ account: CrossAccountId, token: TokenId, at: Option, - ) -> Result; + ) -> Result; #[rpc(name = "nft_allowance")] fn allowance( &self, @@ -72,7 +72,7 @@ spender: CrossAccountId, token: TokenId, at: Option, - ) -> Result; + ) -> Result; #[rpc(name = "nft_adminlist")] fn adminlist(&self, collection: CollectionId, at: Option) -> Result>; @@ -109,7 +109,7 @@ } macro_rules! pass_method { - ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => { + ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => { fn $method_name( &self, $( @@ -124,7 +124,9 @@ code: ErrorCode::ServerError(Error::RuntimeError.into()), message: "Unable to query".into(), data: Some(format!("{:?}", e).into()), - }) + }) $( + .map($mapper) + )? } }; } @@ -145,8 +147,8 @@ pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec); pass_method!(collection_tokens(collection: CollectionId) -> u32); pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32); - pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128); - pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128); + pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string()); + pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string()); pass_method!(adminlist(collection: CollectionId) -> Vec); pass_method!(allowlist(collection: CollectionId) -> Vec); --- a/pallets/common/Cargo.toml +++ b/pallets/common/Cargo.toml @@ -21,6 +21,9 @@ pallet-evm = { default-features = false, version = "6.0.0-dev", git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.10" } serde = { version = "1.0.130", default-features = false } +scale-info = { version = "1.0.0", default-features = false, features = [ + "derive", +] } [features] default = ["std"] --- a/pallets/common/src/account.rs +++ b/pallets/common/src/account.rs @@ -20,7 +20,14 @@ fn from_eth(account: H160) -> Self; } -#[derive(Eq, Serialize, Deserialize)] +#[derive(Encode, Decode, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +enum BasicCrossAccountIdRepr { + Substrate(AccountId), + Ethereum(H160), +} + +#[derive(Eq)] pub struct BasicCrossAccountId { /// If true - then ethereum is canonical encoding from_ethereum: bool, @@ -84,12 +91,7 @@ } impl Encode for BasicCrossAccountId { fn encode(&self) -> Vec { - let as_result = if !self.from_ethereum { - Ok(self.substrate.clone()) - } else { - Err(self.ethereum) - }; - as_result.encode() + BasicCrossAccountIdRepr::from(self.clone()).encode() } } impl EncodeLike for BasicCrossAccountId {} @@ -98,12 +100,34 @@ where I: codec::Input, { - Ok(match >::decode(input)? { - Ok(s) => Self::from_sub(s), - Err(e) => Self::from_eth(e), - }) + Ok(BasicCrossAccountIdRepr::decode(input)?.into()) } } +impl Serialize for BasicCrossAccountId +where + T: Config, + T::AccountId: Serialize, +{ + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let repr = BasicCrossAccountIdRepr::from(self.clone()); + (&repr).serialize(serializer) + } +} +impl<'de, T> Deserialize<'de> for BasicCrossAccountId +where + T: Config, + T::AccountId: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into()) + } +} impl CrossAccountId for BasicCrossAccountId { fn as_sub(&self) -> &T::AccountId { &self.substrate @@ -126,6 +150,23 @@ } } } +impl From> for BasicCrossAccountId { + fn from(repr: BasicCrossAccountIdRepr) -> Self { + match repr { + BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s), + BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e), + } + } +} +impl From> for BasicCrossAccountIdRepr { + fn from(v: BasicCrossAccountId) -> Self { + if v.from_ethereum { + BasicCrossAccountIdRepr::Ethereum(*v.as_eth()) + } else { + BasicCrossAccountIdRepr::Substrate(v.as_sub().clone()) + } + } +} pub trait EvmBackwardsAddressMapping { fn from_account_id(account_id: AccountId) -> H160; -- gitstuff