# Nibiru激励性测试网第二阶段-智能合约

By [Silent ⚛| validator](https://paragraph.com/@exploring) · 2023-04-11

---

Nibiru是一个基于Cosmos Sdk的DEFI Hub，为衍生品和现货交易提供动力。安全、无需许可、完全上链。其种子轮融资了850万美元，Tribe captital, Republic Crypto 领投，Kraken等机构参投,估值1亿美元。近期，[激励测试网第二阶段刚刚开放](https://nibiru.fi/blog/posts/010-itn-2-cosmwasm-governance.html)，明牌有奖励。要KYC。

[NIT #2](https://nibiru.fi/blog/posts/010-itn-2-cosmwasm-governance.html) 主要围绕两件事：

（1）让 Nibiru Chain 的用户熟悉智能合约部署和交易执行

（2）通过试验更大的验证器集大小来对我们的基础设施进行实时测试。通过这种方式，我们应该对真正去中心化网络的更快出块时间和终结性的一些限制有深刻的理解。

测试网注册：**(必须要先注册才能参与激励测试网,第一阶段注册过的朋友可以跳过)**

[https://gleam.io/yW6Ho/nibiru-incentivized-testnet-registration](https://gleam.io/yW6Ho/nibiru-incentivized-testnet-registration)

这次测试网主要有两类任务。本文为智能合约任务的教程。**本文需要在命令行操作，有一定难度。如果没有相关经验，可以去完成相对简单的**[治理任务](https://mirror.xyz/exploring.eth/kHCPsU3q2PCT5kCXBM-yJsFvIRwJsr9x4eiw7CjTkdY)。

智能合约任务如下：

![智能合约任务](https://storage.googleapis.com/papyrus_images/ab16ce08b857e2fd8b9c8676601c8f84233b0ce5e5bdd806605591bd235777d5.png)

智能合约任务

我们主要需要完成以下操作。

1.  部署合约
    
2.  实例化合约
    
3.  执行合约
    

前提条件
====

在你的linux系统服务器上安装好nibid程序，可以参考我之前的[节点教程](https://mirror.xyz/exploring.eth/Z-K9MVYHJWzvsmLeYvguSMO7JysLpRnE4M_TvZ0-5ak)。

**注：我们不需要运行节点，可以使用别人提供的公共Rpc。**

使用公共RPC作为默认RPC

    nibid config node https://t-nibiru.rpc.utsa.tech:443
    

备选的公共RPC:

    https://nibiru-testnet.nodejumper.io:443
    https://rpc-t.nibiru.nodestake.top:443
    

用助记词导入你的钱包(**如果你已经导入，请忽略**)

    nibid keys add xxx --recover
    

部署合约
====

首先下载已经编译好的合约二进制文件

    wget https://github.com/NibiruChain/cw-nibiru/raw/main/artifacts-cw-plus/cw1_whitelist.wasm
    

部署合约

    KEY_NAME=钱包名
    CONTRACT_WASM="cw1_whitelist.wasm" 
    nibid tx wasm store $CONTRACT_WASM --from $KEY_NAME --gas=2000000 --fees=200000unibi --chain-id nibiru-itn-1  -y
    

**保存得到的txhash**，可以去下面的区块浏览器检查是否成功。也可以用下面的命令可以查询到你刚刚部署的合约的信息。

[

Nibiru (NIBI) Blockchain Explorer - Main Network Overview
---------------------------------------------------------

Get insights into Nibiru (NIBI) network via Nodes.Guru blockchain explorer. Track the latest block, staking APR, inflation rate, your stake and more.

https://nibiru.explorers.guru

![](https://storage.googleapis.com/papyrus_images/c64564917b3336448d9e2e3ebf0aafbacca52caaf53f870dfa3f05948430a96f.jpg)

](https://nibiru.explorers.guru/)

![交易成功](https://storage.googleapis.com/papyrus_images/a434b0760d55e240c1a935bec038412506071101134a3966fc2ca341e7394e92.png)

交易成功

    txhash=刚刚的txhash
    nibid query tx  $txhash --output json | jq -r '.logs[] | .events[]'
    

如果交易成功，上面的命令显示如下。

![query tx显示如下](https://storage.googleapis.com/papyrus_images/2d1b682eba247aaae6742f0856628cef5efccc91f300476cd688dae249689103.png)

query tx显示如下

这里得到的code\_id在实例化合约的时候有用。但是本教程中暂时用不到。

实例化合约
=====

这里我们要实例化的是一个cw20代币合约[https://github.com/CosmWasm/cw-plus/blob/main/packages/cw20/README.md](https://github.com/CosmWasm/cw-plus/blob/main/packages/cw20/README.md)

这个合约目前已经被存储在链上,它的code id为2257。如果你想实例化自己部署的合约，可以去部署一个cw20合约。

首先，设置一些环境变量

    code_id=2257
    address=你的nibiru钱包地址
    KEY_NAME=你的钱包名
    

创建一个json文件，用于传递实例化时需要的参数

    tee args.json > /dev/null <<EOF
    {
      "name": "Custom CW20 token",
      "symbol": "CWXX",
      "decimals": 6,
      "initial_balances": [
        {
          "address": "${address}",
          "amount": "555444000"
        }
      ],
      "mint": { "minter": "${address}" },
      "marketing": {}
    }
    EOF
    

实例化这个cw20合约

    nibid tx wasm inst $code_id "$(cat args.json)" --label="mint CWXX contract" --no-admin --from=$KEY_NAME --fees=5000unibi --chain-id nibiru-itn-1 -y
    

![保存这个txhash](https://storage.googleapis.com/papyrus_images/1d790fcf3c63b6855d01e8fc0f9408c2c48d4741982a2c5e13ee84b5b018bcc7.png)

保存这个txhash

**保存得到的txhash**，可以去[区块浏览器](https://nibiru.explorers.guru/)查询交易是否成功。

    txhash=刚刚得到的txhash
    

如果成功，我们可以用如下命令，获取部署成功得到的合约地址。**我们执行合约时需要这个地址。**

    contract=$(nibid q tx $txhash --output json| jq  -r '.logs[] | .events[0] | .attributes[0] | .value')
    

输出这个地址

    echo $contract
    

执行合约
====

创建一个json文件，用于传递调用合约需要的参数

    address=任意一个nibiru地址，也可以是自己的地址
    KEY_NAME=你的钱包名
    

    tee cw_transfer.json > /dev/null <<EOF
    {
      "transfer": {
        "recipient": "${address}",
        "amount": "50"
      }
    }
    EOF
    

这里的参数来源于[CW20合约](https://github.com/CosmWasm/cw-plus/blob/v1.0.1/packages/cw20/src/msg.rs)的_Cw20ExecuteMsg （这里仅供学习参考，不需要输入）_

    pub enum Cw20ExecuteMsg {
        /// Transfer is a base message to move tokens to another account without triggering actions
        Transfer { recipient: String, amount: Uint128 },
        // ... 
    }
    

执行合约

    nibid tx wasm execute $contract "$(cat cw_transfer.json)" --from $KEY_NAME --gas 8000000 --fees=200000unibi -y --chain-id nibiru-itn-1 -y
    

**保存得到的txhash**。可以去[区块浏览器](https://nibiru.explorers.guru/)查询调用cw20的transfer函数是否成功。

也可以用如下命令检查你这笔交易产生的events

    txhash=刚刚的txhash
    nibid q tx $txhash --output json| jq -r '.logs[] | .events[]'
    

显示应该如下

![执行成功的events](https://storage.googleapis.com/papyrus_images/cebd6ecaac770807d2edbce06123eafb4ec68d1584eec67411f5f0f253e6fb53.png)

执行成功的events

至此，我们就完成了Nibiru激励测试网第二阶段的智能合约相关的任务。🔥

欢迎关注 [https://silentvalidator.com](https://silentvalidator.com)
===============================================================

---

*Originally published on [Silent ⚛| validator](https://paragraph.com/@exploring/nibiru-3)*
