0

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?

2
  • It shouldn't be necessary to wait. Creating the transactions with consecutive nonces should be enough.
    – Ismael
    Commented Jun 19, 2021 at 17:10
  • The fastest way to sell your tokens it to sell everything in a single transaction. Commented Nov 24, 2021 at 11:03

1 Answer 1

1

Have you tried waiting for the txn receipt using wait_for_transaction_receipt()?

txn_hash = web3.eth.send_raw_transaction(signed_txn.rawTransaction)
receipt = web3.eth.wait_for_transaction_receipt(txn_hash)

wait_for_transaction_receipt() also has a timeout argument. If you experience issues with timeout you can possibly raise that timeout as well. Hope this helps.

3
  • I did this and it gives me 5 seconds for the whole transaction to go through. Do you think it can be done faster?
    – Johnny
    Commented Jun 18, 2021 at 19:11
  • @Johnny You could set the poll_latency which may be similar to your original method but this one is at least structured within a loop so it keeps trying until it works. I would suggest you play around with the poll_latency argument here: github.com/fselmo/web3.py/blob/…
    – felipe
    Commented Jun 18, 2021 at 21:09
  • @Johnny Looks like the poll_latency is set to 0.1 seconds already so I'm assuming 5 this is fast enough polling for you. You seem to be getting the fastest time already. If this answer helped you please mark it as satisfactory. Thanks!
    – felipe
    Commented Jun 18, 2021 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.