# Near 学习.1

By [Ball Rich](https://paragraph.com/@ball-rich) · 2022-04-19

---

### 1\. 申请testnet钱包

[https://wallet.testnet.near.org/](https://wallet.testnet.near.org/)

测试网的后缀为.testnet 主网为.near

![申请钱包](https://storage.googleapis.com/papyrus_images/ff183f40150fd0237dba352dd5740ad77eef4094ddfc04ca0968f9e4f1eba0fa.png)

申请钱包

选择一种方式来保证账户的安全

![安全选择](https://storage.googleapis.com/papyrus_images/a61eaa4659ee7e7e2d731d48f413c2d62386b72c25b85eb1af8ab8bf866c6462.png)

安全选择

选择助记词后，复制保存好，进行验证等待

![](https://storage.googleapis.com/papyrus_images/6bf41698655e88abd7c38279a7ecf19b42a7ab99bc3e9377046c00d0e8478aaa.png)

申请完成后，会有200N的测试币使用

![](https://storage.googleapis.com/papyrus_images/244897a377a2a4a193ea0bbad11aecf3b9541401be8b4492022308d0e0862a2b.png)

### 2\. 开发环境配置

我的环境：ubuntu20.04 虚拟机

**安装nvm，用来管理node的版本**

\`//安装nvm #curl -o- [https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh) | bash

//安装完成后，执行环境变量 #export NVM\_DIR="$(\[ -z "${XDG\_CONFIG\_HOME-}" \] && printf %s "${HOME}/.nvm" || printf %s "${XDG\_CONFIG\_HOME}/nvm")" \[ -s "$NVM\_DIR/nvm.sh" \] && . "$NVM\_DIR/nvm.sh" # This loads nvm

#nvm -v #0.39.1\`

**安装node**

\`#nvm install 14

#node -v v14.19.1 #npm -v 6.14.16\`

**安装rust**

`#curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh`

`//记得执行环境变量`

`#cargo --version cargo 1.59.0 (49d8809dc 2022-02-10)`

**安装near-cli**

`#npm -i -g near-cli`

`#near --version 3.2.0`

### 3\. 命令行登录钱包

#near login

会弹出一个授权钱包的网页，选择需要连接的钱包后会自动回调命令行的接口。

因为测试网的RPC总是断掉，可以使用命令所以换一个其他的

#export NEAR\_CLI\_TESTNET\_RPC\_SERVER\_URL=[https://public-rpc.blockpi.io/http/near-testnet](https://public-rpc.blockpi.io/http/near-testnet)

![选择连接钱包](https://storage.googleapis.com/papyrus_images/522d6d55f54f0ba8d320384bc860d17758fdbf16b86c54f59151f55b7a09a592.png)

选择连接钱包

登录成功回调

![登录成功](https://storage.googleapis.com/papyrus_images/9a5ba752a3525db34dde700cbc3ce4a33ab7e3b064cdf94dd5de51159c36b38c.png)

登录成功

### 4\. 试试合约

官方案例：

[https://examples.near.org/](https://examples.near.org/)

拉取合约代码（存值、取值合约）

`#git clone https://github.com/near-examples/rust-status-message.git`

`#cd rust-status-message/`

目录结构

frontend：前端代码

res：已编译好的wasm合约

src：合约代码（rust代码我不会）

Cargo.toml: 管理三方包的

其他的：看不懂（应该不重要）~

![目录结构](https://storage.googleapis.com/papyrus_images/4d490ffda350cf58e07cdd47f7ec1bd8f88e5e31014a16a2bcb79e3c7f3a0966.png)

目录结构

重新编译下合约，添加wasm编译器

`#cargo build --target wasm32-unknown-unknown --release`

等待编译完成。。。。完成后会多出一个target目录

找到status\_message.wasm这个文件就是编译好的合约

![编译好的合约](https://storage.googleapis.com/papyrus_images/c2c8b320cdc4c1b31cac422212f2b6579d1e60af12fb57679fd55d167ce9a651.png)

编译好的合约

部署合约

`#near dev-deploy res/status_message.wasm`

dev-deploy会自动生成一个dev accountID用来存储合约代码，可以用来测试代码

![部署成功](https://storage.googleapis.com/papyrus_images/92dd269f93cdcd2917ccb3603ff3054bd6342b5d4822c1311eac128aef2b6e6e.png)

部署成功

dev-1650358202077-31407618874600为自动生成的合约账户地址，

HRFSfvTM1jfAuwZ26qPjE8pPYEMHrWeyzgQcAHubcpVm 交易hash

都可以在区块链浏览器上查看

![区块浏览器](https://storage.googleapis.com/papyrus_images/93654c37cf1fadec2623725a696933a29258b7d05ff63eed46366fadedfacd21.png)

区块浏览器

在目录会生成一个neardev账户，这两个记录了部署的合约账户地址，以方便前端调用合约时使用

![neardev](https://storage.googleapis.com/papyrus_images/28805e09d084cf6226edd9d690875cba991b4cb656397e78fcc868eef231d155.png)

neardev

调用view查询合约数据

`#near view dev-1650358202077-31407618874600 get_status '{"account_id":"ballwww.testnet"}'`

dev-1650358202077-31407618874600 合约账户地址

get\_status 函数名

'{"account\_id":"ball[www.testnet"}](http://www.testnet%22%7D)' 参数

返回内容

`View call: dev-1650358202077-31407618874600.get_status({"account_id":"ballwww.testnet"}) null`

因为ball[www.testnet在合约里没有状态，所以返回null](http://www.testnet%E5%9C%A8%E5%90%88%E7%BA%A6%E9%87%8C%E6%B2%A1%E6%9C%89%E7%8A%B6%E6%80%81%EF%BC%8C%E6%89%80%E4%BB%A5%E8%BF%94%E5%9B%9Enull)

调用call修改合约数据，使用ball[www.testnet账户来存储状态消息”hello”](http://www.testnet%E8%B4%A6%E6%88%B7%E6%9D%A5%E5%AD%98%E5%82%A8%E7%8A%B6%E6%80%81%E6%B6%88%E6%81%AF%E2%80%9Dhello%E2%80%9D)

`#near call dev-1650358202077-31407618874600 set_status '{"message":"hello"}' --accountId ballwww.testnet`

存储成功

![存储状态消息成功](https://storage.googleapis.com/papyrus_images/9e02bd8787e245c0561bd18f024b62776f0c4da3f6158c007734e15b7818b461.png)

存储状态消息成功

再次查询，可以看到ball[www.testnet的状态消息变为了hello](http://www.testnet%E7%9A%84%E7%8A%B6%E6%80%81%E6%B6%88%E6%81%AF%E5%8F%98%E4%B8%BA%E4%BA%86hello)

![OK](https://storage.googleapis.com/papyrus_images/6e80142c743507bbd10a497bf39ab50abe823675668dcce036884ac553bc3439.png)

OK

\~~~ ok 合约已经完成了，剩下的启动前端代码（react写的，我也不会）

前端代码安装三方包命令： `#npm install`

启动命令：`#npm run start`

![启动前端](https://storage.googleapis.com/papyrus_images/0897c56f4678d74f96cbdc8e6166627ca0b57e0c221b8d302ed3c598b61bb544.png)

启动前端

在浏览器中输入地址：[http://localhost:1234,可以看到页面](http://localhost:1234,%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E9%A1%B5%E9%9D%A2)

![网页](https://storage.googleapis.com/papyrus_images/efe650676e5412eb2f3d81b2ab9edd5bbe3447b9464b7748ca6c84c0b520c9c0.png)

网页

登录ball[www.testnet](http://www.testnet) 就可以看到我刚刚存储的状态值

![登录查看存储消息](https://storage.googleapis.com/papyrus_images/10831e76155c054752748c15d7ca818fd313dc1cdb959da407690b796d9c6968.png)

登录查看存储消息

点击Update更新状态消息

![](https://storage.googleapis.com/papyrus_images/bcaec177f4531b59315180a3efebaa55867a5e2da32ac8f578d474bc5f6ade1f.png)

等待一会，状态消息就更新了，并没有输入密钥等信息；

![](https://storage.googleapis.com/papyrus_images/4b390330f5a2dda2bc55d27d0d5de03a8e03d20d43afdfa0ce0e0aef61ebcf41.png)

ok~~~~ 完事儿了

---

*Originally published on [Ball Rich](https://paragraph.com/@ball-rich/near-1)*
