Rust smart contract development diary (3)
BlockSec
2022-03-29 10:29
本文约4130字,阅读全文需要约17分钟
Contract deployment, function calling, and explorer usage.

related articles:

Rust Smart Contract Development Diary (2) Writing Rust Smart Contract Unit Tests

In this issue of Rust Smart Contract Development Blog, BlockSec will introduce how to deploy a simple sample smart contract to the NEAR test network (TestNet), execute the contract, and query the execution result of the contract.

When the contract developers have implemented the main logic of the contract and performed unit tests, the developers can compile the contract project into WASM bytecode and deploy it to NEAR’s test chain for final deployment to the mainnet (MainNet) prepare for.

1. Introduction to NEAR Network

NEAR Protocol as a protocol means that there can be multiple independent networks based on this protocol. The specifications and standards NEAR Protocol Specifications and Standards formulated by the NEAR protocol specify different chains based on the NEAR protocol, and the chain_id in the GenesisConfig of the creation block configuration is different.

  • TestNet

    There are mainly three types of networks that we commonly use:

  • MainNet

    Every smart contract project ultimately needs to run on the blockchain network to realize the function of the project. However, for a new project, the project team will often deploy the project on the test network (TestNet) to test the functions and related business logic of the smart contract project.

  • LocalNet

    After the contract has undergone multiple rounds of testing and independent security review (if necessary) in the test network, the development team can choose to officially deploy the contract to the main network (MainNet).

LocalNet is different from TestNet. LocalNet runs locally, so the contracts and transaction data deployed on the chain are not disclosed to the external network. If the developer does not want to leak any important information about the contract project before it is deployed to TestNet or MainNet, LocalNet will be a good choice.

2. NEAR CLI installation

$ sudo npm install -g near-cli

NEAR CLI (Command Line Interface) is a NodeJS command line interface, which uses near-api-js to connect to and interact with the network described above. Therefore, before installation, we need to install npm (full name Node Package Manager), and then we will use npm to execute the following installation command in Linux to install NEAR CLI.

$ near --version

Execute the following command to view the installed version of NEAR CLI and check whether the installation is successful.

3. Deploy the contract in TestNet

3.1. Register for a TestNet accounthttps://wallet.testnet.near.orgNew users can go to

To register a new TestNet wallet on the page, you only need to enter the user name to complete the registration, such as statusmessage.testnet.https://explorer.testnet.near.orgthen in

, we can search and view relevant information about the account in TestNet as follows:

In TestNet, each newly created account will contain a raw balance of 200Ⓝ.

3.2. Log in to the account in NEAR CLI

$ near login

Since we have installed NEAR CLI and applied for a user account in NEAR TestNet. You can try to log in to the account in the NAER CLI as follows:

Following the execution of the above command, NEAR CLI will invoke a link and require the user to manually click to log in to the above statusmessage.testnet account.

3.3. View account related information

$ near state statusmessage.testnet
Account statusmessage.testnet
{    amount: '199999959035075000000000000',
     block_hash: '8iomNEFNa4LQB54ehTPBGu8bBfotistoWrBcYJfxm8vA',
     block_height: 68318068,
     code_hash: '11111111111111111111111111111111',
     locked: '0',
     storage_paid_at: 0,
     storage_usage: 264,
     formattedAmount: '199.999959035075' 
}

At the same time, we can query the relevant information of a specific account through the NEAR CLI

3.4. Contract compilation and deployment

$ RUSTFLAGS='-C link-arg=-s' cargo +stable build --target wasm32-unknown-unknown --release

Before deploying the contract, we first need to compile the contract. The following command can be used to compile the specific WASM file status_message.wasm with cargo. It is usually located in the target/wasm32-unknown-unknown/release/ directory of the project.

$ cd target/wasm32-unknown-unknown/release
$ export MASTER_ACCOUNT=statusmessage.testnet
$ near create-account contract01.$MASTER_ACCOUNT
                          --initialBalance 10
                          --masterAccount $MASTER_ACCOUNT
Account contract01.statusmessage.testnet for network "testnet" was created.

At the same time, we can create a sub-account for statusmessage.testnet, named contract01.statusmessage.testnet, for contract deployment

$ near deploy --accountId contract01.$MASTER_ACCOUNT \
                        --wasmFile status_message.wasm

Then we can use the near deploy command to help us deploy the contract.

Starting deployment. Account id: contract01.statusmessage.testnet, node: [https://rpc.testnet.near.org](https://rpc.testnet.near.org), helper: [https://helper.testnet.near.org](https://helper.testnet.near.org), file: status_message.wasm
Transaction Id 4oDYA8wPLJuKwDumJxEGubeJBA9Ep13MLXSf34q9ydRm
To see the transaction in the transaction explorer, please open this url in your browser[https://explorer.testnet.near.org/transactions/4oDYA8wPLJuKwDumJxEGubeJBA9Ep13MLXSf34q9ydRm](https://explorer.testnet.near.org/transactions/4oDYA8wPLJuKwDumJxEGubeJBA9Ep13MLXSf34q9ydRm) Done deploying to contract01.statusmessage.testnet

If the contract deployment is successful, we can get the following information:

3.5 Call contract function

$ near call --accountId $MASTER_ACCOUNT \
      contract01.$MASTER\_ACCOUNT set\_status '{"message":"Hi!2021"}'

When we successfully deploy the contract to TestNet, the account contract01.statusmessage.testnet that deployed the contract is the address of the contract. To verify whether the StatusMessage contract in TestNet can run normally. We can call set_status to set the built-in properties of the contract.

Scheduling a call: contract01.statusmessage.testnet.set_status({"message":"Hi!2021"})
TransactTransaction Id E9dsw8H9ztDN18DDvXKTBPvBLWHew7TtuWsLgg6uvesN
To see the transaction in the transaction explorer, please open this url in your browser[https://explorer.testnet.near.org/transactions/E9dsw8H9ztDN18DDvXKTBPvBLWHew7TtuWsLgg6uvesN](https://explorer.testnet.near.org/transactions/E9dsw8H9ztDN18DDvXKTBPvBLWHew7TtuWsLgg6uvesN) ''

The following is the information returned by calling the function set_status in the contract

It can be seen that the StatusMessage contract function call transaction can be executed normally, and the specific Transaction ID is returned, namely E9dsw8H9ztDN18DDvXKTBPvBLWHew7TtuWsLgg6uvesN

3.6 Transaction Inquiry

NEAR provides the community with a data browsing query platform, NEAR Explorer. Users can search for real-time on-chain information such as account IDs, transaction hashes, and transaction blocks in the specified network on the platform.https://explorer.testnet.near.org/transactions/E9dsw8H9ztDN18DDvXKTBPvBLWHew7TtuWsLgg6uvesN

Use the transaction ID to query specific transaction details in NEAR Explorer.

Summary and preview of this issue

BlockSec
作者文库