If you’re ready to get comfortable with the infrastructure in this SUI, publishing a smart contract is a fine way to get started. The smart contracts on SUI, written in the Move program language, are a secure and efficient way in which to program on-chain applications. This article is a step-by-step guide to deploying your first SUI smart contract—setting up your environment, defining a basic contract, and publishing on the SUI network.
Step 1: Setting Up Your Development Environment
Before you can write and deploy a smart contract on SUI, you need the right tools. Here’s how to set up your environment:
- Install Rust: Since Move is built with Rust underpinnings, you’ll need Rust installed. Head to Rust official website and follow the instructions for your operating system. Run rustup update in your terminal to ensure you’re on the latest stable version.
- Get the SUI CLI: The SUI Command Line Interface (CLI) is your gateway to interacting with the network. Download it by cloning the SUI repository from GitHub (git clone https://github.com/MystenLabs/sui.git), then navigate to the directory and run cargo install –path sui. Verify it’s working with sui –version.
- Set Up a Wallet: You’ll need a SUI wallet to deploy your contract and pay gas fees. Install the SUI Wallet browser extension or use the CLI to generate one (sui client new-address ed25519). Fund it with testnet SUI tokens via the SUI Discord faucet or testnet explorer.
- Choose an IDE: Any text editor works, but Visual Studio Code with the Move extension simplifies syntax highlighting and error checking. Install it from the VS Code marketplace.
Once these are in place, you’re ready to code. Test your setup by running sui client—it should connect to the testnet and show your wallet address.
Step 2: Writing Your First SUI Smart Contract
SUI smart contracts are written in Move, a language designed for safety and simplicity. Let’s create a basic contract that mints a custom token—a great intro to Move’s object-centric model.
- Create a Project: In your terminal, make a new directory (mkdir my-first-contract) and navigate into it (cd my-first-contract). Initialize a Move package with sui move new my_token. This creates a folder structure with a sources directory for your code.
Write the Code: Open sources/my_token.move in your editor and add this simple contract:
module my_token::custom_token {
use sui::object::{Self, UID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};
struct CustomToken has key, store {
id: UID,
value: u64,
}
public entry fun mint(value: u64, ctx: &mut TxContext) {
let token = CustomToken {
id: object::new(ctx),
value,
};
transfer::transfer(token, tx_context::sender(ctx));
}
public entry fun get_value(token: &CustomToken): u64 {
token.value
}
}
What’s happening here? This contract defines a CustomToken object with an ID and a value. The mint function creates a token and sends it to the caller, while get_value lets you check its worth. Move’s has key, store means the token can be owned and transferred—core to SUI’s object system.
Understand the Basics: Move uses modules (like my_token::custom_token) to organize code. The sui library provides tools like object for creating IDs and transfer for ownership changes. The TxContext tracks transaction details, like who’s calling the function.
This is a tiny contract, but it’s enough to see Move in action. Save your file—you’re ready to test it.
Step 3: Building and Testing Your Contract
Before deploying, ensure your SUI smart contract works. SUI’s CLI makes this easy.
- Build the Contract: In your project folder, run sui move build. This compiles your Move code into bytecode SUI can understand. If there are errors (like typos), the CLI will flag them—fix and rebuild until it succeeds.
- Test Locally: SUI offers a testing framework to simulate your contract. Create a sources/my_token_test.move file with:
#[test]
fun test_mint() {
use sui::test_scenario;
use my_token::custom_token;
let owner = @0x1;
let scenario = test_scenario::begin(owner);
{
let ctx = test_scenario::ctx(&mut scenario);
custom_token::mint(100, ctx);
};
test_scenario::end(scenario);
}
- Run sui move test. This spins up a mock blockchain, mints a token with value 100, and checks for crashes. If it passes, you’re golden.
Testing’s lightweight but crucial—Move’s strict rules catch bugs early, saving headaches later.
Step 4: Deploying to the SUI Testnet
Now, let’s put your contract on the blockchain! We’ll use the testnet—a safe sandbox with fake SUI tokens.
- Switch to Testnet: Ensure your CLI is on testnet with sui client switch —network testnet. Check your active address (sui client active-address) matches your funded wallet.
- Publish the Contract: Run sui client publish –gas-budget 10000000 from your project folder. The gas budget (in MIST, SUI’s smallest unit) covers deployment fees—10 million MIST is about 0.01 SUI, plenty for this. The CLI compiles your code, sends it to the network, and returns a transaction digest (e.g., “8x9y…”) if successful. Note the “package ID” in the output—you’ll need it later.
- Verify Deployment: Use the SUI Explorer (testnet.sui.io) to search your digest. You’ll see your contract live on-chain, with its objects and details.
Congrats—your contract’s deployed! It’s now a permanent part of the testnet, ready for interaction.
Step 5: Interacting with Your Contract
Deployment’s just the start—let’s mint a token and check it.
- Call the Mint Function: Run sui client call –package <PACKAGE_ID> –module custom_token –function mint –args 50 –gas-budget 1000000. Replace <PACKAGE_ID> with your contract’s ID from the publish step. The 50 is the token’s value. This creates a CustomToken object and sends it to your wallet.
- Check the Result: Use sui client objects to see your new token object—look for its ID and type (CustomToken). To peek at its value, call get_value with sui client call –package <PACKAGE_ID> –module custom_token –function get_value –args <TOKEN_OBJECT_ID> –gas-budget 1000000. It should return 50.
You’ve just minted and queried your first SUI object! X devs often share tips like “keep gas budgets flexible”—if a call fails, tweak the budget and retry.
Conclusion
Deploying your first smart contract on SUI unlocks its potential—secure assets, fast execution, and a developer-friendly vibe. Move’s object-centric approach makes it intuitive, while the CLI ties it all together. This is just the beginning—next in our series, we’ll explore more complex contracts. For now, pat yourself on the back—you’re a SUI developer!