Create the first hello move project

Hello World Move Project

Tạo project

Cú pháp:

sui move new <project-name>

Ví dụ:

sui move new hello_world

Cấu trúc cơ bản của 1 project Move

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

    • File cấu hình chính của project
    • Chứa thông tin về tên project, dependencies, địa chỉ published
    • Tương tự như package.json trong NodeJS hoặc Cargo.toml trong Rust
  2. sources/

    • Thư mục chứa mã nguồn chính của project
    • Các file .move chứa smart contract code
    • Mỗi module Move được định nghĩa trong một file riêng
  3. tests/

    • Thư mục chứa các file test
    • Các file test thường có hậu tố _tests.move
    • Dùng để viết unit test cho smart contract

Tạo 1 Hello World

// hello_world.move
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 entry fun hello_world(ctx: &mut TxContext) {
        let object = HelloWorldObject {
            id: object::new(ctx),
            text: string::utf8(b"Hello World!")
        };
        transfer::public_transfer(object, tx_context::sender(ctx));
    }
 
}

Build project

sui move build

Kết quả:

UPDATING GIT DEPENDENCY https://github.com/MystenLabs/sui.git
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING hello_world

Deploy smart contract Move

sui client publish --gas-budget 100000000

Lưu ý: Nếu môi trường active của bạn là devnet thì bạn phải config devnet ở Move.toml

/// Move.toml
...
[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/devnet" }
...
Kết quả
Transaction Digest: 2zSX9dfsMUAAG8qYtGHy7scXM2d4jgEurohHrSUtYrGB
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 Transaction Data                                                                                             
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                                   
 Gas Owner: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                                
 Gas Budget: 100000000 MIST                                                                                   
 Gas Price: 1000 MIST                                                                                         
 Gas Payment:                                                                                                 
  ┌──                                                                                                         
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                                    
   Version: 7                                                                                                
   Digest: 68NHQhDagzFW3US6hxqwo5Kg948M7pgmq6akHU5aMXVR                                                      
  └──                                                                                                         
                                                                                                              
 Transaction Kind: Programmable                                                                               
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ 
  Input Objects                                                                                             
 ├──────────────────────────────────────────────────────────────────────────────────────────────────────────┤ 
  0   Pure Arg: Type: address, Value: "0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f"  
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ 
 ╭─────────────────────────────────────────────────────────────────────────╮                                  
  Commands                                                                                                  
 ├─────────────────────────────────────────────────────────────────────────┤                                  
  0  Publish:                                                                                               
                                                                                                           
    Dependencies:                                                                                          
      0x0000000000000000000000000000000000000000000000000000000000000001                                   
      0x0000000000000000000000000000000000000000000000000000000000000002                                   
                                                                                                           
                                                                                                            
  1  TransferObjects:                                                                                       
                                                                                                           
    Arguments:                                                                                             
      Result 0                                                                                             
    Address: Input  0                                                                                      
                                                                                                           
 ╰─────────────────────────────────────────────────────────────────────────╯                                  
                                                                                                              
 Signatures:                                                                                                  
    h2foMg640sscAuasItZSOGtBy0GXMouFgvBQwjUK0X0ERJv4YhS2a0tSoVguI50lWOUmd58kKApa4CQ8x72WCw==                  
                                                                                                              
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
 Transaction Effects                                                                               
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
 Digest: 2zSX9dfsMUAAG8qYtGHy7scXM2d4jgEurohHrSUtYrGB                                              
 Status: Success                                                                                   
 Executed Epoch: 10                                                                                
                                                                                                   
 Created Objects:                                                                                  
  ┌──                                                                                              
   ID: 0x431b31b72639bff5109fcb23656f8891d4a98324eb146126d7d8526a7bc0c4dd                         
   Owner: Immutable                                                                               
   Version: 1                                                                                     
   Digest: 6zsDSiib5Yk2vAeGhLapFiKTUpHGicPoe8xPrasVghM                                            
  └──                                                                                              
  ┌──                                                                                              
   ID: 0xcd9f8e96341ee1d5f30e2880002b06a1a80f50c4630552e0c6e9ec7e004ff912                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 8                                                                                     
   Digest: F4WUtwwV7ymL7dFQnqX4T6AiRkbP2ukzoxDcsFBjrcbV                                           
  └──                                                                                              
 Mutated Objects:                                                                                  
  ┌──                                                                                              
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 8                                                                                     
   Digest: ECftyjH5LM4JcXEsLtMmHdiRYEobMA417ZF8w3nnQUvp                                           
  └──                                                                                              
 Gas Object:                                                                                       
  ┌──                                                                                              
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 8                                                                                     
   Digest: ECftyjH5LM4JcXEsLtMmHdiRYEobMA417ZF8w3nnQUvp                                           
  └──                                                                                              
 Gas Cost Summary:                                                                                 
    Storage Cost: 7911600 MIST                                                                     
    Computation Cost: 1000000 MIST                                                                 
    Storage Rebate: 978120 MIST                                                                    
    Non-refundable Storage Fee: 9880 MIST                                                          
                                                                                                   
 Transaction Dependencies:                                                                         
    V58JkCa5qyDaoRoUrsGjn8kaH6tLhrvbfDkQG7TczWt                                                    
    9a5y4cEDXa6piUnWAm1pah5JvxkwfJWHSx1NPg6ctaar                                                   
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────╮
 No transaction block events 
╰─────────────────────────────╯
 
╭──────────────────────────────────────────────────────────────────────────────────────────────────╮
 Object Changes                                                                                   
├──────────────────────────────────────────────────────────────────────────────────────────────────┤
 Created Objects:                                                                                 
  ┌──                                                                                             
   ObjectID: 0xcd9f8e96341ee1d5f30e2880002b06a1a80f50c4630552e0c6e9ec7e004ff912                  
   Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                    
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f ) │
   ObjectType: 0x2::package::UpgradeCap                                                          
   Version: 8                                                                                    
   Digest: F4WUtwwV7ymL7dFQnqX4T6AiRkbP2ukzoxDcsFBjrcbV                                          
  └──                                                                                             
 Mutated Objects:                                                                                 
  ┌──                                                                                             
   ObjectID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                  
   Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                    
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f ) │
   ObjectType: 0x2::coin::Coin<0x2::sui::SUI>                                                    
   Version: 8                                                                                    
   Digest: ECftyjH5LM4JcXEsLtMmHdiRYEobMA417ZF8w3nnQUvp                                          
  └──                                                                                             
 Published Objects:                                                                               
  ┌──                                                                                             
   PackageID: 0x431b31b72639bff5109fcb23656f8891d4a98324eb146126d7d8526a7bc0c4dd                 
   Version: 1                                                                                    
   Digest: 6zsDSiib5Yk2vAeGhLapFiKTUpHGicPoe8xPrasVghM                                           
   Modules: hello_world                                                                          
  └──                                                                                             
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
 Balance Changes                                                                                   
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
  ┌──                                                                                              
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   CoinType: 0x2::sui::SUI                                                                        
   Amount: -7933480                                                                               
  └──                                                                                              
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯

Call smart contract

Cú pháp:

sui client call --module <module> --function <function> --package <package-id> --gas-budget <gas-budget>

Ví dụ:

sui client call --module hello_world --function hello_world --package 0x431b31b72639bff5109fcb23656f8891d4a98324eb146126d7d8526a7bc0c4dd --gas-budget 100000000
Kết quả
Transaction Digest: 4D9a4ZDxfKKPcwwKxj6Ei8TjhZoeW4q3ryTifs9cusTZ
╭─────────────────────────────────────────────────────────────────────────────────────────────╮
 Transaction Data                                                                            
├─────────────────────────────────────────────────────────────────────────────────────────────┤
 Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                  
 Gas Owner: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f               
 Gas Budget: 100000000 MIST                                                                  
 Gas Price: 1000 MIST                                                                        
 Gas Payment:                                                                                
  ┌──                                                                                        
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                   
   Version: 8                                                                               
   Digest: ECftyjH5LM4JcXEsLtMmHdiRYEobMA417ZF8w3nnQUvp                                     
  └──                                                                                        
                                                                                             
 Transaction Kind: Programmable                                                              
   No input objects for this transaction                                                     
 ╭──────────────────────────────────────────────────────────────────────────────────╮        
  Commands                                                                                 
 ├──────────────────────────────────────────────────────────────────────────────────┤        
  0  MoveCall:                                                                             
                                                                                          
    Function:  hello_world                                                                
    Module:    hello_world                                                                
    Package:   0x431b31b72639bff5109fcb23656f8891d4a98324eb146126d7d8526a7bc0c4dd         
                                                                                          
 ╰──────────────────────────────────────────────────────────────────────────────────╯        
                                                                                             
 Signatures:                                                                                 
    0wu+Mms0Gc7FMUSvIIAct9Xg7DxyoK2gSbFhPPKLBYGPgbmoMCHn25Y0FElE+F7gXuu5qHhcjWsXlW8fZBAbCA== 
                                                                                             
╰─────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
 Transaction Effects                                                                               
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
 Digest: 4D9a4ZDxfKKPcwwKxj6Ei8TjhZoeW4q3ryTifs9cusTZ                                              
 Status: Success                                                                                   
 Executed Epoch: 10                                                                                
                                                                                                   
 Created Objects:                                                                                  
  ┌──                                                                                              
   ID: 0x0d8465e955278f12e240f6ed46544e12ddd5f98357d6ea02a1b37785ff84c906                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 9                                                                                     
   Digest: CqffLKE84ypRjCv9TwPE3m3QVP8jV9UHxWRyUYfzpfgb                                           
  └──                                                                                              
 Mutated Objects:                                                                                  
  ┌──                                                                                              
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 9                                                                                     
   Digest: 7ooPPFYfd4ywYkVTxSPM617dGr7dNU1oTAPzZFuetuGG                                           
  └──                                                                                              
 Gas Object:                                                                                       
  ┌──                                                                                              
   ID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                         
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   Version: 9                                                                                     
   Digest: 7ooPPFYfd4ywYkVTxSPM617dGr7dNU1oTAPzZFuetuGG                                           
  └──                                                                                              
 Gas Cost Summary:                                                                                 
    Storage Cost: 2485200 MIST                                                                     
    Computation Cost: 1000000 MIST                                                                 
    Storage Rebate: 978120 MIST                                                                    
    Non-refundable Storage Fee: 9880 MIST                                                          
                                                                                                   
 Transaction Dependencies:                                                                         
    2zSX9dfsMUAAG8qYtGHy7scXM2d4jgEurohHrSUtYrGB                                                   
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────╮
 No transaction block events 
╰─────────────────────────────╯
 
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 Object Changes                                                                                                    
├───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 Created Objects:                                                                                                  
  ┌──                                                                                                              
   ObjectID: 0x0d8465e955278f12e240f6ed46544e12ddd5f98357d6ea02a1b37785ff84c906                                   
   Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                                     
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )                  │
   ObjectType: 0x431b31b72639bff5109fcb23656f8891d4a98324eb146126d7d8526a7bc0c4dd::hello_world::HelloWorldObject  
   Version: 9                                                                                                     
   Digest: CqffLKE84ypRjCv9TwPE3m3QVP8jV9UHxWRyUYfzpfgb                                                           
  └──                                                                                                              
 Mutated Objects:                                                                                                  
  ┌──                                                                                                              
   ObjectID: 0x9f830e09ef34783e7fba678030a7e2dafce5a8bef1012b4e27fad06d70db1b70                                   
   Sender: 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f                                     
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )                  │
   ObjectType: 0x2::coin::Coin<0x2::sui::SUI>                                                                     
   Version: 9                                                                                                     
   Digest: 7ooPPFYfd4ywYkVTxSPM617dGr7dNU1oTAPzZFuetuGG                                                           
  └──                                                                                                              
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭───────────────────────────────────────────────────────────────────────────────────────────────────╮
 Balance Changes                                                                                   
├───────────────────────────────────────────────────────────────────────────────────────────────────┤
  ┌──                                                                                              
   Owner: Account Address ( 0x9d38536027abeec2274f2b750fadba202174da9fbd9fa75a760ee99c8b3ad82f )  │
   CoinType: 0x2::sui::SUI                                                                        
   Amount: -2507080                                                                               
  └──                                                                                              
╰───────────────────────────────────────────────────────────────────────────────────────────────────╯

Chạy test

Chúng ta cần thêm một số đoạn code ( bạn đừng lo đoạn code này , section tiếp theo mình giới thiệu chi tiết hơn)

  • Thêm hàm get_text() vào module hello_world
 
// sources/hello_world.move
 
public fun get_text(hello: &HelloWorldObject) : String {
    hello.text
 
}
 
  • Thêm hàm test_hello_world() vào module hello_world_tests
// tests/hello_world_tests.move
 
#[test_only]
module hello_world::hello_world_tests;
 
#[test]
fun test_hello_world() {
    use sui::test_scenario;
    use sui::test_utils::assert_eq;
    use hello_world::hello_world::{HelloWorldObject, hello_world};
    use std::string;
    let dummy_address = @0xCAFE;
 
        // First transaction executed by initial owner to create the sword
    let mut scenario = test_scenario::begin(dummy_address);
        {
            // Create hello world
            hello_world( scenario.ctx());
 
 
 
        };
 
    scenario.next_tx(dummy_address);
    {
            let hello = scenario.take_from_sender<HelloWorldObject>();
            let text = hello.get_text();
            std::debug::print(&text);
            assert_eq(text, string::utf8(b"Hello World!"));
 
            scenario.return_to_sender(hello)
    };
 
    scenario.end();
     
}
 
  • Chạy command để test smart contract
sui move test
  • Kết quả
INCLUDING DEPENDENCY Sui
INCLUDING DEPENDENCY MoveStdlib
BUILDING hello_world
Running Move unit tests
[debug] "Hello World!"
[ PASS    ] hello_world::hello_world_tests::test_hello_world
Test result: OK. Total tests: 1; passed: 1; failed: 0