以太坊测试显示余额为负差异,为什么下面代码逻辑的 finalBalance 会小于 initialBalance?

问题描述

我一直在研究智能合约并使用 mocha 测试框架编写测试用例。对于下面的彩票合约,我在 lottery.test.js 中编写了一些测试。 几乎所有的测试都成功了,但不是最后一个。在 it 块中,描述为 only manager can pick winner。它应该向彩票合约发送 2 个以太币,并且在花费一些 Wei 来挑选中奖者(认情况下将是它本身)之后,它应该将 Wei 的数量转回(小于 2,因为它在挑选获胜者时花费了一些 gas优胜者)。但输出显示负差异,如图所示。

谁能帮我理解为什么finalBalance 会小于以下代码逻辑的initialBalance

在 Lottery.sol

pragma solidity^0.4.17;

contract Lottery{
    address public manager;
    address[] public players;

    function Lottery() 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(block.difficulty,Now,players));
    }

    function pickWinner() public restricted{
        uint index = random() % players.length;
        players[index].transfer(this.balance);
        players = new address[](0);
    }

    modifier restricted(){
        require(msg.sender == manager);
        _;
    }

    function getPlayers() public view returns (address[]){
        return players;
    }
}

在 Lottery.test.js

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');
const web3 = new Web3(ganache.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);
  }).timeout(20000);

  it('allows one contract to enter',async () =>{
    await lottery.methods.enter().send({
      from: accounts[0],value: web3.utils.toWei('0.02','ether')
    });

    const players = await lottery.methods.getPlayers().call({
      from: accounts[0]
    });

    assert.equal(accounts[0],players[0]);
    assert.equal(1,players.length);
  });

  it('allows multiple contracts to enter','ether')
    });

    await lottery.methods.enter().send({
      from: accounts[1],'ether')
    });

    await lottery.methods.enter().send({
      from: accounts[2],players[0]);
    assert.equal(accounts[1],players[1]);
    assert.equal(accounts[2],players[2]);
    assert.equal(3,players.length);
  }).timeout(10000);

  it('requires a minimum amount of ether to enter',async () =>{
    try{
      await lottery.methods.enter().send({
        from: accounts[3],value: 10
      });
    } catch(err) {
      assert(err);
    }
  });

  it('only manager can pick winner',async () =>{
    try{
      await lottery.methods.pickWinner().send({
        from: accounts[3]
      });
    } catch(err) {
      assert(err);
    }
  });

  it('sends money to lottery and resets the player array',async () => {
    lottery.methods.enter().send({
      from: accounts[0],value: web3.utils.toWei('2','ether')
    });

    const initialBalance = await web3.eth.getBalance(accounts[0]);

    await lottery.methods.pickWinner().send({ from: accounts[0]});      // for this Transaction we send some amt of gas

    const finalBalance = await web3.eth.getBalance(accounts[0]);

    const difference = finalBalance - initialBalance;
    console.log(finalBalance,initialBalance,difference);
    assert( difference > web3.utils.toWei('1.8','ether'));
  });

});

npm 运行测试后的输出

console output here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)