SUI Documentation - Start Writing a Contract
Before you begin, verify that your SUI client version matches the one required by the sui documentation:
sui --version
Example version:
sui 1.39.3-homebrew
Make sure your version matches to avoid potential issues.
SUI Documentation - Create a Contract Project
Run the following command to generate a new contract project:
sui move new my_first_package
After generating the project, you can check the project directory structure:
pwd
Example output:
/Users/admin/work_atom/move/my_first_package
Navigate to the sources
folder:
cd sources
pwd
Example output:
/Users/admin/work_atom/move/my_first_package/sources
Inside the sources
folder, you can create modules as needed. For example:
ls
Example output:
github.move math.move my_first_package.move
Here's an example of the code inside the my_first_package.move
module:
cat my_first_package.move
module my_first_package::my_first_package {
public fun say_hello(name: vector<u8>): vector<u8> {
let mut result = vector::empty<u8>();
vector::append(&mut result, b"hello ");
vector::append(&mut result, name);
result
}
#[test]
public fun test_say_hello() {
let result = say_hello(b"yyle88");
assert!(result == b"hello yyle88", 101);
}
}
Here's an example of the code inside the math.move
module:
cat math.move
module hello_blockchain::math {
public fun add(a: u64, b: u64): u64 {
return a + b
}
#[test]
public fun test_add() {
let result = add(2, 3);
assert!(result == 5, 101);
}
}
Return to the project root directory:
cd ..
pwd
Example output:
/Users/admin/work_atom/move/my_first_package
Edit the project configuration file Move.toml
to assign an address to the new module:
vim Move.toml
Add hello_blockchain = "0x0"
under the [addresses]
section:
[addresses]
my_first_package = "0x0"
hello_blockchain = "0x0"
Once configured, the address for the hello_blockchain
module is properly set.
SUI Documentation - Build and Test a Contract
Run the following command in the project root directory to compile the contract:
sui move build
Example output:
UPDATING GIT DEPENDENCY https://github.com/MystenLabs/sui.git
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING my_first_package
Run the following command to test the contract:
sui move test
Example output:
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING my_first_package
Running Move unit tests
[ PASS ] hello_blockchain::math::test_add
[ PASS ] hello_blockchain::github::test_page
[ PASS ] hello_blockchain::github::test_page_sui_go_guide
[ PASS ] my_first_package::my_first_package::test_say_hello
Test result: OK. Total tests: 4; passed: 4; failed: 0
SUI Documentation - Publish Your Contract
Run the following command to deploy the contract to the blockchain:
sui client publish --gas-budget 50000000
Example output:
UPDATING GIT DEPENDENCY https://github.com/MystenLabs/sui.git
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING my_first_package
Successfully verified dependencies on-chain against source.
Transaction Digest: HXQT2cv15bABW87o6KUSJxbYuHVHCYGHefWpafLnsC4P
After deployment, the Transaction Digest
serves as the unique transaction hash (similar to txid
or txHash
). You can use it to check the deployment status on a blockchain explorer:
Example Deployment Result Example Deployment Result
Note: Avoid deploying the same contract multiple times unnecessarily, as each deployment creates a new contract instance.
Once the contract is deployed, you can interact with it programmatically.
Give me stars. Thank you!!!