Sui极简入门,部署你的第一个Sui合约

  • A+
所属分类:以太坊ETH

chatGPT账号

Sui极简入门,部署你的第一个Sui合约

Sui 是一个专注于扩展和性能的区块链平台。它由Mysten Labs开发,旨在解决当前区块链面临的可扩展性和效率问题。Sui使用Move作为编程语言,专为区块链和智能合约设计,强调安全性和可验证性。

本文不涉及Move的语法讲解,仅演示Sui的示例合约部署。

安装Sui

在MacOS中最简单的方式是使用Homebrew,其他平台可以参考官方安装指南。

brew install sui

安装好之后可以使用sui --version命令查看安装的版本。如果输出了版本号,说明已经成功安装了Sui。

 ~ sui --version
sui 1.27.2-homebrew

创建项目

使用sui move new [project name]命令创建一个新的Sui项目。

sui move new hello_move

项目创建好后会生成下面的目录结构。

.
├── Move.toml
├── sources
│   └── hello_world.move
└── tests
    └── hello_world_tests.move

Move.toml是一个清单文件,主要描述包的定义和一些配置信息。

[package]
name = "hello_world"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = ""           # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."]      # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
hello_world = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

注意,Move.toml文件中[dependencies]下第8行的rev配置项,确保值为framework/testnet,之后的合约部署会在测试网中进行,有一些脚手架工具自动生成时默认会使用framework/devnet,若不修改会导致报错。

Sui = { 
  git = "https://github.com/MystenLabs/sui.git", 
  subdir = "crates/sui-framework/packages/sui-framework", 
  rev = "framework/testnet" 
}

打开hello_world.move文件,贴入下面的示例代码。

module hello_world::hello {
    use std::ascii::{String, string};
    use sui::object::{Self,UID};
    use sui::transfer::transfer;
    use sui::tx_context::{TxContext, sender};

    public struct Hello has key{
        id:UID,
        say: String
    }

    fun init(ctx: &mut TxContext) {
        let hello_move = Hello {
            id:object::new(ctx),
            say: string(b"CarryWang"),
        };
        transfer(hello_move, sender(ctx));
    }
}

使用sui move build编译项目代码。该命令会检查是否有语法错误,如果编译通过,项目根目录中会生成一个build文件夹。

sui move build

部署上链

使用sui client publish命令可以将hello_world.move部署到测试网。注意,第一次部署,如果你没有本地钱包,直击运行命令后会提示你没有发现配置文件,是否要连接Sui Full node server,根据指示选择y

hello_world sui client publish
Config file ["/Users/carry/.sui/sui_config/client.yaml"] doesn't exist, 
do you want to connect to a Sui Full node server [y/N]?y

之后会要求你设置Sui Full node server的URL,直接敲回车默认会使用Sui的测试网。接着会需要你选择生成钱包地址的加密方式,选择ed25519,输入0,敲回车。

hello_world sui client publish
Config file ["/Users/carry/.sui/sui_config/client.yaml"] doesn't exist, do you want to connect to a Sui Full node server [y/N]?y
Sui Full node server URL (Defaults to Sui Testnet if not specified) : 
Select key scheme to generate keypair (0 for ed25519, 1 for secp256k1, 2: for secp256r1):
0

成功生成后会输出下面的代码。注意第9行,显示了生成的地址别名和hash值。

hello_world sui client publish
Config file ["/Users/carry/.sui/sui_config/client.yaml"] doesn't exist, do you want to connect to a Sui Full node server [y/N]?y
Sui Full node server URL (Defaults to Sui Testnet if not specified) : 
Select key scheme to generate keypair (0 for ed25519, 1 for secp256k1, 2: for secp256r1):
0
Keys saved as Base64 with 33 bytes `flag || privkey` ($BASE64_STR). 
        To see Bech32 format encoding, use `sui keytool export $SUI_ADDRESS` where 
        $SUI_ADDRESS can be found with `sui keytool list`. Or use `sui keytool convert $BASE64_STR`.
Generated new keypair and alias for address with scheme "ed25519" [sleepy-epidote: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb]
Secret Recovery Phrase : [salon dignity abandon glove easy arrange lunch kit cushion tone jungle few]

同时,使用sui client addresses可以查询生成的钱包地址。

sui client addresses
╭────────────────┬────────────────────────────────────────────────────────────────────┬────────────────╮
│ alias          │ address                                                            │ active address │
├────────────────┼────────────────────────────────────────────────────────────────────┼────────────────┤
│ sleepy-epidote │ 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb │ *              │
╰────────────────┴────────────────────────────────────────────────────────────────────┴────────────────╯

部署测试网会消耗SUI,目前本地人钱包里时没有钱的,所以需要先去领水。在浏览器中访问http://getsui.com/0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb,注意将上面的地址替换成你的钱包地址。
浏览器会返回下面的内容。

一切准备就绪,再次运行sui client publish。可以看到Transaction Digest。注意131行的PackageID,这就是部署后的合约地址0x6d99eb76ae3d308a7b224cb76b9b2583bf061b9018b8ba21cea0868402814655

Successfully verified dependencies on-chain against source.
Transaction Digest: D8EDmkZJQPDo2gmk28WRmbuBMnBmTZCrWcEJGFsivRFb
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Data                                                                                             │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Sender: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb                                   │
│ Gas Owner: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb                                │
│ Gas Budget: 10846400 MIST                                                                                    │
│ Gas Price: 1000 MIST                                                                                         │
│ Gas Payment:                                                                                                 │
│  ┌──                                                                                                         │
│  │ ID: 0x46acc1c14022eb1571239a99b9bb6cdad2837a5c9f4f440fc84398558fc778d8                                    │
│  │ Version: 30259534                                                                                         │
│  │ Digest: GBMGFUgkPT6tJSytyyuT69WsVoJXjPzgVCKtLEeNTrDa                                                      │
│  └──                                                                                                         │
│                                                                                                              │
│ Transaction Kind: Programmable                                                                               │
│ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ Input Objects                                                                                            │ │
│ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │
│ │ 0   Pure Arg: Type: address, Value: "0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb" │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ ╭─────────────────────────────────────────────────────────────────────────╮                                  │
│ │ Commands                                                                │                                  │
│ ├─────────────────────────────────────────────────────────────────────────┤                                  │
│ │ 0  Publish:                                                             │                                  │
│ │  ┌                                                                      │                                  │
│ │  │ Dependencies:                                                        │                                  │
│ │  │   0x0000000000000000000000000000000000000000000000000000000000000001 │                                  │
│ │  │   0x0000000000000000000000000000000000000000000000000000000000000002 │                                  │
│ │  └                                                                      │                                  │
│ │                                                                         │                                  │
│ │ 1  TransferObjects:                                                     │                                  │
│ │  ┌                                                                      │                                  │
│ │  │ Arguments:                                                           │                                  │
│ │  │   Result 0                                                           │                                  │
│ │  │ Address: Input  0                                                    │                                  │
│ │  └                                                                      │                                  │
│ ╰─────────────────────────────────────────────────────────────────────────╯                                  │
│                                                                                                              │
│ Signatures:                                                                                                  │
│    wxgbN058244H1RvK6L73mRe25UaDlnymDTDELMJ1G6xCUnhbV34ArQUqlZ4nBra2dasKneHmJIfdroqBo31FBA==                  │
│                                                                                                              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Effects                                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Digest: D8EDmkZJQPDo2gmk28WRmbuBMnBmTZCrWcEJGFsivRFb                                              │
│ Status: Success                                                                                   │
│ Executed Epoch: 415                                                                               │
│                                                                                                   │
│ Created Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0x5cbcb78826d34b202d9847c7b43c8bcce82e3c119475c5e090b5fd8bfcb7138d                         │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb )  │
│  │ Version: 30259535                                                                              │
│  │ Digest: 7K5iNXKQhZDjYg5Jm8atujWBEWipY22VNJHiPLgbf8q7                                           │
│  └──                                                                                              │
│  ┌──                                                                                              │
│  │ ID: 0x6d99eb76ae3d308a7b224cb76b9b2583bf061b9018b8ba21cea0868402814655                         │
│  │ Owner: Immutable                                                                               │
│  │ Version: 1                                                                                     │
│  │ Digest: 2Ujtc2R4HbwQbFQLKm7MPCahCkPrQheTMUeHR6piu3GV                                           │
│  └──                                                                                              │
│  ┌──                                                                                              │
│  │ ID: 0xe411cce236b3a2a670c38d7be9c69715d76f2ed0b202756b7dd4d75103aed7e7                         │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb )  │
│  │ Version: 30259535                                                                              │
│  │ Digest: 8QAAH9gpwUYQcci6TRX1TAMHqXcxXYrNAUPXenhSWRuY                                           │
│  └──                                                                                              │
│ Mutated Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0x46acc1c14022eb1571239a99b9bb6cdad2837a5c9f4f440fc84398558fc778d8                         │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb )  │
│  │ Version: 30259535                                                                              │
│  │ Digest: GUHm5XPsJAz9QzA9eNH1Tw7YCubhmFwNqp1eX28D5KVH                                           │
│  └──                                                                                              │
│ Gas Object:                                                                                       │
│  ┌──                                                                                              │
│  │ ID: 0x46acc1c14022eb1571239a99b9bb6cdad2837a5c9f4f440fc84398558fc778d8                         │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb )  │
│  │ Version: 30259535                                                                              │
│  │ Digest: GUHm5XPsJAz9QzA9eNH1Tw7YCubhmFwNqp1eX28D5KVH                                           │
│  └──                                                                                              │
│ Gas Cost Summary:                                                                                 │
│    Storage Cost: 8846400 MIST                                                                     │
│    Computation Cost: 1000000 MIST                                                                 │
│    Storage Rebate: 978120 MIST                                                                    │
│    Non-refundable Storage Fee: 9880 MIST                                                          │
│                                                                                                   │
│ Transaction Dependencies:                                                                         │
│    3Hry5CyWQUTNV5nRZTNxoWyfN9zxBEcwsYkXUyTHCrmE                                                   │
│    6c3hAbXwoKqSv8Grf4wv7VAJYpT7UnPxV3zqeRjmN1JY                                                   │
│    HCXZg8x9C7sLa4QwjotdM2GWZLkVASyJXHNF8VASedc2                                                   │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────╮
│ No transaction block events │
╰─────────────────────────────╯

╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Object Changes                                                                                   │
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Created Objects:                                                                                 │
│  ┌──                                                                                             │
│  │ ObjectID: 0x5cbcb78826d34b202d9847c7b43c8bcce82e3c119475c5e090b5fd8bfcb7138d                  │
│  │ Sender: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb                    │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb ) │
│  │ ObjectType: 0x6d99eb76ae3d308a7b224cb76b9b2583bf061b9018b8ba21cea0868402814655::hello::Hello  │
│  │ Version: 30259535                                                                             │
│  │ Digest: 7K5iNXKQhZDjYg5Jm8atujWBEWipY22VNJHiPLgbf8q7                                          │
│  └──                                                                                             │
│  ┌──                                                                                             │
│  │ ObjectID: 0xe411cce236b3a2a670c38d7be9c69715d76f2ed0b202756b7dd4d75103aed7e7                  │
│  │ Sender: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb                    │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb ) │
│  │ ObjectType: 0x2::package::UpgradeCap                                                          │
│  │ Version: 30259535                                                                             │
│  │ Digest: 8QAAH9gpwUYQcci6TRX1TAMHqXcxXYrNAUPXenhSWRuY                                          │
│  └──                                                                                             │
│ Mutated Objects:                                                                                 │
│  ┌──                                                                                             │
│  │ ObjectID: 0x46acc1c14022eb1571239a99b9bb6cdad2837a5c9f4f440fc84398558fc778d8                  │
│  │ Sender: 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb                    │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb ) │
│  │ ObjectType: 0x2::coin::Coin<0x2::sui::SUI>                                                    │
│  │ Version: 30259535                                                                             │
│  │ Digest: GUHm5XPsJAz9QzA9eNH1Tw7YCubhmFwNqp1eX28D5KVH                                          │
│  └──                                                                                             │
│ Published Objects:                                                                               │
│  ┌──                                                                                             │
│  │ PackageID: 0x6d99eb76ae3d308a7b224cb76b9b2583bf061b9018b8ba21cea0868402814655                 │
│  │ Version: 1                                                                                    │
│  │ Digest: 2Ujtc2R4HbwQbFQLKm7MPCahCkPrQheTMUeHR6piu3GV                                          │
│  │ Modules: hello                                                                                │
│  └──                                                                                             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Balance Changes                                                                                   │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│  ┌──                                                                                              │
│  │ Owner: Account Address ( 0x5bdcb0dd355f072607a5643b72197ec4409ab053856165d4b32b166a6cdcd6cb )  │
│  │ CoinType: 0x2::sui::SUI                                                                        │
│  │ Amount: -8868280                                                                               │
│  └──                                                                                              │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯

使用Sui的区块链浏览器可以查看更详细的内容。访问https://testnet.suivision.xyz/,在搜索框中贴入PackageID的哈希值。可以在网页中查看到详细的信息以及源代码。

至此,Sui合约部署工作已全部完成。

免责声明

发文时比特币价格:$71249

当前比特币价格:[crypto coins=”BTC” type=”text” show=”price”]

当前比特币涨幅:[crypto coins=”BTC” type=”text” show=”percent”]

免责声明:

本文不代表路远网立场,且不构成投资建议,请谨慎对待。用户由此造成的损失由用户自行承担,与路远网没有任何关系;

路远网不对网站所发布内容的准确性,真实性等任何方面做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,路远网对项目的真实性,准确性等任何方面均不做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,路远网不对其构成任何投资建议,用户由此造成的损失由用户自行承担,与路远网没有任何关系;

路远区块链研究院声明:路远区块链研究院内容由路远网发布,部分来源于互联网和行业分析师投稿收录,内容为路远区块链研究院加盟专职分析师独立观点,不代表路远网立场。

本文是全系列中第74 / 309篇:行业技术

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的电报
  • 这是我的电报扫一扫
  • weinxin
chatGPT账号
路远

发表评论

您必须登录才能发表评论!