What You’ll Build
You’ll create a simple package that defines aSword object with magical properties - a common starting point for understanding Sui’s object model.
Prerequisites
- Set up your development environment
- Basic understanding of programming concepts
Create a New Package
[package]
name = "my_first_package"
version = "0.0.1"
edition = "2024.beta"
[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/mainnet" }
[addresses]
my_first_package = "0x0"
module my_first_package::sword {
use sui::object::{Self, UID};
use sui::transfer;
use sui::tx_context::{Self, TxContext};
/// A sword with magical properties
public struct Sword has key, store {
id: UID,
magic: u64,
strength: u64,
}
/// A forge for creating swords
public struct Forge has key {
id: UID,
swords_created: u64,
}
/// Module initializer - called once when published
fun init(ctx: &mut TxContext) {
let admin = Forge {
id: object::new(ctx),
swords_created: 0,
};
// Transfer forge to publisher
transfer::transfer(admin, tx_context::sender(ctx));
}
/// Create a new sword
public fun new_sword(
forge: &mut Forge,
magic: u64,
strength: u64,
ctx: &mut TxContext
): Sword {
forge.swords_created = forge.swords_created + 1;
Sword {
id: object::new(ctx),
magic,
strength,
}
}
/// Accessor for sword magic
public fun magic(sword: &Sword): u64 {
sword.magic
}
/// Accessor for sword strength
public fun strength(sword: &Sword): u64 {
sword.strength
}
/// Transfer sword to recipient
public fun transfer_sword(
sword: Sword,
recipient: address,
_ctx: &mut TxContext
) {
transfer::public_transfer(sword, recipient);
}
}
Build Your Package
Compile the Move code:Add Tests
Createsources/sword_tests.move:
sword.move for testing:
Run Tests
Execute the test suite:Publish to Testnet
Transaction Digest: <digest>
Published Objects:
PackageID: 0x<package_id>
Version: 1
Created Objects:
ObjectID: 0x<forge_object_id>
Owner: Account Address ( 0x<your_address> )
Interact with Your Package
Call thenew_sword function:
Understanding Key Concepts
Abilities
key: Object can be used as a key in global storage (has UID)store: Object can be stored inside other objectscopy: Object can be copieddrop: Object can be dropped/destroyed implicitly
Module Initializer
Theinit function runs once when the package is published:
Object Creation
All objects on Sui have a unique ID:Next Steps
- Learn about testing Move code
- Understand Move package structure
- Create custom objects
- Explore more examples
Common Issues
Build errors
If you see “unresolved import” errors, ensure dependencies inMove.toml are correct.
Test failures
Usesui move test --verbose for detailed output.