Mocha 测试验证 solidity 合约余额
Mocha test verify solidity contract balance
我有这样的 solidity 合同:
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 > .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;
}
function getBalance() public view returns (uint) {
return this.balance;
}
}
我正在尝试测试在选择获胜者后 balance/pot 是否恢复为零。
这是我的测试文件中的 it
语句:
it('sends money to the winner and resets the players array', async() => {
await 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] });
const finalBalance = await web3.eth.getBalance(accounts[0]);
const difference = finalBalance - initialBalance;
const players = await lottery.methods.getPlayers().call({
from: accounts[0]
});
const pot = await lottery.methods.getBalance();
assert(difference > web3.utils.toWei('1.8', 'ether'));
assert.equal(0, players.length);
assert.equal(0, pot);
});
当我将智能合约部署到 remix - ethereum IDE getBalance returns 想要的余额但是当我 运行 npm 运行 在本地测试然后我得到一个断言错误:
AssertionError [ERR_ASSERTION]: 0 == {
_ethAccounts: <ref *7> Accounts {
_ethereumCall: {
getChainId: [Function: send] {
call: 'eth_chainId',
method: Method {
abiCoder: undefined,
accounts: undefined,
call: 'eth_chainId',
defaultAccount: null,
defaultBlock: 'latest',
defaultChain: undefined,
defaultCommon: undefined,
defaultHardfork: undefined,
extraFormatters: undefined,
handleRevert: undefined,
inputFor...
at Context.<anonymous> (test/Lottery.test.js:101:12)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我该如何解决这个问题?
p.s。这个合约是为了学习目的我知道随机性不是很稳健
当您使用web3js
调用合约只读方法时,您需要使用.call()
函数。
const pot = await lottery.methods.getBalance().call()`; // added `.call()`
而不是
const pot = await lottery.methods.getBalance(); // original code
文档:https://web3js.readthedocs.io/en/v1.4.0/web3-eth-contract.html#methods-mymethod-call
如果您的目的只是为了测试余额,请使用 web3.eth.getBalance
而不是在合约中定义 getBalance
。
const contractBalance = await web3.eth.getBalance(lottery.options.address);
我有这样的 solidity 合同:
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 > .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;
}
function getBalance() public view returns (uint) {
return this.balance;
}
}
我正在尝试测试在选择获胜者后 balance/pot 是否恢复为零。
这是我的测试文件中的 it
语句:
it('sends money to the winner and resets the players array', async() => {
await 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] });
const finalBalance = await web3.eth.getBalance(accounts[0]);
const difference = finalBalance - initialBalance;
const players = await lottery.methods.getPlayers().call({
from: accounts[0]
});
const pot = await lottery.methods.getBalance();
assert(difference > web3.utils.toWei('1.8', 'ether'));
assert.equal(0, players.length);
assert.equal(0, pot);
});
当我将智能合约部署到 remix - ethereum IDE getBalance returns 想要的余额但是当我 运行 npm 运行 在本地测试然后我得到一个断言错误:
AssertionError [ERR_ASSERTION]: 0 == {
_ethAccounts: <ref *7> Accounts {
_ethereumCall: {
getChainId: [Function: send] {
call: 'eth_chainId',
method: Method {
abiCoder: undefined,
accounts: undefined,
call: 'eth_chainId',
defaultAccount: null,
defaultBlock: 'latest',
defaultChain: undefined,
defaultCommon: undefined,
defaultHardfork: undefined,
extraFormatters: undefined,
handleRevert: undefined,
inputFor...
at Context.<anonymous> (test/Lottery.test.js:101:12)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我该如何解决这个问题? p.s。这个合约是为了学习目的我知道随机性不是很稳健
当您使用web3js
调用合约只读方法时,您需要使用.call()
函数。
const pot = await lottery.methods.getBalance().call()`; // added `.call()`
而不是
const pot = await lottery.methods.getBalance(); // original code
文档:https://web3js.readthedocs.io/en/v1.4.0/web3-eth-contract.html#methods-mymethod-call
如果您的目的只是为了测试余额,请使用 web3.eth.getBalance
而不是在合约中定义 getBalance
。
const contractBalance = await web3.eth.getBalance(lottery.options.address);