openzeppelin批量测试Solidity合约
Posted sanqima
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了openzeppelin批量测试Solidity合约相关的知识,希望对你有一定的参考价值。
openzeppelin是用于Solidity合约进行审计、代码安全测试的库,其中test-environment、test-helpers使用起来非常方便。下面,介绍对ERC20GuDingToken.sol、ERC20XiaoHuiToken.sol这2个合约的批量测试。
1、 创建工程onecoin
1.1 创建工程和文件夹
## 1、创建工程
mkdir onecoin
cd onecoin
npm init -y
truffle init
## 2、创建文件夹
mkdir -p test/ERC20
mkdir -p test/inc
mkdir -p contracts/ERC20
mkdir script
1.2 修改package.json
a) 修改onecoin/package.json文件,如下:
{
"name": "onecoin",
"version": "1.0.0",
"description": "",
"main": "",
"directories": {
"test": "test"
},
"scripts": {
"test": "node script/test.js",
"compile": "truffle compile",
"ganache": "ganache-cli -e 1000",
"migrate": "truffle migrate",
"mocha": "mocha --exit --recursive"
},
"mocha": {
"timeout": 100000,
"useColors": true,
"comment": "这里是彩蛋:下面这一行改成nyan然后再运行npm run test试一下",
"reporter": "spec"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^2.5.1",
"@truffle/debug-utils": "^5.1.17",
"@truffle/hdwallet-provider": "^1.5.0",
"bip39": "^3.0.4",
"ethers": "^5.4.7",
"inquirer": "^8.1.5",
"web3": "^1.6.0"
},
"devDependencies": {
"@openzeppelin/test-environment": "^0.1.9",
"@openzeppelin/test-helpers": "^0.5.13",
"chai": "^4.3.4",
"eth-gas-reporter": "^0.2.22",
"mocha": "^9.1.2",
"@babel/core": "^7.15.5",
"@babel/preset-env": "^7.15.6"
}
}
b) 安装依赖包
npm config set registry https://registry.npm.taobao.org
npm install
1.3 修改truffle-config.js
修改后的truffle-config.js如下:
module.exports = {
networks: {
// Useful for testing. The `development` name is special - truffle uses it by default
// if it's defined here and no other network is specified at the command line.
// You should run a client (like ganache-cli, geth or parity) in a separate terminal
// tab if you use this network and you must also set the `host`, `port` and `network_id`
// options below to some value.
//
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 8545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
// Another network with more advanced options...
// advanced: {
// port: 8777, // Custom port
// network_id: 1342, // Custom network
// gas: 8500000, // Gas sent with each transaction (default: ~6700000)
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei)
// from: <address>, // Account to send txs from (default: accounts[0])
// websocket: true // Enable EventEmitter interface for web3 (default: false)
// },
// Useful for deploying to a public network.
// NB: It's important to wrap the provider as a function.
// ropsten: {
// provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
// network_id: 3, // 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 )
// },
// Useful for private networks
// private: {
// provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
// network_id: 2111, // This network is yours, in the cloud.
// production: true // Treats this network as if it was a public net. (default: false)
// }
},
// Set default mocha options here, use special reporters etc.
mocha: {
timeout: 300000,
reporter: 'eth-gas-reporter',
reporterOptions: { excludeContracts: ['Migrations'] }
},
// Configure your compilers
compilers: {
solc: {
version: "0.5.12", // 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"
// }
}
},
// Truffle DB is currently disabled by default; to enable it, change enabled:
// false to enabled: true. The default storage location can also be
// overridden by specifying the adapter settings, as shown in the commented code below.
//
// NOTE: It is not possible to migrate your contracts to truffle DB and you should
// make a backup of your artifacts to a safe location before enabling this feature.
//
// After you backed up your artifacts you can utilize db by running migrate as follows:
// $ truffle migrate --reset --compile-all
//
// db: {
// enabled: false,
// host: "127.0.0.1",
// adapter: {
// name: "sqlite",
// settings: {
// directory: ".db"
// }
// }
// }
};
1.4 ERC20GuDingToken合约
路径: onecoin/contracts/ERC20GuDingToken.sol
pragma solidity >=0.4.21 <0.7.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
//固定总量Token
contract ERC20GuDingToken is ERC20,ERC20Detailed {
constructor(
string memory name, //全称
string memory symbol,//简称
uint8 decimals, //精度
uint256 totalSupply //总量
) public ERC20Detailed(name,symbol,decimals) {
_mint(msg.sender, totalSupply*(10**uint256(decimals)));
}
}
1.5 ERC20XiaoHuiToken合约
路径: onecoin/contracts/ERC20XiaoHuiToken.sol
pragma solidity >=0.4.21 <0.7.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
contract ERC20XiaoHuiToken is ERC20,ERC20Detailed,ERC20Burnable {
constructor(
string memory name, //全称
string memory symbol, //简称
uint8 decimals, //精度
uint256 totalSupply //总量
) public ERC20Detailed(name,symbol,decimals) {
_mint(msg.sender,totalSupply*(10**uint256(decimals)));
}
}
1.6 开启ganache
在 ganache里设置IP:127.0.0.1, 端口:8545,重启ganache,如图(1)所示:
1.7 编写部署脚本
a) ERC20GuDingToken合约的部署脚本:onecoin/migrations/2_deploy_ERC20GuDingToken.js
// 2_deploy_ERC20GuDingToken.js
const ERC20GuDingToken = artifacts.require("ERC20GuDingToken");
module.exports = function(deployer) {
deployer.deploy(ERC20GuDingToken,
"GuDingToken","GDT",18,80000);
};
b) ERC20XiaoHuiToken合约的部署脚本:onecoin/migrations/3_deploy_ERC20XiaoHuiToken.js
// 3_deploy_ERC20XiaoHuiToken.js
const ERC20XiaoHuiToken = artifacts.require("ERC20XiaoHuiToken");
module.exports = function(deployer) {
deployer.deploy(ERC20XiaoHuiToken,
"XiaoHuiToken","XHT",18,80000);
};
1.8 在ganache里部署合约
cd onecoin
truffle console
compile
migrate
1.9 工程目录结构
onecoin的目录结构如下:
2、编写测试脚本
2.1 公共脚本
编写测试ERC20公共功能的脚本: onecoin/test/inc/ERC20.js
//ERC20.js
const assert = require('assert');
const {ether,constants,expectEvent} = require('@openzeppelin/test-helpers');
//导出 detail()函数
exports.detail =() => {
it('Token名称:name()',async function(){
assert.equal(ERC20Param[0], await ERC20Instance.name());
});
it('Token缩写:symbol()',async function(){
assert.equal(ERC20Param[1], await ERC20Instance.symbol());
});
it('Token精度:decimals()',async function(){
assert.equal(ERC20Param[2], (await ERC20Instance.decimals()).toString());
});
it('Token总量:totalSupply()',async function(){
assert.equal(ether(ERC20Param[3]).toString(), (await ERC20Instance.totalSupply()).toString());
});
}
//测试Token总量
exports.totalSupply =(totalSupply) => {
it('Token总量:totalSupply()',async function(){
assert.equal(ether(totalSupply).toString(),(await ERC20Instance.totalSupply()).toString());
});
}
//测试账户余额
exports.balanceOf =(balance,account,desc) => {
it(desc + ':balanceOf()',async function(){
assert.equal(ether(balance).toString(), (await ERC20Instance.balanceOf(account)).toString());
});
}
//测试封顶额度
exports.cap =(cap,desc) => {
it(desc + ':cap()',async function(){
assert.equal(ether(cap).toString(), (await ERC20Instance.cap()).toString());
});
}
//测试Token发送
exports.transfer =(sender,receiver,amount,desc,reject,msg) => {
it(desc + ':transfer()', async function(){
if(reject) {
await assert.rejects(ERC20Instance.transfer(receiver,ether(amount), {from:sender}), msg);
} else {
let receipt = await ERC20Instance.transfer(receiver,ether(amount), {from:sender});
expectEvent(receipt, 'Transfer', {
from: sender,
to: receiver,
value: ether(amount),
});
}
});
}
//测试批准额度
exports.approve = (sender,receiver,amount,desc,reject,msg) => {
it(desc + ':approve()', async function(){
if(reject) {
await assert.rejects(ERC20Instance.approve(receiver,ether(amount),{from:sender}),msg);
} else {
let receipt = await ERC20Instance.approve(receiver, ether(amount), { from: sender });
expectEvent(receipt, 'Approval', {
owner: sender,
spender: receiver,
value: ether(amount),
});
}
});
}
//测试批准发送
exports.transferFrom =(owner,sender,receiver,amount,desc,reject,msg) => {
it(desc + ':transferFrom()', async function(){
if (reject) {
await assert.rejects(ERC20Instance.transferFrom(owner,receiver,ether(amount),{from:sender}), msg);
} else {
let receipt = await ERC20Instance.transferFrom(owner,receiver,ether(amount),{from:sender});
expectEvent(receipt, 'Transfer', {
from: owner,
to: receiver,
value: ether(amount),
});
}
});
}
//测试批准数额
exports.allowance = (owner,sender,amount,desc) => {
it(desc + ':allowance()',async function(){
assert.equal(ether(amount), (await ERC20Instance.allowance(owner,sender)).toString());
});
}
//测试增加批准数额
exports.increaseAllowance = (sender,receiver,amount,desc,reject,msg) => {
it(desc + ':increaseAllowance()', async function() {
if(reject) {
await assert.rejects(ERC20Instance.increaseAllowance(receiver,ether(amount), {from:sender}),msg);
} else {
let receipt = await ERC20Instance.increaseAllowance(receiver,ether(amount),{from:sender});
expectEvent(receipt,'Approval',{
owner:sender,
spender: receiver,
});
}
});
}
//批准减少批准额度
exports.decreaseAllowance = (sender,receiver,amount,desc,reject,msg) => {
it(desc + ': decreaseAllowance()', async function () {
if(reject) {
await assert.rejects(ERC20Instance.decreaseAllowance(receiver, ether(amount), { from: sender }), msg);
} else {
let receipt = await ERC20Instance.decreaseAllowance(receiver,ether(amount),{from:sender});
expectEvent(receipt,'Approval',{
owner: sender,
spender:receiver,
});
}
});
}
//测试销毁方法
exports.burn = (sender,amount,desc,reject,msg) => {
it(desc + ':burn()',async function(){
if(reject){
await assert.rejects(ERC20Instance.burn(ether(amount),{from:sender}), msg);
} else {
let receipt = await ERC20Instance.burn(ether(amount), { from: sender });
expectEvent(receipt, 'Transfer', {
from: sender,
to: constants.ZERO_ADDRESS,
value: ether(amount),
});
}
});
<以上是关于openzeppelin批量测试Solidity合约的主要内容,如果未能解决你的问题,请参考以下文章
openzeppelin-solidity/contracts的代码学习——payment