Building Hello World Smart Contracts: Solidity vs. Soroban Rust SDK - A Step-by-Step Guide
Tags: rust, solidity, ethereum, smartcontract
Published: 2024-02-06 19:36:14

In this tutorial, we'll explore the intricacies of two major smart contract programming environments: Ethereum's Solidity and Soroban’s Rust SDK.

## The EVM and Soroban

### What is the EVM?

The Ethereum Virtual Machine (EVM) is a core component of the Ethereum blockchain network. It is a virtual environment that allows for the execution of smart contracts and decentralized applications (dApps). While Ethereum is the primary network utilizing the EVM, other blockchain platforms have adopted or created compatible versions of the EVM. For instance:

- Avalanche has its own virtual machine, the Avalanche Virtual Machine (AVM), but it also supports the EVM through its C-Chain, enabling compatibility with Ethereum-based applications.

- Optimism and Polygon are Layer 2 solutions built on top of the Ethereum blockchain. They use Optimistic Rollups and Polygon's own technology, respectively, but are compatible with the EVM. This means they can run Ethereum smart contracts and dApps.

Each blockchain network can have its own consensus mechanisms, underlying architecture, and protocol implementations. Geth (Go Ethereum, an implementation of an Ethereum node in the Go programming language) is specifically an Ethereum client, and while other networks might draw inspiration or use aspects of Ethereum's technology, they often have distinct core protocols and implementations.

In EVM Land, Solidity is the go-to language for developing smart contracts. Here's a quick rundown for my fellow builders:

- **​​Object-Oriented Approach**: Just like other OOP languages, Solidity organizes code around data and objects, not just functions and logic.
- **High-Level Language**: It abstracts away from the nitty-gritty of computer hardware, making development smoother and more intuitive.
- **Statically-Typed**: Solidity checks your code for errors and type mismatches at compile time, saving you from a lot of headaches later.

What makes Solidity stand out is its role in powering decentralized transactions and managing blockchain accounts. Plus, if you're comfortable with JavaScript, C++, and Python you'll find Solidity's syntax familiar.
 
##What is Soroban?

Soroban is a smart contracts platform with sensibility, built-to-scale, batteries-included and developer-friendly wants.

While it works great with Stellar being that it shares the blockchain's values of scale and sensibility, it neither depends nor requires Stellar at all and can be used by any transaction processor, including other blockchains, L2s, and permissioned ledgers.

Currently, Soroban is available as a part of the v20 of Stellar protocol stable release. The package for the module consists of - smart contracts environment, a Rust SDK, A CLI, and an RPC server. Writing and testing of the contracts can be carried out on developers' local machines or deployed to Testnet.

## Which programming language is used for Stellar smart contracts? 

Introduced in 2022, the Soroban Rust SDK is a suite of tools specifically for writing smart contracts on the Soroban platform. Built on Rust, it enables developers to create decentralized finance applications, automated market makers, and tokenized assets, while also leveraging some of Stellar's core functionalities.

## Why would a blockchain developer choose Rust? 

In the blockchain and smart contract realm, Rust is a standout choice for developers, and here's why:
Speed & Efficiency: Rust whizzes through tasks like a sports car in style. It is super fast, even outpacing C++ when it comes to speed and efficiency so that your blockchain operations are not just quick but also smart, saving on resources.

- Type Safety: Picture Rust's type system as a meticulous inspector, who is watching each bit of your code at compile time. This means fewer errors and a safer environment for your smart contracts.
- Memory Safety Without the Overhead: Rust boasts top-shelf memory safety, acting as an invisible shield against vulnerabilities critical in the blockchain world. And it does this leanly without needing a garbage collector, keeping your projects lean and fortified.
- Conquering Concurrency with Ease: In blockchain, handling simultaneous transactions is like juggling fireballs. Rust excels in managing multiple operations seamlessly, preventing the common complications seen in other languages. This leads to faster, safer processing of transactions, enhancing the overall performance of your smart contracts.

Rust combines speed, safety, and execution efficiency making it an ideal language for blockchain development where such qualities are demanded. So how does Rust stack up against Solidity?

## How to Build a Hello World Smart Contract

We will create two "Hello World" contracts: first in Solidity, then using the Soroban Rust SDK.

Here is the video if you want to follow along:
{% embed https://youtu.be/s8Ron2--78Y?feature=shared %}

## Solidity Version

Open the remix-ide in your browser by navigating to: https://remix.ethereum.org/


Click on the “file explorer” icon onto the left side bar 
Type the file name “HelloWorld.sol” and enter the following code into it:

```
// SPDX-License-Identifier: MIT
// compiler version must be greater than or equal to 0.8.20 and less than 0.9.0
pragma solidity ^0.8.20;

contract HelloWorld {
    function hello(string memory to) public pure returns(string memory){
        string memory greeting = string(abi.encodePacked("hello", to));
        return greeting;
    }
}
```


Once done click the icon just below the “file explorer” icon 
Click “Compile HelloWorld.sol”. 

Once compiled successfully click the icon below “Solidity Compiler” that is “Deploy and run transactions”. 

Without changing any of the values as shown above just click the “Deploy” button to deploy your smart contract. Once deployed you will find your smart contract just below in “Deployed Contracts” heading:

Click “>” before your contract you will see a button “hello” below as our contract has a hello function variable that returns a string composed of “hello” + the value you passed in for the `to` argument. 

Define an value for to and then click the “hello” button to return the greeting:

## Soroban Rust SDK Version

Open the okashi-ide in your browser by navigating to: [https://okashi.dev/](https://okashi.dev/)

Start a new project and name it HelloWorld.
Enter the following code into the ide:



```
#![no_std]
use soroban_sdk::{contract, contractimpl, symbol_short, vec, Env, Symbol, Vec};

#[contract]
pub struct Contract;

#[contractimpl]
impl Contract {
    /// Say Hello to someone or something.
    /// Returns a length-2 vector/array containing 'Hello' and then the value passed as `to`.
    pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
        vec![&env, symbol_short!("Hello"), to]
    }
}

```

Compile the code by clicking the compile button or pushing “cmd+k”

Open the contract tab and push the `hello()` button

Pass in an value for to and click the “confirm” button

## Comparison and Conclusion 

Both Solidity and Soroban provide the functionality to declare public functions. However, their approaches to data handling and state management differ, influenced by their core languages – JavaScript for Solidity and Rust for Soroban. Solidity is ideal for those familiar with JavaScript, while Soroban's Rust foundation offers advantages in concurrency and safety.

## Additional Resources

For developers interested in transitioning from EVM to Soroban, we have comprehensive documentation that covers everything from the basics of the Soroban Rust SDK  compared to Solidity, up to deploying your own smart contracts with Rust. Learn more about migrating from EVM here. 
If you’re looking for more tools and want to learn more about the sdk, you can check out the official Soroban docs [here](https://soroban.stellar.org/docs/migrate/evm/introduction-to-solidity-and-rust).

- [Soroban CLI](https://soroban.stellar.org/docs/getting-started/setup)
- [Soroban Rust SDK](https://soroban.stellar.org/docs/reference/sdks/write-contracts)

Stay tuned for more insights and tutorials in this series, and happy coding in the world of smart contracts!