-1

I am doing a simple request that returns a confirmation number from a blockchain using a random hash from polygonscan.

The problem I have is with the while loop, it keeps looping with the same confirmation number and not updating the true number that is represented on the blockchain.

e.g when a hash confirmation is 100 it will keep printing 100 while blockchain confirmations go up on polygonscan.

I want the ctc variable to update to the true confirmation within the while loop.

from web3 import Web3
web3 = Web3(Web3.HTTPProvider(<APIKEY>))


check_txn_confirmations = web3.eth.blockNumber - web3.eth.getTransaction('0x7a0b596a664e5b56091b775d294d374364db00cab531b8dc18c70932896ccf44ec').blockNumber

ctc = check_txn_confirmations

    while ctc < 260:
        print("confirmations are:", ctc)
        time.sleep(10)
        print("waiting 10seconds..")
    else:
        print("confirmations are larger")
TylerH
  • 20,799
  • 66
  • 75
  • 101
axell2017
  • 41
  • 5
  • Sending API requests in a While loop is a very bad idea, I would recommend changing the structure of the program. – Geom Jul 08 '21 at 05:31
  • Thanks for that. Are you able to suggest a way I can ping multiple times in the program with the same goal in mind? – axell2017 Jul 08 '21 at 05:33
  • Why can't you move the call inside the while loop? There is no other way to do it, unless you want to write a callback-based application, or a multithreaded/asyncio application. – Paul Cornelius Jul 08 '21 at 05:39
  • I cant move the call into the loop as the output is part of the while condition. Unless I am missing something? – axell2017 Jul 08 '21 at 05:40
  • You’re currently only making one request and then keep looping without ever changing the number. You need to make another request inside the loop to change the number, or it will never change. – deceze Jul 08 '21 at 05:49
  • 2
    If you change the value of "ctc" inside the loop, the new value will get used the next time the `while` is evaluated. In fact, it's always the case that you change the condition inside the loop, otherwise no while loop will ever exit. – Paul Cornelius Jul 08 '21 at 05:50

1 Answers1

2

This is what you want to do but also what you shouldn't do:

while True:

    check_txn_confirmations = web3.eth.blockNumber - web3.eth.getTransaction('0x7a0b596a664e5b56091b775d294d374364db00cab531b8dc18c70932896ccf44ec').blockNumber
    ctc = check_txn_confirmations

    if ctc < 260:
        print("confirmations are:", ctc)
        time.sleep(10)
        print("waiting 10seconds..")
    else:
        print("confirmations are larger")
        break

There are a number of problems with sending requests inside a while loop.

Most of the API's have a request limit and the request limit is often tied to pay per usage type of agreement. If/when your program gets accidentally stuck in an infinite while loop you have a problem. Either your request limit reaches or your balance reaches 0(Exaggerating but you get the idea).

Instead, I would recommend looking into Callbacks and Async. But for simple applications callbacks should be enough.

Geom
  • 1,491
  • 1
  • 10
  • 23
  • Thank you for the solution and a great explanation as to the issues that may arise due to this structure! – axell2017 Jul 08 '21 at 05:57
  • How do callbacks and async prevent the problems you describe? They are just an elaborate form of a loop that lets your computer do other things instead of repeatedly sleeping for 10 seconds. – Paul Cornelius Jul 08 '21 at 05:57