v2 of getSmartContractInfo

This commit is contained in:
Vivek Teega 2022-04-04 13:24:30 +00:00
parent 25ceaa0574
commit 7894562e0c

View File

@ -2,12 +2,12 @@ from collections import defaultdict
import sqlite3 import sqlite3
import json import json
import os import os
import requests import requests
import sys import sys
import time import time
from datetime import datetime from datetime import datetime
from quart import jsonify, make_response, Quart, render_template, request, flash, redirect, url_for from quart import jsonify, make_response, Quart, render_template, request, flash, redirect, url_for
from quart_cors import cors from quart_cors import cors
import asyncio import asyncio
from typing import Optional from typing import Optional
@ -19,7 +19,7 @@ import subprocess
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.background import BackgroundScheduler
import atexit import atexit
import pdb
app = Quart(__name__) app = Quart(__name__)
app.clients = set() app.clients = set()
@ -85,12 +85,21 @@ def transactiondetailhelper(transactionHash):
conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db')) conn = sqlite3.connect(os.path.join(dbfolder, 'latestCache.db'))
c = conn.cursor() c = conn.cursor()
c.execute( c.execute(f"select jsonData, parsedFloData, transactionType, db_reference from latestTransactions where transactionHash='{transactionHash}'")
f"select jsonData,parsedFloData from latestTransactions where transactionHash='{transactionHash}'")
transactionJsonData = c.fetchall() transactionJsonData = c.fetchall()
return transactionJsonData return transactionJsonData
def smartContractInfo_output(contractName, contractAddress, contractType, subtype):
if contractType == 'continuos-event' and contractType == 'tokenswap':
pass
elif contractType == 'one-time-event' and contractType == 'userchoice':
pass
elif contractType == 'one-time-event' and contractType == 'timetrigger':
pass
# FLO TOKEN APIs # FLO TOKEN APIs
@app.route('/api/v1.0/broadcastTx/<raw_transaction_hash>') @app.route('/api/v1.0/broadcastTx/<raw_transaction_hash>')
async def broadcastTx(raw_transaction_hash): async def broadcastTx(raw_transaction_hash):
@ -516,16 +525,14 @@ async def getContractInfo():
if contractAddress is None: if contractAddress is None:
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(), contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip())
contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(dbfolder, '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)
c = conn.cursor() c = conn.cursor()
c.execute( c.execute('SELECT attribute,value FROM contractstructure')
'SELECT attribute,value FROM contractstructure')
result = c.fetchall() result = c.fetchall()
contractStructure = {} contractStructure = {}
@ -540,7 +547,7 @@ async def getContractInfo():
if len(conditionDict) > 0: if len(conditionDict) > 0:
contractStructure['exitconditions'] = conditionDict contractStructure['exitconditions'] = conditionDict
del counter, conditionDict del counter, conditionDict
returnval = contractStructure returnval = contractStructure
returnval['userChoice'] = contractStructure['exitconditions'] returnval['userChoice'] = contractStructure['exitconditions']
returnval.pop('exitconditions') returnval.pop('exitconditions')
@ -556,8 +563,7 @@ async def getContractInfo():
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db')) conn = sqlite3.connect(os.path.join(dbfolder, 'system.db'))
c = conn.cursor() c = conn.cursor()
c.execute('select status, incorporationDate, expiryDate, closeDate from activecontracts where contractName=="{}" and contractAddress=="{}"'.format( c.execute('select status, incorporationDate, expiryDate, closeDate from activecontracts where contractName=="{}" and contractAddress=="{}"'.format(contractName.strip(), contractAddress.strip()))
contractName.strip(), contractAddress.strip()))
results = c.fetchall() results = c.fetchall()
if len(results) == 1: if len(results) == 1:
@ -598,20 +604,19 @@ async def getContractInfo():
'select participantAddress, winningAmount from contractparticipants where winningAmount is not null') 'select participantAddress, winningAmount from contractparticipants where winningAmount is not null')
winnerparticipants = c.fetchall() winnerparticipants = c.fetchall()
returnval['numberOfWinners'] = len( returnval['numberOfWinners'] = len(winnerparticipants)
winnerparticipants)
else: else:
return jsonify(result='error', description='There is more than 1 trigger in the database for the smart contract. Please check your code, this shouldnt happen') return jsonify(result='error', description='There is more than 1 trigger in the database for the smart contract. Please check your code, this shouldnt happen')
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress, contractInfo=returnval) return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress, contractInfo=returnval)
else: else:
return jsonify(result='error', details='Smart Contract with the given name doesn\'t exist') return jsonify(result='error', details='Smart Contract with the given name doesn\'t exist')
@app.route('/api/v1.0/getSmartContractParticipants', methods=['GET']) @app.route('/api/v2.0/getSmartContractInfo', methods=['GET'])
async def getcontractparticipants(): async def getContractInfo2():
contractName = request.args.get('contractName') contractName = request.args.get('contractName')
contractAddress = request.args.get('contractAddress') contractAddress = request.args.get('contractAddress')
@ -621,16 +626,14 @@ async def getcontractparticipants():
if contractAddress is None: if contractAddress is None:
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(), contractDbName = '{}-{}.db'.format(contractName.strip(),contractAddress.strip())
contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(dbfolder, '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)
c = conn.cursor() c = conn.cursor()
c.execute( c.execute('SELECT attribute,value FROM contractstructure')
'SELECT attribute,value FROM contractstructure')
result = c.fetchall() result = c.fetchall()
contractStructure = {} contractStructure = {}
@ -646,20 +649,93 @@ async def getcontractparticipants():
contractStructure['exitconditions'] = conditionDict contractStructure['exitconditions'] = conditionDict
del counter, conditionDict del counter, conditionDict
returnval = contractStructure
pdb.set_trace()
# Categorize into what type of contract it is right now
if contractStructure['contractType'] == 'continuos-event' and contractStructure['subtype'] == 'tokenswap':
c.execute('select count(participantAddress), sum(tokenAmount), sum(winningAmount) from contractparticipants')
participation_details = c.fetchall()
returnval['numberOfParticipants'] = participation_details[0][0]
returnval['totalParticipationAmount'] = participation_details[0][1]
returnval['totalHonorAmount'] = participation_details[0][2]
c.execute('SELECT COUNT(DISTINCT transactionHash) FROM contractdeposits')
returnval['numberOfDeposits'] = c.fetchall()[0][0]
c.execute('SELECT COUNT(DISTINCT transactionHash) FROM contractdeposits')
returnval['numberOfDeposits'] = c.fetchall()[0][0]
conn.close()
elif contractStructure['contractType'] == 'one-time-event' and 'exitconditions' in contractStructure.keys():
returnval['userChoice'] = contractStructure['exitconditions']
returnval.pop('exitconditions')
conn = sqlite3.connect(os.path.join(dbfolder, 'system.db'))
c = conn.cursor()
c.execute('select status, incorporationDate, expiryDate, closeDate from activecontracts where contractName=="{}" and contractAddress=="{}"'.format(contractName.strip(), contractAddress.strip()))
results = c.fetchall()
if len(results) == 1:
for result in results:
returnval['status'] = result[0]
returnval['incorporationDate'] = result[1]
if result[2]:
returnval['expiryDate'] = result[2]
if result[3]:
returnval['closeDate'] = result[3]
elif contractStructure['contractType'] == 'one-time-event' and 'payeeAddress' in contractStructure.keys():
pass
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress, contractInfo=returnval)
else:
return jsonify(result='error', details='Smart Contract with the given name doesn\'t exist')
@app.route('/api/v1.0/getSmartContractParticipants', methods=['GET'])
async def getcontractparticipants():
contractName = request.args.get('contractName')
contractAddress = request.args.get('contractAddress')
if contractName is None:
return jsonify(result='error', description='Smart Contract\'s name hasn\'t been passed')
if contractAddress is None:
return jsonify(result='error', description='Smart Contract\'s address hasn\'t been passed')
contractDbName = '{}-{}.db'.format(contractName.strip(), contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName)
if os.path.isfile(filelocation):
# Make db connection and fetch data
conn = sqlite3.connect(filelocation)
c = conn.cursor()
c.execute('SELECT attribute,value FROM contractstructure')
result = c.fetchall()
contractStructure = {}
conditionDict = {}
counter = 0
for item in result:
if list(item)[0] == 'exitconditions':
conditionDict[counter] = list(item)[1]
counter = counter + 1
else:
contractStructure[list(item)[0]] = list(item)[1]
if len(conditionDict) > 0:
contractStructure['exitconditions'] = conditionDict
del counter, conditionDict
if 'exitconditions' in contractStructure: if 'exitconditions' in contractStructure:
# contract is of the type external trigger # contract is of the type external trigger
# check if the contract has been closed # check if the contract has been closed
c.execute( c.execute('select * from contractTransactionHistory where transactionType="trigger"')
'select * from contractTransactionHistory where transactionType="trigger"')
trigger = c.fetchall() trigger = c.fetchall()
if len(trigger) == 1: if len(trigger) == 1:
c.execute( c.execute('select value from contractstructure where attribute="tokenIdentification"')
'select value from contractstructure where attribute="tokenIdentification"')
token = c.fetchall() token = c.fetchall()
token = token[0][0] token = token[0][0]
c.execute( c.execute('SELECT id,participantAddress, tokenAmount, userChoice, transactionHash, winningAmount FROM contractparticipants')
'SELECT id,participantAddress, tokenAmount, userChoice, transactionHash, winningAmount FROM contractparticipants')
result = c.fetchall() result = c.fetchall()
conn.close() conn.close()
returnval = {} returnval = {}
@ -667,20 +743,14 @@ async def getcontractparticipants():
returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3], returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3],
'transactionHash': row[4], 'winningAmount': row[5], 'tokenIdentification': token} 'transactionHash': row[4], 'winningAmount': row[5], 'tokenIdentification': token}
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress,
participantInfo=returnval)
elif len(trigger) == 0: elif len(trigger) == 0:
c.execute( c.execute('SELECT id,participantAddress, tokenAmount, userChoice, transactionHash FROM contractparticipants')
'SELECT id,participantAddress, tokenAmount, userChoice, transactionHash FROM contractparticipants')
result = c.fetchall() result = c.fetchall()
conn.close() conn.close()
returnval = {} returnval = {}
for row in result: for row in result:
returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3], returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3], 'transactionHash': row[4]}
'transactionHash': row[4]}
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress,
participantInfo=returnval)
else: else:
return jsonify(result='error', description='More than 1 trigger present. This is unusual, please check your code') return jsonify(result='error', description='More than 1 trigger present. This is unusual, please check your code')
@ -695,8 +765,24 @@ async def getcontractparticipants():
returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3], returnval[row[1]] = {'participantFloAddress': row[1], 'tokenAmount': row[2], 'userChoice': row[3],
'transactionHash': row[4]} 'transactionHash': row[4]}
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress, elif contractStructure['contractType'] == 'continuos-event' and contractStructure['subtype'] == 'tokenswap':
participantInfo=returnval) pdb.set_trace()
c.execute('SELECT * FROM contractparticipants')
contract_participants = c.fetchall()
returnval = {}
for row in contract_participants:
returnval[row[1]] = {
'participantFloAddress': row[1],
'participationAmount': row[2],
'swapPrice': float(row[3]),
'transactionHash': row[4],
'blockNumber': row[5],
'blockHash': row[6],
'swapAmount': row[7]
}
conn.close()
return jsonify(result='ok', contractName=contractName, contractAddress=contractAddress, participantInfo=returnval)
else: else:
return jsonify(result='error', description='Smart Contract with the given name doesn\'t exist') return jsonify(result='error', description='Smart Contract with the given name doesn\'t exist')
@ -714,89 +800,148 @@ async def getParticipantDetails():
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')
#if os.path.isfile(dblocation) and os.path.isfile(contract_db):
if os.path.isfile(dblocation): if os.path.isfile(dblocation):
# Make db connection and fetch data # Make db connection and fetch data
conn = sqlite3.connect(dblocation) conn = sqlite3.connect(dblocation)
c = conn.cursor() c = conn.cursor()
# Check if its a participant address if contractName is not None:
queryString = f"SELECT id, address,contractName, contractAddress, tokenAmount, transactionHash, blockNumber, blockHash FROM contractAddressMapping where address='{floAddress}' and addressType='participant'" c.execute(f'SELECT * FROM contractAddressMapping WHERE address="{floAddress}" AND addressType="participant" AND contractName="{contractName}" AND contractAddress="{contractAddress}"')
c.execute(queryString)
result = c.fetchall()
if len(result) != 0:
participationDetailsList = []
for row in result:
detailsDict = {}
detailsDict['contractName'] = row[2]
detailsDict['contractAddress'] = row[3]
detailsDict['tokenAmount'] = row[4]
detailsDict['transactionHash'] = row[5]
c.execute(
f"select status, tokenIdentification, contractType, blockNumber, blockHash, incorporationDate, expiryDate, closeDate from activecontracts where contractName='{detailsDict['contractName']}' and contractAddress='{detailsDict['contractAddress']}'")
temp = c.fetchall()
detailsDict['status'] = temp[0][0]
detailsDict['tokenIdentification'] = temp[0][1]
detailsDict['contractType'] = temp[0][2]
detailsDict['blockNumber'] = temp[0][3]
detailsDict['blockHash'] = temp[0][4]
detailsDict['incorporationDate'] = temp[0][5]
if temp[0][6]:
detailsDict['expiryDate'] = temp[0][6]
if temp[0][7]:
detailsDict['closeDate'] = temp[0][7]
# check if the contract has been closed
contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(),
detailsDict['contractAddress'].strip())
filelocation = os.path.join(
dbfolder, 'smartContracts', contractDbName)
if os.path.isfile(filelocation):
# Make db connection and fetch data
conn = sqlite3.connect(filelocation)
c = conn.cursor()
c.execute(
'SELECT attribute,value FROM contractstructure')
result = c.fetchall()
contractStructure = {}
conditionDict = {}
counter = 0
for item in result:
if list(item)[0] == 'exitconditions':
conditionDict[counter] = list(item)[1]
counter = counter + 1
else:
contractStructure[list(item)[0]] = list(item)[1]
if len(conditionDict) > 0:
contractStructure['exitconditions'] = conditionDict
del counter, conditionDict
if 'exitconditions' in contractStructure:
# contract is of the type external trigger
# check if the contract has been closed
c.execute(
'select * from contractTransactionHistory where transactionType="trigger"')
trigger = c.fetchall()
if detailsDict['status'] == 'closed':
c.execute(
f"SELECT userChoice, winningAmount FROM contractparticipants where participantAddress='{floAddress}'")
result = c.fetchall()
conn.close()
detailsDict['userChoice'] = result[0][0]
detailsDict['winningAmount'] = result[0][1]
else:
c.execute(
f"SELECT userChoice FROM contractparticipants where participantAddress='{floAddress}'")
result = c.fetchall()
conn.close()
detailsDict['userChoice'] = result[0][0]
participationDetailsList.append(detailsDict)
return jsonify(result='ok', floAddress=floAddress, type='participant', participatedContracts=participationDetailsList)
else: else:
return jsonify(result='error', description='Address hasn\'t participanted in any other contract') c.execute(f'SELECT * FROM contractAddressMapping WHERE address="{floAddress}" AND addressType="participant"')
participant_address_contracts = c.fetchall()
if len(participant_address_contracts) != 0:
participationDetailsList = []
for contract in participant_address_contracts:
detailsDict = {}
contract_db = os.path.join(dbfolder, 'smartContracts', f"{contract[3]}-{contract[4]}.db")
# Make db connection and fetch contract structure
conn = sqlite3.connect(contract_db)
c = conn.cursor()
# Get details of the type of Smart Contract
c.execute('SELECT attribute,value FROM contractstructure')
result = c.fetchall()
contractStructure = {}
conditionDict = {}
counter = 0
for item in result:
if list(item)[0] == 'exitconditions':
conditionDict[counter] = list(item)[1]
counter = counter + 1
else:
contractStructure[list(item)[0]] = list(item)[1]
if len(conditionDict) > 0:
contractStructure['exitconditions'] = conditionDict
del counter, conditionDict
if contractStructure['contractType']=='continuos-event' and contractStructure['subtype']=='tokenswap':
# normal result + swap details
# what is a api detail
c.execute('SELECT * FROM contractparticipants WHERE participantAddress=?',(floAddress,))
participant_details = c.fetchall()
pdb.set_trace()
if len(participant_details) > 0:
participationList = []
for participation in participant_details:
c.execute("SELECT value FROM contractstructure WHERE attribute='selling_token'")
structure = c.fetchall()
detailsDict['participationAddress'] = floAddress
detailsDict['participationAmount'] = participation[2]
detailsDict['receivedAmount'] = float(participation[3])
detailsDict['participationToken'] = contractStructure['accepting_token']
detailsDict['receivedToken'] = contractStructure['selling_token']
detailsDict['swapPrice_received_to_participation'] = float(participation[7])
detailsDict['transactionHash'] = participation[4]
detailsDict['blockNumber'] = participation[5]
detailsDict['blockHash'] = participation[6]
participationList.append(detailsDict)
participationDetailsList.append(participationList)
elif contractStructure['contractType']=='one-time-event' and 'payeeAddress' in contractStructure.keys():
# normal results
pass
elif contractStructure['contractType']=='one-time-event' and 'exitconditions' in contractStructure.keys():
# normal results + winning/losing details
conn = sqlite3.connect(dblocation)
c = conn.cursor()
detailsDict = {}
detailsDict['contractName'] = row[3]
detailsDict['contractAddress'] = row[4]
detailsDict['tokenAmount'] = row[5]
detailsDict['transactionHash'] = row[6]
c.execute(f"select status, tokenIdentification, contractType, blockNumber, blockHash, incorporationDate, expiryDate, closeDate from activecontracts where contractName='{detailsDict['contractName']}' and contractAddress='{detailsDict['contractAddress']}'")
temp = c.fetchall()
detailsDict['status'] = temp[0][0]
detailsDict['tokenIdentification'] = temp[0][1]
detailsDict['contractType'] = temp[0][2]
detailsDict['blockNumber'] = temp[0][3]
detailsDict['blockHash'] = temp[0][4]
detailsDict['incorporationDate'] = temp[0][5]
if temp[0][6]:
detailsDict['expiryDate'] = temp[0][6]
if temp[0][7]:
detailsDict['closeDate'] = temp[0][7]
# check if the contract has been closed
contractDbName = '{}-{}.db'.format(detailsDict['contractName'].strip(), detailsDict['contractAddress'].strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName)
if os.path.isfile(filelocation):
# Make db connection and fetch data
conn = sqlite3.connect(filelocation)
c = conn.cursor()
c.execute(
'SELECT attribute,value FROM contractstructure')
result = c.fetchall()
contractStructure = {}
conditionDict = {}
counter = 0
for item in result:
if list(item)[0] == 'exitconditions':
conditionDict[counter] = list(item)[1]
counter = counter + 1
else:
contractStructure[list(item)[0]] = list(item)[1]
if len(conditionDict) > 0:
contractStructure['exitconditions'] = conditionDict
del counter, conditionDict
if 'exitconditions' in contractStructure:
# contract is of the type external trigger
# check if the contract has been closed
c.execute(
'select * from contractTransactionHistory where transactionType="trigger"')
trigger = c.fetchall()
if detailsDict['status'] == 'closed':
c.execute(
f"SELECT userChoice, winningAmount FROM contractparticipants where participantAddress='{floAddress}'")
result = c.fetchall()
conn.close()
detailsDict['userChoice'] = result[0][0]
detailsDict['winningAmount'] = result[0][1]
else:
c.execute(
f"SELECT userChoice FROM contractparticipants where participantAddress='{floAddress}'")
result = c.fetchall()
conn.close()
detailsDict['userChoice'] = result[0][0]
participationDetailsList.append(detailsDict)
return jsonify(result='ok', floAddress=floAddress, type='participant', participatedContracts=participationDetailsList)
else:
return jsonify(result='error', description='Address hasn\'t participated in any other contract')
else: else:
return jsonify(result='error', description='System error. System db is missing') return jsonify(result='error', description='System error. System db is missing')
@ -806,14 +951,15 @@ async def getsmartcontracttransactions():
contractName = request.args.get('contractName') contractName = request.args.get('contractName')
contractAddress = request.args.get('contractAddress') contractAddress = request.args.get('contractAddress')
pdb.set_trace()
if contractName is None: if contractName is None:
return jsonify(result='error', description='Smart Contract\'s name hasn\'t been passed') return jsonify(result='error', description='Smart Contract\'s name hasn\'t been passed')
if contractAddress is None: if contractAddress is None:
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(), contractDbName = '{}-{}.db'.format(contractName.strip(), contractAddress.strip())
contractAddress.strip())
filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName) filelocation = os.path.join(dbfolder, 'smartContracts', contractDbName)
if os.path.isfile(filelocation): if os.path.isfile(filelocation):
@ -862,6 +1008,85 @@ async def gettransactiondetails(transactionHash):
return jsonify(result='error', description='Transaction doesn\'t exist in database') return jsonify(result='error', description='Transaction doesn\'t exist in database')
@app.route('/api/v2.0/getTransactionDetails/<transactionHash>', methods=['GET'])
async def gettransactiondetails1(transactionHash):
transactionJsonData = transactiondetailhelper(transactionHash)
if len(transactionJsonData) != 0:
transactionJson = json.loads(transactionJsonData[0][0])
parseResult = json.loads(transactionJsonData[0][1])
operation = transactionJsonData[0][2]
db_reference = transactionJsonData[0][3]
operationDetails = {}
if operation == 'smartContractDeposit':
# open the db reference and check if there is a deposit return
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db")
c = conn.cursor()
c.execute("SELECT depositAmount, blockNumber FROM contractdeposits WHERE status='deposit-return' AND transactionHash=?",(transactionJson['txid'],))
returned_deposit_tx = c.fetchall()
if len(returned_deposit_tx) == 1:
operationDetails['returned_depositAmount'] = returned_deposit_tx[0][0]
operationDetails['returned_blockNumber'] = returned_deposit_tx[0][1]
c.execute("SELECT depositAmount, blockNumber FROM contractdeposits WHERE status='deposit-honor' AND transactionHash=?",(transactionJson['txid'],))
deposit_honors = c.fetchall()
operationDetails['depositHonors'] = {}
operationDetails['depositHonors']['list'] = []
operationDetails['depositHonors']['count'] = len(deposit_honors)
for deposit_honor in deposit_honors:
operationDetails['depositHonors']['list'].append({'honor_amount':deposit_honor[0],'blockNumber':deposit_honor[1]})
c.execute("SELECT depositBalance FROM contractdeposits WHERE id=(SELECT max(id) FROM contractdeposits WHERE transactionHash=?)",(transactionJson['txid'],))
depositBalance = c.fetchall()
operationDetails['depositBalance'] = depositBalance[0][0]
operationDetails['consumedAmount'] = parseResult['depositAmount'] - operationDetails['depositBalance']
elif operation == 'tokenswap-participation':
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db")
c = conn.cursor()
c.execute('SELECT tokenAmount, winningAmount, userChoice FROM contractparticipants WHERE transactionHash=?',(transactionJson['txid'],))
swap_amounts = c.fetchall()
c.execute("SELECT value FROM contractstructure WHERE attribute='selling_token'")
structure = c.fetchall()
operationDetails['participationAmount'] = swap_amounts[0][0]
operationDetails['receivedAmount'] = swap_amounts[0][1]
operationDetails['participationToken'] = parseResult['tokenIdentification']
operationDetails['receivedToken'] = structure[0][0]
operationDetails['swapPrice_received_to_participation'] = float(swap_amounts[0][2])
elif operation == 'smartContractPays':
pdb.set_trace()
# Find what happened because of the trigger
# Find who
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db")
c = conn.cursor()
c.execute('SELECT participantAddress, tokenAmount, userChoice, winningAmount FROM contractparticipants WHERE winningAmount IS NOT NULL')
winner_participants = c.fetchall()
if len(winner_participants) != 0:
operationDetails['total_winners'] = len(winner_participants)
operationDetails['winning_choice'] = winner_participants[0][2]
operationDetails['winner_list'] = []
for participant in winner_participants:
winner_details = {}
winner_details['participantAddress'] = participant[0]
winner_details['participationAmount'] = participant[1]
winner_details['winningAmount'] = participant[3]
operationDetails['winner_list'].append(winner_details)
elif operation == 'ote-externaltrigger-participation':
# Find if this guy has won
conn = sqlite3.connect(f"{dbfolder}/smartContracts/{db_reference}.db")
c = conn.cursor()
c.execute('SELECT winningAmount FROM contractparticipants WHERE transactionHash=?',(transactionHash,))
winningAmount = c.fetchall()
if winningAmount[0][0] is not None:
operationDetails['winningAmount'] = winningAmount[0][0]
return jsonify(parsedFloData=parseResult, transactionDetails=transactionJson, transactionHash=transactionHash, operation=operation, operationDetails=operationDetails, result='ok')
else:
return jsonify(result='error', description='Transaction doesn\'t exist in database')
@app.route('/api/v1.0/getLatestTransactionDetails', methods=['GET']) @app.route('/api/v1.0/getLatestTransactionDetails', methods=['GET'])
async def getLatestTransactionDetails(): async def getLatestTransactionDetails():
@ -885,8 +1110,7 @@ async def getLatestTransactionDetails():
tx_parsed_details['transactionDetails'] = json.loads(item[3]) tx_parsed_details['transactionDetails'] = json.loads(item[3])
tx_parsed_details['parsedFloData'] = json.loads(item[5]) tx_parsed_details['parsedFloData'] = json.loads(item[5])
tx_parsed_details['parsedFloData']['transactionType'] = item[4] tx_parsed_details['parsedFloData']['transactionType'] = item[4]
tx_parsed_details['transactionDetails']['blockheight'] = int( tx_parsed_details['transactionDetails']['blockheight'] = int(item[2])
item[2])
tempdict[json.loads(item[3])['txid']] = tx_parsed_details tempdict[json.loads(item[3])['txid']] = tx_parsed_details
else: else:
c.execute('''SELECT * FROM latestTransactions WHERE blockNumber IN (SELECT DISTINCT blockNumber FROM latestTransactions ORDER BY blockNumber DESC LIMIT 100) ORDER BY id ASC;''') c.execute('''SELECT * FROM latestTransactions WHERE blockNumber IN (SELECT DISTINCT blockNumber FROM latestTransactions ORDER BY blockNumber DESC LIMIT 100) ORDER BY id ASC;''')
@ -899,8 +1123,7 @@ async def getLatestTransactionDetails():
tx_parsed_details['transactionDetails'] = json.loads(item[3]) tx_parsed_details['transactionDetails'] = json.loads(item[3])
tx_parsed_details['parsedFloData'] = json.loads(item[5]) tx_parsed_details['parsedFloData'] = json.loads(item[5])
tx_parsed_details['parsedFloData']['transactionType'] = item[4] tx_parsed_details['parsedFloData']['transactionType'] = item[4]
tx_parsed_details['transactionDetails']['blockheight'] = int( tx_parsed_details['transactionDetails']['blockheight'] = int(item[2])
item[2])
tempdict[json.loads(item[3])['txid']] = tx_parsed_details tempdict[json.loads(item[3])['txid']] = tx_parsed_details
return jsonify(result='ok', latestTransactions=tempdict) return jsonify(result='ok', latestTransactions=tempdict)
@ -1076,7 +1299,8 @@ class ServerSentEvent:
return message.encode('utf-8') return message.encode('utf-8')
"""@app.route('/', methods=['GET']) """
@app.route('/', methods=['GET'])
async def index(): async def index():
return await render_template('index.html') return await render_template('index.html')
@ -1086,7 +1310,8 @@ async def broadcast():
data = await request.get_json() data = await request.get_json()
for queue in app.clients: for queue in app.clients:
await queue.put(data['message']) await queue.put(data['message'])
return jsonify(True) """ return jsonify(True)
"""
@app.route('/sse') @app.route('/sse')
@ -1117,7 +1342,6 @@ async def sse():
@app.route('/api/v1.0/getPrices', methods=['GET']) @app.route('/api/v1.0/getPrices', methods=['GET'])
async def getPriceData(): async def getPriceData():
# read system.db for price data # read system.db for price data
conn = sqlite3.connect('system.db') conn = sqlite3.connect('system.db')
c = conn.cursor() c = conn.cursor()
@ -1133,7 +1357,6 @@ async def getPriceData():
''' Stuff required for getPrices endpoint ''' ''' Stuff required for getPrices endpoint '''
def updatePrices(): def updatePrices():
prices = {} prices = {}
# USD -> INR # USD -> INR
@ -1151,7 +1374,7 @@ def updatePrices():
# FLO->USD | FLO->INR # FLO->USD | FLO->INR
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"] = 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'])
# 3. update latest price data # 3. update latest price data
@ -1192,4 +1415,4 @@ scheduler.start()
atexit.register(lambda: scheduler.shutdown()) atexit.register(lambda: scheduler.shutdown())
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=5009) app.run(debug=True, host='0.0.0.0', port=5009)