Skip to main content

Cross-Contract

A cross-contract invocation is a powerful (yet expensive) kind of contract interaction. A contract invocation is similar to starting a new process because the code that runs will be in a separate address space, meaning that they do not share any data other than what was passed in the invocation. While a contract invocation typically transfers control to a different contract, it is possible to transfer control to the currently running contract. Regardless of whether the contract that receives control is a different contract or the currently running contract, the value returned by get_invoking_contract will be the previous value of get_current_contract. A contract invocation can only access the public methods of a contract.

If a contract contains a public function f, then invoking f can be done by making a Rust function call to f::invoke.

Some contracts, such as the token contract, only export the contract invocation functions. In doing so, they are able to assign those functions friendly names. For example, initialize

#[contractimpl(export_if = "export")]
impl TokenTrait for Token {
fn initialize(e: Env, admin: Identifier, decimal: u32, name: Binary, symbol: Binary) {

is exported as

pub use crate::contract::initialize::invoke as initialize;

This function is then easily called by the liquidity pool contract.