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
20 fn from_eth(account: H160) -> Self;20 fn from_eth(account: H160) -> Self;
21}21}
22
23#[derive(Encode, Decode, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase")]
25enum BasicCrossAccountIdRepr<AccountId> {
26 Substrate(AccountId),
27 Ethereum(H160),
28}
2229
23#[derive(Eq, Serialize, Deserialize)]30#[derive(Eq)]
24pub struct BasicCrossAccountId<T: Config> {31pub struct BasicCrossAccountId<T: Config> {
25 /// If true - then ethereum is canonical encoding32 /// If true - then ethereum is canonical encoding
26 from_ethereum: bool,33 from_ethereum: bool,
84}91}
85impl<T: Config> Encode for BasicCrossAccountId<T> {92impl<T: Config> Encode for BasicCrossAccountId<T> {
86 fn encode(&self) -> Vec<u8> {93 fn encode(&self) -> Vec<u8> {
87 let as_result = if !self.from_ethereum {
88 Ok(self.substrate.clone())94 BasicCrossAccountIdRepr::from(self.clone()).encode()
89 } else {
90 Err(self.ethereum)
91 };
92 as_result.encode()
93 }95 }
94}96}
95impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}97impl<T: Config> EncodeLike for BasicCrossAccountId<T> {}
98 where100 where
99 I: codec::Input,101 I: codec::Input,
100 {102 {
101 Ok(match <Result<T::AccountId, H160>>::decode(input)? {103 Ok(BasicCrossAccountIdRepr::decode(input)?.into())
102 Ok(s) => Self::from_sub(s),
103 Err(e) => Self::from_eth(e),
104 })
105 }104 }
106}105}
106impl<T> Serialize for BasicCrossAccountId<T>
107where
108 T: Config,
109 T::AccountId: Serialize,
110{
111 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
112 where
113 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>
120where
121 T: Config,
122 T::AccountId: Deserialize<'de>,
123{
124 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
125 where
126 D: serde::Deserializer<'de>,
127 {
128 Ok(BasicCrossAccountIdRepr::deserialize(deserializer)?.into())
129 }
130}
107impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {131impl<T: Config> CrossAccountId<T::AccountId> for BasicCrossAccountId<T> {
108 fn as_sub(&self) -> &T::AccountId {132 fn as_sub(&self) -> &T::AccountId {
109 &self.substrate133 &self.substrate
126 }150 }
127 }151 }
128}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}
129170
130pub trait EvmBackwardsAddressMapping<AccountId> {171pub trait EvmBackwardsAddressMapping<AccountId> {
131 fn from_account_id(account_id: AccountId) -> H160;172 fn from_account_id(account_id: AccountId) -> H160;