git.delta.rocks / unique-network / refs/commits / 5fa1e1446963

difftreelog

fix serde rpc

Yaroslav Bolyukin2021-11-01parent: #a02890f.patch.diff
in: master

3 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- a/client/rpc/src/lib.rs
+++ b/client/rpc/src/lib.rs
@@ -63,7 +63,7 @@
 		account: CrossAccountId,
 		token: TokenId,
 		at: Option<BlockHash>,
-	) -> Result<u128>;
+	) -> Result<String>;
 	#[rpc(name = "nft_allowance")]
 	fn allowance(
 		&self,
@@ -72,7 +72,7 @@
 		spender: CrossAccountId,
 		token: TokenId,
 		at: Option<BlockHash>,
-	) -> Result<u128>;
+	) -> Result<String>;
 
 	#[rpc(name = "nft_adminlist")]
 	fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
@@ -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<u8>);
 	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<AccountId>);
 	pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);
modifiedpallets/common/Cargo.tomldiffbeforeafterboth
--- 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"]
modifiedpallets/common/src/account.rsdiffbeforeafterboth
before · pallets/common/src/account.rs
1use crate::Config;2use codec::{Encode, EncodeLike, Decode};3use sp_core::H160;4use sp_core::crypto::AccountId32;5use core::cmp::Ordering;6use serde::{Serialize, Deserialize};7use pallet_evm::AddressMapping;8use sp_std::vec::Vec;9use sp_std::clone::Clone;1011pub trait CrossAccountId<AccountId>:12	Encode + EncodeLike + Decode + Clone + PartialEq + Ord + core::fmt::Debug + Default13// +14// Serialize + Deserialize<'static>15{16	fn as_sub(&self) -> &AccountId;17	fn as_eth(&self) -> &H160;1819	fn from_sub(account: AccountId) -> Self;20	fn from_eth(account: H160) -> Self;21}2223#[derive(Eq, Serialize, Deserialize)]24pub struct BasicCrossAccountId<T: Config> {25	/// If true - then ethereum is canonical encoding26	from_ethereum: bool,27	substrate: T::AccountId,28	ethereum: H160,29}3031impl<T: Config> Default for BasicCrossAccountId<T> {32	fn default() -> Self {33		Self::from_sub(T::AccountId::default())34	}35}3637impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {38	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {39		if self.from_ethereum {40			fmt.debug_tuple("CrossAccountId::Ethereum")41				.field(&self.ethereum)42				.finish()43		} else {44			fmt.debug_tuple("CrossAccountId::Substrate")45				.field(&self.substrate)46				.finish()47		}48	}49}5051impl<T: Config> PartialOrd for BasicCrossAccountId<T> {52	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {53		Some(self.substrate.cmp(&other.substrate))54	}55}5657impl<T: Config> Ord for BasicCrossAccountId<T> {58	fn cmp(&self, other: &Self) -> Ordering {59		self.partial_cmp(other)60			.expect("substrate account is total ordered")61	}62}6364impl<T: Config> PartialEq for BasicCrossAccountId<T> {65	fn eq(&self, other: &Self) -> bool {66		if self.from_ethereum == other.from_ethereum {67			self.substrate == other.substrate && self.ethereum == other.ethereum68		} else if self.from_ethereum {69			// ethereum is canonical encoding, but we need to compare derived address70			self.substrate == other.substrate71		} else {72			self.ethereum == other.ethereum73		}74	}75}76impl<T: Config> Clone for BasicCrossAccountId<T> {77	fn clone(&self) -> Self {78		Self {79			from_ethereum: self.from_ethereum,80			substrate: self.substrate.clone(),81			ethereum: self.ethereum,82		}83	}84}85impl<T: Config> Encode for BasicCrossAccountId<T> {86	fn encode(&self) -> Vec<u8> {87		let as_result = if !self.from_ethereum {88			Ok(self.substrate.clone())89		} else {90			Err(self.ethereum)91		};92		as_result.encode()93	}94}95impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}96impl<T: Config> Decode for BasicCrossAccountId<T> {97	fn decode<I>(input: &mut I) -> Result<Self, codec::Error>98	where99		I: codec::Input,100	{101		Ok(match <Result<T::AccountId, H160>>::decode(input)? {102			Ok(s) => Self::from_sub(s),103			Err(e) => Self::from_eth(e),104		})105	}106}107impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {108	fn as_sub(&self) -> &T::AccountId {109		&self.substrate110	}111	fn as_eth(&self) -> &H160 {112		&self.ethereum113	}114	fn from_sub(substrate: T::AccountId) -> Self {115		Self {116			ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),117			substrate,118			from_ethereum: false,119		}120	}121	fn from_eth(ethereum: H160) -> Self {122		Self {123			ethereum,124			substrate: T::EvmAddressMapping::into_account_id(ethereum),125			from_ethereum: true,126		}127	}128}129130pub trait EvmBackwardsAddressMapping<AccountId> {131	fn from_account_id(account_id: AccountId) -> H160;132}133134/// Should have same mapping as EnsureAddressTruncated135pub struct MapBackwardsAddressTruncated;136impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {137	fn from_account_id(account_id: AccountId32) -> H160 {138		let mut out = [0; 20];139		out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);140		H160(out)141	}142}
after · pallets/common/src/account.rs
1use crate::Config;2use codec::{Encode, EncodeLike, Decode};3use sp_core::H160;4use sp_core::crypto::AccountId32;5use core::cmp::Ordering;6use serde::{Serialize, Deserialize};7use pallet_evm::AddressMapping;8use sp_std::vec::Vec;9use sp_std::clone::Clone;1011pub trait CrossAccountId<AccountId>:12	Encode + EncodeLike + Decode + Clone + PartialEq + Ord + core::fmt::Debug + Default13// +14// Serialize + Deserialize<'static>15{16	fn as_sub(&self) -> &AccountId;17	fn as_eth(&self) -> &H160;1819	fn from_sub(account: AccountId) -> Self;20	fn from_eth(account: H160) -> Self;21}2223#[derive(Encode, Decode, Serialize, Deserialize)]24#[serde(rename_all = "camelCase")]25enum BasicCrossAccountIdRepr<AccountId> {26	Substrate(AccountId),27	Ethereum(H160),28}2930#[derive(Eq)]31pub struct BasicCrossAccountId<T: Config> {32	/// If true - then ethereum is canonical encoding33	from_ethereum: bool,34	substrate: T::AccountId,35	ethereum: H160,36}3738impl<T: Config> Default for BasicCrossAccountId<T> {39	fn default() -> Self {40		Self::from_sub(T::AccountId::default())41	}42}4344impl<T: Config> core::fmt::Debug for BasicCrossAccountId<T> {45	fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::fmt::Result {46		if self.from_ethereum {47			fmt.debug_tuple("CrossAccountId::Ethereum")48				.field(&self.ethereum)49				.finish()50		} else {51			fmt.debug_tuple("CrossAccountId::Substrate")52				.field(&self.substrate)53				.finish()54		}55	}56}5758impl<T: Config> PartialOrd for BasicCrossAccountId<T> {59	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {60		Some(self.substrate.cmp(&other.substrate))61	}62}6364impl<T: Config> Ord for BasicCrossAccountId<T> {65	fn cmp(&self, other: &Self) -> Ordering {66		self.partial_cmp(other)67			.expect("substrate account is total ordered")68	}69}7071impl<T: Config> PartialEq for BasicCrossAccountId<T> {72	fn eq(&self, other: &Self) -> bool {73		if self.from_ethereum == other.from_ethereum {74			self.substrate == other.substrate && self.ethereum == other.ethereum75		} else if self.from_ethereum {76			// ethereum is canonical encoding, but we need to compare derived address77			self.substrate == other.substrate78		} else {79			self.ethereum == other.ethereum80		}81	}82}83impl<T: Config> Clone for BasicCrossAccountId<T> {84	fn clone(&self) -> Self {85		Self {86			from_ethereum: self.from_ethereum,87			substrate: self.substrate.clone(),88			ethereum: self.ethereum,89		}90	}91}92impl<T: Config> Encode for BasicCrossAccountId<T> {93	fn encode(&self) -> Vec<u8> {94		BasicCrossAccountIdRepr::from(self.clone()).encode()95	}96}97impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}98impl<T: Config> Decode for BasicCrossAccountId<T> {99	fn decode<I>(input: &mut I) -> Result<Self, codec::Error>100	where101		I: codec::Input,102	{103		Ok(BasicCrossAccountIdRepr::decode(input)?.into())104	}105}106impl<T> Serialize for BasicCrossAccountId<T>107where108	T: Config,109	T::AccountId: Serialize,110{111	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>112	where113		S: serde::Serializer,114	{115		let repr = BasicCrossAccountIdRepr::from(self.clone());116		(&repr).serialize(serializer)117	}118}119impl<'de, T> Deserialize<'de> for BasicCrossAccountId<T>120where121	T: Config,122	T::AccountId: Deserialize<'de>,123{124	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>125	where126		D: serde::Deserializer<'de>,127	{128		Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())129	}130}131impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {132	fn as_sub(&self) -> &T::AccountId {133		&self.substrate134	}135	fn as_eth(&self) -> &H160 {136		&self.ethereum137	}138	fn from_sub(substrate: T::AccountId) -> Self {139		Self {140			ethereum: T::EvmBackwardsAddressMapping::from_account_id(substrate.clone()),141			substrate,142			from_ethereum: false,143		}144	}145	fn from_eth(ethereum: H160) -> Self {146		Self {147			ethereum,148			substrate: T::EvmAddressMapping::into_account_id(ethereum),149			from_ethereum: true,150		}151	}152}153impl<T: Config> From<BasicCrossAccountIdRepr<T::AccountId>> for BasicCrossAccountId<T> {154	fn from(repr: BasicCrossAccountIdRepr<T::AccountId>) -> Self {155		match repr {156			BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s),157			BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e),158		}159	}160}161impl<T: Config> From<BasicCrossAccountId<T>> for BasicCrossAccountIdRepr<T::AccountId> {162	fn from(v: BasicCrossAccountId<T>) -> Self {163		if v.from_ethereum {164			BasicCrossAccountIdRepr::Ethereum(*v.as_eth())165		} else {166			BasicCrossAccountIdRepr::Substrate(v.as_sub().clone())167		}168	}169}170171pub trait EvmBackwardsAddressMapping<AccountId> {172	fn from_account_id(account_id: AccountId) -> H160;173}174175/// Should have same mapping as EnsureAddressTruncated176pub struct MapBackwardsAddressTruncated;177impl EvmBackwardsAddressMapping<AccountId32> for MapBackwardsAddressTruncated {178	fn from_account_id(account_id: AccountId32) -> H160 {179		let mut out = [0; 20];180		out.copy_from_slice(&(account_id.as_ref() as &[u8])[0..20]);181		H160(out)182	}183}