Estoy tratando de llamar a una función de un contrato que se implementó en Ganache. Estoy tratando de llamar a una función que realiza pagos a algunas direcciones y solo puede ser llamada por el propietario. Cuando estoy llamando a la función, me sale este error:
The send transactions "from" field must be defined!
no entiendo el problema Todo lo que hace este contrato es que transfiere el saldo del contrato a algunas direcciones. No es pagable. Entonces, ¿cómo resuelvo este error?
Función del contrato:
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);
}
}
Función de reacción donde se llama a la función de contrato:
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})
}
Si está utilizando accounts[0]
como su cuenta predeterminada, modifique esta línea:
await contract.distributePrizes();
a:
await contract.distributePrizes({from: accounts[0]});
O cuando estás creando tu web3
objeto:
let accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0]
Otra opción: si tienes geth
o truffle
instalado, abre una consola, por ejemplo haciendo:
geth attach http://127.0.0.1:8545
otruffle console
y luego:
eth.defaultAccount = eth.accounts[0]
(para geth
) o
let accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0]
paratruffle
Si quieres accounts[0]
que sea tu cuenta por defecto, por supuesto.
Nota: web3.eth.accounts
está en desuso. Usar web3.eth.getAccounts()
en su lugar
buena vibración
Rohan Dar