# Near 学习.1 **Published by:** [Ball Rich](https://paragraph.com/@ball-rich/) **Published on:** 2022-04-19 **URL:** https://paragraph.com/@ball-rich/near-1 ## Content 1. 申请testnet钱包https://wallet.testnet.near.org/ 测试网的后缀为.testnet 主网为.near申请钱包选择一种方式来保证账户的安全安全选择选择助记词后,复制保存好,进行验证等待申请完成后,会有200N的测试币使用2. 开发环境配置我的环境:ubuntu20.04 虚拟机 安装nvm,用来管理node的版本 `//安装nvm #curl -o- 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.03. 命令行登录钱包#near login 会弹出一个授权钱包的网页,选择需要连接的钱包后会自动回调命令行的接口。 因为测试网的RPC总是断掉,可以使用命令所以换一个其他的 #export NEAR_CLI_TESTNET_RPC_SERVER_URL=https://public-rpc.blockpi.io/http/near-testnet选择连接钱包登录成功回调登录成功4. 试试合约官方案例: 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: 管理三方包的 其他的:看不懂(应该不重要)~目录结构重新编译下合约,添加wasm编译器 #cargo build --target wasm32-unknown-unknown --release 等待编译完成。。。。完成后会多出一个target目录 找到status_message.wasm这个文件就是编译好的合约编译好的合约部署合约 #near dev-deploy res/status_message.wasm dev-deploy会自动生成一个dev accountID用来存储合约代码,可以用来测试代码部署成功dev-1650358202077-31407618874600为自动生成的合约账户地址, HRFSfvTM1jfAuwZ26qPjE8pPYEMHrWeyzgQcAHubcpVm 交易hash 都可以在区块链浏览器上查看区块浏览器在目录会生成一个neardev账户,这两个记录了部署的合约账户地址,以方便前端调用合约时使用neardev调用view查询合约数据 #near view dev-1650358202077-31407618874600 get_status '{"account_id":"ballwww.testnet"}' dev-1650358202077-31407618874600 合约账户地址 get_status 函数名 '{"account_id":"ballwww.testnet"}' 参数 返回内容 View call: dev-1650358202077-31407618874600.get_status({"account_id":"ballwww.testnet"}) null 因为ballwww.testnet在合约里没有状态,所以返回null 调用call修改合约数据,使用ballwww.testnet账户来存储状态消息”hello” #near call dev-1650358202077-31407618874600 set_status '{"message":"hello"}' --accountId ballwww.testnet 存储成功存储状态消息成功再次查询,可以看到ballwww.testnet的状态消息变为了helloOK~~~ ok 合约已经完成了,剩下的启动前端代码(react写的,我也不会) 前端代码安装三方包命令: #npm install 启动命令:#npm run start启动前端在浏览器中输入地址:http://localhost:1234,可以看到页面网页登录ballwww.testnet 就可以看到我刚刚存储的状态值登录查看存储消息点击Update更新状态消息等待一会,状态消息就更新了,并没有输入密钥等信息;ok~~~~ 完事儿了 ## Publication Information - [Ball Rich](https://paragraph.com/@ball-rich/): Publication homepage - [All Posts](https://paragraph.com/@ball-rich/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@ball-rich): Subscribe to updates