I'm having some problem with the arguments of a function written in solidity and called in JavaScript. I'm using the Remix ballot default example, except I removed the first require in it. It looks like this:
function giveRightToVote(address voter) public {
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;}
As far as I'm understanding, I should give it an address so it can give permission to that address. This is the JavaScript code that calls that function:
const permission = async () => {
const ethereumAddress = web3.utils.toChecksumAddress(account);
const gasEstimate = await window.contract.methods.giveRightToVote([ethereumAddress]).estimateGas({ from: account });
console.log("1");
const result = await window.contract.methods.giveRightToVote(account).send({ gas: gasEstimate, from: account});
document.getElementById("permissionArea").innerHTML = "Permiso para votar obtenido";
/**
await window.contract.methods.giveRightToVote(account).send({ from: account });
document.getElementById("permissionArea").innerHTML = "Permiso para votar obtenido";
*/
}
The last commented part was the original code. I have changed it a lot trying to make it work. I kind of guess the problem is the argument account. I have tried using [account] which has worked in another function, but I seem to not be able to figure out how it wants the parameters. I know the account is right, I get it like this:
const accounts = await ethereum.request({method: "eth_requestAccounts"});
account = accounts[0];
And when I print it it's the right address. The full error reads:
Error: types/value length mismatch (argument="tuple", value=["0x40bCba26F1ED151Fa34232E3CFf55d9C2867bCf1"], code=INVALID_ARGUMENT, version=abi/5.7.0)
This is not the only function that shows this same problem, in fact, I can't send any argument to the solidity functions. I have this vote function that only requires a integer (uint) as an argument. If I just send 0 for example it gives me the error I just addressed. But if I make a javascript Uint8Array and send the complete array like this:
let numero = new Uint8Array(1);
numero[0]=selectedCandidate;
const gasEstimate = await window.contract.methods.vote(numero).estimateGas({ from: account });
await window.contract.methods.vote(numero).send({ gas: gasEstimate, from: account });
It shows a different error:
MetaMask - RPC Error: Internal JSON-RPC error. {code: -32603, message: 'Internal JSON-RPC error.', data: {…}} inpage.js:1
And
Uncaught (in promise) Error: Internal JSON-RPC error. web3.min.js:2
{
"message": "VM Exception while processing transaction: revert",
"stack": "RuntimeError: VM Exception while processing transaction: revert\n at exactimate (C:\\Users\\pc\\AppData\\Local\\Programs\\Ganache\\resources\\static\\node\\node_modules\\ganache\\dist\\node\\1.js:2:182136)",
"code": -32000,
"name": "RuntimeError",
"data": {
"hash": null,
"programCounter": 130,
"result": "0x",
"reason": null,
"message": "revert"
}
}
I'm still confused that it wants a tuple when the smart contract only takes one argument; any idea what problem I just run into?