hackquest logo
M

Martin yeung

HONG KONG

9

Article

7223

View

7

Followers

0
0

Sui - MOVE合約 (1) - 編寫攻略

Martin yeung
2025-08-08 00:35
0
0

MOVE合約: 創建一個用戶產生object及管理object的簡單合約。
合約可以分成7個部分,不是每一個部分都會用上,有些可能不會用上,主要是輔助大家去認識合約中的架構及相關理論。
然而不同的人有不學習的方法,導師分享的習慣也是很好,有利學習及方便自己檢查合約,我自己也是用相同方式學習,效果是不錯的,而且在實戰中,也真的方便自己可以在短時間內檢查合約。

簡單來說,可以分成Dependencies、Error Codes、Structs、Event Structs、Entry Functions、Getters、Helper Functions。

//==============================================================================================// Dependencies//==============================================================================================
use std::string::{String};
use sui::event;
use sui::table::{Self, Table};

在 Dependencies,是指在合約中會用到的東西,可以通過use的指令來接通。

//==============================================================================================// Error Codes //==============================================================================================const EProfileExisted: u64 = 0;

在 Error Codes,是自己在合約中加入的錯誤代碼,可以在不同檢查中使用不同的錯誤代碼,目的是方便查找bug,當然這個是個人習慣喜好,可以用自己的方式進行bug檢查。

//==============================================================================================// Structs //==============================================================================================
public struct State has key{
    id: UID,
    users: Table<address,address>,
}

public struct Profile has key{
    id: UID,
    name: String,
    description: String,
}

在 Struct,是指結構體,結構體是自定義類型。
結構體的介紹: https://sui-book.com/advanced/ch01.struct.html

//==============================================================================================// Init//==============================================================================================
fun init(ctx: &mut TxContext){
    transfer::share_object(State{
        id: object::new(ctx),
        users: table::new(ctx),
});
}

在 Init,所有初始化的需求,可以在這部分處理。
Init的介紹: https://docs.sui.io/concepts/sui-move-concepts/init

//==============================================================================================// Event Structs//==============================================================================================
public struct ProfileCreated has copy, drop {
    id: ID,
    owner: address
}

在 Event Struct,是指事件結構體,與 emit 配合使用。
Event 的介紹: https://docs.sui.io/guides/developer/sui-101/using-events#move-event-structure

//==============================================================================================// Entry Functions//==============================================================================================
public entry fun create_profile(
    name: String,
    description: String,
    state: &mut State,
    ctx: &mut TxContext,
){
    let owner = tx_context::sender(ctx);
    assert!(!table::contains(&state.users,owner), EProfileExisted);
    let uid = object::new(ctx);
    let id = object::uid_to_inner(&uid);
    let new_profile = Profile{
        id:uid,
        name,
        description,
    };
    transfer::transfer(new_profile, owner);
    table::add(&mut state.users,owner,object::id_to_address(&id));
    event::emit(ProfileCreated{
        id, 
        owner
    });
}

在 Entry Functions ,入口函數(entry function)修飾符是用於可以直接被調用的函數。
在這合約中,只有一個入口函數,create_profile 是創建一個 Profile 的 object。
Entry Functions 的介紹: https://examples.sui-book.com/basics/entry-functions.html

//==============================================================================================// Getters//==============================================================================================//
public fun check_if_has_profile(
    user_address: address,
    state: &State,
):Option<address>{
    if(table::contains(&state.users, user_address)){
        option::some(*table::borrow(&state.users, user_address))
    }else{
        option::none()
    }
}

在 Getters ,這類函數主要是讀取合約內的數據。

//==============================================================================================// Helper Function//==============================================================================================
#[test_only]
public fun init_for_testing(ctx: &mut TxContext){
    init(ctx);
}

在 Helper Function,可以放置其他跟合約相關的函數,例如這帶有 [test_only] 的函數,以表示在 test 的情況下才會被執行。

Original
Ecosystem:Sui
Topic:Move
Tag:
sui
move
Update at2025-08-08 00:35
0 / 1000