当我执行npm运行测试时,我得到了这个错误:
无法对属性interface
进行结构分析,该属性未定义或为null
我已经尝试修复这个错误好几天了,但是没有结果。我在其他一些帖子中看到,它通常与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);
});
});
发布于 2021-02-11 11:36:48
下面是我修复它的方法:
在运行'npm run test‘时,我遇到了类似的错误。对我来说起作用的似乎是卸载您当前版本的solc并运行npm install --save solc@0.4.25
发布于 2020-05-19 20:16:09
在您的合同中做以下修改,测试就会运行得很好。
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不支持这一点。
发布于 2019-04-21 05:41:20
我也有同样的问题。问题出在你正在使用的固定性编译器上。在我的特殊情况下,我尝试了0.5.4,但由于某种原因,我遇到了同样的错误。
“solc 0.5.0及更高版本的合同编译不同”
一种可能的解决方案是使用较低的solc版本:如0.4.25 (在我的情况下,这很好用)。
当编译发生时,问题会激增,操作返回null,所以没有任何输出,这就是为什么您会得到一个错误,告诉您有关null变量的信息。
https://stackoverflow.com/questions/55572357
复制相似问题