使用Ganache-Cli、Mocha、Web3、Solc 0.8.6编译器运行智能合约



在学习和理解智能合约的udemy课程之后,我决定使用最新的solc编译器0.8.6创建一个彩票合约,因为原始课程合约是使用solc编译器0.4.17创建的

Lottery.sol

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.6;
contract Lottery {
address public manager;
address[] public players;
constructor() {
manager=msg.sender;
}
function enter() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() public view returns(uint) {
return uint(keccak256 (abi.encodePacked(block.difficulty, block.timestamp, players)));
}
function pickWinner() public restricted {
uint index = random () % players.length;
payable(players[index]).transfer(address(this).balance);
players = new address[](0);
}
modifier restricted() {
require(msg.sender == manager);
_;
}
function getPlayers() public view returns(address[] memory) {
return players;
}

}

Compile.js文件

const path = require('path');
const fs = require('fs');
const solc = require('solc');
const lotteryPath = path.resolve(__dirname, 'contracts', 'lottery.sol');
const source = fs.readFileSync(lotteryPath, 'UTF-8');
var input = {
language: 'Solidity',
sources: {
'lottery.sol' : {
content: source
}
},
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
};
var output = JSON.parse(solc.compile(JSON.stringify(input)));
exports.abi = output.contracts['lottery.sol']['Lottery'].abi;
exports.bytecode = output.contracts['lottery.sol']['Lottery'].evm.bytecode.object;

Lottery.test.js (Using Mocha, Ganache-Cli, web3)

我试着先运行基本命令,这样我就可以测试它,然后再用我的测试条件测试它。

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const {abi, bytecode} = require('./compile');
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log('Attempting to deploy from account',accounts[0]);
const result = await new web3.eth.Contract(abi)
.deploy({data: '0x' + bytecode})
.send({from: accounts[0]});
console.log('contract deployed to', result);
}
deploy();

当我运行npm run test时,它给了我这个错误.

$ npm run test
> lottery@1.0.0 test C:cygwin64homeKKLlottery
> mocha

Error: Cannot find module './compile'
Require stack:
- C:cygwin64homeKKLlotterytestlottery.test.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:92:18)
at Object.<anonymous> (C:cygwin64homeKKLlotterytestlottery.test.js:5:25)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:14)
at ModuleWrap.<anonymous> (internal/modules/esm/translators.js:199:29)
at ModuleJob.run (internal/modules/esm/module_job.js:169:25)
at Loader.import (internal/modules/esm/loader.js:177:24)
at formattedImport (C:cygwin64homeKKLlotterynode_modulesmochalibesm-utils.js:7:14)
at Object.exports.requireOrImport (C:cygwin64homeKKLlotterynode_modulesmochalibesm-utils.js:48:32)
at Object.exports.loadFilesAsync (C:cygwin64homeKKLlotterynode_modulesmochalibesm-utils.js:88:20)
at singleRun (C:cygwin64homeKKLlotterynode_modulesmochalibclirun-helpers.js:125:3)
at Object.exports.handler (C:cygwin64homeKKLlotterynode_modulesmochalibclirun.js:366:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! lottery@1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the lottery@1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR!     C:UsersKKLAppDataRoamingnpm-cache_logs2021-08-06T09_39_41_164Z-debug.log

这是我的第一份合同,如果有人能帮我解释一下问题,我将非常感激。谢谢你:)

您在这行中导入compile.jsconst {abi, bytecode} = require('./compile');

看起来你没有提供compile.js的正确路径。检查compile.js文件在项目目录中的位置。获取文件的正确路径,然后粘贴。

基于Compile.js中的这行代码:

const lotteryPath = path.resolve(__dirname, 'contracts', 'lottery.sol');

这是文件结构

rootDir/contracts/Lottery.sol

我认为compile.js在根目录下。"。/compile.js"表示与测试文件在同一目录下:

var { abi, evm } = require("../compile.js");

最新更新