Attribute Macro pbc_contract_codegen::action

source ·
#[action]
Expand description

Public action contract annotation

OPTIONAL HOOK: This is an optional hook. Most contracts will need at least one action hook.

Annotated function is a contract action that can be called from other contracts and dashboard.

If the annotated function panics the contract state will be rolled back to the state before the action.

Must have a signature of the following format:

#[action(shortname = 0x33)]
pub fn action_internal_name(
  context: ContractContext,
  state: ContractState,
  // ... Invocation RPC arguments
) -> (ContractState, Vec<EventGroup>)

with the following constraints:

The invocation receives the previous state, along with a context, and the declared arguments, and must return the new state, along with a vector of pbc_contract_common::events::EventGroup; a list of interactions with other contracts.

§Example

type ContractState = SortedVecMap<Address, bool>;

#[action(shortname = 0x11)]
pub fn vote(
    context: ContractContext,
    mut state: ContractState,
    vote: bool,
) -> ContractState {
    state.insert(context.sender, vote);
    state
}

§Shortname

In addition to the readable name, every invocation needs a shortname, a small unique identifier. This shortname is automatically generated by default, but for cases where a specific shortname is desirable, it can be set using the shortname = <shortname> attribute. This has to be a u32 and gets encoded as LEB128 (up to 5 bytes). These bytes are then encoded as lowercase zero-padded hex.

For example:

type Metadata = u32;

#[action(shortname = 0x53)]
pub fn some_action(
    context: ContractContext,
    mut state: ContractState,
) -> (ContractState, Vec<EventGroup>) {
  // Do things
  (state, vec![])
}