Cleaned redundancy in v2/participantDetails
Reffering issue https://github.com/ranchimall/ranchimallflo-api/issues/14 * Removed redundancy in fetching contractStructure * Removed redundncy in connecting to system.db and contract's database
This commit is contained in:
parent
646e874d68
commit
b6f123b15c
@ -1866,11 +1866,11 @@ 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()
|
||||||
|
|
||||||
dblocation = os.path.join(dbfolder, 'system.db')
|
systemdb_location = os.path.join(dbfolder, 'system.db')
|
||||||
if os.path.isfile(dblocation):
|
if os.path.isfile(systemdb_location):
|
||||||
# Make db connection and fetch data
|
# Make db connection and fetch data
|
||||||
conn = sqlite3.connect(dblocation)
|
systemdb_conn = sqlite3.connect(systemdb_location)
|
||||||
c = conn.cursor()
|
c = systemdb_conn.cursor()
|
||||||
if contractName is not None:
|
if contractName is not None:
|
||||||
c.execute(f'SELECT * FROM contractAddressMapping WHERE address="{floAddress}" AND addressType="participant" AND contractName="{contractName}" AND contractAddress="{contractAddress}"')
|
c.execute(f'SELECT * FROM contractAddressMapping WHERE address="{floAddress}" AND addressType="participant" AND contractName="{contractName}" AND contractAddress="{contractAddress}"')
|
||||||
else:
|
else:
|
||||||
@ -1883,35 +1883,23 @@ async def participantDetails(floAddress):
|
|||||||
detailsDict = {}
|
detailsDict = {}
|
||||||
contract_db = os.path.join(dbfolder, 'smartContracts', f"{contract[3]}-{contract[4]}.db")
|
contract_db = os.path.join(dbfolder, '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)
|
contractdb_conn = sqlite3.connect(contract_db)
|
||||||
c = conn.cursor()
|
contract_c = contractdb_conn.cursor()
|
||||||
# Get details of the type of Smart Contract
|
# Get details of the type of Smart Contract
|
||||||
c.execute('SELECT attribute,value FROM contractstructure')
|
contract_c.execute('SELECT attribute,value FROM contractstructure')
|
||||||
result = c.fetchall()
|
result = contract_c.fetchall()
|
||||||
|
|
||||||
contractStructure = {}
|
contractStructure = fetchContractStructure(contract[3], contract[4])
|
||||||
conditionDict = {}
|
contractDbName = '{}-{}.db'.format(contract[3], contract[4])
|
||||||
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':
|
if contractStructure['contractType']=='continuos-event' and contractStructure['subtype']=='tokenswap':
|
||||||
# normal result + swap details
|
# normal result + swap details
|
||||||
# what is a api detail
|
# what is a api detail
|
||||||
c.execute('SELECT * FROM contractparticipants WHERE participantAddress=?',(floAddress,))
|
contract_c.execute('SELECT * FROM contractparticipants WHERE participantAddress=?',(floAddress,))
|
||||||
participant_details = c.fetchall()
|
participant_details = contract_c.fetchall()
|
||||||
if len(participant_details) > 0:
|
if len(participant_details) > 0:
|
||||||
participationList = []
|
participationList = []
|
||||||
for participation in participant_details:
|
for participation in participant_details:
|
||||||
c.execute("SELECT value FROM contractstructure WHERE attribute='selling_token'")
|
|
||||||
structure = c.fetchall()
|
|
||||||
detailsDict['participationAddress'] = floAddress
|
detailsDict['participationAddress'] = floAddress
|
||||||
detailsDict['participationAmount'] = participation[2]
|
detailsDict['participationAmount'] = participation[2]
|
||||||
detailsDict['receivedAmount'] = float(participation[3])
|
detailsDict['receivedAmount'] = float(participation[3])
|
||||||
@ -1926,8 +1914,6 @@ async def participantDetails(floAddress):
|
|||||||
|
|
||||||
elif contractStructure['contractType']=='one-time-event' and 'payeeAddress' in contractStructure.keys():
|
elif contractStructure['contractType']=='one-time-event' and 'payeeAddress' in contractStructure.keys():
|
||||||
# normal results
|
# normal results
|
||||||
conn = sqlite3.connect(dblocation)
|
|
||||||
c = conn.cursor()
|
|
||||||
detailsDict = {}
|
detailsDict = {}
|
||||||
detailsDict['contractName'] = contract[3]
|
detailsDict['contractName'] = contract[3]
|
||||||
detailsDict['contractAddress'] = contract[4]
|
detailsDict['contractAddress'] = contract[4]
|
||||||
@ -1948,39 +1934,17 @@ 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
|
||||||
contractDbName = '{}-{}.db'.format(detailsDict['contractName'], detailsDict['contractAddress'])
|
|
||||||
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
|
|
||||||
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 'payeeAddress' in contractStructure:
|
if 'payeeAddress' 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(f"SELECT tokenAmount FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
contract_c.execute(f"SELECT tokenAmount FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
||||||
result = c.fetchall()
|
result = contract_c.fetchall()
|
||||||
conn.close()
|
|
||||||
detailsDict['tokenAmount'] = result[0][0]
|
detailsDict['tokenAmount'] = result[0][0]
|
||||||
|
|
||||||
elif contractStructure['contractType']=='one-time-event' and 'exitconditions' in contractStructure.keys():
|
elif contractStructure['contractType']=='one-time-event' and 'exitconditions' in contractStructure.keys():
|
||||||
# normal results + winning/losing details
|
# normal results + winning/losing details
|
||||||
conn = sqlite3.connect(dblocation)
|
|
||||||
c = conn.cursor()
|
|
||||||
detailsDict = {}
|
detailsDict = {}
|
||||||
detailsDict['contractName'] = contract[3]
|
detailsDict['contractName'] = contract[3]
|
||||||
detailsDict['contractAddress'] = contract[4]
|
detailsDict['contractAddress'] = contract[4]
|
||||||
@ -2001,14 +1965,11 @@ 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
|
||||||
contractDbName = '{}-{}.db'.format(detailsDict['contractName'], detailsDict['contractAddress'])
|
|
||||||
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)
|
contract_c.execute('SELECT attribute,value FROM contractstructure')
|
||||||
c = conn.cursor()
|
result = contract_c.fetchall()
|
||||||
c.execute('SELECT attribute,value FROM contractstructure')
|
|
||||||
result = c.fetchall()
|
|
||||||
contractStructure = {}
|
contractStructure = {}
|
||||||
conditionDict = {}
|
conditionDict = {}
|
||||||
counter = 0
|
counter = 0
|
||||||
@ -2025,19 +1986,14 @@ async def participantDetails(floAddress):
|
|||||||
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('SELECT * FROM contractTransactionHistory WHERE transactionType="trigger"')
|
|
||||||
trigger = c.fetchall()
|
|
||||||
|
|
||||||
if detailsDict['status'] == 'closed':
|
if detailsDict['status'] == 'closed':
|
||||||
c.execute(f"SELECT userChoice, winningAmount FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
contract_c.execute(f"SELECT userChoice, winningAmount FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
||||||
result = c.fetchall()
|
result = contract_c.fetchall()
|
||||||
conn.close()
|
|
||||||
detailsDict['userChoice'] = result[0][0]
|
detailsDict['userChoice'] = result[0][0]
|
||||||
detailsDict['winningAmount'] = result[0][1]
|
detailsDict['winningAmount'] = result[0][1]
|
||||||
else:
|
else:
|
||||||
c.execute(f"SELECT userChoice FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
contract_c.execute(f"SELECT userChoice FROM contractparticipants WHERE participantAddress='{floAddress}'")
|
||||||
result = c.fetchall()
|
result = contract_c.fetchall()
|
||||||
conn.close()
|
|
||||||
detailsDict['userChoice'] = result[0][0]
|
detailsDict['userChoice'] = result[0][0]
|
||||||
participationDetailsList.append(detailsDict)
|
participationDetailsList.append(detailsDict)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user