git.delta.rocks / unique-network / refs/commits / 3d14ec560294

difftreelog

feat generate solidity documentation

Yaroslav Bolyukin2021-11-05parent: #a5e777f.patch.diff
in: master

9 files changed

modifiedcrates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/solidity_interface.rs
+++ b/crates/evm-coder-macros/src/solidity_interface.rs
@@ -6,7 +6,7 @@
 use std::fmt::Write;
 use syn::{
 	Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,
-	NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,
+	MetaNameValue, NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,
 };
 
 use crate::{
@@ -362,19 +362,28 @@
 	has_normal_args: bool,
 	mutability: Mutability,
 	result: Type,
+	docs: Vec<String>,
 }
 impl Method {
 	fn try_from(value: &ImplItemMethod) -> syn::Result<Self> {
 		let mut info = MethodInfo {
 			rename_selector: None,
 		};
+		let mut docs = Vec::new();
 		for attr in &value.attrs {
 			let ident = parse_ident_from_path(&attr.path, false)?;
 			if ident == "solidity" {
 				let args = attr.parse_meta().unwrap();
 				info = MethodInfo::from_meta(&args).unwrap();
 			} else if ident == "doc" {
-				// TODO: Add docs to evm interfaces
+				let args = attr.parse_meta().unwrap();
+				let value = match args {
+					Meta::NameValue(MetaNameValue {
+						lit: Lit::Str(str), ..
+					}) => str.value(),
+					_ => unreachable!(),
+				};
+				docs.push(value);
 			}
 		}
 		let ident = &value.sig.ident;
@@ -457,6 +466,7 @@
 			has_normal_args,
 			mutability,
 			result: result.clone(),
+			docs,
 		})
 	}
 	fn expand_call_def(&self) -> proc_macro2::TokenStream {
@@ -570,10 +580,12 @@
 			.iter()
 			.filter(|a| !a.is_special())
 			.map(MethodArg::expand_solidity_argument);
+		let docs = self.docs.iter();
 		let selector = format!("{} {:0>8x}", self.selector_str, self.selector);
 
 		quote! {
 			SolidityFunction {
+				docs: &[#(#docs),*],
 				selector: #selector,
 				name: #camel_name,
 				mutability: #mutability,
@@ -704,6 +716,7 @@
 					use core::fmt::Write;
 					let interface = SolidityInterface {
 						name: #solidity_name,
+						selector: Self::interface_id(),
 						is: &["Dummy", "ERC165", #(
 							#solidity_is,
 						)* #(
modifiedcrates/evm-coder-macros/src/to_log.rsdiffbeforeafterboth
--- a/crates/evm-coder-macros/src/to_log.rs
+++ b/crates/evm-coder-macros/src/to_log.rs
@@ -183,6 +183,7 @@
 					use evm_coder::solidity::*;
 					use core::fmt::Write;
 					let interface = SolidityInterface {
+						selector: 0,
 						name: #solidity_name,
 						is: &[],
 						functions: (#(
modifiedcrates/evm-coder/src/solidity.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/solidity.rs
+++ b/crates/evm-coder/src/solidity.rs
@@ -84,6 +84,7 @@
 solidity_type_name! {
 	uint8 => "uint8" true = "0",
 	uint32 => "uint32" true = "0",
+	uint64 => "uint64" true = "0",
 	uint128 => "uint128" true = "0",
 	uint256 => "uint256" true = "0",
 	address => "address" true = "0x0000000000000000000000000000000000000000",
@@ -376,6 +377,7 @@
 	Mutable,
 }
 pub struct SolidityFunction<A, R> {
+	pub docs: &'static [&'static str],
 	pub selector: &'static str,
 	pub name: &'static str,
 	pub args: A,
@@ -389,6 +391,12 @@
 		writer: &mut impl fmt::Write,
 		tc: &TypeCollector,
 	) -> fmt::Result {
+		for doc in self.docs {
+			writeln!(writer, "\t//{}", doc)?;
+		}
+		if !self.docs.is_empty() {
+			writeln!(writer, "\t//")?;
+		}
 		writeln!(writer, "\t// Selector: {}", self.selector)?;
 		write!(writer, "\tfunction {}(", self.name)?;
 		self.args.solidity_name(writer, tc)?;
@@ -449,6 +457,7 @@
 }
 
 pub struct SolidityInterface<F: SolidityFunctions> {
+	pub selector: u32,
 	pub name: &'static str,
 	pub is: &'static [&'static str],
 	pub functions: F,
@@ -461,6 +470,9 @@
 		out: &mut impl fmt::Write,
 		tc: &TypeCollector,
 	) -> fmt::Result {
+		if self.selector != 0 {
+			writeln!(out, "// Selector: {:0>8x}", self.selector)?;
+		}
 		if is_impl {
 			write!(out, "contract ")?;
 		} else {
modifiedpallets/fungible/src/stubs/UniqueFungible.soldiffbeforeafterboth
--- a/pallets/fungible/src/stubs/UniqueFungible.sol
+++ b/pallets/fungible/src/stubs/UniqueFungible.sol
@@ -31,6 +31,7 @@
 	);
 }
 
+// Selector: 942e8b22
 contract ERC20 is Dummy, ERC165, ERC20Events {
 	// Selector: name() 06fdde03
 	function name() public view returns (string memory) {
modifiedpallets/nonfungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/erc.rs
+++ b/pallets/nonfungible/src/erc.rs
@@ -61,6 +61,7 @@
 		Ok(string::from_utf8_lossy(&self.token_prefix).into())
 	}
 
+	/// Returns token's const_metadata
 	#[solidity(rename_selector = "tokenURI")]
 	fn token_uri(&self, token_id: uint256) -> Result<string> {
 		let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;
@@ -79,6 +80,7 @@
 		Ok(index)
 	}
 
+	/// Not implemented
 	fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
@@ -103,6 +105,7 @@
 			.owner
 			.as_eth())
 	}
+	/// Not implemented
 	fn safe_transfer_from_with_data(
 		&mut self,
 		_from: address,
@@ -114,6 +117,7 @@
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
+	/// Not implemented
 	fn safe_transfer_from(
 		&mut self,
 		_from: address,
@@ -159,6 +163,7 @@
 		Ok(())
 	}
 
+	/// Not implemented
 	fn set_approval_for_all(
 		&mut self,
 		_caller: caller,
@@ -169,11 +174,13 @@
 		Err("not implemented".into())
 	}
 
+	/// Not implemented
 	fn get_approved(&self, _token_id: uint256) -> Result<address> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
 	}
 
+	/// Not implemented
 	fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {
 		// TODO: Not implemetable
 		Err("not implemented".into())
@@ -197,6 +204,8 @@
 		Ok(false)
 	}
 
+	/// `token_id` should be obtained with `next_token_id` method,
+	/// unlike standard, you can't specify it manually
 	fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {
 		let caller = T::CrossAccountId::from_eth(caller);
 		let to = T::CrossAccountId::from_eth(to);
@@ -223,6 +232,8 @@
 		Ok(true)
 	}
 
+	/// `token_id` should be obtained with `next_token_id` method,
+	/// unlike standard, you can't specify it manually
 	#[solidity(rename_selector = "mintWithTokenURI")]
 	fn mint_with_token_uri(
 		&mut self,
@@ -257,6 +268,7 @@
 		Ok(true)
 	}
 
+	/// Not implemented
 	fn finish_minting(&mut self, _caller: caller) -> Result<bool> {
 		Err("not implementable".into())
 	}
modifiedpallets/nonfungible/src/stubs/UniqueNFT.soldiffbeforeafterboth
51 event MintingFinished();51 event MintingFinished();
52}52}
5353
54contract ERC721 is Dummy, ERC165, ERC721Events {54// Selector: 42966c68
55 // Selector: balanceOf(address) 70a08231
56 function balanceOf(address owner) public view returns (uint256) {
57 require(false, stub_error);
58 owner;
59 dummy;
60 return 0;
61 }
62
63 // Selector: ownerOf(uint256) 6352211e
64 function ownerOf(uint256 tokenId) public view returns (address) {
65 require(false, stub_error);
66 tokenId;
67 dummy;
68 return 0x0000000000000000000000000000000000000000;
69 }
70
71 // Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
72 function safeTransferFromWithData(
73 address from,
74 address to,
75 uint256 tokenId,
76 bytes memory data
77 ) public {
78 require(false, stub_error);
79 from;
80 to;
81 tokenId;
82 data;
83 dummy = 0;
84 }
85
86 // Selector: safeTransferFrom(address,address,uint256) 42842e0e
87 function safeTransferFrom(
88 address from,
89 address to,
90 uint256 tokenId
91 ) public {
92 require(false, stub_error);
93 from;
94 to;
95 tokenId;
96 dummy = 0;
97 }
98
99 // Selector: transferFrom(address,address,uint256) 23b872dd
100 function transferFrom(
101 address from,
102 address to,
103 uint256 tokenId
104 ) public {
105 require(false, stub_error);
106 from;
107 to;
108 tokenId;
109 dummy = 0;
110 }
111
112 // Selector: approve(address,uint256) 095ea7b3
113 function approve(address approved, uint256 tokenId) public {
114 require(false, stub_error);
115 approved;
116 tokenId;
117 dummy = 0;
118 }
119
120 // Selector: setApprovalForAll(address,bool) a22cb465
121 function setApprovalForAll(address operator, bool approved) public {
122 require(false, stub_error);
123 operator;
124 approved;
125 dummy = 0;
126 }
127
128 // Selector: getApproved(uint256) 081812fc
129 function getApproved(uint256 tokenId) public view returns (address) {
130 require(false, stub_error);
131 tokenId;
132 dummy;
133 return 0x0000000000000000000000000000000000000000;
134 }
135
136 // Selector: isApprovedForAll(address,address) e985e9c5
137 function isApprovedForAll(address owner, address operator)
138 public
139 view
140 returns (address)
141 {
142 require(false, stub_error);
143 owner;
144 operator;
145 dummy;
146 return 0x0000000000000000000000000000000000000000;
147 }
148}
149
150contract ERC721Burnable is Dummy, ERC165 {55contract ERC721Burnable is Dummy, ERC165 {
151 // Selector: burn(uint256) 42966c6856 // Selector: burn(uint256) 42966c68
156 }61 }
157}62}
15863
64// Selector: 58800161
159contract ERC721Enumerable is Dummy, ERC165 {65contract ERC721 is Dummy, ERC165, ERC721Events {
160 // Selector: tokenByIndex(uint256) 4f6ccce766 // Selector: balanceOf(address) 70a08231
161 function tokenByIndex(uint256 index) public view returns (uint256) {67 function balanceOf(address owner) public view returns (uint256) {
162 require(false, stub_error);68 require(false, stub_error);
163 index;69 owner;
164 dummy;70 dummy;
165 return 0;71 return 0;
166 }72 }
73
74 // Selector: ownerOf(uint256) 6352211e
75 function ownerOf(uint256 tokenId) public view returns (address) {
76 require(false, stub_error);
77 tokenId;
78 dummy;
79 return 0x0000000000000000000000000000000000000000;
80 }
81
82 // Not implemented
83 //
84 // Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
85 function safeTransferFromWithData(
86 address from,
87 address to,
88 uint256 tokenId,
89 bytes memory data
90 ) public {
91 require(false, stub_error);
92 from;
93 to;
94 tokenId;
95 data;
96 dummy = 0;
97 }
98
99 // Not implemented
100 //
101 // Selector: safeTransferFrom(address,address,uint256) 42842e0e
102 function safeTransferFrom(
103 address from,
104 address to,
105 uint256 tokenId
106 ) public {
107 require(false, stub_error);
108 from;
109 to;
110 tokenId;
111 dummy = 0;
112 }
167113
168 // Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59114 // Selector: transferFrom(address,address,uint256) 23b872dd
169 function tokenOfOwnerByIndex(address owner, uint256 index)115 function transferFrom(
116 address from,
117 address to,
118 uint256 tokenId
119 ) public {
120 require(false, stub_error);
121 from;
122 to;
123 tokenId;
124 dummy = 0;
125 }
126
127 // Selector: approve(address,uint256) 095ea7b3
170 public128 function approve(address approved, uint256 tokenId) public {
129 require(false, stub_error);
130 approved;
131 tokenId;
132 dummy = 0;
133 }
134
135 // Not implemented
136 //
137 // Selector: setApprovalForAll(address,bool) a22cb465
138 function setApprovalForAll(address operator, bool approved) public {
139 require(false, stub_error);
140 operator;
141 approved;
142 dummy = 0;
143 }
144
145 // Not implemented
146 //
147 // Selector: getApproved(uint256) 081812fc
171 view148 function getApproved(uint256 tokenId) public view returns (address) {
172 returns (uint256)
173 {
174 require(false, stub_error);149 require(false, stub_error);
175 owner;150 tokenId;
176 index;
177 dummy;151 dummy;
178 return 0;152 return 0x0000000000000000000000000000000000000000;
179 }153 }
180154
181 // Selector: totalSupply() 18160ddd155 // Not implemented
156 //
157 // Selector: isApprovedForAll(address,address) e985e9c5
182 function totalSupply() public view returns (uint256) {158 function isApprovedForAll(address owner, address operator)
159 public
160 view
161 returns (address)
162 {
183 require(false, stub_error);163 require(false, stub_error);
164 owner;
165 operator;
184 dummy;166 dummy;
185 return 0;167 return 0x0000000000000000000000000000000000000000;
186 }168 }
187}169}
188170
171// Selector: 5b5e139f
189contract ERC721Metadata is Dummy, ERC165 {172contract ERC721Metadata is Dummy, ERC165 {
190 // Selector: name() 06fdde03173 // Selector: name() 06fdde03
191 function name() public view returns (string memory) {174 function name() public view returns (string memory) {
201 return "";184 return "";
202 }185 }
203186
187 // Returns token's const_metadata
188 //
204 // Selector: tokenURI(uint256) c87b56dd189 // Selector: tokenURI(uint256) c87b56dd
205 function tokenURI(uint256 tokenId) public view returns (string memory) {190 function tokenURI(uint256 tokenId) public view returns (string memory) {
206 require(false, stub_error);191 require(false, stub_error);
210 }195 }
211}196}
212197
198// Selector: 68ccfe89
213contract ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {199contract ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {
214 // Selector: mintingFinished() 05d2035b200 // Selector: mintingFinished() 05d2035b
215 function mintingFinished() public view returns (bool) {201 function mintingFinished() public view returns (bool) {
218 return false;204 return false;
219 }205 }
220206
207 // `token_id` should be obtained with `next_token_id` method,
208 // unlike standard, you can't specify it manually
209 //
221 // Selector: mint(address,uint256) 40c10f19210 // Selector: mint(address,uint256) 40c10f19
222 function mint(address to, uint256 tokenId) public returns (bool) {211 function mint(address to, uint256 tokenId) public returns (bool) {
223 require(false, stub_error);212 require(false, stub_error);
227 return false;216 return false;
228 }217 }
229218
219 // `token_id` should be obtained with `next_token_id` method,
220 // unlike standard, you can't specify it manually
221 //
230 // Selector: mintWithTokenURI(address,uint256,string) 50bb4e7f222 // Selector: mintWithTokenURI(address,uint256,string) 50bb4e7f
231 function mintWithTokenURI(223 function mintWithTokenURI(
232 address to,224 address to,
241 return false;233 return false;
242 }234 }
243235
236 // Not implemented
237 //
244 // Selector: finishMinting() 7d64bcb4238 // Selector: finishMinting() 7d64bcb4
245 function finishMinting() public returns (bool) {239 function finishMinting() public returns (bool) {
246 require(false, stub_error);240 require(false, stub_error);
249 }243 }
250}244}
251245
246// Selector: 780e9d63
247contract ERC721Enumerable is Dummy, ERC165 {
248 // Selector: tokenByIndex(uint256) 4f6ccce7
249 function tokenByIndex(uint256 index) public view returns (uint256) {
250 require(false, stub_error);
251 index;
252 dummy;
253 return 0;
254 }
255
256 // Not implemented
257 //
258 // Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
259 function tokenOfOwnerByIndex(address owner, uint256 index)
260 public
261 view
262 returns (uint256)
263 {
264 require(false, stub_error);
265 owner;
266 index;
267 dummy;
268 return 0;
269 }
270
271 // Selector: totalSupply() 18160ddd
272 function totalSupply() public view returns (uint256) {
273 require(false, stub_error);
274 dummy;
275 return 0;
276 }
277}
278
279// Selector: e562194d
252contract ERC721UniqueExtensions is Dummy, ERC165 {280contract ERC721UniqueExtensions is Dummy, ERC165 {
253 // Selector: transfer(address,uint256) a9059cbb281 // Selector: transfer(address,uint256) a9059cbb
254 function transfer(address to, uint256 tokenId) public {282 function transfer(address to, uint256 tokenId) public {
modifiedtests/src/eth/api/ContractHelpers.soldiffbeforeafterboth
--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -12,6 +12,7 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
+// Selector: 31acb1fe
 interface ContractHelpers is Dummy, ERC165 {
 	// Selector: contractOwner(address) 5152b14c
 	function contractOwner(address contractAddress)
modifiedtests/src/eth/api/UniqueFungible.soldiffbeforeafterboth
--- a/tests/src/eth/api/UniqueFungible.sol
+++ b/tests/src/eth/api/UniqueFungible.sol
@@ -22,6 +22,7 @@
 	);
 }
 
+// Selector: 942e8b22
 interface ERC20 is Dummy, ERC165, ERC20Events {
 	// Selector: name() 06fdde03
 	function name() external view returns (string memory);
modifiedtests/src/eth/api/UniqueNFT.soldiffbeforeafterboth
--- a/tests/src/eth/api/UniqueNFT.sol
+++ b/tests/src/eth/api/UniqueNFT.sol
@@ -42,6 +42,13 @@
 	event MintingFinished();
 }
 
+// Selector: 42966c68
+interface ERC721Burnable is Dummy, ERC165 {
+	// Selector: burn(uint256) 42966c68
+	function burn(uint256 tokenId) external;
+}
+
+// Selector: 58800161
 interface ERC721 is Dummy, ERC165, ERC721Events {
 	// Selector: balanceOf(address) 70a08231
 	function balanceOf(address owner) external view returns (uint256);
@@ -49,6 +56,8 @@
 	// Selector: ownerOf(uint256) 6352211e
 	function ownerOf(uint256 tokenId) external view returns (address);
 
+	// Not implemented
+	//
 	// Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
 	function safeTransferFromWithData(
 		address from,
@@ -57,6 +66,8 @@
 		bytes memory data
 	) external;
 
+	// Not implemented
+	//
 	// Selector: safeTransferFrom(address,address,uint256) 42842e0e
 	function safeTransferFrom(
 		address from,
@@ -74,12 +85,18 @@
 	// Selector: approve(address,uint256) 095ea7b3
 	function approve(address approved, uint256 tokenId) external;
 
+	// Not implemented
+	//
 	// Selector: setApprovalForAll(address,bool) a22cb465
 	function setApprovalForAll(address operator, bool approved) external;
 
+	// Not implemented
+	//
 	// Selector: getApproved(uint256) 081812fc
 	function getApproved(uint256 tokenId) external view returns (address);
 
+	// Not implemented
+	//
 	// Selector: isApprovedForAll(address,address) e985e9c5
 	function isApprovedForAll(address owner, address operator)
 		external
@@ -87,25 +104,7 @@
 		returns (address);
 }
 
-interface ERC721Burnable is Dummy, ERC165 {
-	// Selector: burn(uint256) 42966c68
-	function burn(uint256 tokenId) external;
-}
-
-interface ERC721Enumerable is Dummy, ERC165 {
-	// Selector: tokenByIndex(uint256) 4f6ccce7
-	function tokenByIndex(uint256 index) external view returns (uint256);
-
-	// Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
-	function tokenOfOwnerByIndex(address owner, uint256 index)
-		external
-		view
-		returns (uint256);
-
-	// Selector: totalSupply() 18160ddd
-	function totalSupply() external view returns (uint256);
-}
-
+// Selector: 5b5e139f
 interface ERC721Metadata is Dummy, ERC165 {
 	// Selector: name() 06fdde03
 	function name() external view returns (string memory);
@@ -113,17 +112,26 @@
 	// Selector: symbol() 95d89b41
 	function symbol() external view returns (string memory);
 
+	// Returns token's const_metadata
+	//
 	// Selector: tokenURI(uint256) c87b56dd
 	function tokenURI(uint256 tokenId) external view returns (string memory);
 }
 
+// Selector: 68ccfe89
 interface ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {
 	// Selector: mintingFinished() 05d2035b
 	function mintingFinished() external view returns (bool);
 
+	// `token_id` should be obtained with `next_token_id` method,
+	// unlike standard, you can't specify it manually
+	//
 	// Selector: mint(address,uint256) 40c10f19
 	function mint(address to, uint256 tokenId) external returns (bool);
 
+	// `token_id` should be obtained with `next_token_id` method,
+	// unlike standard, you can't specify it manually
+	//
 	// Selector: mintWithTokenURI(address,uint256,string) 50bb4e7f
 	function mintWithTokenURI(
 		address to,
@@ -131,10 +139,30 @@
 		string memory tokenUri
 	) external returns (bool);
 
+	// Not implemented
+	//
 	// Selector: finishMinting() 7d64bcb4
 	function finishMinting() external returns (bool);
 }
 
+// Selector: 780e9d63
+interface ERC721Enumerable is Dummy, ERC165 {
+	// Selector: tokenByIndex(uint256) 4f6ccce7
+	function tokenByIndex(uint256 index) external view returns (uint256);
+
+	// Not implemented
+	//
+	// Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
+	function tokenOfOwnerByIndex(address owner, uint256 index)
+		external
+		view
+		returns (uint256);
+
+	// Selector: totalSupply() 18160ddd
+	function totalSupply() external view returns (uint256);
+}
+
+// Selector: e562194d
 interface ERC721UniqueExtensions is Dummy, ERC165 {
 	// Selector: transfer(address,uint256) a9059cbb
 	function transfer(address to, uint256 tokenId) external;