Skip to content

Contract Deployment

Soroban Contract Deployment
#![no_std]
 
use soroban_sdk::{contract, contractimpl, Address, BytesN, Env, Symbol, Val, Vec, log};
 
#[contract]
pub struct Deployer;
 
#[contractimpl]
impl Deployer {
    /// Deploy the Wasm contract and invoke an init function
    /// with the given arguments afterwards.
    ///
    /// This has to be authorized by `deployer` (Unless the `Deployer` instance
    /// is used itself as the deployer). This way the whole process is atomic
    /// and it is not possible to frontrun the contract initialization.
    ///
    /// Returns the result of the init function and the contract ID.
 
    pub fn deploy (
        env: Env,
        deployer: Address,
        wasm_hash: BytesN<32>,
        salt: BytesN<32>,
        init_fn: Symbol,
        init_args: Vec<Val>,
    ) -> (Address, Val) {
        // Skip authorization if deployer is the current contract
        if deployer != env.current_contract_address() {
            deployer.require_auth();
        }
 
        // Deploy the contract using the uploaded Wasm with the given hash
        let deployed_address = env
            .deployer()
            .with_address(deployer, salt)
            .deploy(wasm_hash);
 
        // Invoke the init function with the given arguments
        let res: Val = env.invoke_contract(&deployed_address, &init_fn, init_args);
 
        /// @dev should remove logs before deploying smart contracts
        log!(&env, "Deployed address: {}, Init invocation result: {}", deployed_address, res);
 
        // Return the address of the deployed contract and the
        // result of invoking the init result.
 
        (deployed_address, res)
    }
}

Explanation

#![no_std] This attribute prevents linking to the standard library, making the code lighter and more efficient for Soroban contracts. It's big so we save on size.

use soroban_sdk::{contract, contractimpl, Env, log} Imports stuffs from the Soroban SDK. Env is basic Soroban type, we need it because we can't use the Rust standard library.

pub Marks a function as external, meaning it can be invoked outside of the context of the contract code itself.

Authorization

If deployer is not the current contract, it checks if deployer has the necessary permissions.

Deployment

Uses the deployer's address and salt to create a new contract address.

Deploys the Wasm module with the given hash to the new address.

Initialization

Invokes the specified initialization function on the deployed contract with the provided arguments.

Return (Output)

Returns a tuple containing the address of the deployed contract and the result of the initialization function.

Run in Playground

Loading playground...