Skip to content

Commit

Permalink
Merge pull request #5 from allora-network/diego/use-volume-and-env-va…
Browse files Browse the repository at this point in the history
…r-on-database

database as env var
  • Loading branch information
xmariachi authored Apr 13, 2024
2 parents 246ce67 + 4d716e1 commit 5ece8d0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 6 additions & 6 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ALLORA_VALIDATOR_API_URL = str(os.environ.get('ALLORA_VALIDATOR_API_URL','http://localhost:1317/emissions/v1/network_loss/'))
app = Flask(__name__)

DATABASE_PATH = 'prices.db'
TRUTH_DATABASE_PATH = os.environ.get('TRUTH_DATABASE_PATH', 'prices.db')


# Define retry decorator
Expand All @@ -22,7 +22,7 @@ def fetch_prices(url):
return response.json()

def check_create_table():
conn = sqlite3.connect(DATABASE_PATH)
conn = sqlite3.connect(TRUTH_DATABASE_PATH)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS prices
(timestamp INTEGER PRIMARY KEY, token TEXT, price REAL)''')
Expand Down Expand Up @@ -53,7 +53,7 @@ def update_price(token_name, token_from, token_to):
token = token_name.lower()

# Save price into database
conn = sqlite3.connect(DATABASE_PATH)
conn = sqlite3.connect(TRUTH_DATABASE_PATH)
cursor = conn.cursor()
cursor.execute("INSERT INTO prices (timestamp, token, price) VALUES (?, ?, ?)", (timestamp, token, price))
conn.commit()
Expand All @@ -67,7 +67,7 @@ def update_price(token_name, token_from, token_to):

@app.route('/gt/<token>/<timestamp>')
def get_eth_price(token, timestamp):
conn = sqlite3.connect(DATABASE_PATH)
conn = sqlite3.connect(TRUTH_DATABASE_PATH)
cursor = conn.cursor()
cursor.execute("SELECT timestamp, price FROM prices WHERE token=? AND timestamp <= ? ORDER BY timestamp DESC LIMIT 1", (token, timestamp))
result = cursor.fetchone()
Expand All @@ -82,7 +82,7 @@ def init_price_token(token_name, token_from, token_to):
try:
check_create_table()
# Check if there is any existing data for the specified token
conn = sqlite3.connect(DATABASE_PATH)
conn = sqlite3.connect(TRUTH_DATABASE_PATH)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM prices WHERE token=? LIMIT 1", (token_name.lower(),))
count = cursor.fetchone()[0]
Expand All @@ -106,7 +106,7 @@ def init_price_token(token_name, token_from, token_to):
historical_data = response.json()['prices']

# Parse and insert historical data into the database
conn = sqlite3.connect(DATABASE_PATH)
conn = sqlite3.connect(TRUTH_DATABASE_PATH)
cursor = conn.cursor()
for data_point in historical_data:
timestamp = int(data_point[0] / 1000) # Convert milliseconds to seconds
Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
dockerfile: Dockerfile_truth
command: python -u /app/app.py
environment:
- TRUTH_DATABASE_PATH=/app/data/prices.db
- TRUTH_API_PORT=8000
# - ALLORA_VALIDATOR_API_URL=https://allora-api.testnet.allora.network/emissions/v1/network_loss/
- ALLORA_VALIDATOR_API_URL=https://localhost:1317/emissions/v1/network_loss/
Expand All @@ -29,7 +30,7 @@ services:
build:
context: .
dockerfile: Dockerfile_b7s
image: allora-truth-base:dev
# image: allora-truth-base:dev
entrypoint:
- "/bin/bash"
- "-c"
Expand Down Expand Up @@ -60,7 +61,7 @@ services:

head:
container_name: head-coin
image: allora-inference-base-head:dev
image: alloranetwork/allora-inference-base-head:dev-latest
environment:
- HOME=/data
entrypoint:
Expand Down Expand Up @@ -98,3 +99,4 @@ networks:
volumes:
worker-data:
head-data:
truth-data:

0 comments on commit 5ece8d0

Please sign in to comment.