Skip to main content
Exports the publicly accessible functions to the Soroban environment. Functions that are publicly accessible in the implementation are invocable by other contracts, or directly by transactions, when deployed.

Syntax

#[contractimpl]
impl ContractName {
    pub fn function_name(env: Env, /* args */) -> ReturnType {
        // implementation
    }
}

Parameters

crate_path
Path
default:"soroban_sdk"
Optional path to the Soroban SDK crate. Use this if you’ve renamed the crate in your Cargo.toml.
contracttrait
bool
default:"false"
Set to true when implementing a trait marked with #[contracttrait]. This enables proper trait implementation and client generation.

Generated Code

The #[contractimpl] macro generates:
  • Contract function exports for the Soroban environment
  • Client implementations for each public function
  • Argument types for each function
  • Contract specification entries
  • Function registration code for test utilities

Visibility

Only pub functions within the impl block are exported as contract functions. Private functions are not accessible from outside the contract.

Example

Define a contract with one function, hello, and call it from within a test using the generated client.
use soroban_sdk::{contract, contractimpl, vec, symbol_short, BytesN, Env, Symbol, Vec};

#[contract]
pub struct HelloContract;

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

#[test]
fn test() {
    let env = Env::default();
    let contract_id = env.register(HelloContract, ());
    let client = HelloContractClient::new(&env, &contract_id);

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

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

See Also