2

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

2 Answers 2

3

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]});
6
  • 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
    Commented Jul 27, 2020 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. Commented Jul 27, 2020 at 9:58
  • Sorry, my fault, I didn't noticed: value should be in hex format, you are passing as decimal. Commented Jul 27, 2020 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
    Commented Jul 27, 2020 at 10:12
  • Cool. I fixed the answer to match the working code. Commented Jul 27, 2020 at 10:16
0

The accepted answer is actually not the best way to handle it. You don't need to go back and forth converting from integer and then to string to get the value. Web3 has everything you need to get the right amount:


const wei = web3.utils.toWei('3', 'ether')
await window.ethereum
    .request({
        method: 'eth_sendTransaction',
        params: [
            {
                from: '0x5c0bC92f7d26F7AD821408e2B1774FC96858C691',
                to: '0x6cC5550509CC3a66Df97Efa42B866A058e12ADE2',
                value: web3.utils.toHex(wei),
            },
        ],
    })

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.