Function ink_env::call::build_create

source ·
pub fn build_create<ContractRef>(
) -> CreateBuilder<<ContractRef as ContractEnv>::Env, ContractRef, Unset<<<ContractRef as ContractEnv>::Env as Environment>::Hash>, Set<LimitParamsV2<<ContractRef as ContractEnv>::Env>>, Unset<<<ContractRef as ContractEnv>::Env as Environment>::Balance>, Unset<ExecutionInput<EmptyArgumentList>>, Unset<Salt>, Unset<ReturnType<()>>>where
    ContractRef: ContractEnv,
Expand description

Returns a new CreateBuilder to build up the parameters to a cross-contract instantiation.

Example

Note: The shown examples panic because there is currently no cross-calling support in the off-chain testing environment. However, this code should work fine in on-chain environments.

Example 1: Returns Address of Instantiated Contract

The below example shows instantiation of contract of type MyContract.

The used constructor:

  • has a selector equal to 0xDEADBEEF
  • is provided with 4000 units of gas for its execution
  • is provided with 25 units of transferred value for the new contract instance
  • receives the following arguments in order 1. an i32 with value 42 2. a bool with value true 3. an array of 32 u8 with value 0x10
let my_contract: MyContractRef = build_create::<MyContractRef>()
    .code_hash(Hash::from([0x42; 32]))
    .endowment(25)
    .exec_input(
        ExecutionInput::new(Selector::new(ink::selector_bytes!("my_constructor")))
            .push_arg(42)
            .push_arg(true)
            .push_arg(&[0x10u8; 32]),
    )
    .salt_bytes(&[0xDE, 0xAD, 0xBE, 0xEF])
    .returns::<MyContractRef>()
    .instantiate();

Example 2: Handles Result from Fallible Constructor

let my_contract: MyContractRef = build_create::<MyContractRef>()
    .code_hash(Hash::from([0x42; 32]))
    .endowment(25)
    .exec_input(
        ExecutionInput::new(Selector::new(ink::selector_bytes!("my_constructor")))
            .push_arg(42)
            .push_arg(true)
            .push_arg(&[0x10u8; 32]),
    )
    .salt_bytes(&[0xDE, 0xAD, 0xBE, 0xEF])
    .returns::<Result<MyContractRef, ConstructorError>>()
    .instantiate()
    .expect("Constructor should have executed successfully.");