I am a beginner using web3py. I want to sell portions of my token holdings at a fast rate. Is it possible to do it without having to put in time.sleep() for the delay of waiting for approval? I tried manually selling a portion of my tokens in PancakeSwap's contract, and it seems to be faster by a long margin.
def sellToken():
approve = sellTokenContract.functions.approve(panRouterContractAddress, int(tokenPortionToSell)).buildTransaction({
'from': address,
'gasPrice': web3.toWei('5','gwei'),
'nonce': web3.eth.get_transaction_count(address),
})
signed_txn = web3.eth.account.sign_transaction(approve, private_key = pk)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Approved: " + web3.toHex(tx_token))
# # Sleep to wait for approval, test 5 second, test 3 second
time.sleep(8)
pancakeswap2_txn = contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens(
int(tokenPortionToSell),
0,
[tosell_contractAddress, wbnb],
address,
(int(time.time()) + 1000000)).buildTransaction({
'from': address,
'gasPrice': web3.toWei('5','gwei'),
'nonce': nonce,
})
signed_txn = web3.eth.account.sign_transaction(pancakeswap2_txn, private_key=pk)
tx_token = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
print("Transaction ID: " + str(web3.toHex(tx_token)))
Sample sell loop
for x in range(1, 3):
print("Transaction: " + str(x) + " processing. . .")
nonce += (x)
sellToken()
print("Transaction: " + str(x) + " done!")
time.sleep(2)
That's a delay of 10 seconds as compared to doing it manually only for 3-5 seconds in Pancakeswap router's contract.
Any idea on how to make an efficient way of this?