Smart Contract Language (eDSL) - contract function form

       

In smart contracts, two types of functions are generally required. Initialization function - init, call contract - invoke;

The main function of the initialization function is to initialize the state of the current contract, and it is generally executed once when the contract is created;

The calling function is the function used by the user to obtain or update the contract after the contract is initialized; for the function of obtaining the state, there are actually two ways,

1. The user directly obtains the local database of the node, that is, an operation that does not go to the chain; 2. The transaction that the user obtains the state, through consensus, leaves traces on the chain;

accomplish:

We still use the process macro of rust to achieve it; mark the ordinary function as the function of our contract;

form:

Parameters and Return Values

#[derive(Serialize, SchemaType)]
struct Address {
    address : AccountAddress,
}

#[derive(Serialize, SchemaType)]
struct Balance {
    balance: u64,
}

init function format

#[init(contract="erc20", parameter="Address", result="Balance", payable,  event)]

invoke function format

#[invoke(contract="erc20",name="balanceof", parameter="Address", result="Balance", payable,  event)]

Attributes:

These two function tags use the same attributes; only the init function does not need to re-set the function name for him.

contract: the name of the contract to which the function belongs;

name: function name, only the receive function has it;

parameter: The parameter of the function, generally the name of the struct or enum type;

result: the return value of the function, generally the name of the struct type;

payable: whether the token can be transferred when the function is executed;

event: enable the event function of the function;

Through the process macro of rust, we can transform the rust language to form our own smart contract language specification. If there are new requirements, you can continue to add new attributes and functions;

Guess you like

Origin blog.csdn.net/xq723310/article/details/123253933