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;
         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


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? Commented Nov 21, 2018 at 18:18
  • I have shared some code
    – Rohan Dhar
    Commented Nov 21, 2018 at 18:20

1 Answer 1


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

await contract.distributePrizes();


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

Or when you are creating your web3 object:

let accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0]

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

geth attach or truffle console

and then:

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

let accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0]

for truffle

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

Note: web3.eth.accounts is deprecated. Use web3.eth.getAccounts() instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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