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}