0

I am trying to deploy this contract on the test network and am able to do so successfully but when i try to call the play function i get gas estimation error and can do nothing further. I am using remix connected to Ropsten. To works fine in javascript vm. Can anyone tell me whats wrong? ps: i didnt forget to send ether when i called the play function

pragma solidity ^0.5.8;

contract test1 {

uint256 count =0;
address payable owner;
address[] public players;

 constructor () public { //constructor
    owner = msg.sender; 

 }


function play() payable public { 
            require (msg.value >= 100000000000000000);
            players[count]=msg.sender;
    count++; 
 }
}
0

This code shouldn't work in the JavaScript VM (or anywhere else).

players is a dynamically-sized array, initially with size 0. So attempting to write to players[0] will fail bounds checking.

You may be used to a language that allows such writes, but in Solidity you have to either explicitly increase the size of the array first (players.length += 1) or just use players.push(msg.sender) to extend the array and put a value in the newly available spot in the array. You can then get rid of the count variable, which is redundant with players.length.

  • i will try that out, appreciated – Sundeep Kumar May 4 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.