-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspore_api_utils.py
51 lines (46 loc) · 1.76 KB
/
spore_api_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import psycopg2, os
from dotenv import load_dotenv
import json
def verify_db_connection():
try:
conn = psycopg2.connect(database=os.getenv("DATABASE_NAME", "spore_db"),
host=os.getenv("host", "localhost"),
user=os.getenv("user", "postgres"),
password=os.getenv("password", ""),
port=os.getenv("port", "5432"))
conn.close()
return True
except psycopg2.Error as e:
print("Unable to connect to the database")
return False
def initialize_connection():
try:
conn = psycopg2.connect(database=os.getenv("DATABASE_NAME", "spore_db"),
host=os.getenv("host", "localhost"),
user=os.getenv("user", "postgres"),
password=os.getenv("password", ""),
port=os.getenv("port", "5432"))
return conn
except psycopg2.Error as e:
print("Unable to connect to the database")
return False
def last_indexed_nft_control():
conn = initialize_connection()
c= conn.cursor()
#return both values of last block indexed
query = """SELECT * FROM nft_control WHERE table_name='nft_buys'"""
c.execute(query)
result = c.fetchall()
#handle an empty response
if len(result) == 0:
nft_buys_last_indexed = 0
else:
nft_buys_last_indexed = result[0][2]
query = """SELECT * FROM nft_control WHERE table_name='nft_prices'"""
c.execute(query)
result = c.fetchall()
if len(result) == 0:
nft_prices_last_indexed = 0
else:
nft_prices_last_indexed = result[0][2]
return nft_buys_last_indexed, nft_prices_last_indexed