I have a problem like this. I am new to solidity development. I have created a contract like this.
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.1 ether);
players.push(msg.sender);
}
function random() public view returns (uint) {
return uint(keccak256(block.difficulty,now,players));//hashing
}
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;
}
}
This is my Test js file is like this.
const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');//we are importing a constructor function so capittalize
//Instance
const web3 = new Web3(ganache.provider());
const { interface, bytecode } = require('../compile');
let accounts;
let lottery;
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', ()=>{
it('deploys a contract',()=>{
assert.ok(lottery.options.address);//check is there a value or not
});
it('allows account 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);
})
});
When I hit npm run test it gives me an error like this.
√ deploys a contract
1) allows account to enter
1 passing (318ms)
1 failing
1) Inbox
allows account to enter:
i: VM Exception while processing transaction: revert
at Function.i.fromResults (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\lib\utils\runtimeerror.js:91:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\lib\blockchain_double.js:539:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\internal\once.js:12:1
at p (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\internal\eachOfLimit.js:61:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\internal\eachOfLimit.js:71:1
at a.default (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\eachLimit.js:43:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\internal\doLimit.js:9:1
at v.r.emit (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async-eventemitter\lib\AsyncEventEmitter.js:42:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\runBlock.js:52:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\runBlock.js:174:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\cache.js:110:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:473:1
at d (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:1006:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:1016:1
at Da (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:3196:1)
at Object.<anonymous> (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:1046:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\cache.js:106:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:969:1
at n (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:5225:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\cache.js:103:1
at n (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:5223:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\cache.js:103:1
at n (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\async\dist\async.js:5223:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\ethereumjs-vm\dist\cache.js:98:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\merkle-patricia-tree\util.js:51:1
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\merkle-patricia-tree\node_modules\async\lib\async.js:52:1
at f (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\merkle-patricia-tree\node_modules\async\lib\async.js:246:1)
at C:\Users\tharindusa\Desktop\slv\node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\merkle-patricia-tree\node_modules\async\lib\async.js:44:1
at Immediate._onImmediate (node_modules\ganache-cli\build\webpack:\ganache\node_modules\ganache-core\node_modules\levelup\lib\levelup.js:286:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\tharindusa\AppData\Roaming\npm-cache\_logs\2018-09-04T05_52_38_807Z-debug.log
I search for a solution but I was unable to find a suitable solution. So can someone help me to solve this problem? Thankyou!!