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
--- a/pallets/nonfungible/src/stubs/UniqueNFT.sol
+++ b/pallets/nonfungible/src/stubs/UniqueNFT.sol
@@ -51,6 +51,17 @@
 	event MintingFinished();
 }
 
+// Selector: 42966c68
+contract ERC721Burnable is Dummy, ERC165 {
+	// Selector: burn(uint256) 42966c68
+	function burn(uint256 tokenId) public {
+		require(false, stub_error);
+		tokenId;
+		dummy = 0;
+	}
+}
+
+// Selector: 58800161
 contract ERC721 is Dummy, ERC165, ERC721Events {
 	// Selector: balanceOf(address) 70a08231
 	function balanceOf(address owner) public view returns (uint256) {
@@ -68,6 +79,8 @@
 		return 0x0000000000000000000000000000000000000000;
 	}
 
+	// Not implemented
+	//
 	// Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
 	function safeTransferFromWithData(
 		address from,
@@ -83,6 +96,8 @@
 		dummy = 0;
 	}
 
+	// Not implemented
+	//
 	// Selector: safeTransferFrom(address,address,uint256) 42842e0e
 	function safeTransferFrom(
 		address from,
@@ -117,6 +132,8 @@
 		dummy = 0;
 	}
 
+	// Not implemented
+	//
 	// Selector: setApprovalForAll(address,bool) a22cb465
 	function setApprovalForAll(address operator, bool approved) public {
 		require(false, stub_error);
@@ -125,6 +142,8 @@
 		dummy = 0;
 	}
 
+	// Not implemented
+	//
 	// Selector: getApproved(uint256) 081812fc
 	function getApproved(uint256 tokenId) public view returns (address) {
 		require(false, stub_error);
@@ -133,6 +152,8 @@
 		return 0x0000000000000000000000000000000000000000;
 	}
 
+	// Not implemented
+	//
 	// Selector: isApprovedForAll(address,address) e985e9c5
 	function isApprovedForAll(address owner, address operator)
 		public
@@ -147,45 +168,7 @@
 	}
 }
 
-contract ERC721Burnable is Dummy, ERC165 {
-	// Selector: burn(uint256) 42966c68
-	function burn(uint256 tokenId) public {
-		require(false, stub_error);
-		tokenId;
-		dummy = 0;
-	}
-}
-
-contract ERC721Enumerable is Dummy, ERC165 {
-	// Selector: tokenByIndex(uint256) 4f6ccce7
-	function tokenByIndex(uint256 index) public view returns (uint256) {
-		require(false, stub_error);
-		index;
-		dummy;
-		return 0;
-	}
-
-	// Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
-	function tokenOfOwnerByIndex(address owner, uint256 index)
-		public
-		view
-		returns (uint256)
-	{
-		require(false, stub_error);
-		owner;
-		index;
-		dummy;
-		return 0;
-	}
-
-	// Selector: totalSupply() 18160ddd
-	function totalSupply() public view returns (uint256) {
-		require(false, stub_error);
-		dummy;
-		return 0;
-	}
-}
-
+// Selector: 5b5e139f
 contract ERC721Metadata is Dummy, ERC165 {
 	// Selector: name() 06fdde03
 	function name() public view returns (string memory) {
@@ -201,6 +184,8 @@
 		return "";
 	}
 
+	// Returns token's const_metadata
+	//
 	// Selector: tokenURI(uint256) c87b56dd
 	function tokenURI(uint256 tokenId) public view returns (string memory) {
 		require(false, stub_error);
@@ -210,6 +195,7 @@
 	}
 }
 
+// Selector: 68ccfe89
 contract ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {
 	// Selector: mintingFinished() 05d2035b
 	function mintingFinished() public view returns (bool) {
@@ -218,6 +204,9 @@
 		return false;
 	}
 
+	// `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) public returns (bool) {
 		require(false, stub_error);
@@ -227,6 +216,9 @@
 		return false;
 	}
 
+	// `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,
@@ -241,6 +233,8 @@
 		return false;
 	}
 
+	// Not implemented
+	//
 	// Selector: finishMinting() 7d64bcb4
 	function finishMinting() public returns (bool) {
 		require(false, stub_error);
@@ -249,6 +243,40 @@
 	}
 }
 
+// Selector: 780e9d63
+contract ERC721Enumerable is Dummy, ERC165 {
+	// Selector: tokenByIndex(uint256) 4f6ccce7
+	function tokenByIndex(uint256 index) public view returns (uint256) {
+		require(false, stub_error);
+		index;
+		dummy;
+		return 0;
+	}
+
+	// Not implemented
+	//
+	// Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
+	function tokenOfOwnerByIndex(address owner, uint256 index)
+		public
+		view
+		returns (uint256)
+	{
+		require(false, stub_error);
+		owner;
+		index;
+		dummy;
+		return 0;
+	}
+
+	// Selector: totalSupply() 18160ddd
+	function totalSupply() public view returns (uint256) {
+		require(false, stub_error);
+		dummy;
+		return 0;
+	}
+}
+
+// Selector: e562194d
 contract ERC721UniqueExtensions is Dummy, ERC165 {
 	// Selector: transfer(address,uint256) a9059cbb
 	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
42 event MintingFinished();42 event MintingFinished();
43}43}
4444
45interface ERC721 is Dummy, ERC165, ERC721Events {45// Selector: 42966c68
46 // Selector: balanceOf(address) 70a08231
47 function balanceOf(address owner) external view returns (uint256);
48
49 // Selector: ownerOf(uint256) 6352211e
50 function ownerOf(uint256 tokenId) external view returns (address);
51
52 // Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
53 function safeTransferFromWithData(
54 address from,
55 address to,
56 uint256 tokenId,
57 bytes memory data
58 ) external;
59
60 // Selector: safeTransferFrom(address,address,uint256) 42842e0e
61 function safeTransferFrom(
62 address from,
63 address to,
64 uint256 tokenId
65 ) external;
66
67 // Selector: transferFrom(address,address,uint256) 23b872dd
68 function transferFrom(
69 address from,
70 address to,
71 uint256 tokenId
72 ) external;
73
74 // Selector: approve(address,uint256) 095ea7b3
75 function approve(address approved, uint256 tokenId) external;
76
77 // Selector: setApprovalForAll(address,bool) a22cb465
78 function setApprovalForAll(address operator, bool approved) external;
79
80 // Selector: getApproved(uint256) 081812fc
81 function getApproved(uint256 tokenId) external view returns (address);
82
83 // Selector: isApprovedForAll(address,address) e985e9c5
84 function isApprovedForAll(address owner, address operator)
85 external
86 view
87 returns (address);
88}
89
90interface ERC721Burnable is Dummy, ERC165 {46interface ERC721Burnable is Dummy, ERC165 {
91 // Selector: burn(uint256) 42966c6847 // Selector: burn(uint256) 42966c68
92 function burn(uint256 tokenId) external;48 function burn(uint256 tokenId) external;
93}49}
9450
51// Selector: 58800161
95interface ERC721Enumerable is Dummy, ERC165 {52interface ERC721 is Dummy, ERC165, ERC721Events {
96 // Selector: tokenByIndex(uint256) 4f6ccce753 // Selector: balanceOf(address) 70a08231
97 function tokenByIndex(uint256 index) external view returns (uint256);54 function balanceOf(address owner) external view returns (uint256);
55
56 // Selector: ownerOf(uint256) 6352211e
57 function ownerOf(uint256 tokenId) external view returns (address);
58
59 // Not implemented
60 //
61 // Selector: safeTransferFromWithData(address,address,uint256,bytes) 60a11672
62 function safeTransferFromWithData(
63 address from,
64 address to,
65 uint256 tokenId,
66 bytes memory data
67 ) external;
68
69 // Not implemented
70 //
71 // Selector: safeTransferFrom(address,address,uint256) 42842e0e
72 function safeTransferFrom(
73 address from,
74 address to,
75 uint256 tokenId
76 ) external;
9877
99 // Selector: tokenOfOwnerByIndex(address,uint256) 2f745c5978 // Selector: transferFrom(address,address,uint256) 23b872dd
100 function tokenOfOwnerByIndex(address owner, uint256 index)79 function transferFrom(
80 address from,
81 address to,
82 uint256 tokenId
83 ) external;
84
85 // Selector: approve(address,uint256) 095ea7b3
101 external86 function approve(address approved, uint256 tokenId) external;
87
88 // Not implemented
89 //
90 // Selector: setApprovalForAll(address,bool) a22cb465
91 function setApprovalForAll(address operator, bool approved) external;
92
93 // Not implemented
94 //
95 // Selector: getApproved(uint256) 081812fc
102 view96 function getApproved(uint256 tokenId) external view returns (address);
103 returns (uint256);97
10498 // Not implemented
105 // Selector: totalSupply() 18160ddd99 //
100 // Selector: isApprovedForAll(address,address) e985e9c5
106 function totalSupply() external view returns (uint256);101 function isApprovedForAll(address owner, address operator)
102 external
103 view
104 returns (address);
107}105}
108106
107// Selector: 5b5e139f
109interface ERC721Metadata is Dummy, ERC165 {108interface ERC721Metadata is Dummy, ERC165 {
110 // Selector: name() 06fdde03109 // Selector: name() 06fdde03
111 function name() external view returns (string memory);110 function name() external view returns (string memory);
112111
113 // Selector: symbol() 95d89b41112 // Selector: symbol() 95d89b41
114 function symbol() external view returns (string memory);113 function symbol() external view returns (string memory);
115114
115 // Returns token's const_metadata
116 //
116 // Selector: tokenURI(uint256) c87b56dd117 // Selector: tokenURI(uint256) c87b56dd
117 function tokenURI(uint256 tokenId) external view returns (string memory);118 function tokenURI(uint256 tokenId) external view returns (string memory);
118}119}
119120
121// Selector: 68ccfe89
120interface ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {122interface ERC721Mintable is Dummy, ERC165, ERC721MintableEvents {
121 // Selector: mintingFinished() 05d2035b123 // Selector: mintingFinished() 05d2035b
122 function mintingFinished() external view returns (bool);124 function mintingFinished() external view returns (bool);
123125
126 // `token_id` should be obtained with `next_token_id` method,
127 // unlike standard, you can't specify it manually
128 //
124 // Selector: mint(address,uint256) 40c10f19129 // Selector: mint(address,uint256) 40c10f19
125 function mint(address to, uint256 tokenId) external returns (bool);130 function mint(address to, uint256 tokenId) external returns (bool);
126131
132 // `token_id` should be obtained with `next_token_id` method,
133 // unlike standard, you can't specify it manually
134 //
127 // Selector: mintWithTokenURI(address,uint256,string) 50bb4e7f135 // Selector: mintWithTokenURI(address,uint256,string) 50bb4e7f
128 function mintWithTokenURI(136 function mintWithTokenURI(
129 address to,137 address to,
130 uint256 tokenId,138 uint256 tokenId,
131 string memory tokenUri139 string memory tokenUri
132 ) external returns (bool);140 ) external returns (bool);
133141
142 // Not implemented
143 //
134 // Selector: finishMinting() 7d64bcb4144 // Selector: finishMinting() 7d64bcb4
135 function finishMinting() external returns (bool);145 function finishMinting() external returns (bool);
136}146}
137147
148// Selector: 780e9d63
149interface ERC721Enumerable is Dummy, ERC165 {
150 // Selector: tokenByIndex(uint256) 4f6ccce7
151 function tokenByIndex(uint256 index) external view returns (uint256);
152
153 // Not implemented
154 //
155 // Selector: tokenOfOwnerByIndex(address,uint256) 2f745c59
156 function tokenOfOwnerByIndex(address owner, uint256 index)
157 external
158 view
159 returns (uint256);
160
161 // Selector: totalSupply() 18160ddd
162 function totalSupply() external view returns (uint256);
163}
164
165// Selector: e562194d
138interface ERC721UniqueExtensions is Dummy, ERC165 {166interface ERC721UniqueExtensions is Dummy, ERC165 {
139 // Selector: transfer(address,uint256) a9059cbb167 // Selector: transfer(address,uint256) a9059cbb
140 function transfer(address to, uint256 tokenId) external;168 function transfer(address to, uint256 tokenId) external;