3

Create Smart Contract

 2 years ago
source link: https://www.inevitable.tech/posts/458d64bd/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Create Smart Contract

发表于 2021-01-01

更新于 2021-01-06

分类于

开发

阅读次数:

Valine: 0

一键搭建初始化以太坊开发环境。

这个项目有点类似 create-react-app。目的是帮助开发者快速初始化开发环境。做起来还是挺简单的。就是写一个 Node 脚本。里面包含初始化项目文件夹,安装依赖和替换模板文件。具体项目使用和介绍可以 看 Github 主页

首先你得已经全局安装 truffle。这样 create-smart-contract myapp,会用 truffle 为你初始化一个项目文件夹。然后为你安装一系列有用的插件和包。最后给你生成一些默认的配置文件。以下是包含的框架,插件,包和配置文件。由于目前 Openzeppelin 不支持 0.8 的 solc,所以我默认使用 0.7 的最新版。

  • truffle
  • hardhat
    • [ @nomiclabs/hardhat-ethers](https://www.npmjs.com/package/@nomiclabs/hardhat-ethers)
    • [@nomiclabs/hardhat-truffle5](https://www.npmjs.com/package/@nomiclabs/hardhat-ethers)
    • [@nomiclabs/hardhat-waffle](https://www.npmjs.com/package/@nomiclabs/hardhat-waffle)
    • [@nomiclabs/hardhat-web3](https://www.npmjs.com/package/@nomiclabs/hardhat-web3)
  • ethers
  • [@openzeppelin/contracts](https://www.npmjs.com/package/@openzeppelin/contracts)
  • chai
  • solidity-docgen
  • mocha
  • prettier
  • prettier-plugin-solidity
  • solc
  • web3
  • ethereum-waffle
  • .gitignore
  • .prettierrc
  • hardhat.config.js
  • slither.config.json
  • solcover.js
  • truffle-config.js

除此之外我还加入了一些有用的 Node 脚本。

  • npm run test: 用来跑测试的。我这采用了 Hardhat。因为它跑 test 的速度快,并且支持在智能合约中使用 console.log
  • npm run doc: 根据 doxygen 自动生成文档。
  • npm run coverage: 根据配置文件 .solcover.js 生成测试覆盖率报告。
  • npm run analyze 根据配置文件 slither.config.json 静态分析智能合约。注意:要求已经安装 Slither

你可以自定义想要的包。更改 index.jsinstallPackages。例如

const installPackages = () => {
return new Promise((resolve) => {
console.log("\nInstalling hardhat\n".cyan);
shell.exec(`npm install --save-dev hardhat`, () => {
console.log("\nFinished installing packages\n".green);
resolve();
});
});
};

你可以自己更改,添加或删除模板。例如你想要添加模板,你需要将模板文件加入 templates/ 然后修改 templates/templates.js。例如

let fs = require("fs");
let path = require("path");
const gitIgnore = fs.readFileSync(path.resolve(__dirname, "./gitignore"));
const solcover = fs.readFileSync(path.resolve(__dirname, "./solcover.js"));
const slither = fs.readFileSync(
path.resolve(__dirname, "./slither.config.json")
);
const hardhatConfig = fs.readFileSync(
path.resolve(__dirname, "./hardhat.config.js")
);
const truffleConfig = fs.readFileSync(
path.resolve(__dirname, "./truffle-config.js")
);
const prettier = fs.readFileSync(path.resolve(__dirname, "./.prettierrc"));
const package = fs.readFileSync(path.resolve(__dirname, "package.json"));
const env = fs.readFileSync(path.resolve(__dirname, ".env"));
module.exports = {
".gitignore": gitIgnore,
"solcover.js": solcover,
"slither.config.json": slither,
"truffle-config.js": truffleConfig,
"hardhat.config.js": hardhatConfig,
".prettierrc": prettier,
"package.json": package,
".env": env,
};

祝你开发愉快。


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK