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
63 account: CrossAccountId,63 account: CrossAccountId,
64 token: TokenId,64 token: TokenId,
65 at: Option<BlockHash>,65 at: Option<BlockHash>,
66 ) -> Result<u128>;66 ) -> Result<String>;
67 #[rpc(name = "nft_allowance")]67 #[rpc(name = "nft_allowance")]
68 fn allowance(68 fn allowance(
69 &self,69 &self,
72 spender: CrossAccountId,72 spender: CrossAccountId,
73 token: TokenId,73 token: TokenId,
74 at: Option<BlockHash>,74 at: Option<BlockHash>,
75 ) -> Result<u128>;75 ) -> Result<String>;
7676
77 #[rpc(name = "nft_adminlist")]77 #[rpc(name = "nft_adminlist")]
78 fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;78 fn adminlist(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<Vec<AccountId>>;
109}109}
110110
111macro_rules! pass_method {111macro_rules! pass_method {
112 ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => {112 ($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty $(=> $mapper:expr)?) => {
113 fn $method_name(113 fn $method_name(
114 &self,114 &self,
115 $(115 $(
124 code: ErrorCode::ServerError(Error::RuntimeError.into()),124 code: ErrorCode::ServerError(Error::RuntimeError.into()),
125 message: "Unable to query".into(),125 message: "Unable to query".into(),
126 data: Some(format!("{:?}", e).into()),126 data: Some(format!("{:?}", e).into()),
127 })127 }) $(
128 .map($mapper)
129 )?
128 }130 }
129 };131 };
130}132}
145 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);147 pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);
146 pass_method!(collection_tokens(collection: CollectionId) -> u32);148 pass_method!(collection_tokens(collection: CollectionId) -> u32);
147 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);149 pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);
148 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);150 pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> String => |v| v.to_string());
149 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);151 pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> String => |v| v.to_string());
150152
151 pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);153 pass_method!(adminlist(collection: CollectionId) -> Vec<AccountId>);
152 pass_method!(allowlist(collection: CollectionId) -> Vec<AccountId>);154 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
--- 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<AccountId> {
+	Substrate(AccountId),
+	Ethereum(H160),
+}
+
+#[derive(Eq)]
 pub struct BasicCrossAccountId<T: Config> {
 	/// If true - then ethereum is canonical encoding
 	from_ethereum: bool,
@@ -84,12 +91,7 @@
 }
 impl<T: Config> Encode for BasicCrossAccountId<T> {
 	fn encode(&self) -> Vec<u8> {
-		let as_result = if !self.from_ethereum {
-			Ok(self.substrate.clone())
-		} else {
-			Err(self.ethereum)
-		};
-		as_result.encode()
+		BasicCrossAccountIdRepr::from(self.clone()).encode()
 	}
 }
 impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}
@@ -98,12 +100,34 @@
 	where
 		I: codec::Input,
 	{
-		Ok(match <Result<T::AccountId, H160>>::decode(input)? {
-			Ok(s) => Self::from_sub(s),
-			Err(e) => Self::from_eth(e),
-		})
+		Ok(BasicCrossAccountIdRepr::decode(input)?.into())
 	}
 }
+impl<T> Serialize for BasicCrossAccountId<T>
+where
+	T: Config,
+	T::AccountId: Serialize,
+{
+	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+	where
+		S: serde::Serializer,
+	{
+		let repr = BasicCrossAccountIdRepr::from(self.clone());
+		(&repr).serialize(serializer)
+	}
+}
+impl<'de, T> Deserialize<'de> for BasicCrossAccountId<T>
+where
+	T: Config,
+	T::AccountId: Deserialize<'de>,
+{
+	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+	where
+		D: serde::Deserializer<'de>,
+	{
+		Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())
+	}
+}
 impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {
 	fn as_sub(&self) -> &T::AccountId {
 		&self.substrate
@@ -126,6 +150,23 @@
 		}
 	}
 }
+impl<T: Config> From<BasicCrossAccountIdRepr<T::AccountId>> for BasicCrossAccountId<T> {
+	fn from(repr: BasicCrossAccountIdRepr<T::AccountId>) -> Self {
+		match repr {
+			BasicCrossAccountIdRepr::Substrate(s) => Self::from_sub(s),
+			BasicCrossAccountIdRepr::Ethereum(e) => Self::from_eth(e),
+		}
+	}
+}
+impl<T: Config> From<BasicCrossAccountId<T>> for BasicCrossAccountIdRepr<T::AccountId> {
+	fn from(v: BasicCrossAccountId<T>) -> Self {
+		if v.from_ethereum {
+			BasicCrossAccountIdRepr::Ethereum(*v.as_eth())
+		} else {
+			BasicCrossAccountIdRepr::Substrate(v.as_sub().clone())
+		}
+	}
+}
 
 pub trait EvmBackwardsAddressMapping<AccountId> {
 	fn from_account_id(account_id: AccountId) -> H160;