Posted By : Yogesh
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. Stellar smart contracts are written in a language called Stellar Smart Contract Language (SSCL), which is a stack-based language designed specifically for the Stellar network.
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.
Also, Explore | Stellar Blockchain Use Cases | A Quick Explainer
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.
Also, Visit | Understanding Soroban | Stellar Smart Contract Platform
Here are the methods for using Soroban to build a smart contract on Stellar:
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.
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
We can begin by creating a new Rust library using the cargo new command.
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
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);
}
}
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 team of Stellar blockchain developers, having significant experience building smart contracts utilizing Soroban, if you're wanting to design and build Stellar Smart Contracts.
November 21, 2024 at 12:19 pm
Your comment is awaiting moderation.