上下文
使用雪崩浏览器,我选择了一个随机的地址,里面有一些AVAX:
https://cchain.explorer.avax.network/address/0xB9F79Fc4B7A2F5fB33493aB5D018dB811c9c2f02/transactions
在我查找该地址时,AVAX余额约为39,166:

代码
我使用下面的REST调用来检索该地址的剩余部分:
curl --location --request POST 'https://api.avax.network/ext/bc/C/rpc' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "eth_getBalance",
"params": [
"0xB9F79Fc4B7A2F5fB33493aB5D018dB811c9c2f02",
"latest"
],
"id": 1
}'在我发出调用时,它返回以下内容:
{"jsonrpc":"2.0","id":1,"result":"0x8868dc30a5d07460032"}问题
如您所见,余额返回为:
0x8868dc30a5d07460032将其转换为小数余额的推荐方法是什么?
如果在developer documentation中讨论了这个问题,请随时给我指出正确的位置。
谢谢!
发布于 2021-09-16 12:38:01
看起来你只是简单地除以18。
下面是一些PowerShell函数Get-AVAX-Balance,它将在给定地址的情况下检索余额:
function Convert-Hexadecimal-to-Decimal ([string]$hexadecimal)
{
$str = '0{0}' -f ($hexadecimal -replace '^0x')
[decimal]([bigint]::Parse($str, [System.Globalization.NumberStyles]::AllowHexSpecifier))
}
function Get-AVAX-Balance ($address)
{
$result = Invoke-RestMethod -Uri 'https://api.avax.network/ext/bc/C/rpc' -Method Post -ContentType 'application/json' -Body (ConvertTo-Json @{
jsonrpc = "2.0"
method = "eth_getBalance"
params = @($address, "latest")
id = 1
})
(Convert-Hexadecimal-to-Decimal $result.result) / [math]::Pow(10,18)
}PS C:\> Get-AVAX-Balance '0xB9F79Fc4B7A2F5fB33493aB5D018dB811c9c2f02'
37667.049457617898011819https://stackoverflow.com/questions/69206343
复制相似问题