How to Create Stellar Smart Contracts, Step by Step
Stellar smart contracts run on Soroban, a Rust platform live since February 2024. This guide walks through setup, code, testing and deployment.
On this page
Short answer
A Stellar smart contract is a program written in Rust, compiled to WebAssembly, and run by Soroban on the Stellar network. You create one by installing Rust and the Stellar CLI, writing the contract with the Soroban SDK, testing it locally, then deploying it to the test network and the main network. Soroban went live on Stellar mainnet on 20 February 2024 with the Protocol 20 upgrade.
What is a Stellar smart contract?
A Stellar smart contract is a program that runs on the Stellar network through Soroban, the network's smart-contract platform. It is written in Rust, compiled to WebAssembly, and executed by the Stellar protocol itself, which means it can read and write ledger data and work with the assets that already exist on Stellar.
Soroban went live on Stellar mainnet on 20 February 2024 with the Protocol 20 upgrade, after a test phase that began on the test network in October 2022. It is not an EVM: there is no Solidity, and contracts run in a WebAssembly runtime with fees priced by the resources they use.
The practical difference is integration. A Soroban contract can work with Stellar's built-in assets through the Stellar Asset Contract, so a contract can hold, send or split a stablecoin without a bridge or a wrapper token.
For the network itself, see what Stellar blockchain is before you start writing code.
What do you need before you start?
You need a small set of tools before you write your first contract. The list is short, and every item is free.
| Tool | What it does | Where it comes from |
|---|---|---|
| Rust toolchain | Compiles contract code | rustup, the Rust installer |
| WebAssembly target | Lets Rust build for the chain | Added with rustup |
| Stellar CLI | Builds, tests and deploys contracts | Stellar's own command-line tool |
| Soroban Rust SDK | Contract code and types | A Rust crate, added to the project |
| Test network account | Free test lumens for trials | Stellar test network faucet |
| Code editor | Where you write the code | Any editor with Rust support |
You also need some Rust. You do not need to be a Rust expert, but you should be comfortable with functions, types and error handling. If Rust is new to you, work through a basic Rust tutorial first, because the contract code will make much more sense.
A test network account is free and holds test lumens, which have no value. Never test with real lumens until the contract has passed its tests.
How to create a Stellar smart contract
You create a Stellar smart contract by setting up the tools, writing the contract in Rust, testing it locally, and deploying it first to the test network and then to the main network.
- Install Rust and the WebAssembly target. Install Rust with rustup, then add the WebAssembly target so Rust can compile code for the chain rather than for your computer.
- Install the Stellar CLI. The CLI is the main tool for Soroban work: it creates projects, builds contracts, runs tests and deploys to the network.
- Create a project. Use the CLI to start a new contract project. It creates the folder, the contract code file and the build settings, with a small example contract already inside.
- Write the contract. Replace the example with your own functions, using the Soroban SDK. Each public function becomes a call other accounts can make. Keep functions small and name them by what they do.
- Test locally. Run the contract's unit tests on your own machine. The SDK includes a test environment that runs the same host functions as the real network, so a passing local test means a lot.
- Deploy to the test network. Build the contract to WebAssembly, create a test account, fund it from the faucet, and deploy. Then call the contract's functions from the CLI and check the results.
- Deploy to the main network. Only after the test network version works end to end. Deploying costs a fee in lumens, and the deployed contract cannot be changed, so check the code once more first.
For a contrasting chain, creating an ERC-20 token shows how the same idea works on Ethereum, where contracts are written in Solidity instead of Rust.
How do Stellar contracts handle money and assets?
Stellar contracts handle money through the ledger itself, not through a separate token contract. The network already knows about accounts, balances and assets, and the contract reads and writes that state directly.
The Stellar Asset Contract is the bridge between classic Stellar assets and Soroban. It wraps any asset issued on Stellar, such as a stablecoin, so a contract can hold it, send it or split it. That means a payment contract does not need to mint its own token to move money.
Contracts can also store their own data in three kinds of storage: temporary, persistent and instance storage. Persistent storage costs rent, which is a fee for keeping data on the ledger, so a contract that stores a lot of data costs more to run.
Fees are priced by the resources a transaction uses: processor time, memory, ledger reads and writes, and rent. The model is designed to be predictable, so a contract call costs roughly the same each time it runs with the same inputs.
Example: an escrow contract for a small sale
Example (hypothetical): two people agree a sale of 1,000 dollars, paid in a stablecoin, and they want the money held until the buyer confirms delivery. A Soroban contract holds the funds and releases them only when the buyer signs off.
The buyer deposits 1,000 stablecoins into the contract. The seller ships the item. The buyer checks it and calls the release function, and the contract sends the 1,000 stablecoins to the seller. If the buyer never confirms, a time limit in the contract returns the funds to the buyer.
The fees are small. The base fee on Stellar is 100 stroops, which is 0.00001 lumens. If the contract makes 1,000 calls in a month, the base fees add up to 1,000 x 0.00001 = 0.01 lumens, so the network cost is almost nothing next to the value being held.
The numbers are illustrative. The point is the shape of the design: the contract is a neutral holder, the release rule is written in code, and neither side can move the money alone.
How much does it cost to run a contract?
Running a Soroban contract costs fees in lumens, priced by the resources each call uses. There is no gas auction like on some other chains; the fee model is designed to be predictable.
Four things make up the cost: processor time, memory used, ledger reads and writes, and rent for stored data. A simple call that reads one balance and writes one result costs little. A call that reads many ledger entries and stores large data costs more.
Rent deserves attention because it is easy to forget. Data stored by a contract stays on the ledger and the contract pays rent for it. A contract that stores a growing list will cost more over time, so good designs store as little as possible and keep bulky data off chain.
Deployment itself costs a fee, and the contract's code is stored on the ledger, so a large contract costs more to deploy than a small one. Keep the contract lean and split big logic into separate contracts where it makes sense.
What are the common mistakes?
The most common mistakes with Stellar contracts are writing Ethereum-style code, ignoring storage rent, and deploying to the main network before the test network has proven the contract.
- Writing Solidity-style code in Rust. Soroban is not an EVM, so patterns from Ethereum, such as re-entrancy guards, do not always apply. Learn the Soroban model first.
- Ignoring storage rent. Data stored by a contract costs rent over time. Store the minimum and keep bulky data off chain.
- Skipping local tests. The SDK test environment runs the same host functions as the network. A contract that passes local tests is far more likely to work on chain.
- Deploying to mainnet too early. A deployed contract cannot be changed. Test the full flow on the test network, including failures, before you pay to deploy.
- Forgetting the asset contract. If your contract moves a stablecoin, use the Stellar Asset Contract rather than minting a new token for the job.
- Leaving admin keys in one place. A contract with an admin function needs more than one key holder, or a single lost key can lock the contract.
Key takeaways
- Soroban contracts are written in Rust and compiled to WebAssembly
- Soroban went live on Stellar mainnet on 20 February 2024 with Protocol 20
- The Stellar Asset Contract lets contracts move existing assets such as stablecoins
- Fees are priced by resources used, including rent for stored data
- Test the full flow on the test network before deploying to mainnet
Frequently asked questions
Can I write Stellar smart contracts in Solidity?
No. Soroban contracts are written in Rust and compiled to WebAssembly. Solidity is for EVM chains such as Ethereum. If you know Solidity, the concepts transfer, but the language, the tooling and the execution model are different.
What is the Stellar CLI used for?
The Stellar CLI is the command-line tool for Soroban work. It creates contract projects, builds the code, runs tests, deploys contracts to the test network and the main network, and lets you call a deployed contract's functions from the terminal.
Do I need lumens to test a contract?
You need test lumens, which are free from the test network faucet and have no value. Real lumens are only needed when you deploy to the main network or run a contract there, so all development and testing should happen on the test network first.
Can a deployed Stellar contract be changed?
No, not directly. Once a contract is deployed, its code is fixed. That is why testing matters so much. Teams that need upgrades usually deploy a new contract and point users at it, or build an upgrade path into the contract from the start.
How are Soroban fees different from Ethereum gas?
Soroban fees are priced by the resources a call uses, such as processor time, memory, ledger reads and writes, and rent. There is no gas auction, so fees are more predictable than on chains where users bid for block space.
What can I build with Stellar smart contracts?
Common uses are escrow, token swaps, lending rules, automated payments and loyalty programmes. Anything that needs custom logic on top of Stellar's payment features is a candidate, and the contract can work directly with the assets already on the network.