get_abi_from_blockscout

Function

def get_abi_from_blockscout(address, attempts=18):
    while attempts > 0:
        try:
            r = requests.get("https://api.scan.pulsechain.com/api/v2/smart-contracts/{}".format(address))
            r.raise_for_status()
        except RequestException:
            attempts -= 1
            if attempts > 0:
                time.sleep(1)
                continue
            else:
                raise RequestException
        else:
            resp = r.json()
            if 'abi' in resp.keys():
                return resp['abi']
            else:
                return []

Description

  • Loop while there are attempts remaining

  • Get the data for the desired contract address from Blockscout’s API

  • Throw an exception if any HTTP errors occur

    • Retry every 1 second while attempts are remaining

  • Get the JSON response from the API endpoint

  • Check if the key “abi” is found in the response

  • Return the value from the “abi” key or an empty list