1、Vscode下安装Solidity插件,change到指定的solidity编译器的版本下
2、Truffle开发框架的安装 :npm install -g truffle
3、在workspace下新建一个truffle-test的项目目录,执行truffle init初始化项目
D:\WorkSpace\
4、在migrations目录下新建一个js文件:1_deploy_contracts.js (注意文件执行顺序与1_, 2_ 开头)
导出的包引入合约(类)
const HelloWorld = artifacts.require(“HelloWorld”);
部署合约时默认无构造器的使用方式:
module.exports = function(deployer) {
deployer.deploy(HelloWorld);
};
部署合约时带构造器和参数的使用方式:
module.exports = function(deployer) {
deployer.deploy(
Trace,
"0x670fDC5ffaa8005c9E92da49a77bd45875164579",
"0x04ECBe02d0711eCcb1903CA89873DFd7010F8C78",
"0xaDC5971E6D30018B9E47abe96A1AE40D030e0a95"
);
};
部署库合约,使用Link进行链接
// 部署LibA库, 然后将LibA链接到合约B, 然后再部署B
deployer.deploy(LibA);
deployer.link(LibA, B);
deployer.deploy(B);
5、在终端输入:truffle compile
6、在终端输入:truffle migrate
7、编写合约测试用例(使用assert断言,关注相关用法)
const HelloWorld = artifacts.require('HelloWorld');
const Asset = artifacts.require("Asset");
contract("HelloWorld", (accounts) => {
it("HelloWorld Test", async() => {
const helloWorldInstance = await HelloWorld.deployed();
await helloWorldInstance.set("hello truffle");
const name = await helloWorldInstance.get();
assert.equal(name, "hello truffle");
})
})
describe("Asset", function () {
it("Asset Test", async() => {
const assetInstance = await Asset.deployed();
await assetInstance.issue("0x6eb35A9339B302541666005FEDFD486E61Ad8BC4", 100);
await assetInstance.send("0x7F0d099dF9b42d2839525CB2Fc9F32d014977555", 20);
const amount = await assetInstance.get("0x6eb35A9339B302541666005FEDFD486E61Ad8BC4");
assert.equal(amount, 80);
})
})
8、在终端输入:truffle test