Skip to content

How to Deploy a Smart Contract on Soroban

 

How to Deploy a Smart Contract on Soroban (2)

As a crucial extension of the Stellar network, Soroban introduces robust capabilities for building and executing smart contracts, fundamentally enhancing how developers can leverage the Stellar ecosystem for decentralized applications. Soroban contracts are programs written in Rust, a performance-oriented programming language known for its safety and speed. This choice ensures that contracts are fast, cost-effective, secure, and less prone to the bugs commonly seen in other contract languages.

With its native compatibility with Stellar's assets and operations, Soroban provides a seamless experience for developers, allowing for the creation of sophisticated logic that can trigger based on on-chain events, manage new types of digital assets, and interact intricately with traditional payments. For more information on what Soroban contracts can and cannot do, please refer to the Soroban FAQs located here.

This tutorial will walk you through the process of deploying a simple Soroban contract on the Stellar Testnet. To read an overview of Soroban and how to get started with Validation Cloud's Node API, read our How-To Guide here

1. Setup Soroban Environment

The following prerequisites are required for developing a Soroban contract locally, and can also be found on Stellar’s developer docs here:

 

If building with a Unix-based system, the Rust toolchain and CLI can be installed like so: 


curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo install --locked soroban-cli
   

If building with Windows, you can install rustup-init.exe to your system. 

Note: Rustup is the preferred toolchain manager. Homebrew is an alternative manager on Unix machines, but it will cause more issues for a Soroban environment.

By this point, ensure that typing the “soroban” command on your terminal outputs the default response for CLI usage commands.

Next, you can configure your CLI for the Stellar Testnet:


    soroban network add \
  --global testnet \
  --rpc-url https://testnet.stellar.validationcloud.io/v1/<YOUR_API_KEY_HERE> \
  --network-passphrase "Test SDF Network ; September 2015"

2. Create Soroban Project & Identity

Once your CLI is configured, you can run the following command to quickly create a Rust workspace project, automatically structuring it in the recommended format:


soroban contract init soroban-validationcloud

# Generate a local identity, pre-loaded with testnet tokens
soroban keys generate --global alice --network testnet

# ensure keys for Alice are funded
   

The config.toml that gets auto-generated comes preconfigured to optimize for a maximum contract size of 64KB. Ethereum, by contrast, only has a maximum contract size of 24KB before it will run out of gas (excluding workarounds like “The Diamond Pattern").

3. Write the Smart Contract

Note the contract lib.rs located in your Soroban Project Directory at soroban-validationcloud > contracts > hello_world > src > lib.rs:


 #![no_std]
use soroban_sdk::{contract, contractimpl, symbol_short, vec, 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]
    }
}

mod test;   

Additionally, find the above contract’s corresponding Cargo.toml file. Every contract should have its own Cargo.toml file that builds upon the top-level Cargo.toml at the root of the project directory.

To test your deployment environment, you can go straight to Step 4 to confirm you are able to get the above hello_world contract deployed to testnet. Or, you can create a new contract that incorporates a new functionality, like incrementing or decrementing some public variable, or transferring a balance from itself back to you.

4. Test the Contract

As you advance along your contract development journey, testing becomes as important, if not more important than the underlying contract development process.


#![cfg(test)]

use super::*;
use soroban_sdk::{symbol_short, vec, Env};

#[test]
fn test() {
    let env = Env::default();
    let contract_id = env.register_contract(None, 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"),]
    );
}



   

While more thorough walkthroughs on testing will be performed later, for now you can activate the default test from initialization (located above, or at soroban-validationcloud > contracts > hello_world > src  > test.rs) to understand what to expect:


 cargo test   

The default test will compile up to nearly 200 items and should ultimately pass. You can experiment by altering certain values in the test to see what makes it fail, and what satisfies passing scores for your relevant use cases.

5. Build and Deploy

To deploy a Soroban contract on the testnet, it must first be built like so:


soroban contract build

#which is similar to this
cargo build --target wasm32-unknown-unknown --release
    

Once built, you can now deploy the contract!


 soroban contract deploy \
  --wasm target/wasm32-unknown-unknown/release/hello_world.wasm \
  --source alice \
  --network testnet
   

The output should be a hash string that you can use to identify your deployment on the Soroban explorer.
More Soroban contract examples can be found here, and a Gitpod environment can also be spun up here to get started with an ephemeral working environment: https://gitpod.io/#https://github.com/stellar/soroban-examples

Start Building on Soroban

For developers seeking familiarity with high-performance blockchains, Soroban offers a compelling case. Its unique focus on specific markets, coupled with high-performance infrastructure and a developer-friendly approach through tools like Validation Cloud's Soroban RPC API, makes it an attractive platform for building the next generation of decentralized applications. Embrace the shift and explore what Soroban has to offer to your Web3 development journey.

About Validation Cloud

Validation Cloud is a Web3 data streaming and infrastructure company that connects organizations into Web3 through a fast, scalable, and intelligent platform. Headquartered in Zug, Switzerland, Validation Cloud offers highly performant and customizable products in staking, node, and data-as-a-service. Learn more at validationcloud.io LinkedIn X