Move examples
Move examples


Move examples

HelloWorld

  1. Define an Event

struct AnyWordEvent has drop, store {

words: vector,

}
  1. Define a Holder

struct EventHolder has key {

any_word_events: Event::EventHandle,

}

  1. Generate a Hello World event in the function

let hello_world = x"68656c6c6f20776f726c64"; //hello world

let holder = borrow_global_mut(addr);

Event::emit_event(&mut holder.any_word_events, AnyWordEvent { words:hello_world });

The complete code


module Hello {

    use StarcoinFramework::Signer;

    use StarcoinFramework::Event;



    struct AnyWordEvent has drop, store {

        words: vector,

    }



    struct EventHolder has key {

        any_word_events: Event::EventHandle,

    }



    public(script) fun hello(account: signer) acquires EventHolder {

        let _account = &account;

        let addr = Signer::address_of(_account);

        if (! exists(copy addr)){

            move_to(_account, EventHolder {

            any_word_events: Event::new_event_handle(_account),

            });

        };

        let hello_world = x"68656c6c6f20776f726c64"; //hello world

        let holder = borrow_global_mut(addr);

        Event::emit_event(&mut holder.any_word_events, AnyWordEvent { words:hello_world });

    }

}

Stdlib Introduction to the whole

Stdlib is a very important feature of Starcoin. It contains some basic modules, common modules, blocks and consensus related modules.

  1. Basic modules:

  2. Account: Account module;

  3. Token: asset module, which defines the Token specification;

  4. STC: STC is the original asset of Starcoin and an implementation of Token;

  5. Timestamp: Timestamp module, which takes the Block time as the on-chain time;

  6. Event: Event processing module;

  7. Math module;

  8. Errors: Exception processing module;

  9. Vector: array module;

  10. Common modules are as follows:

  11. Config: configuration module, such as block reward module RewardConfig, VMConfig, etc., stores various configuration files on the chain for convenient adjustment in the future;

  12. DAO: On-chain governance module;

  13. Twophase: two-phase update Module;

  14. Block and consensus related modules:

  15. Genesis.

  16. Block metadata;

  17. The Epoch was Epoch.

Here are some simple examples of Stdlib operations that are commonly used.

Account basic operation

  1. Create account

let auth_key = x"91e941f5bc09a285705c092dd654b94a7a8e385f898968d4ecfba49609a13461";

let account_address = Account::create_account(auth_key);

  1. Get authentication key with address

let auth_key = Account::authentication_key(account_address);

  1. Get address from authentication key

let auth_key = x"91e941f5bc09a285705c092dd654b94a7a8e385f898968d4ecfba49609a13461";

let expected_address = Authenticator::derived_address(auth_key);

  1. Get balance

let balance = Account::balance(account_address);
  1. Get sequence number

let sequence_number = Account::sequence_number(account_address);

  1. Deposit

let coin = Token::mint(&account, 100);

Account::deposit(account_address, coin);

  1. Determine whether a descriptive Capability is proxied

let is_delegated = Account::delegated_withdraw_capability(account_address);

  1. Gets the address of the corresponding descriptive Capability

let with_cap = Account::extract_withdraw_capability(&account);

let account_address = Account::withdraw_capability_address(&with_cap);

Capability allows you to operate your account

In Starcoin, any modification of the account, such as transfer, updating key and so on, requires the permission to operate the account. A Capability can be understood as an abstraction of a “permission” in a Move, and a different type of operation corresponds to a different type of Capability, such as a mintCapability, a descriptive Capability, and so on. To do something with a Capability, there are usually three steps:

  1. Get the corresponding Capability: extract_xxx_capability

  2. Do it

  3. Store Capability: Restore_XXX_Capability

Here are some examples of using Capability to manipulate accounts (and, by the same token, update permissions for other modules) :

  1. Change authentication key

let rot_cap = Account::extract_key_rotation_capability(&account); //1. get capability

Account::rotate_authentication_key_with_capability(&rot_cap, x"123abc"); //2. change key

Account::restore_key_rotation_capability(rot_cap); //3. restore capability

  1. Pay from capability

let with_cap = Account::extract_withdraw_capability(&account); //1. get capability

Account::pay_from_capability(&with_cap, payee, 10000, x""); //2. pay from capability

Account::restore_withdraw_capability(with_cap); //3. restore capability

These are two typical examples, and a more descriptive account operation is a: withdrawing _with_capability and so on.

Multiple ways to transfer contracts

  1. peer_to_peer

TransferScripts::peer_to_peer_v2(account, payee, amount);

  1. peer_to_peer_with_metadata

TransferScripts::peer_to_peer_with_metadata_v2(account, payee, amount, metadata);

  1. Batch transfer of different amounts

TransferScripts::batch_peer_to_peer_v2(account, payeees, amounts);

Sign multiple accounts

  1. Create one multi-signed account for each 3 accounts

let pubkey1 = x"c48b687a1dd8265101b33df6ae0b6825234e3f28df9ecb38fb286cf76dae919d";

let pubkey2 = x"4b2a60883383be0ba24ed79aa5a6c9379728099a7b0c57edcec193a14ea5fce2";

let pubkey3 = x"323285d3d4b0f19482730c5f481d9f745c2927d73c231bad47859d9b2f7376f1";


let keys = Vector::empty>();

Vector::push_back(&mut keys, pubkey1);

Vector::push_back(&mut keys, pubkey2);

Vector::push_back(&mut keys, pubkey3);

let t = Authenticator::create_multi_ed25519(copy keys, 1);

t = Authenticator::create_multi_ed25519(copy keys, 2);

t = Authenticator::create_multi_ed25519(copy keys, 3);

let auth_key = Authenticator::multi_ed25519_authentication_key(&t);

  1. Get the address of the oversigned account

let account_address = Authenticator::derived_address(auth_key);

Example # # Token

  1. Publish the Token process

Struct myToken has copy, drop, store {}

register Token: Token::register_token< myToken >(account,3);

Accept Token: Account::accept_token< myToken >(& Account);

Let tokens = Token::mint< myToken >(&account, total);

Deposit_to_self < myToken >(& Account, tokens);

The complete code


module MyToken {

    use StarcoinFramework::Token;
    use StarcoinFramework::Account;

    struct MyToken has copy, drop, store { }

    public(script) fun init(account: signer) {

        let _account = &account;
        Token::register_token(_account, 3);
        Account::do_accept_token(_account);

    }

    public(script) fun mint(account: signer, amount: u128) {

        let _account = &account;

        let token = Token::mint(_account, amount);

        Account::deposit_to_self(_account, token)

    }

}

  1. Turn the Token ‘1. Accept Token: Account::accept_token< myToken >(& Account); `

‘2. Forward Token: Account::pay_from< myToken >(& Account, payee, amount); `

Contract hosting

  1. The coder account deploys a contract and hosts the contract to the DAO

module MyToken {

    use StarcoinFramework::Token;
    use StarcoinFramework::Account;
    use StarcoinFramework::Dao;

    struct MyToken has copy, drop, store { }

        public(script) fun init(account: signer) {

            let _account = &account;    
            Token::register_token(_account, 3);
            Account::do_accept_token(_account);
            Dao::plugin(_account, 60 * 1000, 60 * 60 * 1000, 4, 60 * 60 * 1000);

        }

        public(script) fun mint(account: signer, amount: u128) {

            let _account = &account;  
            let token = Token::mint(_account, amount);
            Account::deposit_to_self(_account, token)

        }

}

  1. Modify the DAO configuration of the MyToken module

script {

    use StarcoinFramework::Dao;
    use {{coder}}::MyToken::MyToken;
    use StarcoinFramework::Config;

    fun set_dao_config(signer: signer) {

        let cap = Config::extract_modify_config_capability>(

            &signer

        );
        Dao::set_voting_delay(&mut cap, 30 * 1000);
        Dao::set_voting_period(&mut cap, 30 * 30 * 1000);
        Dao::set_voting_quorum_rate(&mut cap, 50);
        Dao::set_min_action_delay(&mut cap, 30 * 30 * 1000);
        Config::restore_modify_config_capability(cap);
    }

}

  1. Upgrade your contract

! DAO