git.delta.rocks / unique-network / refs/commits / bd5e29f9157c

difftreelog

doc: document #[solidity_interface] macro

Yaroslav Bolyukin2021-12-02parent: #4512cc9.patch.diff
in: master

1 file changed

modifiedcrates/evm-coder-macros/src/lib.rsdiffbeforeafterboth
27/// Returns solidity function selector (first 4 bytes of hash) by its27/// Returns solidity function selector (first 4 bytes of hash) by its
28/// textual representation28/// textual representation
29///29///
30/// ```rs30/// ```ignore
31/// use evm_coder_macros::fn_selector;31/// use evm_coder_macros::fn_selector;
32///32///
33/// assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);33/// assert_eq!(fn_selector!(transfer(address, uint256)), 0xa9059cbb);
5555
56/// Returns solidity topic (hash) by its textual representation56/// Returns solidity topic (hash) by its textual representation
57///57///
58/// ```rs58/// ```ignore
59/// use evm_coder_macros::event_topic;59/// use evm_coder_macros::event_topic;
60///60///
61/// assert_eq!(61/// assert_eq!(
184 Ident::new(&name, ident.span())184 Ident::new(&name, ident.span())
185}185}
186186
187/// Derives call enum implementing [`evm_coder::Callable`], [`evm_coder::Weighted`]
188/// and [`evm_coder::Call`] from impl block
189///
190/// ## Macro syntax
191///
192/// `#[solidity_interface(name, is, inline_is, events)]`
193/// - *name*: used in generated code, and for Call enum name
194/// - *is*: used to provide call inheritance, not found methods will be delegated to all contracts
195/// specified in is/inline_is
196/// - *inline_is*: same as is, but selectors for passed contracts will be used by derived ERC165
197/// implementation
198///
199/// `#[weight(value)]`
200/// Can be added to every method of impl block, used for deriving [`evm_coder::Weighted`], which
201/// is used by substrate bridge
202/// - *value*: expression, which evaluates to weight required to call this method.
203/// This expression can use call arguments to calculate non-constant execution time.
204/// This expression should evaluate faster than actual execution does, and may provide worser case
205/// than one is called
206///
207/// `#[solidity_interface(rename_selector)]`
208/// - *rename_selector*: by default, selector name will be generated by transforming method name
209/// from snake_case to camelCase. Use this option, if other naming convention is required.
210/// I.e: method `token_uri` will be automatically renamed to `tokenUri` in selector, but name
211/// required by ERC721 standard is `tokenURI`, thus we need to specify `rename_selector = "tokenURI"`
212/// explicitly
213///
214/// Also, any contract method may have doc comments, which will be automatically added to generated
215/// solidity interface definitions
216///
217/// ## Example
218///
219/// ```ignore
220/// struct SuperContract;
221/// struct InlineContract;
222/// struct Contract;
223///
224/// #[derive(ToLog)]
225/// enum ContractEvents {
226/// Event(#[indexed] uint32),
227/// }
228///
229/// #[solidity_interface(name = "MyContract", is(SuperContract), inline_is(InlineContract))]
230/// impl Contract {
231/// /// Multiply two numbers
232/// #[weight(200 + a + b)]
233/// #[solidity_interface(rename_selector = "mul")]
234/// fn mul(&mut self, a: uint32, b: uint32) -> Result<uint32> {
235/// Ok(a.checked_mul(b).ok_or("overflow")?)
236/// }
237/// }
238/// ```
187#[proc_macro_attribute]239#[proc_macro_attribute]
188pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream {240pub fn solidity_interface(args: TokenStream, stream: TokenStream) -> TokenStream {
189 let args = parse_macro_input!(args as AttributeArgs);241 let args = parse_macro_input!(args as AttributeArgs);
216 stream268 stream
217}269}
218270
271/// ## Syntax
272///
273/// `#[indexed]`
274/// Marks this field as indexed, so it will appear in [`ethereum::Log`] topics instead of data
219#[proc_macro_derive(ToLog, attributes(indexed))]275#[proc_macro_derive(ToLog, attributes(indexed))]
220pub fn to_log(value: TokenStream) -> TokenStream {276pub fn to_log(value: TokenStream) -> TokenStream {
221 let input = parse_macro_input!(value as DeriveInput);277 let input = parse_macro_input!(value as DeriveInput);