# Truffle 入门 **Published by:** [nightelfamber](https://paragraph.com/@nightelfamber/) **Published on:** 2022-03-03 **URL:** https://paragraph.com/@nightelfamber/truffle ## Content Truff: 编译、部署、测试合约一套开发工具 Ganache: 开发区块链,提供本地模拟的链上环境 https://trufflesuite.com/docs/truffle/ 本人都在linux 系统上面学习,所以下面说说明都是基于cli Truff,Ganache的安装不再进行说明创建工程创建文件mkdir MetaCoin cd MetaCoin 初始化工程,为了方便简单操作,这里我直接使用init的方式truffle init 文件目录contracts : 合约目录 Migrations:迁移文件,用来指示如何部署智能合约 test:智能合约测试用例文件夹 truffle-config.js: 配置文件,配置truffle连接的网络及编译选项编译合约truffle compile truffle-config.js 指定编译版本,这里指定0.8.11指定编译版本输出结果控制台部署合约truffle-config.js配置网络,这里先展示基于Gananche的本地部署 网络配置本地部署编写部署脚本,需要在migrations文件下面进行文件的编写,init 其实已经OK了的,这里可以先看看,熟悉一下const Migrations = artifacts.require("Migrations"); module.exports = function (deployer) { deployer.deploy(Migrations); }; 启动本地网络这里是rpc 以及链上的相关信息,所以我们的配置信息也要改成这样的rpc部署这个就会部署了,成功success部署远程网络配置,mnemonic 密钥和助记词都可以rinkeby: { provider: () => new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/${your-id}`), network_id: 4, // Ropsten's id gas: 5500000, // Ropsten has a lower block limit than mainnet confirmations: 2, // # of confs to wait between deployments. (default: 0) timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) }, 安装,千万不要全局安装npm install @truffle/hdwallet-provider 启动部署truffle migrate --network rinkeby 部署成功result查看交易hash 链上 ## Publication Information - [nightelfamber](https://paragraph.com/@nightelfamber/): Publication homepage - [All Posts](https://paragraph.com/@nightelfamber/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@nightelfamber): Subscribe to updates - [Twitter](https://twitter.com/Stars587): Follow on Twitter