简体   繁体   English

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

[英]How can I get the current WBNB balance once every 3 seconds in python/web3.py?

I need help.我需要帮助。 I want to rotate the FIT token liquidity balance every 3 seconds but I need some help我想每 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'))

I'm getting updated information.我正在获取最新信息。 But I want to get new updated information every 3 seconds.但我想每 3 秒获取一次新的更新信息。 I try this;我试试这个;

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

But this prints me a single result multiple times:(但这会多次打印我一个结果:(

you need to use.call() every time to refresh the balance!每次刷新余额都需要使用.call()!

balance = contract.functions.balanceOf('0x5e8ce185475855E60fA121389331cd6cEd61ea57')

while True:
     print(web3.fromWei(balance.call(), 'ether'))
     time.sleep(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM