Attribute Macro sewup_derive::ewasm_fn[][src]

#[ewasm_fn]
Expand description

helps you to build your handlers in the contract

This macro also generate the function signature, you can use ewasm_fn_sig! macro to get your function signature;

#[ewasm_fn]
fn check_input_object(s: SimpleStruct) -> anyhow::Result<()> {
    Ok(())
}

#[ewasm_main]
fn main() -> Result<()> {
    let contract = Contract::new()?;
    match contract.get_function_selector()? {
        ewasm_fn_sig!(check_input_object) => ewasm_input_from!(contract move check_input_object)?,
        _ => return Err(Error::UnknownHandle.into()),
    };
    Ok(())
}

There are two kind of inputs for ewasm_fn macro, first is functional signature, second and more are the fields of abijson. The functional signature can be specific as following ways. #[ewasm_fn(00fdd58e)] or #[ewasm_fn(“00fdd58e”)]

#[ewasm_fn(00fdd58e,
constant=true,
inputs=[
    { "internalType": "address", "name": "account", "type": "address" },
    { "internalType": "uint256", "name": "token_id", "type": "uint256" }
],
name=balanceOf,
outputs=[
    { "internalType": "uint256", "name": "", "type": "uint256" }
],
payable=false,
stateMutability=view
)]

The fields are not required, it can use default value if not provided. The default values of constant, payable are false; the default values of inputs and outputs are []; the default value of stateMutability is view; the default name is the camel case style of the function name.

The handler also can be restricted by called special account with only_by attribute, following are examples:

#[ewasm_fn(only_by=8663DBF0cC68AaF37fC8BA262F2df4c666a41993)]
#[ewasm_fn(only_by="0x8663DBF0cC68AaF37fC8BA262F2df4c666a41993")]
#[ewasm_fn(only_by=0x8663DBF0cC68AaF37fC8BA262F2df4c666a41993)]
#[ewasm_fn(only_by="8663DBF0cC68AaF37fC8BA262F2df4c666a41993")]