py中钱包中的代币余额小数错误

发布于 2025-01-20 14:25:01 字数 1389 浏览 5 评论 0原文

我正在尝试正确获取钱包中令牌的价值。 令牌有18个小数,但功能contract.functions.decimals()。呼叫()返回9小数。 我尝试了很多示例,但是错误是相同的。 这是我的代码:

import json
import requests
from web3
import Web3

# Check Tokens Balance
MyAddress = '0xD036680F1d50C11B5924Ac487bf7E58632020a79'
TokenAddress = '0xacFC95585D80Ab62f67A14C566C1b7a49Fe91167'

#
Get ABI from BSCscan
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
url_eth = 'https://api.bscscan.com/api'
contract_address = web3.toChecksumAddress(TokenAddress)

API_ENDPOINT = url_eth + '?module=contract&action=getabi&address=' + str(contract_address)
r = requests.get(url = API_ENDPOINT)
response = r.json()
abi = json.loads(response['result'])

# Call contract
contract = web3.eth.contract(address = contract_address, abi = abi)
totalSupply = contract.functions.totalSupply().call()
print(totalSupply)
print(contract.functions.name().call())
print(contract.functions.symbol().call())
print(contract.functions.decimals().call())
address = web3.toChecksumAddress(MyAddress)
balance = contract.functions.balanceOf(address).call()
print(web3.fromWei(balance, 'ether'))

这是控制台的结果:

100000000000000000000000000
FEGtoken
FEG
9
3.660343728974475686

但是在我的钱包中,余额为3660343519.636 FEG,

我不明白如何解决此错误。

I am trying to correctly fetch the value of a token in my Wallet.
The token have 18 decimal but the function contract.functions.decimals().call() returns 9 decimal.
I've tried many examples but the error is the same.
This is my code:

import json
import requests
from web3
import Web3

# Check Tokens Balance
MyAddress = '0xD036680F1d50C11B5924Ac487bf7E58632020a79'
TokenAddress = '0xacFC95585D80Ab62f67A14C566C1b7a49Fe91167'

#
Get ABI from BSCscan
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
url_eth = 'https://api.bscscan.com/api'
contract_address = web3.toChecksumAddress(TokenAddress)

API_ENDPOINT = url_eth + '?module=contract&action=getabi&address=' + str(contract_address)
r = requests.get(url = API_ENDPOINT)
response = r.json()
abi = json.loads(response['result'])

# Call contract
contract = web3.eth.contract(address = contract_address, abi = abi)
totalSupply = contract.functions.totalSupply().call()
print(totalSupply)
print(contract.functions.name().call())
print(contract.functions.symbol().call())
print(contract.functions.decimals().call())
address = web3.toChecksumAddress(MyAddress)
balance = contract.functions.balanceOf(address).call()
print(web3.fromWei(balance, 'ether'))

This is the result in console:

100000000000000000000000000
FEGtoken
FEG
9
3.660343728974475686

But in my wallet the balance is 3660343519.636 FEG

I don't understand how to fix this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2025-01-27 14:25:01

我遇到了类似的问题。您遇到此问题的原因是并非每个标记都具有相同的小数位数。大多数币都有 18 位小数,相当于“ether”,但是正如你所看到的,contract.functions.decimals().call() 返回了 9。这意味着当你打印出 web3.fromwWei 时,你需要调用“wei” '。为了使其更加动态,请传入从decimals().call()方法获得的值。

但问题是:
Contract.functions.decimals().call() 对我来说大约需要 1.4 秒。相比之下,调用 web3.eth.contract(address = Contract_address, abi = abi) 需要 0.01 秒。这是某种 API 错误吗?有没有其他方法可以返回小数值?

I ran into a similar issue. The reason you're getting this problem is that not every token has the same amount of decimals. MOST coins have 18 decimal points, which equals 'ether', but as you can see, contract.functions.decimals().call() returned 9. That means that when you print out web3.fromwWei, you need to call 'wei'. To make it more dynamic, pass in the value you get from the decimals().call() method.

Question though:
contract.functions.decimals().call() takes roughly 1.4 seconds for me. In comparison, calling web3.eth.contract(address = contract_address, abi = abi) takes 0.01 seconds. Is this some sort of API bug? And are there any alternatives for returning decimals value?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文