> For the complete documentation index, see [llms.txt](https://chainide.gitbook.io/chainide-chinese/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chainide.gitbook.io/chainide-chinese/chainide/4.-conflux-ide/3.-shi-yong-confluxtruffle.md).

# 3. 使用 Conflux-truffle

{% hint style="info" %}
此教程适合于 Conflux Core
{% endhint %}

## 1. 打开 Sandbox

注：以 GIthub 方式登录 ChainIDE 后才可使用 Sandbox 功能。

<figure><img src="/files/OqAL5kVKecuX0O17O373" alt=""><figcaption></figcaption></figure>

打开 Sandbox，输入 cfxtruffle version`可查看当前 cfxtruffle 版本`

<figure><img src="/files/H2Rb32RFrVuTBDrj66Zk" alt=""><figcaption></figcaption></figure>

## **2. 创建项目**

创建空项目

```bash
$ cfxtruffle init 
```

另外 truffle 也提供了许多项目模板叫做 [box](https://www.trufflesuite.com/boxes)，可以使用 unbox 命令下载模板，快速开发

```
$ mkdir MetaCoin
$ cd MetaCoin
$ cfxtruffle unbox metacoin
```

创建的项目目录内容如下:

* build 存放合约编译后的文件（json）
* contracts 合约代码目录
* migrations 合约迁移脚本目录 （js 脚本）
* test 测试文件
* truffle-config.js 配置文件

**添加合约 or 迁移脚本 or test**

cfxtruffle create 可以用于创建新的合约文件

```
$ cfxtruffle create contract MetaCoin
$ cfxtruffle create migration MetaCoin
$ cfxtruffle create test MetaCoin
```

命令执行后创建的文件在对应的目录中。另外生成的 migrate 脚本文件名中包含一个时间戳，需要手动将其改为脚本中下一序号，具体原因参看[这里](https://www.trufflesuite.com/docs/truffle/getting-started/running-migrations#migration-files)

## **3. 编译合约**

```
$ cfxtruffle compile
```

编译后的内容会存放在 build 目录中。

## 4. 部署到远端节点

cfxtruffle 同样支持连接到远端节点，只需要在配置文件中配好需要部署用户的私钥(`privateKeys`)即可。

```javascript
development: {
    host: "test.confluxrpc.com",     
    port: 80,
    network_id: "*",       
    privateKeys: ["keys1xxxxxx", "keys1xxxxxx"], 
},
```

这样在部署合约，以及与合约交互的时候会使用这里配置的私钥对应的账号。

## **5. 部署合约**

```
$ cfxtruffle deploy # or cfxtruffle migrate
```

deploy 跟 migrate 本质上是一个命令，truffle 使用 migration 脚本的方式管理合约的部署和迁移，具体来说

1. migration 文件夹中的每一个脚本对应一个部署 task，添加新合约，需要添加对应的 migration 脚本
2. 所有 truffle 项目都会自带一个 Migration 合约，用于存储当前项目上次部署的位置: `last_completed_migration`, `setCompleted()`
3. cfxtruffle deploy 执行时会查询上次的部署位置，并从下一个任务开始执行，执行完成之后也会更新当次部署序号。

另外 cfxtruffle deploy 有几个参数(--reset, --from, --to)可以控制 migration 的执行规则，具体可参看 cfxtruffle help

## **6. 合约交互**

cfxtruffle 也提供了命令行式的交互环境，在该环境中可以直接获取或更新合约的状态。在 console 环境中不仅集成了合约对应的 js 类， 还包含了 `cfx`， `cfxutil` 对象，可以直接使用，非常方便（具体用法可参看 js-conflux-sdk）。

```bash
$ cfxtruffle console  # 在项目目录下执行，开启交互模式
# 初始合约实例
cfxtruffle(develop)> let instance = await MetaCoin.deployed()
cfxtruffle(develop)> instance
# 获取余额
cfxtruffle(develop)> let balance = await instance.getBalance(accounts[0])
cfxtruffle(develop)> balance.toNumber()
# 转账
cfxtruffle(develop)> let result = await instance.sendCoin(accounts[1], 10, {from: accounts[0]})
cfxtruffle(develop)> result
```

## **7. 合约测试**

cfxtruffle 默认集成了 Mocha 和 chai 测试框架，可通过 test 子命令运行。

单元测试代码示例：

```javascript
const MetaCoin = artifacts.require("MetaCoin");

contract('MetaCoin', (accounts) => {
  it('should put 10000 MetaCoin in the first account', async () => {
    const metaCoinInstance = await MetaCoin.deployed();
    const balance = await metaCoinInstance.getBalance.call(accounts[0]);

    assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
  });
}
```

执行测试：

```
$ cfxtruffle test
```

## 8. 未支持的功能

目前 cfxtruffle 迁移还在不断进行中，以下命令还无法支持 conflux

* develop
* build

## 9. 继续学习

本快速入门介绍了 Conflux Truffle 项目生命周期的基础知识，但还有很多东西需要学习。请继续阅读 [Conflux Truffle 官方仓库](https://github.com/Conflux-Chain/conflux-truffle)。
