[Ethereum] MetaMask displaying wrong value when making RPC sendTransaction call

metamaskrpcweb3js

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

Best Answer

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]});