Skip to content

Instantly share code, notes, and snippets.

@hussu010
Created January 5, 2022 08:09
Show Gist options
  • Save hussu010/ee1750cc36694db99943288df50a0318 to your computer and use it in GitHub Desktop.
Save hussu010/ee1750cc36694db99943288df50a0318 to your computer and use it in GitHub Desktop.
'''
Library Required: requests
Installation: `python -m pip install requests`
'''
import requests
PV_IP = "PV_IP" # IP Address of the primary validator of TNBC Chain
def fetch_account_balance(account_number):
success = False # Flag to check if the operation was successful.
message = "" # Variable that hosts the balance/ error message.
pv_balance_endpoint = f"http://{PV_IP}/accounts/{account_number}/balance" # PV endpoint to retrieve account balance
try:
response = requests.get(pv_balance_endpoint)
if response.status_code == 200:
balance = str(response.json()["balance"])
if balance == "None":
message = "This account is possibly not owned by anyone as it has not sent or received any coins."
else:
success = True
message = str(response.json()["balance"])
else:
message = response.status_code
# Handling requests excpetions gracefully
except requests.exceptions.Timeout:
message = "The request timed out."
except requests.exceptions.ConnectionError:
message = "A Connection error occurred."
except requests.exceptions.HTTPError:
message = "An HTTP error occurred."
except requests.exceptions.RequestException as exception:
message = exception
return success, message
print(fetch_account_balance("ACCOUNT_NUMBER"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment