如何在python/web3.py中每3秒获得一次当前WBNB余额?



我需要帮助。我想每3秒轮换一次FIT代币流动性余额,但我需要一些帮助

bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
#WBNB
abi = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs": 
[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"constant":false,"inputs":[{"name":"guy","type":"address"}, 
{"name":"wad","type":"uint256"}],"name":"approve","outputs": 
[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}, 
{"constant":true,"inputs":[],"name":"totalSupply","outputs": 
[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"}, 
{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs": 
[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}, 
{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs": 
[],"payable":false,"stateMutability":"nonpayable","type":"function"}, 
{"constant":true,"inputs":[],"name":"decimals","outputs": 
[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs": 
[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"constant":true,"inputs":[],"name":"symbol","outputs":        
[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"constant":false,"inputs":[{"name":"dst","type":"address"}, 
{"name":"wad","type":"uint256"}],"name":"transfer","outputs": 
[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}, 
{"constant":false,"inputs":[],"name":"deposit","outputs": 
[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs": 
[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs": 
[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}, 
{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs": 
[{"indexed":true,"name":"src","type":"address"}, 
{"indexed":true,"name":"guy","type":"address"}, 
{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"}, 
{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"}, 
{"indexed":true,"name":"dst","type":"address"}, 
{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"}, 
{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"}, 
{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"}, 
{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"}, 
{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]')
address = '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
contract = web3.eth.contract(address=address , abi=abi)
print(contract.functions.name().call())
balance = contract.functions.balanceOf('0x5e8ce185475855E60fA121389331cd6cEd61ea57').call()
print(web3.fromWei(balance, 'ether'))

我得到了最新的信息。但是我想要每3秒更新一次信息。

Ba = balance
while Ba > 0:
if Ba > 0:
print(web3.fromWei(balance, 'ether'))
time.sleep(3)

else:
print('0')   

但这打印我一个结果多次:(

您需要每次使用.call()来刷新余额!

balance = contract.functions.balanceOf('0x5e8ce185475855E60fA121389331cd6cEd61ea57')
while True:
print(web3.fromWei(balance.call(), 'ether'))
time.sleep(3)

最新更新