0

I have been trying to send a transaction using an RPC call with MetaMask's window.ethereum.request injected method.

I have successfully sent a transaction before, by using the web3js method web3.eth.sendTransaction with MetaMask displaying the intended amount of ether. For the conversion from ether to wei I'm using web3.utils.toWei("3", "ether).

However, when I try to send a transaction using an RPC call, the MetaMask pop-up displays an incorrect amount of ether to be sent. The accounts used below are provided by Ganache. The sender address belongs to the account I have successfully connected to my app using MetaMask.

I am using Ganache as a local network and Firefox on Ubuntu. This is the code used to make the call:

 const myParameters = {
    from: "0x5c0bC92f7d26F7AD821408e2B1774FC96858C691",
    to: "0x6cC5550509CC3a66Df97Efa42B866A058e12ADE2",
    value: web3.utils.toWei("3", "ether")
    };

let results = await window.ethereum.request({method:"eth_sendTransaction", params: [myParameters]});

I have also tried converting the value to string, but I get the same wrong amount. Here's the pop-up I get for the above code: MetaMask pop-up

1

Value should be in hex format.

Note: in some updated MetaMask installation, it seems the injected web3 has not the utils package anymore, so you may need to use web3.toWei instead.

 const myParameters = {
    from: "0x5c0bC92f7d26F7AD821408e2B1774FC96858C691",
    to: "0x6cC5550509CC3a66Df97Efa42B866A058e12ADE2",
    value: parseInt(web3.utils.toWei("3","ether")).toString(16)
    };

let results = await window.ethereum.request({method:"eth_sendTransaction", params: [myParameters]});
| improve this answer | |
  • I tried changing it but now I get an error that web3.toWei is not a function. If it helps, I am initializing web3 like this: let web3 = new Web3(window.ethereum);. To phrase it better, the web3 I am using is not the injected by MetaMask object. It's the instance of the web3js library. – FrozoWeb Jul 27 at 9:54
  • I'm using Chrome+MetaMask on Ubuntu and it works fine. Give it a try: if it works I think it's can be a bug related to the MetaMask extension for Firefox not to be fully updated yet to use window.ethereum.request function. – Giuseppe Bertone Jul 27 at 9:58
  • Sorry, my fault, I didn't noticed: value should be in hex format, you are passing as decimal. – Giuseppe Bertone Jul 27 at 10:06
  • 1
    Actually just switched here to answer my post because I figured out it should be hex. For those having the same problem, the value property should be hex, so if you want to use the web3.utils method it should be value: parseInt(web3.utils.toWei("3", "ether")).toString(16) . The web3 method returns a string so you have to convert to integer and then convert that integer to hex string by passing 16 as an argument to the toString method. Thanks for your time! – FrozoWeb Jul 27 at 10:12
  • Cool. I fixed the answer to match the working code. – Giuseppe Bertone Jul 27 at 10:16

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.