3_HelloWorld

基础理论知识

  • Sui以Object(对象)为核心(Object有点类似Bitcoin中的UTXO)

  • Sui标准库发布在0x2下面

  • 发布的模块是不可变Object(immutable object),

    • 不可变Object:
      • 不能被修改+不能被转移+不能被删除
      • 任何人都可以使用
  • 结构体的特性(abilities)

    • copy: value can be copied (or cloned by value)
    • drop: value can be dropped by the end of the scope
    • key: value can be used as a key for global storage operations
    • store: value can be held inside a struct in global storage
  • 函数可见性:

    • private: the default visibility of a function; it can only be accessed by functions inside the same module
    • public: the function is accessible by functions inside the same module and by functions defined in another module
    • public(package): the function is accessible by functions of modules inside the same package

HelloWorld

  • 创建move项目: sui move new hello_world

  • Move.toml中的 rev = "framework/testnet" 改为 rev = "framework/devnet"

  • source/hello_world.move 输入完整代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    module hello_world::hello_world {

    use std::string;

    // 自定义结构体
    /// An object that contains an arbitrary string
    public struct HelloWorldObject has key, store {
    id: UID,
    /// A string contained in the object
    text: string::String
    }

    #[lint_allow(self_transfer)]
    public fun mint(ctx: &mut TxContext) {
    let object = HelloWorldObject {
    id: object::new(ctx),
    text: string::utf8(b"hello fucker!")
    };

    // 将 对象转给调用者
    transfer::public_transfer(object, tx_context::sender(ctx));
    }

    }
  • 编译move代码: sui move build

  • 发布package: sui client publish --gas-budget 20000000 ./

    • 可以看到输出之后的package ID
  • export PACKAGE_ID=xxx

  • 调用package : sui client call --function mint --module hello_world --package $PACKAGE_ID --gas-budget 10000000

  • Copyrights © 2021-2024 youngqqcn

请我喝杯咖啡吧~