1

I am trying to call a function of a contract that has been deployed to Ganache. I am trying to call a function that makes payouts to a few addresses and can be called by owner only. When I am calling the function, I get this error:

 The send transactions "from" field must be defined!

I don't understand the problem. All this contract does is that it transfers the contract balance to a few addresses. It is not payable. So, how do I resolve this error?

Contract Function:

function distributePrizes() public _ownerOnly{
    uint numberWinner = generateNumber();
    address[100] memory winners; // We have to create a temporary in memory array with fixed size
      uint256 count = 0; // This is the count for the array of winners
      for(uint256 i = 0; i < players.length; i++){
         address playerAddress = players[i];
         if(playerInfo[playerAddress].numberSelected == numberWinner){
            winners[count] = playerAddress;
            count++;
         }
         delete playerInfo[playerAddress]; // Delete all the players
      }
      winnerArray = winners;
      players.length = 0; // Delete all the players array
      uint256 winnerEtherAmount = totalBet / winners.length; // How much each winner gets
      for(uint256 j = 0; j < count; j++){
         if(winners[j] != address(0)) // Check that the address in this fixed array is not empty
         winners[j].transfer(winnerEtherAmount);
      }

    }

React function where the contract function is being called:

selectWinner = async (e) =>{
    const { accounts, contract, web3 } = this.state;
    const balance = await web3.eth.getBalance(contract.address);
    //console.log('Winners selected')
    await contract.distributePrizes();
    const newBalance = await web3.eth.getBalance(contract.address);
    const winners = await contract.winnerArray;
    this.setState({winnerAddress: winners, totalBet: newBalance})
  }
  • How about sharing some code here? – goodvibration Nov 21 '18 at 18:18
  • I have shared some code – Rohan Dhar Nov 21 '18 at 18:20
3

If you are using accounts[0] as your default account, modify this line:

await contract.distributePrizes();

to:

await contract.distributePrizes({from: accounts[0]});

Or when you are creating your web3 object:

web3.eth.defaultAccount = web3.eth.accounts[0]

Another option: if you have geth or truffle installed, open a console, for example doing:

geth attach http://127.0.0.1:8545 or truffle console

and then:

eth.defaultAccount = eth.accounts[0] (for geth) or web3.eth.defaultAccount = web3.eth.accounts[0] (for truffle)

If you want accounts[0] to be your default account, of course.

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.