我在测试合同时遇到此错误。除最后一项外,所有测试均通过。我不明白这个错误,请帮忙。 智能合约是使用 truffle 框架创建的。
我的彩票合同 sol 文件:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.5.2;
contract Lottery{
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,block.timestamp,players)));
}
function getBalance() public view returns(uint){
return address(this).balance;
}
function pickWinner() public restricted {
address payable winner;
uint index=random()% players.length;
winner=payable(players[index]);
winner.transfer(getBalance());
players=new address[](0);
}
modifier restricted(){
require(msg.sender==manager);
_;
}
function getPlayers() public view returns(address[] memory){
return players;
}}
抽奖测试文件
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.provider());
const Lottery = artifacts.require('Lottery');
contract('Lottery', (accounts) => {
let lottery;
beforeEach(async () => {
lottery = await Lottery.new({ from: accounts[0] });
});
describe('Lottery Contract', () => {
it('deploys a contract', () => {
assert.ok(lottery.address);
});
it('allows one acc to enter', async () => {
await lottery.enter({
from: accounts[0],
value: web3.utils.toWei('0.02', 'ether'),
});
const players = await lottery.getPlayers({ from: accounts[0] });
assert.equal(accounts[0], players[0]);
assert.equal(1, players.length);
});
it('allows multiple acc to enter', async () => {
await lottery.enter({
from: accounts[0],
value: web3.utils.toWei('0.02', 'ether'),
});
await lottery.enter({
from: accounts[1],
value: web3.utils.toWei('0.02', 'ether'),
});
await lottery.enter({
from: accounts[2],
value: web3.utils.toWei('0.02', 'ether'),
});
const players = await lottery.getPlayers({ from: accounts[0] });
assert.equal(accounts[0], players[0]);
assert.equal(accounts[1], players[1]);
assert.equal(accounts[2], players[2]);
assert.equal(3, players.length);
});
it('requires a min amount of ether to enter', async () => {
try {
await lottery.enter({
from: accounts[0],
value: 0,
});
assert(false);
} catch (err) {
assert(err);
}
});
it('only manager can call pickWinner', async () => {
try {
await lottery.pickWinner({
from: accounts[1],
});
assert(false);
} catch (err) {
assert(err);
}
});
it('sends money to the winner and resets the players', async () => {
await lottery.enter({
from: accounts[0],
value: web3.utils.toWei('2', 'ether'),
});
const initialBal = await web3.eth.getBalance(accounts[0]);
await lottery.pickWinner({
from: accounts[0],
});
const finalBal = await web3.eth.getBalance(accounts[0]);
const difference = finalBal - initialBal;
console.log(difference);
assert(difference > web3.utils.toWei('1.8', 'ether'));
});
});
});
您的 truffle.config.js 与 ganache 没有连接。
将其添加到您的 truffle-config.js
networks: {
development: {
host: "127.0.0.1",
// default ganache gui listens to 7545
port: 7545,
network_id: "*",
},
确保您的甘纳许正在运行并运行测试。
我也是初学者,但我想你在接收余额时应该使用 web3.utils.BN。更多信息请参见:Web3.js 文档中的 BN。
可能是关于字符串的情况,但是当我尝试并得到相同的错误时,这是因为我的“差异”是一个不大于 'web3.utils.toWei("1.8", "以太”)',并且由于“差异”应该更大,断言抛出了错误。