I want to create an RPC call (study purpose) from my app using metamask.
My method on my contract is:
function sayHelloMyName(string memory _name) external pure returns (string memory) {
require(bytes(_name).length != 0, "insert your name");
return string(abi.encodePacked("hello ", _name));
}
its hash is:
$ web3.utils.keccak256("sayHelloMyName(string)").substr(0, 10)
> '0x10a7b27a'
I want to pass my name foo where the hex decimal is 0x666f6f
web3.utils.toHex('foo')
'0x666f6f'
So my call is:
ethereum
.request({
method: 'eth_call',
params: [
{
from: currentAccount,
to: contractAddress,
data: 0x10a7b27a0000000000000000000000000000000000000000000000000000000000666f6f
}, "latest"],
})
.then((txHash) => {
console.log(txHash);
$('#info').html(txHash);
})
.catch((error) => {
console.error;
$('#info').text(JSON.stringify(error.message));
});
where the data is the method signature, and my hex name and padding (total 32 bytes) Unfortunately, I get a revert of it.
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": -32000,
"message": "execution reverted"
}
}
the code works, I can use it with web3 library.