간단한 MOVE 프로젝트

윤따·2025년 2월 27일
0
hello_sui/
│── Move.toml  # 프로젝트 메타데이터 및 의존성 관리 파일
│── Move.lock  # 자동 생성된 의존성 버전 잠금 파일
└── sources/
    └── hello_sui.move  # 스마트 컨트랙트 파일

우선 터미널에 해당 명령어를 입력하면 위치하는 디렉토리에 hello_sui라는 프로젝트가 생성된다.

sui move new hello_sui

Move.toml

MOVE 패키지 설정을 정의하는 파일

[package]
name = "hello_sui"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move


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



[addresses]
hello_sui = "0x0"

[package] → 패키지 이름 및 버전 정보 포함.
[dependencies] → 다른 Move 패키지 의존성을 정의 (기본적으로 Sui 패키지를 포함).
[addresses] → 블록체인에서 사용할 주소(namespace)를 정의.
(스마트 컨트랙트의 주소)
여기서 "0x0"은 기본값이며, 실제 배포될 때 자동으로 변경됨.
스마트 컨트랙트가 배포되는 "기본 네임스페이스" 역할을 함.

Move.lock 파일

Move 패키지 관리자가 프로젝트의 의존성을 잠그는 역할을 하는 파일
해당 파일은 자동 생성됨 (Move 프로젝트를 생성하거나 빌드할 때 자동으로 생성됨)

스마트 컨트랙트

다른 주소로 hello word 메세지가 담긴 객체를 보내는 간단한 smart contract

module hello_sui::hello_sui;

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(recipient: address, ctx: &mut TxContext) {
    let object = HelloWorldObject {
        id: object::new(ctx),
        text: string::utf8(b"Hello World!"),
    };
    transfer::public_transfer(object, recipient);
}

transfer::public_transfer(object, recipient); = transfer 모듈// object 를 recipient 주소로 보낸다.

ctx: &mut TxContext는 Move에서 현재 실행 중인 트랜잭션의 컨텍스트(context)를 나타냄.

배포하기

sui client publish --gas-budget 100000000

트랜잭션을 실행할 때 필요한 가스(Gas) 비용을 설정
✔ 100000000은 최대 100 SUI를 가스비로 사용할 수 있도록 허용한다는 의미
✔ 실제 사용되는 가스비는 이보다 낮을 수 있음

배포 성공

╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Data                                                                                             │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                   │
│ Gas Owner: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                │
│ Gas Budget: 100000000 MIST                                                                                   │
│ Gas Price: 1000 MIST                                                                                         │
│ Gas Payment:                                                                                                 │
│  ┌──                                                                                                         │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                                    │
│  │ Version: 349161830                                                                                        │
│  │ Digest: DbtVRhVZWy69HvCDZ5mJCnqmZ5Q9oocCKBXqZKS8FA1x                                                      │
│  └──                                                                                                         │
│                                                                                                              │
│ Transaction Kind: Programmable                                                                               │
│ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ Input Objects                                                                                            │ │
│ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │
│ │ 0   Pure Arg: Type: address, Value: "0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a" │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ ╭─────────────────────────────────────────────────────────────────────────╮                                  │
│ │ Commands                                                                │                                  │
│ ├─────────────────────────────────────────────────────────────────────────┤                                  │
│ │ 0  Publish:                                                             │                                  │
│ │  ┌                                                                      │                                  │
│ │  │ Dependencies:                                                        │                                  │
│ │  │   0x0000000000000000000000000000000000000000000000000000000000000001 │                                  │
│ │  │   0x0000000000000000000000000000000000000000000000000000000000000002 │                                  │
│ │  └                                                                      │                                  │
│ │                                                                         │                                  │
│ │ 1  TransferObjects:                                                     │                                  │
│ │  ┌                                                                      │                                  │
│ │  │ Arguments:                                                           │                                  │
│ │  │   Result 0                                                           │                                  │
│ │  │ Address: Input  0                                                    │                                  │
│ │  └                                                                      │                                  │
│ ╰─────────────────────────────────────────────────────────────────────────╯                                  │
│                                                                                                              │
│ Signatures:                                                                                                  │
│    5X5ZrZ2XGFrdq17yAujHH7d+lVVdCnY1sgvvZ4lcxElAPhyuIgh8LHvKddf1GrdbXpyejzRumXRQYfsTibswAA==                  │
│                                                                                                              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Effects                                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Digest: 5buU8mCMapoyaqCmJCknHD6uytHnT5XLGe6x1h6AAvss                                              │
│ Status: Success                                                                                   │
│ Executed Epoch: 658                                                                               │
│                                                                                                   │
│ Created Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a                         │
│  │ Owner: Immutable                                                                               │
│  │ Version: 1                                                                                     │
│  │ Digest: CLbkbPmQoL5pW62Kt88AdEfwikJTDHotcYek9U7Utjkr                                           │
│  └──                                                                                              │
│  ┌──                                                                                              │
│  │ ID: 0xa77e79c929061296f5996865d73da797e4d222437967032481afd1ac3db26377                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: AuHy31DbK7LMXvyqpyMVCixGsCzvmUhMaTefk5ZpMbWz                                           │
│  └──                                                                                              │
│ Mutated Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                           │
│  └──                                                                                              │
│ Gas Object:                                                                                       │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                           │
│  └──                                                                                              │
│ Gas Cost Summary:                                                                                 │
│    Storage Cost: 7782400 MIST                                                                     │
│    Computation Cost: 1000000 MIST                                                                 │
│    Storage Rebate: 978120 MIST                                                                    │
│    Non-refundable Storage Fee: 9880 MIST                                                          │
│                                                                                                   │
│ Transaction Dependencies:                                                                         │
│    5XvhL1EeEbZcMfKaGHzkiVvbkVk1DM7Adp4cMbku4gQ4                                                   │
│    6Gh9oKGzgh65Kp7WLMJFiy7aJj7mGHU7a7pZxR5zYwTG                                                   │
│    DkL6y2gSzSCEV1KZ8LBAEsmLgofSfNKBtooJyvATggXv                                                   │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────╮
│ No transaction block events │
╰─────────────────────────────╯

╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Object Changes                                                                                   │
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Created Objects:                                                                                 │
│  ┌──                                                                                             │
│  │ ObjectID: 0xa77e79c929061296f5996865d73da797e4d222437967032481afd1ac3db26377                  │
│  │ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                    │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a ) │
│  │ ObjectType: 0x2::package::UpgradeCap                                                          │
│  │ Version: 349161831                                                                            │
│  │ Digest: AuHy31DbK7LMXvyqpyMVCixGsCzvmUhMaTefk5ZpMbWz                                          │
│  └──                                                                                             │
│ Mutated Objects:                                                                                 │
│  ┌──                                                                                             │
│  │ ObjectID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                  │
│  │ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                    │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a ) │
│  │ ObjectType: 0x2::coin::Coin<0x2::sui::SUI>                                                    │
│  │ Version: 349161831                                                                            │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                          │
│  └──                                                                                             │
│ Published Objects:                                                                               │
│  ┌──                                                                                             │
│  │ PackageID: 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a                 │
│  │ Version: 1                                                                                    │
│  │ Digest: CLbkbPmQoL5pW62Kt88AdEfwikJTDHotcYek9U7Utjkr                                          │
│  │ Modules: hello_sui                                                                            │
│  └──                                                                                             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Balance Changes                                                                                   │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│  ┌──                                                                                              │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ CoinType: 0x2::sui::SUI                                                                        │
│  │ Amount: -7804280                                                                               │
│  └──                                                                                              │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯

Sender와 GAS Owner를 보면
현재 활성화 된 주소와 같은 것을 확인할 수 있다.

Gas Payment:                                                                                                 │
│  ┌──                                                                                                         │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                                    │
│  │ Version: 349161830                                                                                        │
│  │ Digest: DbtVRhVZWy69HvCDZ5mJCnqmZ5Q9oocCKBXqZKS8FA1x                                                      │
│  └──                                                                                                         │

sui 블록체인에 모든 객체는 ID를 가지기 때문에 가스비용 지불에 사용된 코인에도 ID가 존재하는 것을 확인할 수 있다.

트랜잭션 결과

╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Effects                                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Digest: 5buU8mCMapoyaqCmJCknHD6uytHnT5XLGe6x1h6AAvss                                              │
│ Status: Success                                                                                   │
│ Executed Epoch: 658                                                                               │
│                                                                                                   │
│ Created Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a                         │
│  │ Owner: Immutable                                                                               │
│  │ Version: 1                                                                                     │
│  │ Digest: CLbkbPmQoL5pW62Kt88AdEfwikJTDHotcYek9U7Utjkr                                           │
│  └──                                                                                              │
│  ┌──                                                                                              │
│  │ ID: 0xa77e79c929061296f5996865d73da797e4d222437967032481afd1ac3db26377                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: AuHy31DbK7LMXvyqpyMVCixGsCzvmUhMaTefk5ZpMbWz                                           │
│  └──                                                                                              │
│ Mutated Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                           │
│  └──                                                                                              │
│ Gas Object:                                                                                       │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161831                                                                             │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                           │
│  └──                                                                                              │
│ Gas Cost Summary:                                                                                 │
│    Storage Cost: 7782400 MIST                                                                     │
│    Computation Cost: 1000000 MIST                                                                 │
│    Storage Rebate: 978120 MIST                                                                    │
│    Non-refundable Storage Fee: 9880 MIST                                                          │
│                                                                                                   │
│ Transaction Dependencies:                                                                         │
│    5XvhL1EeEbZcMfKaGHzkiVvbkVk1DM7Adp4cMbku4gQ4                                                   │
│    6Gh9oKGzgh65Kp7WLMJFiy7aJj7mGHU7a7pZxR5zYwTG                                                   │
│    DkL6y2gSzSCEV1KZ8LBAEsmLgofSfNKBtooJyvATggXv                                                   │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯

Digest: 5buU8mCMapoyaqCmJCknHD6uytHnT5XLGe6x1h6AAvss -> 트랜잭션 해시

Create object: 생성된 객체

(1) 스마트 계약 패키지 (Published Package)
ID: 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a
소유권: Immutable (변경 불가능)
버전: 1
Digest: CLbkbPmQoL5pW62Kt88AdEfwikJTDHotcYek9U7Utjkr

(2)업그레이드 권한 객체 (UpgradeCap)
ID: 0xa77e79c929061296f5996865d73da797e4d222437967032481afd1ac3db26377
소유자:
0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a
배포된 스마트 계약을 업그레이드 할 수 있는 권한을 가진 사람
배포한 계정으로 설정되어있는 것을 확인할 수 있다.

수정된 객체 (Mutated Objects)_
트랜잭션 실행 중 변경된 객체

(1) 코인 객체 (SUI)
ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822
소유자:
0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a
버전: 349161831

해당 객체는 트랜잭션 가스 결제에 사용된 SUI 코인
가스 비용이 차감된 후, 새로운 버전(349161831)과 Digest가 생성됨

배포한 스마트 계약 실행해보기

(powerShell 일 경우만 가능(`)이거 사용해서)

sui client call `
  --package 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a `
  --module hello_sui `
  --function mint `
  --args 0x2595afe114dffcf430e04d78a3339fa98e57e00408fee481cdf6be009da7b439

다른 터미널일 경우

sui client call --package 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a --module hello_sui --function mint --args 0x8eee78f8827f60ffa1cbbfe35a4727da70af1b07f5d88a1e41018e562228e1df --gas-budget 10000000

주소로 줄 인자는 내가 만든 주소임 내주소

Transaction Digest: DNfP9Gd8rSctbw5VPx26QiGjN7w9CJCmCV64KFKMk8K6
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Data                                                                                             │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                   │
│ Gas Owner: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                │
│ Gas Budget: 4470000 MIST                                                                                     │
│ Gas Price: 1000 MIST                                                                                         │
│ Gas Payment:                                                                                                 │
│  ┌──                                                                                                         │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                                    │
│  │ Version: 349161831                                                                                        │
│  │ Digest: 2UNgffXKx6g3ZhTE7yDszq6DEGaWqgBVtw49WBNwTDe8                                                      │
│  └──                                                                                                         │
│                                                                                                              │
│ Transaction Kind: Programmable                                                                               │
│ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ Input Objects                                                                                            │ │
│ ├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤ │
│ │ 0   Pure Arg: Type: address, Value: "0x2595afe114dffcf430e04d78a3339fa98e57e00408fee481cdf6be009da7b439" │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ ╭──────────────────────────────────────────────────────────────────────────────────╮                         │
│ │ Commands                                                                         │                         │
│ ├──────────────────────────────────────────────────────────────────────────────────┤                         │
│ │ 0  MoveCall:                                                                     │                         │
│ │  ┌                                                                               │                         │
│ │  │ Function:  mint                                                               │                         │
│ │  │ Module:    hello_sui                                                          │                         │
│ │  │ Package:   0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a │                         │
│ │  │ Arguments:                                                                    │                         │
│ │  │   Input  0                                                                    │                         │
│ │  └                                                                               │                         │
│ ╰──────────────────────────────────────────────────────────────────────────────────╯                         │
│                                                                                                              │
│ Signatures:                                                                                                  │
│    gHHsQoxDb1QbnN013SlfcNCCT0tQKFpO/ULbjDnVKvl3UvrkFwoijOGoxaS9o6FCGYwGNNfepqcPPCHGKp9LDQ==                  │
│                                                                                                              │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Transaction Effects                                                                               │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Digest: DNfP9Gd8rSctbw5VPx26QiGjN7w9CJCmCV64KFKMk8K6                                              │
│ Status: Success                                                                                   │
│ Executed Epoch: 659                                                                               │
│                                                                                                   │
│ Created Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0x6773ebfe18a9761c50e0b92108f52879d67f742a695b3caf7782dffe870be688                         │
│  │ Owner: Account Address ( 0x2595afe114dffcf430e04d78a3339fa98e57e00408fee481cdf6be009da7b439 )  │
│  │ Version: 349161832                                                                             │
│  │ Digest: HGqdr8JohKYef675y29vDqYkkNdmwEPdr3AKMNtQ1B7f                                           │
│  └──                                                                                              │
│ Mutated Objects:                                                                                  │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161832                                                                             │
│  │ Digest: 5c2NrfGqjFVotUdb82hxkyaMA4zr6TeFaHHoXhuvEFGU                                           │
│  └──                                                                                              │
│ Gas Object:                                                                                       │
│  ┌──                                                                                              │
│  │ ID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                         │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ Version: 349161832                                                                             │
│  │ Digest: 5c2NrfGqjFVotUdb82hxkyaMA4zr6TeFaHHoXhuvEFGU                                           │
│  └──                                                                                              │
│ Gas Cost Summary:                                                                                 │
│    Storage Cost: 2470000 MIST                                                                     │
│    Computation Cost: 1000000 MIST                                                                 │
│    Storage Rebate: 978120 MIST                                                                    │
│    Non-refundable Storage Fee: 9880 MIST                                                          │
│                                                                                                   │
│ Transaction Dependencies:                                                                         │
│    5buU8mCMapoyaqCmJCknHD6uytHnT5XLGe6x1h6AAvss                                                   │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────╮
│ No transaction block events │
╰─────────────────────────────╯

╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Object Changes                                                                                                  │
├─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Created Objects:                                                                                                │
│  ┌──                                                                                                            │
│  │ ObjectID: 0x6773ebfe18a9761c50e0b92108f52879d67f742a695b3caf7782dffe870be688                                 │
│  │ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                   │
│  │ Owner: Account Address ( 0x2595afe114dffcf430e04d78a3339fa98e57e00408fee481cdf6be009da7b439 )                │
│  │ ObjectType: 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a::hello_sui::HelloWorldObject  │
│  │ Version: 349161832                                                                                           │
│  │ Digest: HGqdr8JohKYef675y29vDqYkkNdmwEPdr3AKMNtQ1B7f                                                         │
│  └──                                                                                                            │
│ Mutated Objects:                                                                                                │
│  ┌──                                                                                                            │
│  │ ObjectID: 0xa2e4c8f0ecc5dda0b35ae9085aa9cb2b31e65d5a5f17b6705beb0507c82f7822                                 │
│  │ Sender: 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a                                   │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )                │
│  │ ObjectType: 0x2::coin::Coin<0x2::sui::SUI>                                                                   │
│  │ Version: 349161832                                                                                           │
│  │ Digest: 5c2NrfGqjFVotUdb82hxkyaMA4zr6TeFaHHoXhuvEFGU                                                         │
│  └──                                                                                                            │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Balance Changes                                                                                   │
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
│  ┌──                                                                                              │
│  │ Owner: Account Address ( 0x2f7879784ef9eacb96e6eb6e8605766f71d1b432a006c106837a4c668052754a )  │
│  │ CoinType: 0x2::sui::SUI                                                                        │
│  │ Amount: -2491880                                                                               │
│  └──                                                                                              │
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯


서명 (Signatures)

gHHsQoxDb1QbnN013SlfcNCCT0tQKFpO/ULbjDnVKvl3UvrkFwoijOGoxaS9o6FCGYwGNNfepqcPPCHGKp9LDQ==
서명은 보낸 사람의 개인 키로 이루어진 디지털 서명으로, 이 트랜잭션이 해당 계정에 의해 승인되었음을 보장한다.

객체 조회


C:\Users\itsme>sui client object 0x6773ebfe18a9761c50e0b92108f52879d67f742a695b3caf7782dffe870be688
╭───────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ objectId      │  0x6773ebfe18a9761c50e0b92108f52879d67f742a695b3caf7782dffe870be688
                    │
│ version       │  349161832
                    │
│ digest        │  HGqdr8JohKYef675y29vDqYkkNdmwEPdr3AKMNtQ1B7f
                    │
│ objType       │  0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a::hello_sui::HelloWorldObject                          │
│ owner         │ ╭──────────────┬──────────────────────────────────────────────────────────────────────╮
                    │
│               │ │ AddressOwner │  0x2595afe114dffcf430e04d78a3339fa98e57e00408fee481cdf6be009da7b439  │
                    │
│               │ ╰──────────────┴──────────────────────────────────────────────────────────────────────╯
                    │
│ prevTx        │  DNfP9Gd8rSctbw5VPx26QiGjN7w9CJCmCV64KFKMk8K6
                    │
│ storageRebate │  1482000
                    │
│ content       │ ╭───────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│               │ │ dataType          │  moveObject
                  │ │
│               │ │ type              │  0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a::hello_sui::HelloWorldObject  │ │
│               │ │ hasPublicTransfer │  true
                  │ │
│               │ │ fields            │ ╭──────┬───────────────────────────────────────────────────────────────────────────────╮          │ │
│               │ │                   │ │ id   │ ╭────┬──────────────────────────────────────────────────────────────────────╮ │          │ │
│               │ │                   │ │      │ │ id │  0x6773ebfe18a9761c50e0b92108f52879d67f742a695b3caf7782dffe870be688  │ │          │ │
│               │ │                   │ │      │ ╰────┴──────────────────────────────────────────────────────────────────────╯ │          │ │
│               │ │                   │ │ text │  Hello World!
       │          │ │
│               │ │                   │ ╰──────┴───────────────────────────────────────────────────────────────────────────────╯          │ │
│               │ ╰───────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────╯ │
╰───────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

지금 아까 만들어진 객체를 조회하는 명령어를 실행하면 보낸 주소가 소유자로서 잘 객체가 만들어진 것을 알 수 있다.

혹은 링크텍스트 여기에 객체 아이디로 검색해볼 수 있다. (Devnet 일 경우만)
테스트넷일 때는 여기

Dapp 만들기

이제 스마트 컨트랙트를 배포했으니, 정말 정말 간단한 dapp 을 만들어보자.

Sui TypeScript SDK & dApp Kit을 이용한 dApp 개발
sui 네트워크와 상호작용 하기
여기에 적어놨지만 간단하게 설명하자면

1. Sui TypeScript SDK란? (@mysten/sui)
Sui 블록체인과 상호작용할 수 있도록 해주는 TypeScript SDK.
트랜잭션 전송, 스마트 계약 호출, 데이터 조회 등을 수행할 수 있음.
웹 앱, Node.js 앱, React Native 등 다양한 TypeScript 기반 프로젝트에서 사용 가능.

2. dApp Kit (@mysten/dapp-kit)이란?
Sui 블록체인 기반 dApp 개발을 쉽게 할 수 있도록 도와주는 React 라이브러리.
React Hooks & Components를 제공하여 지갑 연결, RPC 데이터 조회, 트랜잭션 실행 등을 간편하게 구현 가능.

React 기반의 Sui dApp 프로젝트 자동으로 설정

 npm create @mysten/dapp@latest -- --template react-client-dapp

실행 결과 파일 구조

필요한 패키지 설치

npm install @mysten/sui @mysten/dapp-kit @tanstack/react-query

버전 오류

Found: eslint@9.21.0
Could not resolve dependency:
peer eslint@"^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0-0"
from eslint-plugin-react-hooks@4.6.2

이런 오류가 났는데 npm install이 안되는 상황이었다.

eslint@9.x가 설치되었음 (최신 버전이므로 문제 발생).
하지만 eslint-plugin-react-hooks@4.6.2는 eslint@8.x까지만 지원하기 때문이다.

npm install eslint@8 --save-dev

eslint@8의 버전을 낮추면 해결됨

Provider 설정

dapp kit 를 사용하기 위해서는 Provider 설정을 해줘야한다.
vite를 사용한다면 src/main 파일을 아래 코드로 대체한다.

import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
//QueryClient : React Query에서 요청(데이터 가져오기, 캐싱 등)을 관리하는 핵심 객체
//QueryClientProvider : QueryClient를 React 앱에서 사용할 수 있도록 감싸는 Provider

const queryClient = new QueryClient();

ReactDOM.createRoot(document.getElementById('root')!).render(
	<React.StrictMode>
		<QueryClientProvider client={queryClient}>
			<App />
		</QueryClientProvider>
	</React.StrictMode>,
);

dApp Kit은 블록체인 관련 기능을 쉽게 사용하도록 도와주는 라이브러리
따라서 dapp kit가 제공하는 기능 중에는 데이터 요청 (API 호출)이나 상태관리가 필요한 기능이 많음 -> React Query라는 라이브러리를 설정해서 데이터를 효율적으로 관리할 수 있도록 도와줌

암튼 여기에 (React + dApp Kit + Sui + React Query 세팅)을 추가해준다.

import React from "react";
import ReactDOM from "react-dom/client";
import "@mysten/dapp-kit/dist/index.css";
import "@radix-ui/themes/styles.css";
import { SuiClientProvider, WalletProvider } from '@mysten/dapp-kit';
import { getFullnodeUrl } from '@mysten/sui/client';
import type { SuiNetwork } from '@mysten/sui/client';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import App from "./App.tsx";

const queryClient = new QueryClient();
const networks: Record<SuiNetwork, { url: string }> = {
    testnet: { url: getFullnodeUrl('testnet') },
    devnet: { url: getFullnodeUrl('devnet') },
    mainnet: { url: getFullnodeUrl('mainnet') },
};

ReactDOM.createRoot(document.getElementById('root')!).render(
    <React.StrictMode>
        <QueryClientProvider client={queryClient}>
            <SuiClientProvider networks={networks} defaultNetwork="testnet">
                <WalletProvider>
                    <App />
                </WalletProvider>
            </SuiClientProvider>
        </QueryClientProvider>
    </React.StrictMode>,
);

SuiClientProvider:
Sui 블록체인 네트워크와 통신하는 역할
dApp이 Sui 블록체인 데이터를 가져오거나 트랜잭션을 수행할 때 필요

WalletProvider:
Sui 지갑(예: Sui Wallet, Ethos Wallet 등)과 연동하는 Provider
사용자의 지갑을 연결하고 트랜잭션을 실행할 수 있도록 해줌

getFullnodeUrl('devnet' | 'mainnet')
Sui 블록체인의 특정 네트워크(devnet 또는 mainnet)의 노드 URL을 가져오는 함수
Sui 블록체인의 데이터를 가져오는 데 사용됨

QueryClient & QueryClientProvider
React Query를 설정하여 데이터 요청(캐싱, 리팩치, 상태관리 등)을 최적화하는 기능을 추가

사용자 지갑과 연결

import React from "react";
import { ConnectButton, useCurrentAccount } from '@mysten/dapp-kit';
import {useSuiClientQuery } from '@mysten/dapp-kit';
import { SuiClient } from '@mysten/sui/client';
function App() {
    return (
        <div className="App">
            <header className="App-header">
                <h1>My Sui dApp</h1>
                <ConnectButton />
            </header>

            <ConnectedAccount />
        </div>
    );
}

function ConnectedAccount() {
    const account = useCurrentAccount();

    if (!account) {
        return null;
    }

    return (
        <div>
            <div>Connected to {account.address}</div>;
            <OwnedObjects address={account.address} />
        </div>
    );
}

function OwnedObjects({ address }: { address: string }) {
    const { data } = useSuiClientQuery<typeof SuiClient.prototype.getOwnedObjects>(
        "getOwnedObjects",
        { owner: address }
    );
    if (!data) {
        return null;
    }

    return (
        <ul>
            {data.data.map((object) => (
                <li key={object.data?.objectId}>
                    <a href={`https://example-explorer.com/object/${object.data?.objectId}`}>
                        {object.data?.objectId}
                    </a>
                </li>
            ))}
        </ul>
    );
}
export default App;

프론트엔드(React)에서 자동으로 데이터를 가져오려면 useSuiClientQuery를 사용했따.(자동으로 캐싱이 되기 때문에)

실행하기

 npm run dev

스마트 컨트랙트와 연결

스마트 컨트랙트와 연결을 할 때, 원래는 SUI Client 를 사용해서 요청을 보내려고 했다. 터미널에서 이런 요청을 네트워크에 보냈던 것처럼, 그냥 해당 행위(?)를 코드가 할 수 있게 해주면 되지 않을까 생각했다.

sui client call --package 0x4838969c5cb4fd965b969c8e1d6fb19c714aa1cf5eab7860fb649d59bf3a665a --module hello_sui --function mint --args 0x8eee78f8827f60ffa1cbbfe35a4727da70af1b07f5d88a1e41018e562228e1df --gas-budget 10000000

그러나,

sui cli를 실행해서 트랜잭션을 보낼 때는 Sui Cli 에서 자동으로 서명을 해주지만 백엔드에서 같은 행위를 하려고 하면 서버에서 private key로 서명해야하기 때문에 보안상 더 까다롭다. 그리고 애초에 지갑을 연결하는데 이딴짓을 할 이유가 없다.

프론트엔드에서 실행

signAndExecuteTransaction !!!

사용자가 직접 지갑에서 서명하고 승인하는 방식
가스를 직접 지불해야 함
사용자의 Private Key는 보호됨 (지갑 관리)

signAndExecuteTransaction를 사용하여 트랜잭션 생성, 트랜잭션 서명, 제출, 반환을 해줄것이다.

signAndExecuteTransaction 예시

await signAndExecuteTransactionBlock({
    transactionBlock: txb,
    options: { showEffects: true },
    requestType: "WaitForLocalExecution",
});

transactionBlock: 실행할 트랜잭션 (TransactionBlock 객체)
options.showEffects:true로 설정하면 트랜잭션의 변경된 객체 상태 확인 가능
requestType: "WaitForLocalExecution"이면 블록체인에서 실행 완료 후 결과 반환

profile
윤따와더나은인생

0개의 댓글