sendTransaction的值格式有问题



我在MetaMask中有一个关于值格式的交易问题,我把代码放在下面:

<script type="text/javascript">
const sendEthButton = document.querySelector('.sendEthButton');
sendEthButton.addEventListener('click', () => {
ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: ethereum.selectedAddress,
to: '{{contractCreator}}',
value: '0x29a2241af62c0000',
},
],
})
.then((txHash) => console.log(txHash))
.catch((error) => console.error);
});
</script>

我已经将MetaMask正确地连接到网站和Ganache,当我按下页面中的按钮时,交易也出现在插件中,但只有在代码中使用此值,我看到了正确的一个(3),但我不知道格式如何。

我试过web3。toWei(number, 'ether')和web3.toHex(number),但是它显示的值很奇怪。

例如,如果我将这个值替换为web3。在MetaMask窗口中显示4722.366483

对我有用的是

const amount = web3.utils.toWei('1', 'ether');
const value = web3.utils.toHex(amount);

当将amount作为字符串发送时,您将在Metamask中看到4722.366483 ETH。

发送value,您将看到1 ETH。

最新更新