Attribute Macro soroban_sdk::contractclient

source ·
#[contractclient]
Expand description

Generates a client for a contract trait.

Can be used to create clients for contracts that live outside the current crate, using a trait that has been published as a standard or shared interface.

Primarily useful when needing to generate a client for someone elses contract where they have only shared a trait interface.

Note that contractimpl also automatically generates a client, and so it is unnecessary to use contractclient for contracts that live in the current crate.

Note that contractimport also automatically generates a client when importing someone elses contract where they have shared a .wasm file.

Examples

use soroban_sdk::{contractclient, contractimpl, symbol, vec, BytesN, Env, Symbol, Vec};

#[contractclient(name = "Client")]
pub trait HelloInteface {
    fn hello(env: Env, to: Symbol) -> Vec<Symbol>;
}

pub struct HelloContract;

#[contractimpl]
impl HelloContract {
    pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
        vec![&env, symbol!("Hello"), to]
    }
}

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

    // Register the hello contract.
    let contract_id = env.register_contract(None, HelloContract);

    // Create a client for the hello contract, that was constructed using
    // the trait.
    let client = Client::new(&env, &contract_id);

    let words = client.hello(&symbol!("Dev"));

    assert_eq!(words, vec![&env, symbol!("Hello"), symbol!("Dev"),]);
}