# 使用 Conflux-Truffle 部署合约

Truffle 是以太坊生态著名的智能合约开发工具，提供编译，链接，测试，部署等实用功能，为广大 Solidity 开发者所喜爱。Conflux 作为新一代高性能公链，不仅在完全去中心化的前提下实现了两个量级的性能提升，还实现了跟 EVM 兼容的虚拟机， 意味着 Dapp 开发者不用学习新开发语言即可以在 Conflux 网络上开发应用。为了提升 Conflux 的合约开发体验，官方最近也对 Truffle 进行了迁移改造，打造了 Conflux-Truffle，使之能够支持 Conflux 的合约开发。

使用 Conflux-Truffle 部署合约，更多资料和相关信息:

* Conflux-Truffle 完全使用指南: <https://juejin.cn/post/6862239117934067726>
* Conflux-Truffle: <https://github.com/Pana/conflux-101/blob/master/docs/conflux-truffle.md>
* 使用 Conflux-Truffle 智能合约开发工作流: <https://shimo.im/docs/e1Az4M7EwbCVadqW>
* 以及 Lecture7.1 - Conflux-Truffle 及标准合约介绍: [https://confluxedu.wixsite.com/beidoutianxuan2021/课程录屏](https://confluxedu.wixsite.com/beidoutianxuan2021/%E8%AF%BE%E7%A8%8B%E5%BD%95%E5%B1%8F)

熟悉 Node.js 的小伙伴可以通过以下步骤快速安装并使用 Conflux-Truffle:

```
// 全局安装cfxtruffle
npm install -g conflux-truffle
// 初始化一个项目
cfxtruffle init project-name
// 添加合约
cfxtruffle create contract contract-name
// 安装@openzeppelin/contracts以引入需要的合约
npm install @openzeppelin/contracts
// 编译写好的合约
// 编译完成后会生成一个build文件夹
// 里面有合约相关的json文件
// 想要重新编译所有文件可以使用 cfxtruffle compile --all
cfxtruffle compile


// 配置truffle-config.js文件
// 通过远程节点将合约部署到Conflux主网
module.exports = {
  networks: {
    Tethys: { 
        url: "https://main.confluxrpc.com", // Conflux主网rpc
        network_id: "1029", // 主网id
        /**
         * 用于发送交易部署合约的账户私钥
         * 需要预先在账户里放一些cfx
         * 部署合约的费用通常在11-14cfx
         */
        privateKeys: [""],
    },
  },
  // 在此处指定solc编译器版本，即你要部署的合约所使用的版本
  compilers: {
    solc: {
      //  Note: 也可以不指定，使用 version: "pragma" 来自动检测编译器版本
      version: "^0.8.0",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    }
  },
}
// 添加合约部署脚本
cfxtruffle create migration contract-name
/**
  * Note:
  * 创建部署脚本后需要将其重命名，按照所有合约部署的历史顺序
  * 如初始化后预设的第一个脚本名称是 1_initial_migration.js
  * 那么如果你现在要部署自己的合约
  * 则应该创建一个名称为 2_any_maybe_your_new_contract_name.js
  * 的部署脚本
  */


// 在部署脚本中加入以下代码
const any = artifacts.require("contract-name");
module.exports = function(_deployer, network) {
  // Use deployer to state migration tasks.
  _deployer.deploy(any)
  /**
    * Note:
    * _deployer.deploy() 的参数由你的合约构造器 constructor() 的参数决定
    * 此处 _deployer.deploy(any) 假设 constructor() 不需要额外参数
    * 如果你的合约在部署的时候需要额外传入参数给构造器，比如
    * constructor(
        string memory name_,
        string memory symbol_,
        string memory uri_
      )
    * 那么就需要把参数按顺序传给 deploy 函数，像这样
    * _deployer.deploy(any, 'arg1', 'arg2', 'arg3', ......)
    * 如果没有在部署的时候正确传入参数，就会产生如下报错
    * "Invalid number of parameters for "undefined". Got 0 expected 3!"
    */
};
/**

 * 运行部署命令
 * --reset用于重新部署所有合约
 * --network指定使用的网络，默认使用development网络
 */
cfxtruffle deploy --reset --network Testnet
// deploy命令执行完成之后，会输出部署的结果
// 比如交易hash，合约地址，花费的费用等
// 可以在conflux浏览器 https://confluxscan.net/ 复制合约地址来查看
```

**Tips**: 如果你在 VSCode 编辑器上使用 Conflux-Truffle 框架进行开发，可以安装一个 Solidity Contract Flattener 插件，它可以把你的合约联同其所有 import 的依赖项压缩到同一个文件。在某些场景，比如出于开源的目的，你希望将合约公布到区块链浏览器(e.g. ConfluxScan) 进行验证并供别人参阅，同时你的合约拥有复杂的依赖关系时用这个插件会方便很多。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://conflux-technical-support.gitbook.io/conflux-nft-kai-fa-zhi-nan/step-4-zai-lian-shang-bu-shu-ni-de-he-yue/shi-yong-confluxtruffle-bu-shu-he-yue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
