contractimport!() { /* proc-macro */ }
Expand description

Import a contract from its WASM file, generating a client, types, and constant holding the contract file.

Generates in the current module:

  • A Contract trait that matches the contracts interface.
  • A ContractClient struct that has functions for each function in the contract.
  • Types for all contract types defined in the contract.

Examples

use soroban_sdk::{contractimpl, BytesN, Env, Symbol};

mod contract_a {
    soroban_sdk::contractimport!(file = "contract_a.wasm");
}

pub struct ContractB;

#[contractimpl]
impl ContractB {
    pub fn add_with(env: Env, contract_id: BytesN<32>, x: u32, y: u32) -> u32 {
        let client = contract_a::ContractClient::new(&env, contract_id);
        client.add(&x, &y)
    }
}

#[test]
fn test() {
    let env = Env::default();

    // Register contract A using the imported WASM.
    let contract_a_id = env.register_contract_wasm(None, contract_a::WASM);

    // Register contract B defined in this crate.
    let contract_b_id = env.register_contract(None, ContractB);

    // Create a client for calling contract B.
    let client = ContractBClient::new(&env, &contract_b_id);

    // Invoke contract B via its client.
    let sum = client.add_with(&contract_a_id, &5, &7);
    assert_eq!(sum, 12);
}