ink_codegen/generator/
solidity.rs

1use ir::{
2    Callable,
3    CallableWithSelector,
4    Message,
5};
6use proc_macro2::{
7    Ident,
8    TokenStream as TokenStream2,
9};
10use quote::{
11    format_ident,
12    quote,
13};
14use syn::Type;
15
16/// Returns Solidity ABI compatible selector of an ink! message.
17pub(crate) fn solidity_selector(message: &CallableWithSelector<Message>) -> TokenStream2 {
18    let call_type_ident = solidity_call_type_ident(message);
19    quote!(
20        {
21            <__ink_sol_interop__::#call_type_ident>::SELECTOR
22        }
23    )
24}
25
26/// Returns a `u32` representation of the Solidity ABI compatible selector of an ink!
27/// message.
28pub(crate) fn solidity_selector_id(
29    message: &CallableWithSelector<Message>,
30) -> TokenStream2 {
31    let call_type_ident = solidity_call_type_ident(message);
32    quote!(
33        {
34            <__ink_sol_interop__::#call_type_ident>::SELECTOR_ID
35        }
36    )
37}
38
39/// Returns the Solidity call info type identifier for an ink! message.
40///
41/// See [`ink::alloy_sol_types::sol`]
42pub(crate) fn solidity_call_type_ident(message: &CallableWithSelector<Message>) -> Ident {
43    let ident = message.ident();
44    let id = message.composed_selector().into_be_u32();
45    format_ident!("{ident}{id}Call")
46}
47
48/// Returns [`ink::alloy_sol_types::SolType`] representation for the given type.
49pub(crate) fn sol_type(ty: &Type) -> TokenStream2 {
50    match ty {
51        // TODO: (@davidsemakula) replace with more robust solution before release v6
52        // release. Necessary because `alloy_sol_types::SolValue` is not
53        // implemented for u8.
54        Type::Path(ty) if ty.path.is_ident("u8") => {
55            quote! {
56                ::ink::alloy_sol_types::sol_data::Uint<8>
57            }
58        }
59        Type::Reference(ty) => sol_type(&ty.elem),
60        Type::Tuple(tys) => {
61            let tuple_tys = tys.elems.iter().map(sol_type);
62            quote! {
63                (#(#tuple_tys,)*)
64            }
65        }
66        _ => {
67            quote! {
68                <#ty as ::ink::alloy_sol_types::SolValue>::SolType
69            }
70        }
71    }
72}