我使用Alchemy作为我的web3提供程序,并获得了如下所示的事务数据:
{
blockHash: null,
blockNumber: null,
from: '0xa12e1462d0ced572f396f58b6e2d03894cd7c8a4',
gas: '0x440f0',
gasPrice: '0x3b9aca000',
hash: '0x4e4d44a84404dbaff0d96357729d732a204a4b4e924bb1cccd1173018e33e229',
input: '0x7ff36ab500000000000000000000000000000000000000000000000001c95a8b7d8bf0e2000000000000000000000000000000000000000000000000000000000000008000000000000000000000000048c04ed5691981c42154c6167398f95e8f38a7ff000000000000000000000000000000000000000000000000000000006391cc960000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000007db5af2b9624e1b3b4bb69d6debd9ad1016a58ac',
nonce: '0x1dc3',
to: '0x7a250d5630b4cf539739df2c5dacb4c659f2488d',
transactionIndex: null,
value: '0x20db9bccc4e8cb1',
type: '0x0',
chainId: '0x1',
v: '0x25',
r: '0x461d8ff7fa9920577a428a6433e4a6f7a6dbfb6678952ee26638e8e2ad3fa6a3',
s: '0x35bdf93cffe336c178a81432f9adc4f40ff1eeb665673cc027f494f726ae1308'
}我想知道如何将gas和value从散列(例如0x3b9aca000)转换为Gwei或ether。
与以太扫描一样,此事务的详细信息是人类可读的格式。我想知道如何在API https://etherscan.io/tx/0x4e4d44a84404dbaff0d96357729d732a204a4b4e924bb1cccd1173018e33e229中实现它。

发布于 2022-12-08 12:06:20
这并不难。尝尝这个。
要将gas和值字段从哈希转换为Gwei或Ether,可以使用Web3库提供的web3.utils.fromWei()方法。此方法以魏(以太最小单位)为单位,并将其转换为指定的单位。例如,要将您提供的事务数据中的gasPrice值转换为Gwei,可以使用以下代码:
const gasPriceInGwei = web3.utils.fromWei(transactionData.gasPrice, 'gwei');同样,要将value字段转换为Ether,可以使用以下代码:
const valueInEther = web3.utils.fromWei(transactionData.value, 'ether');然后,可以使用gasPriceInGwei和valueInEther变量在应用程序中以可读的格式显示值。
https://ethereum.stackexchange.com/questions/140829
复制相似问题