How can i make this web3 python script faster?

Issue

I want to make a python script (for BSC) which keeps track of the balance of that particular token in the wallet. I need the python script to be very fast. Currently with the below code, it takes about 6 seconds for the script to detect the token entering the wallet. Is there a faster, more efficient way to do it? (I added the sleep func to act as some kind of buffer. Don’t know if its a good idea though?)
Edit: removed the sleep function but still takes 6s.

from web3 import Web3
import json

bsc = "https://bsc-dataseed.binance.org/"
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())

main_address = "wallet to be tracked"
contract_address = "token contract address"
abi = json.loads('the abi')

contract = web3.eth.contract(address=contract_address, abi = abi)

balanceOfToken = contract.functions.balanceOf(main_address).call()
print(web3.fromWei(balanceOfToken, 'ether'))

while(True):
    balanceOfToken = contract.functions.balanceOf(main_address).call()
    if(balanceOfToken > web3.fromWei(0.5, 'ether')):
        break
    
    time.sleep(1.1)
    x+=1
    print(f"Still looking {x}")
    continue

second_address = "the other wallet address"
main_key = "private key of first wallet"

nonce = web3.eth.getTransactionCount(main_address)

token_tx = contract.functions.transfer(second_address, balanceOfToken).buildTransaction({
    'chainId':56, 'gas': 90000, 'gasPrice': web3.toWei('5', 'gwei'), 'nonce':nonce
})

signed_tx = web3.eth.account.signTransaction(token_tx, main_key)
web3.eth.sendRawTransaction(signed_tx.rawTransaction)

print(contract.functions.balanceOf(my_address).call() + " " + contract.functions.name().call())

Solution

Key to answering your question is: What takes 6 seconds?

  1. Running the code from start to finish?

If I run the code on my laptop – using the same node – the code executes in 0.45-0.55s. So perhaps it is not the code itself, but your connection to the node that is slowing down calls or broadcasting the transaction? If so, maybe trying another node will speed up execution. See Binance’s docs for alternatives or check a 3rd party provider.

Unlikely, but it could also be the lack of available processing power on your laptop (?)

  1. Starting the code until the transaction shows up in the block?

The code takes c. 0.5 to run. Add the 3s target block time on BSC and you are already at 3.5s, assuming there’s space in the block (/your fee is sufficient to be included) and assuming it gets broadcasted and picked up immediately. I am unsure what the lower bound should be, but it will take a couple of seconds.

PS. As mentioned by – @Mikko Ohtamaa – Aug 17 ’21 at 5:07 "Instead of polling, you can subscribe to all new blocks and filter out events in the block yourself. (..)" To do this, you can have a look at filtering in web3py.

Answered By – Niels van Egmond

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published