0

I'm new to ethereum and when using web3.utils.toWei("1", "ether") it doesn't work properly.

const transactionParameters = {
 from: metamaskaddr,
 gasPrice: '0x09184e72a000', // customizable by user during MetaMask confirmation.
 gas: '0x2710', // customizable by user during MetaMask confirmation.
 to: '0x1e518d4dDd1f75aa884906980E59e3F40745351c', // Required except during contract publications.
 value: web3.utils.toWei("1", "ether"), // Only required to send ether to the recipient from the initiating external account.
 data:
 web3.utils.toHex(algoaddr), // Optional, but used for defining smart contract creation and interaction.
 };

 // txHash is a hex string
 // As with any RPC call, it may throw an error
 const txHash = await ethereum.request({
 method: 'eth_sendTransaction',
 params: [transactionParameters],
 });

I'm trying to create a metamask notification to send eth. When the notification appears, it isn't 1 eth, it is 4722.36648287 eth??? I am using client-side javascript.

5
  • Can you show an screenshot of the operation? Which network are you connected to?
    – Ismael
    Jan 31, 2022 at 6:33
  • ibb.co/WtTfJ14 i'm using matic network and it does the same on eth network
    – NCoin
    Jan 31, 2022 at 17:08
  • I mean a screenshot of metamask showing the value.
    – Ismael
    Feb 1, 2022 at 17:36
  • 1
    i have figured it out anyways
    – NCoin
    Feb 1, 2022 at 18:19
  • That's fine. It is an strange problem, maybe it is that ethereum.request doesn't understand web3.utils.toWei return value (a BN object).
    – Ismael
    Feb 2, 2022 at 1:45

2 Answers 2

0

I ran into the same problem because I was using an older Alchemy version of web3 which interprets the amount given to value as a hex number.

1000000000000000000 hex = 4722366482869645213696 dec.
Also see this issue: https://github.com/alchemyplatform/alchemy-web3/issues/61.

To solve it you can wrap it in a web3.utils.toHex:
value: web3.utils.toHex(web3.utils.toWei("1", "ether" ))

0

I figured out I didn't need to do "toWei" and just provide Amount*1000000000000000000 as hex

0

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.