当我执行npm run test时,我得到此错误:
无法解构'undefined'或'null'的属性interface
我一直在尝试修复此错误达数天之久,但没有结果。我在其他一些帖子中看到它通常与compile.js文件有关...我认为一切正常,所以我找不到问题。
我将粘贴所有代码(注意,我正在尝试使用最新版本的solidity,以便学习新功能...也许有我的错误...)
谢谢!
我的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, 'utf8');
module.exports = solc.compile(source, 1).contracts[':Lottery'];
.sol:
pragma solidity ^0.5.1;
contract Lottery {
address public manager;
address payable [] public players;
constructor() public payable {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > .01 ether);
players.push(msg.sender);
}
function random() private view returns (uint){
return uint(keccak256(abi.encode(block.difficulty, now, players)));
}
function getPlayers() public view returns (address payable[] memory){
return players;
}
function pickWinner() public payable restricted{
uint index = random() % players.length;
address(players[index]).transfer(address(this).balance);
players = new address payable[](0);
}
modifier restricted(){
require(msg.sender==manager);
_;
}
}
测试:
const assert = require ('assert');
const ganache = require ('ganache-cli');
const Web3 = require ('web3');
const provider = ganache.provider();
const web3 = new Web3(provider);
const { interface, bytecode } = require ('../compile');
let lottery;
let accounts;
beforeEach(async () => {
accounts = await web3.eth.getAccounts();
lottery = await new web3.eth.Contract(JSON.parse(interface))
.deploy({ data: bytecode })
.send({ from: accounts[0], gas: '1000000' });
});
describe('Lottery Contract', () =>{
it('deploys a contract', () =>{
assert.ok(lottery.options.address);
});
});
答案 0 :(得分:3)
我是如何修复它的:
我在运行“npm run test”时遇到了类似的错误。似乎对我有用的是卸载您当前版本的 solc
并运行 npm install --save solc@0.4.25
答案 1 :(得分:1)
在您的合同中进行以下修改,测试就可以正常进行。
pragma solidity ^0.4.17; //make change here
contract Lottery{
address public manager;
address[] public players;
function Lottery()public{
manager = msg.sender;
} // use this instead of using constructor
function enter() public payable{
require(msg.value>.1 ether);
players.push(msg.sender);
}
function random() private view returns(uint){
return uint(keccak256(block.difficulty, now, players));
} // make change here
function pickWinner()public{
require(msg.sender==manager);
uint index = random() % players.length;
players[index].transfer(address(this).balance);
players = new address[](0);
}
function getPlayers()public view returns(address[]){
return players;
}
}
您可能已经对该合同进行了修改,以使其可以在混音中进行编译,但V@0.4.17不支持此协议。
答案 2 :(得分:0)
我遇到了完全相同的问题。问题与您使用的Solidity编译器有关。在我的特定情况下,我尝试使用0.5.4,由于某种原因,我遇到了相同的错误。
“合约编译在solc 0.5.0及更高版本中有所不同”
一个可能的解决方案是使用较低的solc版本:例如0.4.25(在我的情况下,这很好用)。
编译时问题激增,操作返回null,因此没有任何输出,这就是为什么您会收到一条错误消息,告诉您有关null变量的原因。
答案 3 :(得分:0)
当我尝试运行测试时,会出现相同的问题 您可以尝试两件事:
1:尝试较低版本的Solidity。也许V0.4.17可以帮助您
npm uninstall solc //to uninstall existing solc version
npm i --save solc@0.4.17 //to re-install solc with lower version
2:尝试在compile.js文件中使用console.log()
语句检查合同是否在编译中
console.log(solc.compile(source, 1));
如果输出为undefined
,则可能是您的合同有问题。
答案 4 :(得分:0)
实用性>> 0.4.21 <0.7.0;
合同彩票{
address public manager;
address[] public players;
constructor() public {
manager = msg.sender;
}
function enter() public payable {
require(msg.value > 0.01 ether);
players.push(msg.sender);
}
function random() private view returns (uint) {
return uint(keccak256(abi.encodePacked(block.difficulty,now,players)));
}
function pickWinner() public restricted {
uint index = random() % players.length;
address(players[index]).transfer(address(this).balance);
players = new address[](0);
}
modifier restricted() {
require(msg.sender == manager);
_;
}
function getPlayers()public view returns(address[]){
return players;
}
}