Waffle 安装学习
注:本文全部内容为转载内容,仅为方便个人学习整理备份使用,原文 Zero_Nothing,原作者文章清晰易读,便于初学者参考,学习使用建议阅读原文。安装准备# 个人环境 macOS、nvm、npm、yarn# nvm 两种方式 curl or wgetcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh |
·
注:本文内容主要为转载内容与个人实践补充,仅为方便个人学习按需整理备份使用,原文 Zero_Nothing,原作者文章清晰易读,便于初学者参考,学习使用建议阅读原文。
安装准备
# 个人环境 macOS、nvm、npm、yarn
# nvm 两种方式 curl or wget
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
# or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
# 使用source命令,如下
source ~/.bashrc
# 如果是 zsh,请用
source ~/.zshrc
# 查看 nvm 帮助信息
nvm
### 安装指定版本 node
# 列出全部可以安装的版本号
nvm ls-remote
# 例安装 v14.14.0 版
nvm install v14.14.0 && nvm use v14.14.0
# 查看当前版本
nvm current
npm -v
node -v
# 安装 yarn
npm install --global yarn
# 设置 GitHub 国内镜像源,防止后续依赖安装,由于连接 GitHub 不稳定失败
git config --global url."https://hub.fastgit.org".insteadOf https://github.com
开始安装 waffle
mkdir waffle
cd waffle
yarn init
mkdir src
mkdir test
# 安装依赖
yarn add --dev ethereum-waffle
yarn add @openzeppelin/contracts -D
yarn add --dev mocha chai
yarn add ts-node -D
yarn add typescript -D
yarn add @types/chai -D
yarn add @types/mocha -D
vim package.json
{
"name": "wt1",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"@openzeppelin/contracts": "^3.4.1",
"@types/chai": "^4.2.15",
"@types/mocha": "^8.2.1",
"chai": "^4.3.4",
"ethereum-waffle": "^3.3.0",
"mocha": "^8.3.2",
"ts-node": "^9.1.1",
"typescript": "^4.2.3"
},
"scripts": {
"build": "waffle",
"test": "export NODE_ENV=test && mocha"
},
"mocha": {
"extension": [
"ts"
],
"spec": "./test/**/*.test.ts",
"require": "ts-node/register",
"timeout": 12000
}
}
vim waffle.json
{
"compilerType": "solcjs",
"compilerVersion": "0.6.2",
"sourceDirectory": "./src",
"outputDirectory": "./build"
}
vim src/BasicToken.sol
pragma solidity ^0.6.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
// Example class - a mock class using delivering from ERC20
contract BasicToken is ERC20 {
constructor(uint256 initialBalance) ERC20("Basic", "BSC") public {
_mint(msg.sender, initialBalance);
}
}
# 注:运行 yarn build,使用上述 waffle.json 配置个人运行成功,因环境版本问题如失败,请参照原文修改
yarn build
vim tsconfig.json
{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"lib": [
"ES2018"
],
"module": "CommonJS",
"moduleResolution": "node",
"outDir": "dist",
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2018"
}
}
vim test/BasicToken.test.ts
import {expect, use} from 'chai';
import {Contract} from 'ethers';
import {deployContract, MockProvider, solidity} from 'ethereum-waffle';
import BasicToken from '../build/BasicToken.json';
use(solidity);
describe('BasicToken', () => {
const [wallet, walletTo] = new MockProvider().getWallets();
let token: Contract;
beforeEach(async () => {
token = await deployContract(wallet, BasicToken, [1000]);
});
it('Assigns initial balance', async () => {
expect(await token.balanceOf(wallet.address)).to.equal(1000);
});
it('Transfer adds amount to destination account', async () => {
await token.transfer(walletTo.address, 7);
expect(await token.balanceOf(walletTo.address)).to.equal(7);
});
it('Transfer emits event', async () => {
await expect(token.transfer(walletTo.address, 7))
.to.emit(token, 'Transfer')
.withArgs(wallet.address, walletTo.address, 7);
});
it('Can not transfer above the amount', async () => {
await expect(token.transfer(walletTo.address, 1007)).to.be.reverted;
});
it('Can not transfer from empty account', async () => {
const tokenFromOtherWallet = token.connect(walletTo);
await expect(tokenFromOtherWallet.transfer(wallet.address, 1))
.to.be.reverted;
});
it('Calls totalSupply on BasicToken contract', async () => {
await token.totalSupply();
expect('totalSupply').to.be.calledOnContract(token);
});
it('Calls balanceOf with sender address on BasicToken contract', async () => {
await token.balanceOf(wallet.address);
expect('balanceOf').to.be.calledOnContractWith(token, [wallet.address]);
});
});
yarn test
# 输出如下
yarn run v1.22.10
$ export NODE_ENV=test && mocha
BasicToken
✓ Assigns initial balance
✓ Transfer adds amount to destination account (109ms)
✓ Transfer emits event (89ms)
✓ Can not transfer above the amount
✓ Can not transfer from empty account
✓ Calls totalSupply on BasicToken contract
✓ Calls balanceOf with sender address on BasicToken contract
7 passing (2s)
✨ Done in 7.70s.
更多推荐
已为社区贡献1条内容
所有评论(0)