我对 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>
Run Code Online (Sandbox Code Playgroud)
我已将 MetaMask 正确连接到网站和 Ganache,当我按下页面中的按钮时,交易也会出现在插件中,但只有在代码中使用此值时,我才能看到正确的值 (3),但我不知道如何是格式化。
我尝试过 web3.toWei(number, 'ether') 和 web3.toHex(number) 但它显示了奇怪的值。
例如,如果我用 web3.toWei(1, ether) 替换该值,它会在 MetaMask 窗口中显示 4722.366483。