Move on Sui 基本概念

近期 Sui 出了一个新的教程 Let's Move Sui, 所有的 lesson 我都做了一遍, 分享我的总结。 本文代码: github.com/upupnoah/mo...

Module (模块)

  • Move 代码被组织成模块 , 可以把一个模块看成是区块链上的一个智能合约

  • 可以通过调用这些模块中的函数来与模块进行交互,可以通过事务 或其他 Move 代码来实现, 事务将被发送到并由Sui区块链 进行处理,一旦执行完成,结果的更改将被保存

  • 一个模块module 关键字 + 地址 + 模块名组成, 其中地址可以写成 alias (定义在 Move.toml中), 例如 lets_move_sui

    arduino 复制代码
    module lets_move_sui::sui_fren {
    
    }

基本数据类型

  • 无符号整数: u8, u16, u32, u64, u128, u256
  • Boolean: bool
  • Addresses: address
  • 字符串: String
  • Vector: vector, 例如 vector<u64>
  • 自定义的结构体类型, 例如下面定义的 Struct

Struct (结构体)

  • 结构体是 Sui Move 中的一个基本概念

  • 结构体可以看成是一组相关字段的组合, 每个字段都有自己的类型

  • Sui Move 可以为结构体添加4 种能力: key、store、drop、copy, 这些能力后续会慢慢涉及

  • 下面是一个基本的结构体名为 AdminCap, 内部含有一个 num_frens字段

    arduino 复制代码
        public struct AdminCap {
            num_frens: u64,
        }
  • 添加一个私有函数 init, 这是一个特殊的函数, 会在 module 部署到区块链时, 自动调用

    kotlin 复制代码
        fun init(ctx: &mut TxContext) {
            let admin_cap = AdminCap {
                id: object::new(ctx),
                num_frens: 1000,
            };
            transfer::share_object(admin_cap);
        }

Object

  • Object 是 Sui 中的一个基本概念
  • 在 Sui 中, 所有数据可以视为不同 Object 内部的字段
  • 使用结构体(Struct)来表示Object
  • 可以根据 module 中定义的函数进行创建, 读取, 修改,交互 Object

创建一个 Object

  • 一个 Object 一般都有 key 能力以及含有一个类型为 UIDid 字段

    • sui::object::new : 创建一个新对象。返回必须存储在 Sui 对象中的 UID

      kotlin 复制代码
          public fun new(ctx: &mut TxContext): UID {
              UID {
                  id: ID { bytes: tx_context::fresh_object_address(ctx) },
              }
          }
  • 创建 Ticket: create_ticket

  • 示例代码

    rust 复制代码
    module lets_move_sui::ticket_module {
        use sui::clock::{Self, Clock};
        use sui::object::{Self, ID, UID};
        use sui::transfer;
        use sui::tx_context::{Self, TxContext};
    
        public struct Ticket has key {
            id: UID,
        }
        
        public fun create_ticket(ctx: &mut TxContext, clock: &Clock) {
            let uid = object::new(ctx);
            let ticket = Ticket {
                id: uid,
                expiration_time: clock::timestamp_ms(clock),
            };
            transfer::transfer(ticket, tx_context::sender(ctx));
        }
    }

读取 Ojbect 中的字段

  • 一个 Ticket 一般都有过期时间

    • Ticket 结构体中添加一个 expiration_time 字段
    • ticket_module 中添加一个 is_expired 方法来检测是否过期
rust 复制代码
module lets_move_sui::ticket_module {
    use sui::clock::{Self, Clock};
    use sui::object::{Self, ID, UID};
    use sui::transfer;
    use sui::tx_context::{Self, TxContext};

    public struct Ticket has key {
        id: UID,
        expiration_time: u64,
    }
    
    public fun create_ticket(ctx: &mut TxContext, clock: &Clock) {
        let uid = object::new(ctx);
        let ticket = Ticket {
            id: uid,
            expiration_time: clock::timestamp_ms(clock),
        };
        transfer::transfer(ticket, tx_context::sender(ctx));
    }
    
    public fun is_expired(ticket: &Ticket, clock: &Clock): bool {
        ticket.expiration_time <= clock::timestamp_ms(clock)
    }
}

修改 Object 中的字段

  • 需要在函数中传入一个可变引用

    kotlin 复制代码
    module 0x123::my_module {
       use std::vector;
       use sui::object::{Self, UID};
       use sui::transfer;
       use sui::tx_context::TxContext;
    
       struct MyObject has key {
           id: UID,
           value: u64,
       }
    
       fun init(ctx: &mut TxContext) {
           let my_object = MyObject {
               id: object::new(ctx),
               value: 10,
           };
           transfer::share_object(my_object);
       }
    
       public fun set_value(global_data: &mut MyObject, value: u64) {
           global_data.value = value;
       }
    }

删除一个 Object

  • sui::object::delete : 删除该对象及其 UID 。这是消除 UID 的唯一方法

    kotlin 复制代码
        public fun delete(id: UID) {
            let UID { id: ID { bytes } } = id;
            delete_impl(bytes)
        }
  • 示例代码

    rust 复制代码
    module 0x123::ticket_module {
      use sui::clock::{Self, Clock};
      use sui::object::{Self, UID};
      use sui::transfer;
      use sui::tx_context::TxContext;
     
      struct Ticket has key {
          id: UID,
          expiration_time: u64,
      }
     
      public fun clip_ticket(ticket: Ticket) {
         let Ticket {
             id,
             expiration_time: _,
         } = ticket;
         object::delete(id);
      }
    }

Math

  • 在 Move 中数学运算和其他编程语言非常相似
  • 需要注意的是 x^y 表示 以 x 为底, 指数为 y

类型转换

  • 相同的类型可以直接进行算术运算, 但是不同的类型之间想要进行算术运算, 则需要进行转换

  • 在 Move 中类型转换可以使用这种形式: (x as <type>), 例如(x as u64)

    kotlin 复制代码
    fun mixed_types_math_error(): u64 {
       let x: u8 = 1;
       let y: u64 = 2;
       // This will fail to compile as x and y are different types. One is u8, the other is u64.
       x + y
    }
    
    fun mixed_types_math_ok(): u64 {
       let x: u8 = 1;
       let y: u64 = 2;
       
       // Ok
       (x as u64) + y
    }

Vector

  • vector 相当于是一个动态数组, 这是 Move 内置的一个数据结构, 后续会了解到更多

  • 创建 vector

    ini 复制代码
       // The empty vector does not yet have a type declared. The first value added will determine its type.
       let empty_vector = vector[];
       let int_vector = vector[1, 2, 3];
       let bool_vector = vector[true, true, false];
  • vector in struct field

    arduino 复制代码
       struct MyObject has key {
           id: UID,
           values: vector<u64>,
           bool_values: vector<bool>,
           address_values: vector<address>,
       }

Public function and Private function

  • init 函数必须是 Private 的, 它会在合约部署的时候由 Sui 虚拟机(VM)调用
  • Public 意味着它可以被任何其他的 Move moduletransactions 调用
  • Private 意味着只能被当前 Move module 调用, 并且不能从 transaction 中调用
kotlin 复制代码
module lets_move_sui::sui_fren {
    use sui::tx_context::{Self, TxContext};
    use sui::transfer;
    use sui::object::{Self, ID, UID};
    use std::string::String;
    // use std::vector; // built-in
    use sui::event;

    public struct AdminCap has key {
        id: UID,
        num_frens: u64,
    }

    public struct SuiFren has key {
        id: UID,
        generation: u64,
        birthdate: u64,
        attributes: vector<String>,
    }
    
    fun init(ctx: &mut TxContext) {
        let admin_cap = AdminCap {
            id: object::new(ctx),
            num_frens: 10^3, // 1000
        };
        transfer::share_object(admin_cap);
    }
    
    // change the mint function to transfer the object to the sender
    public fun mint(generation: u64, birthdate: u64, attributes: vector<String>, ctx: &mut TxContext) {
        let uid = object::new(ctx);
        let sui_fren = SuiFren {
            id: uid,
            generation,
            birthdate,
            attributes,
        };
        transfer::transfer(sui_fren, tx_context::sender(ctx));
    }

    public fun burn(sui_fren: SuiFren) {
        let SuiFren {
            id,
            generation: _,
            birthdate: _,
            attributes: _,
        } = sui_fren;
        object::delete(id);
    }
    
    public fun get_attributes(sui_fren: &SuiFren): vector<String> {
        sui_fren.attributes
    }

    public fun update_attributes(sui_fren: &mut SuiFren, attributes: vector<String>) {
        sui_fren.attributes = attributes;
    }
}

Shared Object and Owned Object

  • Shared Objects 可以被任何用户读取和修改

    • 不能并行处理(例如修改), 并且需要严格的检查, 性能慢, 可扩展性差
  • Owned Objects 是私有对象,只有拥有它们的用户才能读取和修改(所有权)

    • 只允许直接所有权,因此如果用户A拥有对象B,而对象B拥有对象C,则用户A无法发送包含对象C的事务, 这个问题可以使用 Receiving<T> 解决(后续会提及)
    • 可以并行处理, 因为涉及它们的 transaction (事务) 不会相互重叠
  • sui::transfer::share_object : 将给定的对象转换为一个可变的共享对象,每个人都可以访问和修改

    kotlin 复制代码
    public fun share_object<T: key>(obj: T) {
        share_object_impl(obj)
    }
  • sui::transfer::transfer : 将 obj 的所有权转移给接收者。obj 必须具有 key 属性

    kotlin 复制代码
    public fun transfer<T: key>(obj: T, recipient: address) {
        transfer_impl(obj, recipient)
    }
  • 示例代码

    kotlin 复制代码
    module lets_move_sui::shared_and_owned {
        use sui::object::{Self, UID};
        use sui::tx_context::{Self, TxContext};
        use sui::transfer;
        public struct SharedObject has key {
            id: UID,
        }
    
        public struct OwnedObject has key {
            id: UID,
        }
    
        public fun create_shared_object(ctx: &mut TxContext) {
            let shared_object = SharedObject {
            id: object::new(ctx),
            };
            transfer::share_object(shared_object);
        }
    
        public fun create_owned_object(ctx: &mut TxContext) {
            let owned_object = OwnedObject {
                id: object::new(ctx),
            };
            transfer::transfer(owned_object, tx_context::sender(ctx));
        }
    }

Event (事件)

  • 什么是 Event? Event 是你的module 将区块链上发生的事情传达给应用程序前端的一种方式

  • 应用程序可以监听某些 Event 来采取行动

  • Event 主要用来给"链下"组件 与 "链上"组件进行交互

  • sui::event::emit : 发出自定义的 Move Event,将数据发送到链下。 由下面的函数签名可知, emit 的参数需要一个包含 copydrop 能力的类型

    kotlin 复制代码
    public native fun emit<T: copy + drop>(event: T);
  • 示例代码

    rust 复制代码
    module 0x123::ticket_module {
      use sui::clock::{Self, Clock};
      use sui::event;
      use sui::object::{Self, ID, UID};
      use sui::transfer;
      use sui::tx_context::{Self, TxContext};
     
      struct Ticket has key {
          id: UID,
          expiration_time: u64,
      }
     
      struct CreateTicketEvent has copy, drop {
         id: ID,
      }
     
      struct ClipTicketEvent has copy, drop {
         id: ID,
      }
     
       public fun create_ticket(ctx: &mut TxContext, clock: &Clock) {
         let uid = object::new(ctx);
         let id = object::uid_to_inner(&uid);
         let ticket = Ticket {
               id: uid,
               expiration_time: clock::timestamp_ms(clock),
         };
         transfer::transfer(ticket, tx_context::sender(ctx));
         event::emit(CreateTicketEvent {
             id,
         });
       }
     
      public fun clip_ticket(ticket: Ticket) {
         let Ticket { id, expiration_time: _ } = ticket;
         object::delete(id);
         event::emit(ClipTicketEvent {
            id: object::uid_to_inner(&id),
         });
      }
    }

总结

模块(module) 组织

Move代码被组织成模块,每个模块类似于其他区块链上的单个智能合约。这种模块化设计在Sui中得到了强调,旨在促使开发者保持模块小巧且分布在不同文件中,同时坚持清晰的数据结构和代码规范。这样做既方便应用程序集成,也便于用户理解。

API和交互

模块通过入口和公共函数提供API,用户可以通过事务或其他Move代码调用这些函数来与模块交互。这些交互由Sui区块链处理,并且会保存任何结果变更。

结构体(Struct)

结构是由相关字段组成的集合,每个字段都有自己的类型,如数字、布尔值和向量。每个结构都可以拥有"能力",包括键(key)、存储(store)、放置(drop)、复制(copy)。这些能力描述了结构在语言中的行为方式。

数据类型 (data type)

Sui Move 支持以下数据类型:无符号整数、布尔值、地址、字符串、向量以及自定义结构类型

对象 (object)

理解对象的生命周期、如何阅读、更新以及删除对象是学习Move语言的重要部分。此外,还需要区分共享对象与拥有对象的概念。

向量 (vector)

向量被理解为动态数组,对于管理智能合约中的项目列表至关重要,反映了区块链应用程序中对于灵活数据结构的需求。

事件 (event)

事件是模块用来通知应用程序前端区块链上发生了某些事情的一种方式。应用可以监听特定的事件,并在这些事件发生时采取行动。

函数 (fun)

  • 公共函数 (使用关键字public):可以从任何其他Move模块和事务中被调用。
  • 私有函数 (使用关键字private):只能在同一个模块中调用,不能从事务中调用。

加入组织, 一起交流/学习!

相关推荐
NineData10 小时前
从个人开发到企业专属集群,NineData 的产品矩阵怎么做的?
运维·数据库·程序员
集成显卡13 小时前
别局限于 Oh-My-Posh,试试 Rust 编写的 starship:极简超快且无限可定制的命令行提示符
程序员·代码规范·命令行
陈随易13 小时前
我也曾离猝死很近
前端·后端·程序员
SimonKing17 小时前
IntelliJ IDEA 配置与插件全部迁移到其他盘,彻底释放C盘空间
java·后端·程序员
程序员cxuan1 天前
说点掏心窝子的话
后端·程序员
本末倒置1831 天前
告别"话痨"提交记录!Git 压缩 Commit 实战指南,代码洁癖党狂喜
面试·程序员·代码规范
程序员鱼皮1 天前
刚刚,微信终于能用 OpenClaw 了!安卓 iOS 都行,附保姆级教程
ai·程序员·编程·ai编程·openclaw
孟陬2 天前
国外技术周刊第 2 期 — 本周热门 🔥 YouTube 视频 TED 演讲 AI 如何能够拯救(而非摧毁)教育
前端·后端·程序员
陈随易2 天前
深度拆解技术架构的三大鸿沟:企业级Claw vs OpenClaw的工程差异
前端·后端·程序员
得物技术2 天前
Claude Code + OpenSpec 正在加速 AICoding 落地:从模型博弈到工程化的范式转移|得物技术
程序员·ai编程·claude