<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Hrishikesh Banait's blog]]></title><description><![CDATA[Hrishikesh Banait's blog]]></description><link>https://hrishibanait.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 28 Oct 2026 13:27:11 GMT</lastBuildDate><atom:link href="https://hrishibanait.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[An In-Depth Guide to Building a Stellar Smart Contract with Soroban]]></title><description><![CDATA[Stellar has long been a pioneer in the world of decentralized finance (DeFi), providing a scalable, secure, and fast platform for digital asset…]]></description><link>https://hrishibanait.hashnode.dev/an-in-depth-guide-to-building-a-stellar-smart-contract-with-soroban</link><guid isPermaLink="true">https://hrishibanait.hashnode.dev/an-in-depth-guide-to-building-a-stellar-smart-contract-with-soroban</guid><category><![CDATA[Blockchain]]></category><category><![CDATA[Blockchain technology]]></category><category><![CDATA[Web3]]></category><category><![CDATA[Stellar]]></category><category><![CDATA[Rust]]></category><dc:creator><![CDATA[Hrishikesh Banait]]></dc:creator><pubDate>Sat, 31 Aug 2024 19:21:16 GMT</pubDate><content:encoded><![CDATA[<p>Stellar has long been a pioneer in the world of decentralized finance (DeFi), providing a scalable, secure, and fast platform for digital asset transactions. With the introduction of Soroban, Stellar’s native smart contract platform, developers now have the tools to build complex decentralized applications (dApps) directly on the Stellar network. This blog post will take a deep dive into the technical aspects of building a smart contract on Stellar using Soroban, complete with code snippets, diagrams, and practical examples.  </p>
<p>Understanding Soroban:<br />Stellar’s ​​Smart Contract System Soroban is designed to facilitate the creation and deployment of smart contracts on the Stellar network. Unlike other blockchain platforms where smart contracts can be resource intensive and expensive to make, Soroban provides an efficient and simple solution that uses Stellar's consensus protocol and low transaction fees, making it a choice ideal for developers who want to build scalable dApps.</p>
<p>Soroban’s architecture is built around several key elements:</p>
<p>Contract source: The basic logic of smart contracts, typically written in a high-level programming language such as Rust. Contract ID: A unique identifier for each smart contract, ensuring that it can be easily referenced and interacted with. Contract State: A persistent repository for smart contracts, which can maintain state between services.  </p>
<h3 id="heading-setting-up-your-development-environment"><strong>Setting Up Your Development Environment</strong></h3>
<ol>
<li><p><strong>Install Rust:</strong> Soroban contracts are written in Rust, so the first step is to install Rust on your machine. You can do this by visiting the official Rust website and following the installation instructions.</p>
<pre><code class="lang-plaintext"> bashCopy codecurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
</code></pre>
</li>
<li><p><strong>Install the Soroban CLI:</strong> The Soroban Command Line Interface (CLI) is a powerful tool for compiling, deploying, and interacting with smart contracts on Stellar. You can install the Soroban CLI using Cargo, Rust’s package manager.</p>
<pre><code class="lang-plaintext"> bashCopy codecargo install soroban-cli
</code></pre>
</li>
<li><p><strong>Set Up a Stellar Testnet Account:</strong> To deploy and test your contracts, you’ll need a Stellar testnet account. You can create one using the Stellar Laboratory or any Stellar-compatible wallet that supports testnet.</p>
</li>
<li><p><strong>Connect to the Testnet:</strong> Configure your CLI to connect to the Stellar testnet.</p>
<pre><code class="lang-plaintext"> bashCopy codesoroban config set network testnet https://horizon-testnet.stellar.org
</code></pre>
<h3 id="heading-ia"> </h3>
<p> <strong>Writing Your First Smart Contract</strong></p>
<p> Let’s start by writing a simple smart contract that manages a basic token on Stellar. The contract will allow users to mint new tokens, transfer tokens to other accounts, and check their token balance.</p>
<h5 id="heading-step-1-define-the-contract"><strong>Step 1: Define the Contract</strong></h5>
<p> Start by creating a new Rust project for your smart contract:</p>
<pre><code class="lang-plaintext"> bashCopy codecargo new simple_token --lib
 cd simple_token
</code></pre>
<p> Next, define the basic structure of your contract in <code>src/</code><a target="_blank" href="http://lib.rs"><code>lib.rs</code></a>:</p>
<pre><code class="lang-plaintext"> rustCopy code#![no_std]

 use soroban_sdk::{contractimpl, Env};

 pub struct SimpleToken;

 #[contractimpl]
 impl SimpleToken {
     pub fn mint(env: Env, to: Address, amount: i128) {
         let key = SimpleToken::balance_key(&amp;to);
         let current_balance = env.storage().get::&lt;i128&gt;(&amp;key).unwrap_or(0);
         env.storage().set(&amp;key, current_balance + amount);
     }

     pub fn transfer(env: Env, from: Address, to: Address, amount: i128) {
         let from_key = SimpleToken::balance_key(&amp;from);
         let to_key = SimpleToken::balance_key(&amp;to);

         let from_balance = env.storage().get::&lt;i128&gt;(&amp;from_key).unwrap_or(0);
         if from_balance &lt; amount {
             panic!("insufficient balance");
         }

         env.storage().set(&amp;from_key, from_balance - amount);
         let to_balance = env.storage().get::&lt;i128&gt;(&amp;to_key).unwrap_or(0);
         env.storage().set(&amp;to_key, to_balance + amount);
     }

     pub fn balance(env: Env, account: Address) -&gt; i128 {
         env.storage().get::&lt;i128&gt;(&amp;SimpleToken::balance_key(&amp;account)).unwrap_or(0)
     }

     fn balance_key(account: &amp;Address) -&gt; Vec&lt;u8&gt; {
         let mut key = Vec::new();
         key.extend_from_slice(b"balance:");
         key.extend_from_slice(account.as_ref());
         key
     }
 }
</code></pre>
<p> In this contract:</p>
<ul>
<li><p><code>mint</code> allows new tokens to be created and assigned to an account.</p>
</li>
<li><p><code>transfer</code> enables the transfer of tokens between accounts.</p>
</li>
<li><p><code>balance</code> returns the token balance of a specific account.</p>
</li>
</ul>
</li>
</ol>
<h5 id="heading-step-2-compile-and-deploy-the-contract"><strong>Step 2: Compile and Deploy the Contract</strong></h5>
<p>    Compile your smart contract using the Soroban CLI:</p>
<pre><code class="lang-plaintext">    bashCopy codesoroban build
</code></pre>
<p>    After compiling, deploy your contract to the Stellar testnet:</p>
<pre><code class="lang-plaintext">    bashCopy codesoroban deploy --wasm target/wasm32-unknown-unknown/release/simple_token.wasm
</code></pre>
<h3 id="heading-ia-1"> </h3>
<p>    <strong>Interacting with Your Smart Contract</strong></p>
<p>    Once your contract is deployed, you can start interacting with it using the Soroban CLI. Let’s mint some tokens and check the balance of an account.</p>
<h5 id="heading-minting-tokens"><strong>Minting Tokens</strong></h5>
<p>    To mint 100 tokens for a specific account:</p>
<pre><code class="lang-plaintext">    bashCopy codesoroban invoke --id &lt;contract_id&gt; --function mint --arg &lt;account_address&gt; --arg 100
</code></pre>
<h5 id="heading-checking-the-balance"><strong>Checking the Balance</strong></h5>
<p>    To check the balance of the account:</p>
<pre><code class="lang-plaintext">    bashCopy codesoroban invoke --id &lt;contract_id&gt; --function balance --arg &lt;account_address&gt;
</code></pre>
<h3 id="heading-ia-2"> </h3>
<p>    <strong>Advanced Concepts: Adding Logic and Security</strong></p>
<p>    Building a secure and efficient smart contract involves more than just basic functionality. Let’s explore some advanced concepts to enhance our SimpleToken contract.</p>
<h5 id="heading-1-adding-permissions"><strong>1. Adding Permissions</strong></h5>
<p>    To prevent unauthorized minting of tokens, you can add permission checks. One way to do this is to define an admin account and restrict the <code>mint</code> function to only allow calls from this account.</p>
<pre><code class="lang-plaintext">    rustCopy code#[contractimpl]
    impl SimpleToken {
        pub fn mint(env: Env, to: Address, amount: i128) {
            let admin = env.storage().get::&lt;Address&gt;("admin").unwrap();
            if env.invoker() != admin {
                panic!("unauthorized");
            }
            // rest of the mint function...
        }
    }
</code></pre>
<h5 id="heading-2-handling-events"><strong>2. Handling Events</strong></h5>
<p>    Soroban allows you to emit events, which can be used for logging or triggering off-chain actions. For example, you can emit an event every time a transfer occurs:</p>
<pre><code class="lang-plaintext">    rustCopy codeenv.events().publish((from, to), amount);
</code></pre>
<h5 id="heading-3-optimizing-gas-usage"><strong>3. Optimizing Gas Usage</strong></h5>
<p>    Smart contract execution costs gas, so it’s important to write efficient code. Avoid unnecessary computations, and make use of storage wisely. For example, instead of storing large datasets on-chain, consider off-chain storage options and only store references or hashes on-chain.</p>
]]></content:encoded></item></channel></rss>