Skip to main content

Crate miden_base_macros

Crate miden_base_macros 

Source
Expand description

Module for Miden SDK macros

§How to use WIT generation.

An account component is written in three parts:

  1. A #[component_storage] struct declaring the component’s #[storage(...)] fields.
  2. A #[component] trait declaring the component’s API. The trait name yields the WIT interface name and its methods yield the exported functions.
  3. A #[component] impl Trait for Storage block providing the behavior.

Add #[export_type] on every type that is used in an exported method signature.

Example:


#[export_type]
pub struct StructA {
    pub foo: Word,
    pub asset: Asset,
}

#[export_type]
pub struct StructB {
    pub bar: Felt,
    pub baz: Felt,
}

#[component_storage]
struct MyAccountStorage;

#[component]
trait MyAccount {
    fn foo(&self, a: StructA) -> StructB;
}

#[component]
impl MyAccount for MyAccountStorage {
    fn foo(&self, a: StructA) -> StructB {
        ...
    }
}

Custom #[export_type] types referenced in component method signatures must be nameable from the crate root (declared at, or use-imported into, the crate root).

§Escape hatch (disable WIT generation)

in a small fraction of the cases where the WIT generation is not possible (think a type defined only in an external WIT file) or not desirable the WIT generation can be disabled:

To disable WIT interface generation:

  • Don’t use the #[component] trait/impl macros; keep #[component_storage] on the storage struct;

To use manually crafted WIT interface:

  • Put the WIT file in the wit folder;
  • call miden::generate!(); and bindings::export!(MyAccountStorage);
  • implement impl Guest for MyAccountStorage;

Macros§

generate
Generate bindings for an input WIT document.

Attribute Macros§

account
Generates typed active and foreign account bindings for account dependencies on an empty wrapper struct.
auth_script
Marks a component method as the authentication procedure entrypoint (#[auth_script]).
component
Defines an account component’s API and generates the WIT interface.
component_storage
Wires storage metadata for an account component’s storage struct.
export_type
Generates an equvalent type in the WIT interface. Required for every type mentioned in the public methods of an account component.
note
Marks a type/impl as a note script definition.
note_script
Marks a method as the note script entrypoint (#[note_script]).
tx_script
Marks the function as a transaction script