Update for merging api and scanner

This commit is contained in:
Sai Raj 2024-07-18 05:28:17 -04:00
parent cc7b51d37a
commit 31ebfb9278
2 changed files with 160 additions and 132 deletions

View File

@ -16,11 +16,11 @@ RETRY_TIMEOUT_REQUEST = 10 * 60 # 10 minsd
prices = {} prices = {}
# 1. fetch old price data if its there, else create an empty db # 1. fetch old price data if its there, else create an empty db
def connect_database(): def connect_database():
if not os.path.isfile(f"system.db"): if not os.path.isfile("prices.db"):
# create an empty db # create an empty db
while True: while True:
try: try:
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
c.execute('''CREATE TABLE ratepairs c.execute('''CREATE TABLE ratepairs
(id integer primary key, ratepair text, price real)''') (id integer primary key, ratepair text, price real)''')
@ -32,7 +32,7 @@ def connect_database():
conn.commit() conn.commit()
conn.close() conn.close()
except: except:
print(f"Unable to create system.db, retrying in {RETRY_TIMEOUT_DB} sec") print(f"Unable to create prices.db, retrying in {RETRY_TIMEOUT_DB} sec")
time.sleep(RETRY_TIMEOUT_DB) time.sleep(RETRY_TIMEOUT_DB)
else: else:
break break
@ -43,7 +43,7 @@ def connect_database():
global prices global prices
while True: while True:
try: try:
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
ratepairs = c.execute('select ratepair, price from ratepairs') ratepairs = c.execute('select ratepair, price from ratepairs')
ratepairs = ratepairs.fetchall() ratepairs = ratepairs.fetchall()
@ -51,7 +51,7 @@ def connect_database():
ratepair = list(ratepair) ratepair = list(ratepair)
prices[ratepair[0]] = ratepair[1] prices[ratepair[0]] = ratepair[1]
except: except:
print(f"Unable to read system.db, retrying in {RETRY_TIMEOUT_DB} sec") print(f"Unable to read prices.db, retrying in {RETRY_TIMEOUT_DB} sec")
time.sleep(RETRY_TIMEOUT_DB) time.sleep(RETRY_TIMEOUT_DB)
else: else:
break break
@ -129,14 +129,14 @@ def fetch_cryptocompare():
def update_latest_prices(): def update_latest_prices():
while True: while True:
try: try:
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
for pair in list(prices.items()): for pair in list(prices.items()):
pair = list(pair) pair = list(pair)
c.execute(f"UPDATE ratepairs SET price={pair[1]} WHERE ratepair='{pair[0]}'") c.execute(f"UPDATE ratepairs SET price={pair[1]} WHERE ratepair='{pair[0]}'")
conn.commit() conn.commit()
except: except:
print(f"Unable to write to system.db, retrying in {RETRY_TIMEOUT_DB} sec") print(f"Unable to write to prices.db, retrying in {RETRY_TIMEOUT_DB} sec")
time.sleep(RETRY_TIMEOUT_DB) time.sleep(RETRY_TIMEOUT_DB)
else: else:
break break

View File

@ -10,7 +10,6 @@ from quart import jsonify, make_response, Quart, render_template, request, flash
from quart_cors import cors from quart_cors import cors
import asyncio import asyncio
from typing import Optional from typing import Optional
from config import dbfolder, apiUrl, FLO_DATA_DIR, API_VERIFY, debug_status, HOST, PORT, APP_ADMIN
import parsing import parsing
import subprocess import subprocess
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
@ -43,6 +42,7 @@ def check_flo_address(floaddress, is_testnet=False):
def check_integer(value): def check_integer(value):
return str.isdigit(value) return str.isdigit(value)
""" ??? NOT USED???
# Helper functions # Helper functions
def retryRequest(tempserverlist, apicall): def retryRequest(tempserverlist, apicall):
if len(tempserverlist) != 0: if len(tempserverlist) != 0:
@ -69,6 +69,7 @@ def multiRequest(apicall, net):
return retryRequest(mainserverlist, apicall) return retryRequest(mainserverlist, apicall)
elif net == 'testnet': elif net == 'testnet':
return retryRequest(testserverlist, apicall) return retryRequest(testserverlist, apicall)
"""
def blockdetailhelper(blockdetail): def blockdetailhelper(blockdetail):
@ -80,7 +81,7 @@ def blockdetailhelper(blockdetail):
blockHeight = None blockHeight = None
# open the latest block database # open the latest block database
conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'latestCache.db'))
c = conn.cursor() c = conn.cursor()
if blockHash: if blockHash:
c.execute(f"select jsonData from latestBlocks where blockHash='{blockHash}'") c.execute(f"select jsonData from latestBlocks where blockHash='{blockHash}'")
@ -91,7 +92,7 @@ def blockdetailhelper(blockdetail):
def transactiondetailhelper(transactionHash): def transactiondetailhelper(transactionHash):
# open the latest block database # open the latest block database
conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'latestCache.db'))
c = conn.cursor() c = conn.cursor()
c.execute(f"SELECT jsonData, parsedFloData, transactionType, db_reference FROM latestTransactions WHERE transactionHash='{transactionHash}'") c.execute(f"SELECT jsonData, parsedFloData, transactionType, db_reference FROM latestTransactions WHERE transactionHash='{transactionHash}'")
transactionJsonData = c.fetchall() transactionJsonData = c.fetchall()
@ -99,10 +100,13 @@ def transactiondetailhelper(transactionHash):
def update_transaction_confirmations(transactionJson): def update_transaction_confirmations(transactionJson):
url = f"{apiUrl}api/v1/tx/{transactionJson['txid']}" url = f"{apiUrl}api/v1/tx/{transactionJson['txid']}"
try:
response = requests.get(url) response = requests.get(url)
if response.status_code == 200: if response.status_code == 200:
response_data = response.json() response_data = response.json()
transactionJson['confirmations'] = response_data['confirmations'] transactionJson['confirmations'] = response_data['confirmations']
except Exception as e:
print(e)
return transactionJson return transactionJson
def smartcontract_morph_helper(smart_contracts): def smartcontract_morph_helper(smart_contracts):
@ -172,14 +176,14 @@ def return_smart_contracts(connection, contractName=None, contractAddress=None):
def create_database_connection(type, parameters=None): def create_database_connection(type, parameters=None):
if type == 'token': if type == 'token':
filelocation = os.path.join(dbfolder, 'tokens', parameters['token_name']) filelocation = os.path.join(DATA_PATH, 'tokens', parameters['token_name'])
elif type == 'smart_contract': elif type == 'smart_contract':
contractDbName = '{}-{}.db'.format(parameters['contract_name'].strip(), parameters['contract_address'].strip()) contractDbName = '{}-{}.db'.format(parameters['contract_name'].strip(), parameters['contract_address'].strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
elif type == 'system_dbs': elif type == 'system_dbs':
filelocation = os.path.join(dbfolder, 'system.db') filelocation = os.path.join(DATA_PATH, 'system.db')
elif type == 'latest_cache': elif type == 'latest_cache':
filelocation = os.path.join(dbfolder, 'latestCache.db') filelocation = os.path.join(DATA_PATH, 'latestCache.db')
conn = sqlite3.connect(filelocation) conn = sqlite3.connect(filelocation)
c = conn.cursor() c = conn.cursor()
@ -188,7 +192,7 @@ def create_database_connection(type, parameters=None):
def fetchContractStructure(contractName, contractAddress): def fetchContractStructure(contractName, contractAddress):
# Make connection to contract database # Make connection to contract database
contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip()) contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# fetch from contractStructure # fetch from contractStructure
conn = sqlite3.connect(filelocation) conn = sqlite3.connect(filelocation)
@ -246,28 +250,36 @@ def extract_ip_op_addresses(transactionJson):
def updatePrices(): def updatePrices():
prices = {} prices = {}
# USD -> INR # USD -> INR
try:
response = requests.get(f"https://api.exchangerate-api.com/v4/latest/usd") response = requests.get(f"https://api.exchangerate-api.com/v4/latest/usd")
price = response.json() price = response.json()
prices['USDINR'] = price['rates']['INR'] prices['USDINR'] = price['rates']['INR']
except Exception as e:
print(e)
# Blockchain stuff : BTC,FLO -> USD,INR # Blockchain stuff : BTC,FLO -> USD,INR
# BTC->USD | BTC->INR # BTC->USD | BTC->INR
try:
response = requests.get(f"https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,flo&vs_currencies=usd,inr") response = requests.get(f"https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,flo&vs_currencies=usd,inr")
price = response.json() price = response.json()
prices['BTCUSD'] = price['bitcoin']['usd'] prices['BTCUSD'] = price['bitcoin']['usd']
prices['BTCINR'] = price['bitcoin']['inr'] prices['BTCINR'] = price['bitcoin']['inr']
except Exception as e:
print(e)
# FLO->USD | FLO->INR # FLO->USD | FLO->INR
try:
response = requests.get(f"https://api.coinlore.net/api/ticker/?id=67") response = requests.get(f"https://api.coinlore.net/api/ticker/?id=67")
price = response.json() price = response.json()
prices["FLOUSD"] = float(price[0]['price_usd']) prices["FLOUSD"] = float(price[0]['price_usd'])
prices["FLOINR"] = float(prices["FLOUSD"]) * float(prices['USDINR']) prices["FLOINR"] = float(prices["FLOUSD"]) * float(prices['USDINR'])
except Exception as e:
print(e)
# 3. update latest price data # 3. update latest price data
print('Prices updated at time: %s' % datetime.now()) print('Prices updated at time: %s' % datetime.now())
print(prices) print(prices)
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
for pair in list(prices.items()): for pair in list(prices.items()):
pair = list(pair) pair = list(pair)
@ -414,7 +426,7 @@ def transaction_post_processing(transactionJsonData):
return rowarray_list return rowarray_list
def fetch_token_transactions(token, senderFloAddress=None, destFloAddress=None, limit=None, use_and=False): def fetch_token_transactions(token, senderFloAddress=None, destFloAddress=None, limit=None, use_and=False):
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
@ -461,7 +473,7 @@ def fetch_token_transactions(token, senderFloAddress=None, destFloAddress=None,
return transaction_post_processing(transactionJsonData) return transaction_post_processing(transactionJsonData)
def fetch_contract_transactions(contractName, contractAddress, _from=0, to=100): def fetch_contract_transactions(contractName, contractAddress, _from=0, to=100):
sc_file = os.path.join(dbfolder, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress)) sc_file = os.path.join(DATA_PATH, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress))
conn = sqlite3.connect(sc_file) conn = sqlite3.connect(sc_file)
c = conn.cursor() c = conn.cursor()
# Find token db names and attach # Find token db names and attach
@ -470,8 +482,8 @@ def fetch_contract_transactions(contractName, contractAddress, _from=0, to=100):
if contractStructure['contractType'] == 'continuos-event': if contractStructure['contractType'] == 'continuos-event':
token1 = contractStructure['accepting_token'] token1 = contractStructure['accepting_token']
token2 = contractStructure['selling_token'] token2 = contractStructure['selling_token']
token1_file = f"{dbfolder}/tokens/{token1}.db" token1_file = f"{DATA_PATH}/tokens/{token1}.db"
token2_file = f"{dbfolder}/tokens/{token2}.db" token2_file = f"{DATA_PATH}/tokens/{token2}.db"
conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db") conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db")
conn.execute(f"ATTACH DATABASE '{token2_file}' AS token2db") conn.execute(f"ATTACH DATABASE '{token2_file}' AS token2db")
@ -497,7 +509,7 @@ def fetch_contract_transactions(contractName, contractAddress, _from=0, to=100):
elif contractStructure['contractType'] == 'one-time-event': elif contractStructure['contractType'] == 'one-time-event':
token1 = contractStructure['tokenIdentification'] token1 = contractStructure['tokenIdentification']
token1_file = f"{dbfolder}/tokens/{token1}.db" token1_file = f"{DATA_PATH}/tokens/{token1}.db"
conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db") conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db")
transaction_query = f''' transaction_query = f'''
@ -526,15 +538,15 @@ def fetch_contract_transactions(contractName, contractAddress, _from=0, to=100):
def fetch_swap_contract_transactions(contractName, contractAddress, transactionHash=None): def fetch_swap_contract_transactions(contractName, contractAddress, transactionHash=None):
sc_file = os.path.join(dbfolder, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress)) sc_file = os.path.join(DATA_PATH, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress))
conn = sqlite3.connect(sc_file) conn = sqlite3.connect(sc_file)
c = conn.cursor() c = conn.cursor()
# Find token db names and attach # Find token db names and attach
contractStructure = fetchContractStructure(contractName, contractAddress) contractStructure = fetchContractStructure(contractName, contractAddress)
token1 = contractStructure['accepting_token'] token1 = contractStructure['accepting_token']
token2 = contractStructure['selling_token'] token2 = contractStructure['selling_token']
token1_file = f"{dbfolder}/tokens/{token1}.db" token1_file = f"{DATA_PATH}/tokens/{token1}.db"
token2_file = f"{dbfolder}/tokens/{token2}.db" token2_file = f"{DATA_PATH}/tokens/{token2}.db"
conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db") conn.execute(f"ATTACH DATABASE '{token1_file}' AS token1db")
conn.execute(f"ATTACH DATABASE '{token2_file}' AS token2db") conn.execute(f"ATTACH DATABASE '{token2_file}' AS token2db")
@ -606,7 +618,7 @@ def refresh_committee_list(admin_flo_id, api_url, blocktime):
if response.status_code == 200: if response.status_code == 200:
return response.json() return response.json()
else: else:
print('Response from the Flosight API failed') print('Response from the Blockbook API failed')
sys.exit(0) sys.exit(0)
url = f'{api_url}api/v1/address/{admin_flo_id}?details=txs' url = f'{api_url}api/v1/address/{admin_flo_id}?details=txs'
@ -635,7 +647,7 @@ async def systemData():
try: try:
# query for the number of flo addresses in tokenAddress mapping # query for the number of flo addresses in tokenAddress mapping
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
tokenAddressCount = c.execute('select count(distinct tokenAddress) from tokenAddressMapping').fetchall()[0][0] tokenAddressCount = c.execute('select count(distinct tokenAddress) from tokenAddressMapping').fetchall()[0][0]
tokenCount = c.execute('select count(distinct token) from tokenAddressMapping').fetchall()[0][0] tokenCount = c.execute('select count(distinct token) from tokenAddressMapping').fetchall()[0][0]
@ -644,7 +656,7 @@ async def systemData():
conn.close() conn.close()
# query for total number of validated blocks # query for total number of validated blocks
conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'latestCache.db'))
c = conn.cursor() c = conn.cursor()
validatedBlockCount = c.execute('select count(distinct blockNumber) from latestBlocks').fetchall()[0][0] validatedBlockCount = c.execute('select count(distinct blockNumber) from latestBlocks').fetchall()[0][0]
validatedTransactionCount = c.execute('select count(distinct transactionHash) from latestTransactions').fetchall()[0][0] validatedTransactionCount = c.execute('select count(distinct transactionHash) from latestTransactions').fetchall()[0][0]
@ -671,8 +683,8 @@ async def broadcastTx(raw_transaction_hash):
async def getTokenList(): async def getTokenList():
try: try:
filelist = [] filelist = []
for item in os.listdir(os.path.join(dbfolder, 'tokens')): for item in os.listdir(os.path.join(DATA_PATH, 'tokens')):
if os.path.isfile(os.path.join(dbfolder, 'tokens', item)): if os.path.isfile(os.path.join(DATA_PATH, 'tokens', item)):
filelist.append(item[:-3]) filelist.append(item[:-3])
return jsonify(tokens=filelist, result='ok') return jsonify(tokens=filelist, result='ok')
except Exception as e: except Exception as e:
@ -687,7 +699,7 @@ async def getTokenInfo():
if token is None: if token is None:
return jsonify(result='error', description='token name hasnt been passed') return jsonify(result='error', description='token name hasnt been passed')
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -732,7 +744,7 @@ async def getTokenTransactions():
if token is None: if token is None:
return jsonify(result='error', description='token name hasnt been passed') return jsonify(result='error', description='token name hasnt been passed')
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
@ -784,7 +796,7 @@ async def getTokenBalances():
if token is None: if token is None:
return jsonify(result='error', description='token name hasnt been passed') return jsonify(result='error', description='token name hasnt been passed')
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -813,7 +825,7 @@ async def getFloAddressInfo():
if floAddress is None: if floAddress is None:
return jsonify(description='floAddress hasn\'t been passed'), 400 return jsonify(description='floAddress hasn\'t been passed'), 400
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -826,7 +838,7 @@ async def getFloAddressInfo():
detailList = {} detailList = {}
for token in tokenNames: for token in tokenNames:
token = token[0] token = token[0]
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tempdict = {} tempdict = {}
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
@ -872,7 +884,7 @@ async def getAddressBalance():
return jsonify(result='error', description='floAddress hasn\'t been passed') return jsonify(result='error', description='floAddress hasn\'t been passed')
if token is None: if token is None:
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -885,7 +897,7 @@ async def getAddressBalance():
for token in tokenNames: for token in tokenNames:
token = token[0] token = token[0]
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tempdict = {} tempdict = {}
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
@ -902,7 +914,7 @@ async def getAddressBalance():
# Address is not associated with any token # Address is not associated with any token
return jsonify(result='error', description='FLO address is not associated with any tokens') return jsonify(result='error', description='FLO address is not associated with any tokens')
else: else:
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -930,14 +942,14 @@ async def getFloAddressTransactions():
return jsonify(result='error', description='floAddress has not been passed') return jsonify(result='error', description='floAddress has not been passed')
if token is None: if token is None:
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
c.execute('SELECT token FROM tokenAddressMapping WHERE tokenAddress="{}"'.format(floAddress)) c.execute('SELECT token FROM tokenAddressMapping WHERE tokenAddress="{}"'.format(floAddress))
tokenNames = c.fetchall() tokenNames = c.fetchall()
else: else:
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tokenNames = [[str(token), ]] tokenNames = [[str(token), ]]
else: else:
@ -947,7 +959,7 @@ async def getFloAddressTransactions():
allTransactionList = {} allTransactionList = {}
for tokenname in tokenNames: for tokenname in tokenNames:
tokenname = tokenname[0] tokenname = tokenname[0]
dblocation = dbfolder + '/tokens/' + str(tokenname) + '.db' dblocation = DATA_PATH + '/tokens/' + str(tokenname) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tempdict = {} tempdict = {}
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
@ -985,7 +997,7 @@ async def getContractList():
contractName = request.args.get('contractName') contractName = request.args.get('contractName')
contractAddress = request.args.get('contractAddress') contractAddress = request.args.get('contractAddress')
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
contractList = [] contractList = []
@ -1090,7 +1102,7 @@ async def getContractInfo():
return jsonify(result='error', description='Smart Contract\'s address hasn\'t been passed') return jsonify(result='error', description='Smart Contract\'s address hasn\'t been passed')
contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip()) contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
@ -1125,7 +1137,7 @@ async def getContractInfo():
returnval['tokenAmountDeposited'] = totalAmount returnval['tokenAmountDeposited'] = totalAmount
conn.close() conn.close()
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
c.execute('select status, incorporationDate, expiryDate, closeDate from activecontracts where contractName=="{}" and contractAddress=="{}"'.format(contractName.strip(), contractAddress.strip())) c.execute('select status, incorporationDate, expiryDate, closeDate from activecontracts where contractName=="{}" and contractAddress=="{}"'.format(contractName.strip(), contractAddress.strip()))
results = c.fetchall() results = c.fetchall()
@ -1196,7 +1208,7 @@ async def getcontractparticipants():
contractName = contractName.strip() contractName = contractName.strip()
contractAddress = contractAddress.strip() contractAddress = contractAddress.strip()
filelocation = os.path.join(dbfolder, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress)) filelocation = os.path.join(DATA_PATH, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress))
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
@ -1278,7 +1290,7 @@ async def getParticipantDetails():
if floAddress is None: if floAddress is None:
return jsonify(result='error', description='FLO address hasn\'t been passed') return jsonify(result='error', description='FLO address hasn\'t been passed')
dblocation = os.path.join(dbfolder, 'system.db') dblocation = os.path.join(DATA_PATH, 'system.db')
if (contractName and contractAddress is None) or (contractName is None and contractAddress): if (contractName and contractAddress is None) or (contractName is None and contractAddress):
return jsonify(result='error', description='pass both, contractName and contractAddress as url parameters') return jsonify(result='error', description='pass both, contractName and contractAddress as url parameters')
@ -1299,7 +1311,7 @@ async def getParticipantDetails():
participationDetailsList = [] participationDetailsList = []
for contract in participant_address_contracts: for contract in participant_address_contracts:
detailsDict = {} detailsDict = {}
contract_db = os.path.join(dbfolder, 'smartContracts', f"{contract[3]}-{contract[4]}.db") contract_db = os.path.join(DATA_PATH, 'smartContracts', f"{contract[3]}-{contract[4]}.db")
# Make db connection and fetch contract structure # Make db connection and fetch contract structure
conn = sqlite3.connect(contract_db) conn = sqlite3.connect(contract_db)
c = conn.cursor() c = conn.cursor()
@ -1370,7 +1382,7 @@ async def getParticipantDetails():
# check if the contract has been closed # check if the contract has been closed
contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(), detailsDict['contractAddress'].strip()) contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(), detailsDict['contractAddress'].strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
conn = sqlite3.connect(filelocation) conn = sqlite3.connect(filelocation)
@ -1423,7 +1435,7 @@ async def getParticipantDetails():
# check if the contract has been closed # check if the contract has been closed
contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(), detailsDict['contractAddress'].strip()) contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(), detailsDict['contractAddress'].strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
conn = sqlite3.connect(filelocation) conn = sqlite3.connect(filelocation)
@ -1488,7 +1500,7 @@ async def getsmartcontracttransactions():
return jsonify(result='error', description='Smart Contract\'s address hasn\'t been passed') return jsonify(result='error', description='Smart Contract\'s address hasn\'t been passed')
contractDbName = '{}-{}.db'.format(contractName.strip(), contractAddress.strip()) contractDbName = '{}-{}.db'.format(contractName.strip(), contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
@ -1553,7 +1565,7 @@ async def getLatestTransactionDetails():
numberOfLatestBlocks = request.args.get('numberOfLatestBlocks') numberOfLatestBlocks = request.args.get('numberOfLatestBlocks')
dblocation = dbfolder + '/latestCache.db' dblocation = DATA_PATH + '/latestCache.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1599,7 +1611,7 @@ async def getLatestTransactionDetails():
async def getLatestBlockDetails(): async def getLatestBlockDetails():
try: try:
limit = request.args.get('limit') limit = request.args.get('limit')
dblocation = dbfolder + '/latestCache.db' dblocation = DATA_PATH + '/latestCache.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1662,14 +1674,14 @@ async def categoriseString(urlstring):
return jsonify(type='block') return jsonify(type='block')
else: else:
# check urlstring is a token name # check urlstring is a token name
tokenfolder = os.path.join(dbfolder, 'tokens') tokenfolder = os.path.join(DATA_PATH, 'tokens')
onlyfiles = [f[:-3] onlyfiles = [f[:-3]
for f in os.listdir(tokenfolder) if os.path.isfile(os.path.join(tokenfolder, f))] for f in os.listdir(tokenfolder) if os.path.isfile(os.path.join(tokenfolder, f))]
if urlstring.lower() in onlyfiles: if urlstring.lower() in onlyfiles:
return jsonify(type='token') return jsonify(type='token')
else: else:
contractfolder = os.path.join(dbfolder, 'system.db') contractfolder = os.path.join(DATA_PATH, 'system.db')
conn = sqlite3.connect(contractfolder) conn = sqlite3.connect(contractfolder)
conn.row_factory = lambda cursor, row: row[0] conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor() c = conn.cursor()
@ -1690,12 +1702,12 @@ async def getTokenSmartContractList():
try: try:
# list of tokens # list of tokens
filelist = [] filelist = []
for item in os.listdir(os.path.join(dbfolder, 'tokens')): for item in os.listdir(os.path.join(DATA_PATH, 'tokens')):
if os.path.isfile(os.path.join(dbfolder, 'tokens', item)): if os.path.isfile(os.path.join(DATA_PATH, 'tokens', item)):
filelist.append(item[:-3]) filelist.append(item[:-3])
# list of smart contracts # list of smart contracts
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
contractList = [] contractList = []
c.execute('SELECT * FROM activecontracts') c.execute('SELECT * FROM activecontracts')
@ -1732,7 +1744,7 @@ async def getTokenSmartContractList():
async def info(): async def info():
try: try:
# query for the number of flo addresses in tokenAddress mapping # query for the number of flo addresses in tokenAddress mapping
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
tokenAddressCount = c.execute('SELECT COUNT(distinct tokenAddress) FROM tokenAddressMapping').fetchall()[0][0] tokenAddressCount = c.execute('SELECT COUNT(distinct tokenAddress) FROM tokenAddressMapping').fetchall()[0][0]
tokenCount = c.execute('SELECT COUNT(distinct token) FROM tokenAddressMapping').fetchall()[0][0] tokenCount = c.execute('SELECT COUNT(distinct token) FROM tokenAddressMapping').fetchall()[0][0]
@ -1741,7 +1753,7 @@ async def info():
conn.close() conn.close()
# query for total number of validated blocks # query for total number of validated blocks
conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'latestCache.db'))
c = conn.cursor() c = conn.cursor()
validatedBlockCount = c.execute('SELECT COUNT(distinct blockNumber) FROM latestBlocks').fetchall()[0][0] validatedBlockCount = c.execute('SELECT COUNT(distinct blockNumber) FROM latestBlocks').fetchall()[0][0]
validatedTransactionCount = c.execute('SELECT COUNT(distinct transactionHash) FROM latestTransactions').fetchall()[0][0] validatedTransactionCount = c.execute('SELECT COUNT(distinct transactionHash) FROM latestTransactions').fetchall()[0][0]
@ -1770,8 +1782,8 @@ async def broadcastTx_v2(raw_transaction_hash):
async def tokenList(): async def tokenList():
try: try:
filelist = [] filelist = []
for item in os.listdir(os.path.join(dbfolder, 'tokens')): for item in os.listdir(os.path.join(DATA_PATH, 'tokens')):
if os.path.isfile(os.path.join(dbfolder, 'tokens', item)): if os.path.isfile(os.path.join(DATA_PATH, 'tokens', item)):
filelist.append(item[:-3]) filelist.append(item[:-3])
return jsonify(tokens=filelist), 200 return jsonify(tokens=filelist), 200
except Exception as e: except Exception as e:
@ -1787,7 +1799,7 @@ async def tokenInfo(token):
return jsonify(description='Token name hasnt been passed'), 400 return jsonify(description='Token name hasnt been passed'), 400
# todo : input validation # todo : input validation
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1849,7 +1861,7 @@ async def tokenTransactions(token):
if to<1: if to<1:
return jsonify(description='to validation failed'), 400 return jsonify(description='to validation failed'), 400
filelocation = os.path.join(dbfolder, 'tokens', f'{token}.db') filelocation = os.path.join(DATA_PATH, 'tokens', f'{token}.db')
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
transactionJsonData = fetch_token_transactions(token, senderFloAddress, destFloAddress, limit, use_AND) transactionJsonData = fetch_token_transactions(token, senderFloAddress, destFloAddress, limit, use_AND)
@ -1869,7 +1881,7 @@ async def tokenBalances(token):
if token is None: if token is None:
return jsonify(description='Token name hasnt been passed'), 400 return jsonify(description='Token name hasnt been passed'), 400
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1898,7 +1910,7 @@ async def floAddressInfo(floAddress):
if not check_flo_address(floAddress, is_testnet): if not check_flo_address(floAddress, is_testnet):
return jsonify(description='floAddress validation failed'), 400 return jsonify(description='floAddress validation failed'), 400
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1911,7 +1923,7 @@ async def floAddressInfo(floAddress):
detailList = {} detailList = {}
for token in tokenNames: for token in tokenNames:
token = token[0] token = token[0]
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tempdict = {} tempdict = {}
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
@ -1958,7 +1970,7 @@ async def floAddressBalance(floAddress):
token = request.args.get('token') token = request.args.get('token')
if token is None: if token is None:
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -1969,7 +1981,7 @@ async def floAddressBalance(floAddress):
detailList = {} detailList = {}
for token in tokenNames: for token in tokenNames:
token = token[0] token = token[0]
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tempdict = {} tempdict = {}
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
@ -1984,7 +1996,7 @@ async def floAddressBalance(floAddress):
# Address is not associated with any token # Address is not associated with any token
return jsonify(floAddress=floAddress, floAddressBalances={}), 200 return jsonify(floAddress=floAddress, floAddressBalances={}), 200
else: else:
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -2013,14 +2025,14 @@ async def floAddressTransactions(floAddress):
token = request.args.get('token') token = request.args.get('token')
if token is None: if token is None:
dblocation = dbfolder + '/system.db' dblocation = os.path.join(DATA_PATH, 'system.db')
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
c.execute('SELECT token FROM tokenAddressMapping WHERE tokenAddress="{}"'.format(floAddress)) c.execute('SELECT token FROM tokenAddressMapping WHERE tokenAddress="{}"'.format(floAddress))
tokenNames = c.fetchall() tokenNames = c.fetchall()
else: else:
dblocation = dbfolder + '/tokens/' + str(token) + '.db' dblocation = DATA_PATH + '/tokens/' + str(token) + '.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
tokenNames = [[str(token), ]] tokenNames = [[str(token), ]]
else: else:
@ -2062,7 +2074,7 @@ async def getContractList_v2():
return jsonify(description='contractAddress validation failed'), 400 return jsonify(description='contractAddress validation failed'), 400
contractList = [] contractList = []
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
smart_contracts = return_smart_contracts(c, contractName, contractAddress) smart_contracts = return_smart_contracts(c, contractName, contractAddress)
smart_contracts_morphed = smartcontract_morph_helper(smart_contracts) smart_contracts_morphed = smartcontract_morph_helper(smart_contracts)
@ -2175,7 +2187,7 @@ async def getcontractparticipants_v2():
if not check_flo_address(contractAddress, is_testnet): if not check_flo_address(contractAddress, is_testnet):
return jsonify(description='contractAddress validation failed'), 400 return jsonify(description='contractAddress validation failed'), 400
filelocation = os.path.join(dbfolder, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress)) filelocation = os.path.join(DATA_PATH, 'smartContracts', '{}-{}.db'.format(contractName, contractAddress))
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
contractStructure = fetchContractStructure(contractName, contractAddress) contractStructure = fetchContractStructure(contractName, contractAddress)
@ -2262,7 +2274,7 @@ async def participantDetails(floAddress):
return jsonify(description='pass both, contractName and contractAddress as url parameters'), 400 return jsonify(description='pass both, contractName and contractAddress as url parameters'), 400
contractName = contractName.strip().lower() contractName = contractName.strip().lower()
systemdb_location = os.path.join(dbfolder, 'system.db') systemdb_location = os.path.join(DATA_PATH, 'system.db')
if os.path.isfile(systemdb_location): if os.path.isfile(systemdb_location):
# Make db connection and fetch data # Make db connection and fetch data
systemdb_conn = sqlite3.connect(systemdb_location) systemdb_conn = sqlite3.connect(systemdb_location)
@ -2277,7 +2289,7 @@ async def participantDetails(floAddress):
participationDetailsList = [] participationDetailsList = []
for contract in participant_address_contracts: for contract in participant_address_contracts:
detailsDict = {} detailsDict = {}
contract_db = os.path.join(dbfolder, 'smartContracts', f"{contract[3]}-{contract[4]}.db") contract_db = os.path.join(DATA_PATH, 'smartContracts', f"{contract[3]}-{contract[4]}.db")
# Make db connection and fetch contract structure # Make db connection and fetch contract structure
contractdb_conn = sqlite3.connect(contract_db) contractdb_conn = sqlite3.connect(contract_db)
contract_c = contractdb_conn.cursor() contract_c = contractdb_conn.cursor()
@ -2330,7 +2342,7 @@ async def participantDetails(floAddress):
detailsDict['closeDate'] = temp[0][7] detailsDict['closeDate'] = temp[0][7]
# check if the contract has been closed # check if the contract has been closed
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
if 'payeeAddress' in contractStructure: if 'payeeAddress' in contractStructure:
# contract is of the type external trigger # contract is of the type external trigger
@ -2361,7 +2373,7 @@ async def participantDetails(floAddress):
detailsDict['closeDate'] = temp[0][7] detailsDict['closeDate'] = temp[0][7]
# check if the contract has been closed # check if the contract has been closed
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
contract_c.execute('SELECT attribute,value FROM contractstructure') contract_c.execute('SELECT attribute,value FROM contractstructure')
@ -2427,7 +2439,7 @@ async def smartcontracttransactions():
return jsonify(description='to validation failed'), 400 return jsonify(description='to validation failed'), 400
contractDbName = '{}-{}.db'.format(contractName, contractAddress) contractDbName = '{}-{}.db'.format(contractName, contractAddress)
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# Make db connection and fetch data # Make db connection and fetch data
@ -2460,7 +2472,7 @@ async def smartcontractdeposits():
return jsonify(description='contractAddress validation failed'), 400 return jsonify(description='contractAddress validation failed'), 400
contractDbName = '{}-{}.db'.format(contractName, contractAddress) contractDbName = '{}-{}.db'.format(contractName, contractAddress)
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(DATA_PATH, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
# active deposits # active deposits
conn = sqlite3.connect(filelocation) conn = sqlite3.connect(filelocation)
@ -2532,7 +2544,7 @@ async def transactiondetails1(transactionHash):
operationDetails = {} operationDetails = {}
if operation == 'smartContractDeposit': if operation == 'smartContractDeposit':
# open the db reference and check if there is a deposit return # open the db reference and check if there is a deposit return
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db") conn = sqlite3.connect(f"{DATA_PATH}/smartContracts/{db_reference}.db")
c = conn.cursor() c = conn.cursor()
c.execute("SELECT depositAmount, blockNumber FROM contractdeposits WHERE status='deposit-return' AND transactionHash=?",(transactionJson['txid'],)) c.execute("SELECT depositAmount, blockNumber FROM contractdeposits WHERE status='deposit-return' AND transactionHash=?",(transactionJson['txid'],))
returned_deposit_tx = c.fetchall() returned_deposit_tx = c.fetchall()
@ -2553,7 +2565,7 @@ async def transactiondetails1(transactionHash):
operationDetails['consumedAmount'] = parseResult['depositAmount'] - operationDetails['depositBalance'] operationDetails['consumedAmount'] = parseResult['depositAmount'] - operationDetails['depositBalance']
elif operation == 'tokenswap-participation': elif operation == 'tokenswap-participation':
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db") conn = sqlite3.connect(f"{DATA_PATH}/smartContracts/{db_reference}.db")
c = conn.cursor() c = conn.cursor()
c.execute('SELECT tokenAmount, winningAmount, userChoice FROM contractparticipants WHERE transactionHash=?',(transactionJson['txid'],)) c.execute('SELECT tokenAmount, winningAmount, userChoice FROM contractparticipants WHERE transactionHash=?',(transactionJson['txid'],))
swap_amounts = c.fetchall() swap_amounts = c.fetchall()
@ -2568,7 +2580,7 @@ async def transactiondetails1(transactionHash):
elif operation == 'smartContractPays': elif operation == 'smartContractPays':
# Find what happened because of the trigger # Find what happened because of the trigger
# Find who # Find who
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db") conn = sqlite3.connect(f"{DATA_PATH}/smartContracts/{db_reference}.db")
c = conn.cursor() c = conn.cursor()
c.execute('SELECT participantAddress, tokenAmount, userChoice, winningAmount FROM contractparticipants WHERE winningAmount IS NOT NULL') c.execute('SELECT participantAddress, tokenAmount, userChoice, winningAmount FROM contractparticipants WHERE winningAmount IS NOT NULL')
winner_participants = c.fetchall() winner_participants = c.fetchall()
@ -2585,7 +2597,7 @@ async def transactiondetails1(transactionHash):
elif operation == 'ote-externaltrigger-participation': elif operation == 'ote-externaltrigger-participation':
# Find if this guy has won # Find if this guy has won
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db") conn = sqlite3.connect(f"{DATA_PATH}/smartContracts/{db_reference}.db")
c = conn.cursor() c = conn.cursor()
c.execute('SELECT winningAmount FROM contractparticipants WHERE transactionHash=?',(transactionHash,)) c.execute('SELECT winningAmount FROM contractparticipants WHERE transactionHash=?',(transactionHash,))
winningAmount = c.fetchall() winningAmount = c.fetchall()
@ -2594,7 +2606,7 @@ async def transactiondetails1(transactionHash):
elif operation == 'tokenswapParticipation': elif operation == 'tokenswapParticipation':
contractName, contractAddress = db_reference.rsplit('-',1) contractName, contractAddress = db_reference.rsplit('-',1)
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db") conn = sqlite3.connect(f"{DATA_PATH}/smartContracts/{db_reference}.db")
c = conn.cursor() c = conn.cursor()
txhash_txs = fetch_swap_contract_transactions(contractName, contractAddress, transactionHash) txhash_txs = fetch_swap_contract_transactions(contractName, contractAddress, transactionHash)
mergeTx['subTransactions'] = [] mergeTx['subTransactions'] = []
@ -2620,7 +2632,7 @@ async def latestTransactionDetails():
if limit is not None and not check_integer(limit): if limit is not None and not check_integer(limit):
return jsonify(description='limit validation failed'), 400 return jsonify(description='limit validation failed'), 400
dblocation = dbfolder + '/latestCache.db' dblocation = DATA_PATH + '/latestCache.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -2660,7 +2672,7 @@ async def latestBlockDetails():
if limit is not None and not check_integer(limit): if limit is not None and not check_integer(limit):
return jsonify(description='limit validation failed'), 400 return jsonify(description='limit validation failed'), 400
dblocation = dbfolder + '/latestCache.db' dblocation = DATA_PATH + '/latestCache.db'
if os.path.exists(dblocation): if os.path.exists(dblocation):
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
@ -2723,13 +2735,13 @@ async def categoriseString_v2(urlstring):
return jsonify(type='block'), 200 return jsonify(type='block'), 200
else: else:
# check urlstring is a token name # check urlstring is a token name
tokenfolder = os.path.join(dbfolder, 'tokens') tokenfolder = os.path.join(DATA_PATH, 'tokens')
onlyfiles = [f[:-3] onlyfiles = [f[:-3]
for f in os.listdir(tokenfolder) if os.path.isfile(os.path.join(tokenfolder, f))] for f in os.listdir(tokenfolder) if os.path.isfile(os.path.join(tokenfolder, f))]
if urlstring.lower() in onlyfiles: if urlstring.lower() in onlyfiles:
return jsonify(type='token'), 200 return jsonify(type='token'), 200
else: else:
systemdb = os.path.join(dbfolder, 'system.db') systemdb = os.path.join(DATA_PATH, 'system.db')
conn = sqlite3.connect(systemdb) conn = sqlite3.connect(systemdb)
conn.row_factory = lambda cursor, row: row[0] conn.row_factory = lambda cursor, row: row[0]
c = conn.cursor() c = conn.cursor()
@ -2750,8 +2762,8 @@ async def tokenSmartContractList():
try: try:
# list of tokens # list of tokens
filelist = [] filelist = []
for item in os.listdir(os.path.join(dbfolder, 'tokens')): for item in os.listdir(os.path.join(DATA_PATH, 'tokens')):
if os.path.isfile(os.path.join(dbfolder, 'tokens', item)): if os.path.isfile(os.path.join(DATA_PATH, 'tokens', item)):
filelist.append(item[:-3]) filelist.append(item[:-3])
# list of smart contracts # list of smart contracts
@ -2765,7 +2777,7 @@ async def tokenSmartContractList():
contractAddress = contractAddress.strip() contractAddress = contractAddress.strip()
if not check_flo_address(contractAddress, is_testnet): if not check_flo_address(contractAddress, is_testnet):
return jsonify(description='contractAddress validation failed'), 400 return jsonify(description='contractAddress validation failed'), 400
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(DATA_PATH, 'system.db'))
c = conn.cursor() c = conn.cursor()
smart_contracts = return_smart_contracts(c, contractName, contractAddress) smart_contracts = return_smart_contracts(c, contractName, contractAddress)
smart_contracts_morphed = smartcontract_morph_helper(smart_contracts) smart_contracts_morphed = smartcontract_morph_helper(smart_contracts)
@ -2832,8 +2844,8 @@ async def sse():
@app.route('/api/v2/prices', methods=['GET']) @app.route('/api/v2/prices', methods=['GET'])
async def priceData(): async def priceData():
try: try:
# read system.db for price data # read prices.db for price data
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
ratepairs = c.execute('select ratepair, price from ratepairs') ratepairs = c.execute('select ratepair, price from ratepairs')
ratepairs = ratepairs.fetchall() ratepairs = ratepairs.fetchall()
@ -2851,10 +2863,11 @@ async def priceData():
####################### #######################
####################### #######################
# if system.db isn't present, initialize it def initialize_db():
if not os.path.isfile(f"system.db"): # if prices.db isn't present, initialize it
if not os.path.isfile(f"prices.db"):
# create an empty db # create an empty db
conn = sqlite3.connect('system.db') conn = sqlite3.connect('prices.db')
c = conn.cursor() c = conn.cursor()
c.execute('''CREATE TABLE ratepairs (id integer primary key, ratepair text, price real)''') c.execute('''CREATE TABLE ratepairs (id integer primary key, ratepair text, price real)''')
c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCBTC', 1)") c.execute("INSERT INTO ratepairs(ratepair, price) VALUES ('BTCBTC', 1)")
@ -2869,12 +2882,27 @@ if not os.path.isfile(f"system.db"):
# update the prices once # update the prices once
updatePrices() updatePrices()
# assign a scheduler for updating prices in the background def set_configs(config):
scheduler = BackgroundScheduler() global DATA_PATH, apiUrl, FLO_DATA_DIR, API_VERIFY, debug_status, HOST, PORT, APP_ADMIN
scheduler.add_job(func=updatePrices, trigger="interval", seconds=600) DATA_PATH = config["DATA_PATH"]
scheduler.start() apiUrl = config["apiUrl"]
# Shut down the scheduler when exiting the app FLO_DATA_DIR = config["FLO_DATA_DIR"]
atexit.register(lambda: scheduler.shutdown()) API_VERIFY = config["API_VERIFY"] or True
debug_status = config["debug_status"]
HOST = config["HOST"]
PORT = config["PORT"]
APP_ADMIN = config["APP_ADMIN"]
if __name__ == "__main__": def init_process():
initialize_db()
# assign a scheduler for updating prices in the background
scheduler = BackgroundScheduler()
scheduler.add_job(func=updatePrices, trigger="interval", seconds=600)
scheduler.start()
# Shut down the scheduler when exiting the app
atexit.register(lambda: scheduler.shutdown())
def start_api_server(config):
set_configs(config)
init_process()
app.run(debug=debug_status, host=HOST, port=PORT) app.run(debug=debug_status, host=HOST, port=PORT)