我是靠谱客的博主 健壮太阳,最近开发中收集的这篇文章主要介绍NEAR 智能合约开发1. 引言参考资料,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 引言

NEAR系列博客有:

  • NEAR协议简介
  • NEAR共识机制
  • NEAR light client
  • Aurora与NEAR的关系
  • NEAR的storage staking
  • Rainbow Bridge:trustless bridge between NEAR and Ethereum
  • NEAR Rainbow Bridge代码解析

NEAR官方推荐使用Rust语言来编写智能合约,然后编译为WebAssembly并部署在NEAR链上。

near.dev中有各种合约示例。

Rust工具中需增加wasm支持:

rustup target add wasm32-unknown-unknown

NEAR合约基本架构为:

.
├── Cargo.toml
└── src
└── lib.rs

Cargo.toml中引入相应的依赖:

[
dependencies
]
near-sdk = "3.1.0"
[
profile.release
]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
overflow-checks = true

实际合约文件中会引用相关依赖:

use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen};

主要的合约逻辑体现在lib.rs文件中,定义struct并实现相应的function:

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]
pub struct Counter {
val: i8, // i8 is signed. unsigned integers are also available: u8, u16, u32, u64, u128
}
#[near_bindgen]
impl Counter {

其中

#[near_bindgen]
#[derive(Default, BorshDeserialize, BorshSerialize)]

表示该struct为合约对外主体。

可为合约编写相应的测试用例,测试没问题后,可编译合约:

cargo build --target wasm32-unknown-unknown --release

编译的.wasm文件可部署到NEAR链上。可借助near-cli工具进行部署。

near deploy --wasmFile target/wasm32-unknown-unknown/release/rust_counter_tutorial.wasm --accountId YOUR_ACCOUNT_HERE

合约调用方法可为:

near call YOUR_ACCOUNT_HERE decrement --accountId YOUR_ACCOUNT_HERE

相应结果为:

Scheduling a call: YOUR_ACCOUNT.testnet.decrement()
Receipt: 3HiRrL4fg9Q62VV9aVfAdRk8bQ5nYEYTni9482qjPJ71
Log [YOUR_ACCOUNT.testnet]: Decreased number to 0
Log [YOUR_ACCOUNT.testnet]: Make sure you don't overflow, my friend.
Transaction Id 7jVgp677evM7srG697bVWErErzieBWExLvkSiBfvZ8YC

合约查看可为:

near view YOUR_ACCOUNT_HERE get_num --accountId YOUR_ACCOUNT_HERE

相应结果为:

View call: YOUR_ACCOUNT.testnet.get_num()
1

参考资料

[1] NEAR docs Building a Smart Contract in Rust

最后

以上就是健壮太阳为你收集整理的NEAR 智能合约开发1. 引言参考资料的全部内容,希望文章能够帮你解决NEAR 智能合约开发1. 引言参考资料所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部