Attribute Macro brush_lang::wrapper

source · []
#[wrapper]
Expand description

This macro allows you to define a wrapper type for traits defined via #[brush::trait_definition]. It is a wrapper for AccountId that knows how to do cross-contract calls to another contract.

To define a wrapper you need to use the follow construction: type TraitName = dyn Trait_1 + Trait_2 ... + Trait_n, where Trait_i contains ink! messages and defined via #[brush::trait_definition]. If Trait_i doesn’t contain ink! messages, then you don’t need to create a wrapper for that trait, because the wrapped methods are created only for ink! messages. Otherwise, you will get an error like

use of undeclared crate or module trait_i_external``

** Note ** The first argument of method should be a reference on AccountId of callee contract(even if the signature of the method requires a mutable reference). ** Note ** Crated wrapper is only a type, so you can’t create an instance of this object. ** Note ** The wrapper contains only ink’s methods of the trait, it doesn’t include a method of super traits. If you want to wrap them too, you need to explicitly specify them.

Example: Definition

{
use brush::traits::AccountId;

#[brush::trait_definition]
pub trait Trait1 {
    #[ink(message)]
    fn foo(&mut self) -> bool;
}

#[brush::wrapper]
type Trait1Ref = dyn Trait1;

#[brush::trait_definition]
pub trait Trait2 {
    #[ink(message)]
    fn bar(&mut self, callee: brush::traits::AccountId) {
        let foo_bool = Trait1Ref::foo(&callee);
    }
}

#[brush::wrapper]
type Trait1and2Ref = dyn Trait1 + Trait2;

// Example of explicit call
let to: AccountId = [0; 32].into();
let callee: AccountId = [0; 32].into();
Trait1and2Ref::bar(&to, callee);

// Example of implicit call
let to: &Trait1and2Ref = &to;
to.bar(callee);

// Example how to get ink! call builder
let to: AccountId = [0; 32].into();
let builder_for_foo: ::ink_env::call::CallBuilder<_, _, _, _> = Trait1and2Ref::foo_builder(&to);
let ink_result: Result<bool, ink_env::Error> = builder_for_foo.fire();
}