logo
logo
Sign in

How to create Stellar Smart Contract using Soroban

avatar
arslan siddiqui
How to create Stellar Smart Contract using Soroban

A Brief about the Stellar Blockchain

Stellar is a public blockchain. Stellar is fast, cheaper, and more efficient than other blockchain-based systems. The lumen is the name of the original cryptocurrency used by Stellar. Smart contract development with Stellar is done in a language called Stellar Smart Contract Language (SSCL), which is a stack-based language designed specifically for the Stellar network.

Understanding Stellar Smart Contracts

Unlike Ethereum, Stellar does not include a built-in smart contract language or virtual machine that allows developers to create smart contract code and enhance the logic of the contract to enable functionalities. A smart contract on Stellar, however, combines transactions and a number of limitations to get the desired outcome.

What is Soroban?

Developed by the Stellar foundation, Sorobon is a smart contract platform that enables the creation and deployment of smart contracts on the Stellar network. It is designed to be quick, scalable, and secure. It uses the same consensus algorithm and smart contract system as Stellar.

Creating a Stellar Smart Contract using Sorobon

Here are the methods for using Soroban to build a smart contract on Stellar:

Install Rust

Whether you use macOS, Linux, or any Unix-like operating system, installing a Rust toolchain is the simplest way to install rustup, i. With the following command, install rustup.

curl — proto ‘=https’ — tlsv1.2 -sSf https://sh.rustup.rs | sh

Install the Soroban CLI”‹

The Soroban CLI can run Soroban contracts in a local sandbox, which is the same environment as the network where the contract would run.

Now, you need to install the Soroban CLI using cargo install

cargo install soroban-cli

Now, Let’s Create a New Project”‹

We can begin by creating a new Rust library using the cargo new command.

cargo new — lib stellar-cal

Now, Open the Cargo.toml,:

[package]

name = “stellar-cal”

version = “0.1.0”

edition = “2021”

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]

crate-type = [“cdylib”]

[features]

testutils = [“soroban-sdk/testutils”]

[dependencies]

soroban-sdk = “0.6.0”

[dev_dependencies]

soroban-sdk = { version = “0.6.0”, features = [“testutils”] }

[profile.release]

opt-level = “z”

overflow-checks = true

debug = 0

strip = “symbols”

debug-assertions = false

panic = “abort”

codegen-units = 1

lto = true

[profile.release-with-logs]

inherits = “release”

debug-assertions = true

Write the Code”‹

Once the above-mentioned steps are completed, now you need to open the src/lib.rs file and copy-paste the given code.

#![no_std]

use soroban_sdk::contractimpl;

pub struct Contract;

#[contractimpl]

impl Contract {

pub fn add(a: u32, b: u32) -> u32 {

a + b

}

pub fn sub(a: u32, b: u32) -> u32 {

a — b

}

pub fn mul(a: u32, b: u32) -> u32 {

a * b

}

pub fn div(a: u32, b: u32) -> u32 {

a / b

}

}

#[cfg(test)]

mod test {

use super::{Contract, ContractClient};

use soroban_sdk::Env;

fn init() -> ContractClient {

let env = Env::default();

let contract_id = env.register_contract(None, Contract);

let client = ContractClient::new(&env, &contract_id);

client

}

#[test]

fn add() {

let add = init().add(&10, &12);

assert_eq!(add, 22);

}

#[test]

fn sub() {

let sub = init().sub(&20, &5);

assert_eq!(sub, 15);

}

#[test]

fn mul() {

let mul = init().mul(&5, &6);

assert_eq!(mul, 30);

}

#[test]

fn div() {

let div = init().div(&14, &7);

assert_eq!(div, 2);

}

}

Run the Tests”‹

Once we have written the code, we will now run the “cargo test” and observe the contract run. We should be getting the following output:

running 4 tests

test test::div … ok

test test::add … ok

test test::sub … ok

test test::mul … ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

We can also try to change the values in the test to find out how it works.

Get in touch with our smart contract developers, having significant experience building smart contracts utilizing Soroban, if you’re wanting to design and build Stellar Smart Contracts.

collect
0
avatar
arslan siddiqui
guide
Zupyak is the world’s largest content marketing community, with over 400 000 members and 3 million articles. Explore and get your content discovered.
Read more