2209 lines
160 KiB
Python
Executable File
2209 lines
160 KiB
Python
Executable File
import argparse
|
|
import configparser
|
|
import json
|
|
import logging
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import pyflo
|
|
import requests
|
|
import socketio
|
|
from sqlalchemy import create_engine, func
|
|
from sqlalchemy.orm import sessionmaker
|
|
import time
|
|
import arrow
|
|
import parsing
|
|
from config import *
|
|
from datetime import datetime
|
|
from ast import literal_eval
|
|
from models import SystemData, TokenBase, ActiveTable, ConsumedTable, TransferLogs, TransactionHistory, TokenContractAssociation, ContractBase, ContractStructure, ContractParticipants, ContractTransactionHistory, ContractDeposits, ConsumedInfo, ContractWinners, ContinuosContractBase, ContractStructure2, ContractParticipants2, ContractDeposits2, ContractTransactionHistory2, SystemBase, ActiveContracts, SystemData, ContractAddressMapping, TokenAddressMapping, DatabaseTypeMapping, TimeActions, RejectedContractTransactionHistory, RejectedTransactionHistory, LatestCacheBase, LatestTransactions, LatestBlocks
|
|
from statef_processing import process_stateF
|
|
import pdb
|
|
|
|
goodblockset = {}
|
|
goodtxset = {}
|
|
|
|
|
|
def newMultiRequest(apicall):
|
|
current_server = serverlist[0]
|
|
while True:
|
|
try:
|
|
response = requests.get('{}api/{}'.format(current_server, apicall))
|
|
except:
|
|
current_server = switchNeturl(current_server)
|
|
logger.info(f"newMultiRequest() switched to {current_server}")
|
|
time.sleep(2)
|
|
else:
|
|
if response.status_code == 200:
|
|
return json.loads(response.content)
|
|
else:
|
|
current_server = switchNeturl(current_server)
|
|
logger.info(f"newMultiRequest() switched to {current_server}")
|
|
time.sleep(2)
|
|
|
|
|
|
def pushData_SSEapi(message):
|
|
signature = pyflo.sign_message(message.encode(), privKey)
|
|
headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Signature': signature}
|
|
|
|
'''try:
|
|
r = requests.post(sseAPI_url, json={'message': '{}'.format(message)}, headers=headers)
|
|
except:
|
|
logger.error("couldn't push the following message to SSE api {}".format(message))'''
|
|
print('')
|
|
|
|
|
|
def check_database_existence(type, parameters):
|
|
if type == 'token':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'tokens', f'{parameters["token_name"]}.db')
|
|
return os.path.isfile(path)
|
|
|
|
if type == 'smart_contract':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'smartContracts', f"{parameters['contract_name']}-{parameters['contract_address']}.db")
|
|
return os.path.isfile(path)
|
|
|
|
|
|
def create_database_connection(type, parameters):
|
|
if type == 'token':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'tokens', f"{parameters['token_name']}.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=True)
|
|
elif type == 'smart_contract':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'smartContracts', f"{parameters['contract_name']}-{parameters['contract_address']}.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=True)
|
|
elif type == 'system_dbs':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], f"system.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=False)
|
|
elif type == 'latest_cache':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], f"latestCache.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=False)
|
|
|
|
connection = engine.connect()
|
|
return connection
|
|
|
|
|
|
def create_database_session_orm(type, parameters, base):
|
|
if type == 'token':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'tokens', f"{parameters['token_name']}.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=True)
|
|
base.metadata.create_all(bind=engine)
|
|
session = sessionmaker(bind=engine)()
|
|
|
|
elif type == 'smart_contract':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], 'smartContracts', f"{parameters['contract_name']}-{parameters['contract_address']}.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=True)
|
|
base.metadata.create_all(bind=engine)
|
|
session = sessionmaker(bind=engine)()
|
|
|
|
elif type == 'system_dbs':
|
|
path = os.path.join(config['DEFAULT']['DATA_PATH'], f"{parameters['db_name']}.db")
|
|
engine = create_engine(f"sqlite:///{path}", echo=False)
|
|
base.metadata.create_all(bind=engine)
|
|
session = sessionmaker(bind=engine)()
|
|
|
|
return session
|
|
|
|
|
|
def convert_datetime_to_arrowobject(expiryTime):
|
|
expirytime_split = expiryTime.split(' ')
|
|
parse_string = '{}/{}/{} {}'.format(expirytime_split[3], parsing.months[expirytime_split[1]],
|
|
expirytime_split[2], expirytime_split[4])
|
|
expirytime_object = parsing.arrow.get(parse_string, 'YYYY/M/D HH:mm:ss').replace(tzinfo=expirytime_split[5][3:])
|
|
return expirytime_object
|
|
|
|
|
|
def is_a_contract_address(floAddress):
|
|
# check contract address mapping db if the address is present, and return True or False based on that
|
|
system_db = create_database_session_orm('system_dbs', {'db_name':'system'}, SystemBase)
|
|
contract_number = system_db.query(func.sum(ContractAddressMapping.contractAddress)).filter(ContractAddressMapping.contractAddress == floAddress).all()[0][0]
|
|
if contract_number is None:
|
|
return False
|
|
else:
|
|
return True
|
|
|
|
|
|
def processBlock(blockindex=None, blockhash=None):
|
|
if blockindex is not None and blockhash is None:
|
|
logger.info(f'Processing block {blockindex}')
|
|
# Get block details
|
|
response = newMultiRequest(f"block-index/{blockindex}")
|
|
blockhash = response['blockHash']
|
|
|
|
blockinfo = newMultiRequest(f"block/{blockhash}")
|
|
|
|
pause_index = 2291750
|
|
if blockindex == pause_index:
|
|
print(f'Paused at {blockindex}')
|
|
#sys.exit(0)
|
|
# Check smartContracts which will be triggered locally, and not by the contract committee
|
|
#checkLocaltriggerContracts(blockinfo)
|
|
# Check if any deposits have to be returned
|
|
#checkReturnDeposits(blockinfo)
|
|
checkLocal_expiry_trigger_deposit(blockinfo)
|
|
|
|
# todo Rule 8 - read every transaction from every block to find and parse flodata
|
|
counter = 0
|
|
acceptedTxList = []
|
|
# Scan every transaction
|
|
logger.info("Before tx loop")
|
|
transaction_counter = 0
|
|
|
|
for transaction in blockinfo["tx"]:
|
|
transaction_counter = transaction_counter + 1
|
|
logger.info(f"Transaction {transaction_counter} : {transaction}")
|
|
current_index = -1
|
|
|
|
if transaction in ['8eed51ae47575fd78413f9be5a7909cf2653b6fedacf093e35b592319e478b21']:
|
|
pdb.set_trace()
|
|
|
|
# TODO CLEANUP - REMOVE THIS WHILE SECTION, WHY IS IT HERE?
|
|
while(current_index == -1):
|
|
transaction_data = newMultiRequest(f"tx/{transaction}")
|
|
try:
|
|
text = transaction_data["floData"]
|
|
# TODO CLEANUP - REMOVE TEXT STUB AFTER TESTING
|
|
'''text = Create Smart Contract with the name swap-bitcoin-bioscope@ of the type continuous-event*
|
|
at the address stateF=address:oPkHWcvqBHfCortTHScrVBjXLsZhWie99C:bitcoin_price_source:bitpay:usd_inr_exchange_source:bitpay end-stateF oYzeeUBWRpzRuczW6myh2LHGnXPyR2Bc6k$ with contract-conditions :
|
|
(1) subtype = tokenswap
|
|
(2) accepting_token = bitcoin#
|
|
(3) selling_token = bioscope#
|
|
(4) price = "15"
|
|
(5) priceType="statef" end-contract-conditions'''
|
|
text = text.replace("\n", " \n ")
|
|
current_index = 2
|
|
except:
|
|
logger.info("The API has passed the Block height test but failed transaction_data['floData'] test")
|
|
logger.info(f"Block Height : {blockinfo['height']}")
|
|
logger.info(f"Transaction {transaction} data : ")
|
|
logger.info(transaction_data)
|
|
logger.info('Program will wait for 1 seconds and try to reconnect')
|
|
time.sleep(1)
|
|
|
|
# todo Rule 9 - Reject all noise transactions. Further rules are in parsing.py
|
|
returnval = None
|
|
parsed_data = parsing.parse_flodata(text, blockinfo, config['DEFAULT']['NET'])
|
|
if parsed_data['type'] != 'noise':
|
|
logger.info(f"Processing transaction {transaction}")
|
|
logger.info(f"flodata {text} is parsed to {parsed_data}")
|
|
returnval = processTransaction(transaction_data, parsed_data, blockinfo)
|
|
|
|
if returnval == 1:
|
|
acceptedTxList.append(transaction)
|
|
elif returnval == 0:
|
|
logger.info("Transfer for the transaction %s is illegitimate. Moving on" % transaction)
|
|
|
|
if len(acceptedTxList) > 0:
|
|
tempinfo = blockinfo['tx'].copy()
|
|
for tx in blockinfo['tx']:
|
|
if tx not in acceptedTxList:
|
|
tempinfo.remove(tx)
|
|
blockinfo['tx'] = tempinfo
|
|
updateLatestBlock(blockinfo)
|
|
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
entry = session.query(SystemData).filter(SystemData.attribute == 'lastblockscanned').all()[0]
|
|
entry.value = str(blockinfo['height'])
|
|
session.commit()
|
|
session.close()
|
|
|
|
|
|
def updateLatestTransaction(transactionData, parsed_data, db_reference, transaction_type=None ):
|
|
# connect to latest transaction db
|
|
conn = create_database_connection('latest_cache', {'db_name':"latestCache"})
|
|
if transaction_type is None:
|
|
transaction_type = parsed_data['type']
|
|
conn.execute("INSERT INTO latestTransactions(transactionHash, blockNumber, jsonData, transactionType, parsedFloData, db_reference) VALUES (?,?,?,?,?,?)", (transactionData['txid'], transactionData['blockheight'], json.dumps(transactionData), transaction_type, json.dumps(parsed_data), db_reference))
|
|
#conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def updateLatestBlock(blockData):
|
|
# connect to latest block db
|
|
conn = create_database_connection('latest_cache', {'db_name':"latestCache"})
|
|
conn.execute('INSERT INTO latestBlocks(blockNumber, blockHash, jsonData) VALUES (?,?,?)', (blockData['height'], blockData['hash'], json.dumps(blockData)))
|
|
#conn.commit()
|
|
conn.close()
|
|
|
|
|
|
def process_pids(entries, session, piditem):
|
|
for entry in entries:
|
|
'''consumedpid_dict = literal_eval(entry.consumedpid)
|
|
total_consumedpid_amount = 0
|
|
for key in consumedpid_dict.keys():
|
|
total_consumedpid_amount = total_consumedpid_amount + float(consumedpid_dict[key])
|
|
consumedpid_dict[piditem[0]] = total_consumedpid_amount
|
|
entry.consumedpid = str(consumedpid_dict)'''
|
|
entry.orphaned_parentid = entry.parentid
|
|
entry.parentid = None
|
|
#session.commit()
|
|
return 1
|
|
|
|
|
|
def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress, transaction_data=None, parsed_data=None, isInfiniteToken=None, blockinfo=None):
|
|
session = create_database_session_orm('token', {'token_name': f"{tokenIdentification}"}, TokenBase)
|
|
|
|
if isInfiniteToken == True:
|
|
# Make new entry
|
|
session.add(ActiveTable(address=outputAddress, consumedpid='1', transferBalance=float(tokenAmount), blockNumber=blockinfo['height']))
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
|
transferAmount=tokenAmount, blockNumber=blockinfo['height'],
|
|
blockHash=blockinfo['hash'], time=blockinfo['time'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
return 1
|
|
|
|
else:
|
|
availableTokens = session.query(func.sum(ActiveTable.transferBalance)).filter_by(address=inputAddress).all()[0][0]
|
|
commentTransferAmount = float(tokenAmount)
|
|
if availableTokens is None:
|
|
logger.info(f"The sender address {inputAddress} doesn't own any {tokenIdentification.upper()} tokens")
|
|
session.close()
|
|
return 0
|
|
|
|
elif availableTokens < commentTransferAmount:
|
|
logger.info("The transfer amount passed in the comments is more than the user owns\nThis transaction will be discarded\n")
|
|
session.close()
|
|
return 0
|
|
|
|
elif availableTokens >= commentTransferAmount:
|
|
table = session.query(ActiveTable).filter(ActiveTable.address == inputAddress).all()
|
|
pidlst = []
|
|
checksum = 0
|
|
for row in table:
|
|
if checksum >= commentTransferAmount:
|
|
break
|
|
pidlst.append([row.id, row.transferBalance])
|
|
checksum = checksum + row.transferBalance
|
|
|
|
if checksum == commentTransferAmount:
|
|
consumedpid_string = ''
|
|
# Update all pids in pidlist's transferBalance to 0
|
|
lastid = session.query(ActiveTable)[-1].id
|
|
piddict = {}
|
|
for piditem in pidlst:
|
|
entry = session.query(ActiveTable).filter(ActiveTable.id == piditem[0]).all()
|
|
consumedpid_string = consumedpid_string + '{},'.format(piditem[0])
|
|
piddict[piditem[0]] = piditem[1]
|
|
session.add(TransferLogs(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
|
transferAmount=entry[0].transferBalance, sourceId=piditem[0],
|
|
destinationId=lastid + 1,
|
|
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
|
transactionHash=transaction_data['txid']))
|
|
entry[0].transferBalance = 0
|
|
|
|
if len(consumedpid_string) > 1:
|
|
consumedpid_string = consumedpid_string[:-1]
|
|
|
|
# Make new entry
|
|
receiverAddress_details = session.query(ActiveTable).filter(ActiveTable.address==outputAddress, ActiveTable.addressBalance!=None).first()
|
|
if receiverAddress_details is None:
|
|
addressBalance = commentTransferAmount
|
|
else:
|
|
addressBalance = receiverAddress_details.addressBalance + commentTransferAmount
|
|
receiverAddress_details.addressBalance = None
|
|
session.add(ActiveTable(address=outputAddress, consumedpid=str(piddict), transferBalance=commentTransferAmount, addressBalance=addressBalance, blockNumber=blockinfo['height']))
|
|
|
|
senderAddress_details = session.query(ActiveTable).filter_by(address=inputAddress).order_by(ActiveTable.id.desc()).first()
|
|
senderAddress_details.addressBalance = senderAddress_details.addressBalance - commentTransferAmount
|
|
|
|
# Migration
|
|
# shift pid of used utxos from active to consumed
|
|
for piditem in pidlst:
|
|
# move the parentids consumed to consumedpid column in both activeTable and consumedTable
|
|
entries = session.query(ActiveTable).filter(ActiveTable.parentid == piditem[0]).all()
|
|
process_pids(entries, session, piditem)
|
|
|
|
entries = session.query(ConsumedTable).filter(ConsumedTable.parentid == piditem[0]).all()
|
|
process_pids(entries, session, piditem)
|
|
|
|
# move the pids consumed in the transaction to consumedTable and delete them from activeTable
|
|
session.execute('INSERT INTO consumedTable (id, address, parentid, consumedpid, transferBalance, addressBalance, orphaned_parentid, blockNumber) SELECT id, address, parentid, consumedpid, transferBalance, addressBalance, orphaned_parentid, blockNumber FROM activeTable WHERE id={}'.format(piditem[0]))
|
|
session.execute('DELETE FROM activeTable WHERE id={}'.format(piditem[0]))
|
|
session.commit()
|
|
session.commit()
|
|
|
|
elif checksum > commentTransferAmount:
|
|
consumedpid_string = ''
|
|
# Update all pids in pidlist's transferBalance
|
|
lastid = session.query(ActiveTable)[-1].id
|
|
piddict = {}
|
|
for idx, piditem in enumerate(pidlst):
|
|
entry = session.query(ActiveTable).filter(ActiveTable.id == piditem[0]).all()
|
|
if idx != len(pidlst) - 1:
|
|
session.add(TransferLogs(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
|
transferAmount=entry[0].transferBalance, sourceId=piditem[0],
|
|
destinationId=lastid + 1,
|
|
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
|
transactionHash=transaction_data['txid']))
|
|
entry[0].transferBalance = 0
|
|
piddict[piditem[0]] = piditem[1]
|
|
consumedpid_string = consumedpid_string + '{},'.format(piditem[0])
|
|
else:
|
|
session.add(TransferLogs(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
|
transferAmount=piditem[1] - (checksum - commentTransferAmount),
|
|
sourceId=piditem[0],
|
|
destinationId=lastid + 1,
|
|
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
|
transactionHash=transaction_data['txid']))
|
|
entry[0].transferBalance = checksum - commentTransferAmount
|
|
|
|
if len(consumedpid_string) > 1:
|
|
consumedpid_string = consumedpid_string[:-1]
|
|
|
|
# Make new entry
|
|
receiverAddress_details = session.query(ActiveTable).filter(ActiveTable.address==outputAddress, ActiveTable.addressBalance!=None).first()
|
|
if receiverAddress_details is None:
|
|
addressBalance = commentTransferAmount
|
|
else:
|
|
addressBalance = receiverAddress_details.addressBalance + commentTransferAmount
|
|
receiverAddress_details.addressBalance = None
|
|
session.add(ActiveTable(address=outputAddress, parentid=pidlst[-1][0], consumedpid=str(piddict), transferBalance=commentTransferAmount, addressBalance=addressBalance, blockNumber=blockinfo['height']))
|
|
|
|
senderAddress_details = session.query(ActiveTable).filter_by(address=inputAddress).order_by(ActiveTable.id.desc()).first()
|
|
senderAddress_details.addressBalance = senderAddress_details.addressBalance - commentTransferAmount
|
|
|
|
# Migration
|
|
# shift pid of used utxos from active to consumed
|
|
for piditem in pidlst[:-1]:
|
|
# move the parentids consumed to consumedpid column in both activeTable and consumedTable
|
|
entries = session.query(ActiveTable).filter(ActiveTable.parentid == piditem[0]).all()
|
|
process_pids(entries, session, piditem)
|
|
|
|
entries = session.query(ConsumedTable).filter(ConsumedTable.parentid == piditem[0]).all()
|
|
process_pids(entries, session, piditem)
|
|
|
|
# move the pids consumed in the transaction to consumedTable and delete them from activeTable
|
|
session.execute('INSERT INTO consumedTable (id, address, parentid, consumedpid, transferBalance, addressBalance, orphaned_parentid, blockNumber) SELECT id, address, parentid, consumedpid, transferBalance, addressBalance, orphaned_parentid, blockNumber FROM activeTable WHERE id={}'.format(piditem[0]))
|
|
session.execute('DELETE FROM activeTable WHERE id={}'.format(piditem[0]))
|
|
session.commit()
|
|
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress,transferAmount=tokenAmount, blockNumber=blockinfo['height'], blockHash=blockinfo['hash'], time=blockinfo['time'], transactionHash=transaction_data['txid'], blockchainReference=blockchainReference, jsonData=json.dumps(transaction_data), transactionType=parsed_data['type'], parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
return 1
|
|
|
|
|
|
def trigger_internal_contract(tokenAmount_sum, contractStructure, transaction_data, blockinfo, parsed_data, connection, contract_name, contract_address, transaction_subType):
|
|
# Trigger the contract
|
|
if tokenAmount_sum <= 0:
|
|
# Add transaction to ContractTransactionHistory
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{contract_name}", 'contract_address': f"{contract_address}"}, ContractBase)
|
|
session.add(ContractTransactionHistory(transactionType='trigger',
|
|
transactionSubType='zero-participation',
|
|
sourceFloAddress='',
|
|
destFloAddress='',
|
|
transferAmount=0,
|
|
blockNumber=blockinfo['height'],
|
|
blockHash=blockinfo['hash'],
|
|
time=blockinfo['time']))
|
|
session.commit()
|
|
session.close()
|
|
else:
|
|
payeeAddress = json.loads(contractStructure['payeeAddress'])
|
|
tokenIdentification = contractStructure['tokenIdentification']
|
|
|
|
for floaddress in payeeAddress.keys():
|
|
transferAmount = tokenAmount_sum * (payeeAddress[floaddress]/100)
|
|
returnval = transferToken(tokenIdentification, transferAmount, contract_address, floaddress, transaction_data=transaction_data, blockinfo = blockinfo, parsed_data = parsed_data)
|
|
if returnval == 0:
|
|
logger.critical("Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
|
return 0
|
|
|
|
# Add transaction to ContractTransactionHistory
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{contract_name}", 'contract_address': f"{contract_address}"}, ContractBase)
|
|
session.add(ContractTransactionHistory(transactionType='trigger',
|
|
transactionSubType=transaction_subType,
|
|
sourceFloAddress=contract_address,
|
|
destFloAddress=floaddress,
|
|
transferAmount=transferAmount,
|
|
blockNumber=blockinfo['height'],
|
|
blockHash=blockinfo['hash'],
|
|
time=blockinfo['time']))
|
|
session.commit()
|
|
session.close()
|
|
|
|
return 1
|
|
|
|
|
|
def process_minimum_subscriptionamount(contractStructure, connection):
|
|
minimumsubscriptionamount = float(contractStructure['minimumsubscriptionamount'])
|
|
tokenAmount_sum = connection.execute('select IFNULL(sum(tokenAmount), 0) from contractparticipants').fetchall()[0][0]
|
|
if tokenAmount_sum < minimumsubscriptionamount:
|
|
# Initialize payback to contract participants
|
|
contractParticipants = connection.execute('select participantAddress, tokenAmount, transactionHash from contractparticipants').fetchall()[0][0]
|
|
|
|
for participant in contractParticipants:
|
|
tokenIdentification = contractStructure['tokenIdentification']
|
|
contractAddress = connection.execute('select * from contractstructure where attribute="contractAddress"').fetchall()[0][0]
|
|
returnval = transferToken(tokenIdentification, participant[1], contractAddress, participant[0], blockinfo = blockinfo)
|
|
if returnval is None:
|
|
logger.critical("Something went wrong in the token transfer method while doing local Smart Contract Trigger. THIS IS CRITICAL ERROR")
|
|
return
|
|
connection.execute('update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format((participant[1], participant[0], participant[2])))
|
|
|
|
# add transaction to ContractTransactionHistory
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{contractStructure['contractName']}", 'contract_address': f"{contractStructure['contractAddress']}"}, ContractBase)
|
|
session.add(ContractTransactionHistory(transactionType='trigger',
|
|
transactionSubType='minimumsubscriptionamount-payback',
|
|
transferAmount=None,
|
|
blockNumber=blockinfo['height'],
|
|
blockHash=blockinfo['hash'],
|
|
time=blockinfo['time']))
|
|
session.commit()
|
|
session.close()
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
|
|
def process_maximum_subscriptionamount(contractStructure, connection, status):
|
|
maximumsubscriptionamount = float(contractStructure['maximumsubscriptionamount'])
|
|
if tokenAmount_sum >= maximumsubscriptionamount:
|
|
# Trigger the contract
|
|
if status == 'close':
|
|
success_returnval = trigger_internal_contract(tokenAmount_sum, contractStructure, transaction_data, blockinfo, parsed_data, connection, contract_name=query.contractName, contract_address=query.contractAddress, transaction_subType='maximumsubscriptionamount')
|
|
if not success_returnval:
|
|
return 0
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('update activecontracts set status="closed", closeDate="{}", expiryDate="{}" where contractName="{}" and contractAddress="{}"'.format(blockinfo['time'], blockinfo['time'], query.contractName, query.contractAddress))
|
|
connection.execute('update time_actions set status="closed" where contractName="{}" and contractAddress="{}"'.format(query.contractName, query.contractAddress))
|
|
connection.close()
|
|
return 1
|
|
else:
|
|
return 0
|
|
|
|
def check_contract_status(connection, session, contractName, contractAddress):
|
|
# Status of the contract is at 2 tables in system.db
|
|
# activecontracts and time_actions
|
|
# select the last entry form the colum
|
|
contract_status = connection.execute('SELECT status FROM time_actions WHERE id=(SELECT MAX(id) FROM time_actions WHERE contractName="" AND contractAddress="")').fetchall()
|
|
return contract_status[0][0]
|
|
|
|
|
|
def close_expire_contract(contractStructure, contractStatus, transactionHash, blockNumber, blockHash, incorporationDate, expiryDate, closeDate, trigger_time, trigger_activity, contractName, contractAddress, contractType, tokens_db, parsed_data, blockHeight):
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], contractStatus, contractStructure['tokenIdentification'], contractStructure['contractType'], transactionHash, blockNumber, blockHash, incorporationDate, expiryDate, closeDate))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, trigger_time, trigger_activity, contractStatus, contractName, contractAddress, contractType, tokens_db, parsed_data, transactionHash, blockHeight))
|
|
connection.close()
|
|
|
|
|
|
def return_active_contracts(session):
|
|
# find all the contracts which are active
|
|
contract_ids = session.query(func.max(TimeActions.id)).group_by(TimeActions.contractName, TimeActions.contractAddress).all()
|
|
if len(contract_ids) == 0:
|
|
return []
|
|
active_contracts = session.query(TimeActions).filter(TimeActions.id.in_(contract_ids[0]), TimeActions.status=='active', TimeActions.activity=='contract-time-trigger').all()
|
|
return active_contracts
|
|
|
|
|
|
def return_active_deposits(session):
|
|
# find all the deposits which are active
|
|
# todo - sqlalchemy gives me warning with the following method
|
|
subquery_filter = session.query(TimeActions.id).group_by(TimeActions.transactionHash).having(func.count(TimeActions.transactionHash)==1).subquery()
|
|
active_deposits = session.query(TimeActions).filter(TimeActions.id.in_(subquery_filter), TimeActions.status=='active', TimeActions.activity=='contract-deposit').all()
|
|
return active_deposits
|
|
|
|
|
|
def checkLocal_expiry_trigger_deposit(blockinfo):
|
|
# Connect to system.db with a session
|
|
systemdb_session = create_database_session_orm('system_dbs', {'db_name':'system'}, SystemBase)
|
|
timeactions_tx_hashes = []
|
|
active_contracts = return_active_contracts(systemdb_session)
|
|
active_deposits = return_active_deposits(systemdb_session)
|
|
|
|
for query in active_contracts:
|
|
query_time = convert_datetime_to_arrowobject(query.time)
|
|
blocktime = parsing.arrow.get(blockinfo['time']).to('Asia/Kolkata')
|
|
if query.activity == 'contract-deposit':
|
|
if blocktime > query_time:
|
|
# find the status of the deposit
|
|
# the deposit is unique
|
|
# find the total sum to be returned from the smart contract's participation table
|
|
contract_db = create_database_session_orm('smart_contract', {'contract_name': query.contractName, 'contract_address': query.contractAddress}, ContractBase)
|
|
deposit_query = contract_db.query(ContractDeposits).filter(ContractDeposits.transactionHash == query.transactionHash).first()
|
|
depositorAddress = deposit_query.depositorAddress
|
|
total_deposit_amount = deposit_query.depositAmount
|
|
amount_participated = contract_db.query(func.sum(ContractParticipants.tokenAmount)).all()[0][0]
|
|
if amount_participated is None:
|
|
amount_participated = 0
|
|
returnAmount = float(total_deposit_amount) - float(amount_participated)
|
|
# Do a token transfer back to the deposit address
|
|
sellingToken = contract_db.query(ContractStructure.value).filter(ContractStructure.attribute == 'selling_token').first()[0]
|
|
tx_block_string = f"{query.transactionHash}{blockinfo['height']}".encode('utf-8').hex()
|
|
parsed_data = {}
|
|
parsed_data['type'] = 'expired_deposit'
|
|
transaction_data = {}
|
|
#transaction_data['txid'] = pyflo.sha256(tx_block_string).hex()
|
|
transaction_data['txid'] = query.transactionHash
|
|
transaction_data['blockheight'] = blockinfo['height']
|
|
returnval = transferToken(sellingToken, returnAmount, query.contractAddress, depositorAddress, transaction_data=transaction_data, parsed_data=parsed_data, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.critical("Something went wrong in the token transfer method while return contract deposit. THIS IS CRITICAL ERROR")
|
|
return
|
|
else:
|
|
old_depositBalance = contract_db.query(ContractDeposits.depositBalance).order_by(ContractDeposits.id.desc()).first()
|
|
if old_depositBalance is None:
|
|
logger.info('Something is wrong in the databases. Cannot do a deposit return without any previously available deposits in the database')
|
|
return 0
|
|
else:
|
|
old_depositBalance = old_depositBalance[0]
|
|
|
|
contract_db.add(ContractDeposits(
|
|
depositorAddress = deposit_query.depositorAddress,
|
|
depositAmount = returnAmount,
|
|
depositBalance = old_depositBalance - returnAmount,
|
|
expiryTime = deposit_query.expiryTime,
|
|
unix_expiryTime = 0,
|
|
status = 'deposit-return',
|
|
transactionHash = deposit_query.transactionHash,
|
|
blockNumber = blockinfo['height'],
|
|
blockHash = blockinfo['hash']
|
|
))
|
|
|
|
contract_db.add(ContractTransactionHistory(
|
|
transactionType = 'smartContractDepositReturn',
|
|
transactionSubType = '',
|
|
sourceFloAddress = query.contractAddress,
|
|
destFloAddress = depositorAddress,
|
|
transferAmount = returnAmount,
|
|
blockNumber = blockinfo['height'],
|
|
blockHash = blockinfo['hash'],
|
|
time = blockinfo['time'],
|
|
transactionHash = deposit_query.transactionHash,
|
|
blockchainReference = '',
|
|
jsonData = '',
|
|
parsedFloData = ''
|
|
))
|
|
|
|
systemdb_session.add(TimeActions(
|
|
time = query.time,
|
|
activity = query.activity,
|
|
status = 'returned',
|
|
contractName = query.contractName,
|
|
contractAddress = query.contractAddress,
|
|
contractType = query.contractType,
|
|
tokens_db = query.tokens_db,
|
|
parsed_data = query.parsed_data,
|
|
transactionHash = query.transactionHash,
|
|
blockNumber = blockinfo['height']
|
|
))
|
|
|
|
contract_db.commit()
|
|
systemdb_session.commit()
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{query.contractName}-{query.contractAddress}")
|
|
|
|
elif query.activity == 'contract-time-trigger':
|
|
contractStructure = extract_contractStructure(query.contractName, query.contractAddress)
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{query.contractName}", 'contract_address':f"{query.contractAddress}"})
|
|
if contractStructure['contractType'] == 'one-time-event':
|
|
if 'exitconditions' in contractStructure: # Committee trigger contract type
|
|
tokenAmount_sum = connection.execute('select IFNULL(sum(tokenAmount), 0) from contractparticipants').fetchall()[0][0]
|
|
# maximumsubscription check, if reached then expire the contract
|
|
if 'maximumsubscriptionamount' in contractStructure:
|
|
maximumsubscriptionamount = connection.execute('select value from contractstructure where attribute=="maximumsubscriptionamount"').fetchall()[0][0]
|
|
maximumsubscriptionamount = float(maximumsubscriptionamount)
|
|
if tokenAmount_sum >= maximumsubscriptionamount:
|
|
# Expire the contract
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'expired', contractStructure['tokenIdentification'], contractStructure['contractType'], query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', 'query.expiryDate', 'query.closeDate'))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, query.time, query.activity, 'expired', query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, query.transactionHash, blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'expired', query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', 'query.expiryDate', 'query.closeDate', query.time, query.activity, query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, blockinfo['height'])
|
|
|
|
if blocktime > query_time:
|
|
if 'minimumsubscriptionamount' in contractStructure:
|
|
if process_minimum_subscriptionamount(contractStructure, connection):
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'closed', contractStructure['tokenIdentification'], contractStructure['contractType'], query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time']))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, query.time, query.activity, 'closed', query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, query.transactionHash, blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'closed', query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time'], query.time, query.activity, query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, blockinfo['height'])
|
|
return
|
|
|
|
# Expire the contract
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'expired', contractStructure['tokenIdentification'], contractStructure['contractType'], query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], 'query.closeDate'))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, query.time, query.activity, 'expired', query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, query.transactionHash, blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'expired', query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], 'query.closeDate', query.time, query.activity, query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, blockinfo['height'])
|
|
|
|
elif 'payeeAddress' in contractStructure: # Internal trigger contract type
|
|
# TODO - FIGURE A BETTER SOLUTION FOR THIS
|
|
transaction_data = {}
|
|
transaction_data['txid'] = 'internalTrigger'
|
|
parsed_data = {}
|
|
parsed_data['type'] = 'internalTrigger'
|
|
|
|
tokenAmount_sum = connection.execute('select IFNULL(sum(tokenAmount), 0) from contractparticipants').fetchall()[0][0]
|
|
# maximumsubscription check, if reached then trigger the contract
|
|
if 'maximumsubscriptionamount' in contractStructure:
|
|
maximumsubscriptionamount = connection.execute('select value from contractstructure where attribute=="maximumsubscriptionamount"').fetchall()[0][0]
|
|
maximumsubscriptionamount = float(maximumsubscriptionamount)
|
|
if tokenAmount_sum >= maximumsubscriptionamount:
|
|
# Trigger the contract
|
|
success_returnval = trigger_internal_contract(tokenAmount_sum, contractStructure, transaction_data, blockinfo, parsed_data, connection, contract_name=query.contractName, contract_address=query.contractAddress, transaction_subType='maximumsubscriptionamount')
|
|
if not success_returnval:
|
|
return 0
|
|
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('update activecontracts set status="closed", closeDate="{}", expiryDate="{}" where contractName="{}" and contractAddress="{}"'.format(blockinfo['time'], blockinfo['time'], query.contractName, query.contractAddress))
|
|
connection.execute('update time_actions set status="closed" where contractName="{}" and contractAddress="{}"'.format(query.contractName, query.contractAddress))
|
|
connection.close()
|
|
return
|
|
|
|
if blocktime > query_time:
|
|
if 'minimumsubscriptionamount' in contractStructure:
|
|
if process_minimum_subscriptionamount(contractStructure, connection):
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'closed', contractStructure['tokenIdentification'], contractStructure['contractType'], query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time']))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, query.time, query.activity, 'closed', query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, query.transactionHash, blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'closed', query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time'], query.time, query.activity, query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, blockinfo['height'])
|
|
return
|
|
|
|
# Trigger the contract
|
|
success_returnval = trigger_internal_contract(tokenAmount_sum, contractStructure, transaction_data, blockinfo, parsed_data, connection, contract_name=query.contractName, contract_address=query.contractAddress, transaction_subType='expiryTime')
|
|
if not success_returnval:
|
|
return 0
|
|
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'expired', contractStructure['tokenIdentification'], contractStructure['contractType'], query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time']))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None,query.time, query.activity, 'expired', query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, query.transactionHash, blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'expired', query.transactionHash, query.blockNumber, 'query.blockHash', 'query.incorporationDate', blockinfo['time'], blockinfo['time'], query.time, query.activity, query.contractName, query.contractAddress, query.contractType, query.tokens_db, query.parsed_data, blockinfo['height'])
|
|
return
|
|
|
|
|
|
def extract_contractStructure(contractName, contractAddress):
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{contractName}", 'contract_address':f"{contractAddress}"})
|
|
attributevaluepair = connection.execute("select attribute, value from contractstructure where attribute != 'flodata'").fetchall()
|
|
contractStructure = {}
|
|
conditionDict = {}
|
|
counter = 0
|
|
for item in attributevaluepair:
|
|
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
|
|
|
|
return contractStructure
|
|
|
|
|
|
def rejected_transaction_history(transaction_data, parsed_data, sourceFloAddress, destFloAddress, rejectComment):
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, TokenBase)
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(RejectedTransactionHistory(tokenIdentification=parsed_data['tokenIdentification'],
|
|
sourceFloAddress=sourceFloAddress, destFloAddress=destFloAddress,
|
|
transferAmount=parsed_data['tokenAmount'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
rejectComment=rejectComment,
|
|
transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
|
|
def rejected_contract_transaction_history(transaction_data, parsed_data, transactionType, contractAddress, sourceFloAddress, destFloAddress, rejectComment):
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(RejectedContractTransactionHistory(transactionType=transactionType,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=contractAddress,
|
|
sourceFloAddress=sourceFloAddress,
|
|
destFloAddress=destFloAddress,
|
|
transferAmount=None,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
rejectComment=rejectComment,
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
|
|
|
|
def processTransaction(transaction_data, parsed_data, blockinfo):
|
|
# Do the necessary checks for the inputs and outputs
|
|
# todo Rule 38 - Here we are doing FLO processing. We attach asset amounts to a FLO address, so every FLO address
|
|
# will have multiple feed ins of the asset. Each of those feedins will be an input to the address.
|
|
# an address can also spend the asset. Each of those spends is an output of that address feeding the asset into some
|
|
# other address an as input
|
|
# Rule 38 reframe - For checking any asset transfer on the flo blockchain it is possible that some transactions may use more than one
|
|
# vins. However in any single transaction the system considers valid, they can be only one source address from which the flodata is
|
|
# originting. To ensure consistency, we will have to check that even if there are more than one vins in a transaction, there should be
|
|
# exactly one FLO address on the originating side and that FLO address should be the owner of the asset tokens being transferred
|
|
|
|
|
|
# Create vinlist and outputlist
|
|
vinlist = []
|
|
querylist = []
|
|
|
|
#totalinputval = 0
|
|
#inputadd = ''
|
|
|
|
# todo Rule 40 - For each vin, find the feeding address and the fed value. Make an inputlist containing [inputaddress, n value]
|
|
for vin in transaction_data["vin"]:
|
|
vinlist.append([vin["addr"], float(vin["value"])])
|
|
|
|
totalinputval = float(transaction_data["valueIn"])
|
|
|
|
# todo Rule 41 - Check if all the addresses in a transaction on the input side are the same
|
|
for idx, item in enumerate(vinlist):
|
|
if idx == 0:
|
|
temp = item[0]
|
|
continue
|
|
if item[0] != temp:
|
|
logger.info(f"System has found more than one address as part of vin. Transaction {transaction_data['txid']} is rejected")
|
|
return 0
|
|
|
|
inputlist = [vinlist[0][0], totalinputval]
|
|
inputadd = vinlist[0][0]
|
|
|
|
# todo Rule 42 - If the number of vout is more than 2, reject the transaction
|
|
if len(transaction_data["vout"]) > 2:
|
|
logger.info(f"System has found more than 2 address as part of vout. Transaction {transaction_data['txid']} is rejected")
|
|
return 0
|
|
|
|
# todo Rule 43 - A transaction accepted by the system has two vouts, 1. The FLO address of the receiver
|
|
# 2. Flo address of the sender as change address. If the vout address is change address, then the other adddress
|
|
# is the recevier address
|
|
|
|
outputlist = []
|
|
addresscounter = 0
|
|
inputcounter = 0
|
|
for obj in transaction_data["vout"]:
|
|
if obj["scriptPubKey"]["type"] == "pubkeyhash":
|
|
addresscounter = addresscounter + 1
|
|
if inputlist[0] == obj["scriptPubKey"]["addresses"][0]:
|
|
inputcounter = inputcounter + 1
|
|
continue
|
|
outputlist.append([obj["scriptPubKey"]["addresses"][0], obj["value"]])
|
|
|
|
if addresscounter == inputcounter:
|
|
outputlist = [inputlist[0]]
|
|
elif len(outputlist) != 1:
|
|
logger.info(f"Transaction's change is not coming back to the input address. Transaction {transaction_data['txid']} is rejected")
|
|
return 0
|
|
else:
|
|
outputlist = outputlist[0]
|
|
|
|
logger.info(f"Input address list : {inputlist}")
|
|
logger.info(f"Output address list : {outputlist}")
|
|
|
|
# All FLO checks completed at this point.
|
|
# Semantic rules for parsed data begins
|
|
|
|
# todo Rule 44 - Process as per the type of transaction
|
|
if parsed_data['type'] == 'transfer':
|
|
logger.info(f"Transaction {transaction_data['txid']} is of the type transfer")
|
|
|
|
# todo Rule 45 - If the transfer type is token, then call the function transferToken to adjust the balances
|
|
if parsed_data['transferType'] == 'token':
|
|
if not is_a_contract_address(inputlist[0]) and not is_a_contract_address(outputlist[0]):
|
|
# check if the token exists in the database
|
|
if check_database_existence('token', {'token_name':f"{parsed_data['tokenIdentification']}"}):
|
|
# Pull details of the token type from system.db database
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
db_details = connection.execute("select db_name, db_type, keyword, object_format from databaseTypeMapping where db_name='{}'".format(parsed_data['tokenIdentification']))
|
|
db_details = list(zip(*db_details))
|
|
if db_details[1][0] == 'infinite-token':
|
|
db_object = json.loads(db_details[3][0])
|
|
if db_object['root_address'] == inputlist[0]:
|
|
isInfiniteToken = True
|
|
else:
|
|
isInfiniteToken = False
|
|
else:
|
|
isInfiniteToken = False
|
|
|
|
# Check if the transaction hash already exists in the token db
|
|
connection = create_database_connection('token', {'token_name':f"{parsed_data['tokenIdentification']}"})
|
|
blockno_txhash = connection.execute('select blockNumber, transactionHash from transactionHistory').fetchall()
|
|
connection.close()
|
|
blockno_txhash_T = list(zip(*blockno_txhash))
|
|
|
|
if transaction_data['txid'] in list(blockno_txhash_T[1]):
|
|
logger.warning(f"Transaction {transaction_data['txid']} already exists in the token db. This is unusual, please check your code")
|
|
pushData_SSEapi(f"Error | Transaction {transaction_data['txid']} already exists in the token db. This is unusual, please check your code")
|
|
return 0
|
|
|
|
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'], inputlist[0],outputlist[0], transaction_data, parsed_data, isInfiniteToken=isInfiniteToken, blockinfo = blockinfo)
|
|
if returnval is None:
|
|
logger.info("Something went wrong in the token transfer method")
|
|
pushData_SSEapi(f"Error | Something went wrong while doing the internal db transactions for {transaction_data['txid']}")
|
|
return 0
|
|
else:
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['tokenIdentification']}", transaction_type='token-transfer')
|
|
|
|
# If this is the first interaction of the outputlist's address with the given token name, add it to token mapping
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
firstInteractionCheck = connection.execute(f"select * from tokenAddressMapping where tokenAddress='{outputlist[0]}' and token='{parsed_data['tokenIdentification']}'").fetchall()
|
|
|
|
if len(firstInteractionCheck) == 0:
|
|
connection.execute(f"INSERT INTO tokenAddressMapping (tokenAddress, token, transactionHash, blockNumber, blockHash) VALUES ('{outputlist[0]}', '{parsed_data['tokenIdentification']}', '{transaction_data['txid']}', '{transaction_data['blockheight']}', '{transaction_data['blockhash']}')")
|
|
|
|
connection.close()
|
|
|
|
# Pass information to SSE channel
|
|
headers = {'Accept': 'application/json', 'Content-Type': 'application/json'}
|
|
# r = requests.post(tokenapi_sse_url, json={f"message': 'Token Transfer | name:{parsed_data['tokenIdentification']} | transactionHash:{transaction_data['txid']}"}, headers=headers)
|
|
return 1
|
|
else:
|
|
rejectComment = f"Token transfer at transaction {transaction_data['txid']} rejected as a token with the name {parsed_data['tokenIdentification']} doesnt not exist"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"Token transfer at transaction {transaction_data['txid']} rejected as either the input address or the output address is part of a contract address"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# todo Rule 46 - If the transfer type is smart contract, then call the function transferToken to do sanity checks & lock the balance
|
|
elif parsed_data['transferType'] == 'smartContract':
|
|
if check_database_existence('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"}):
|
|
# Check type of contract and categorize between into ote-participation or continuous-event participation
|
|
# todo - replace all connection queries with session queries
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"})
|
|
contract_session = create_database_session_orm('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"}, ContractBase)
|
|
contract_type = contract_session.query(ContractStructure.value).filter(ContractStructure.attribute == 'contractType').first()[0]
|
|
|
|
contractStructure = extract_contractStructure(parsed_data['contractName'], outputlist[0])
|
|
|
|
if contract_type == 'one-time-event':
|
|
# Check if the transaction hash already exists in the contract db (Safety check)
|
|
participantAdd_txhash = connection.execute('select participantAddress, transactionHash from contractparticipants').fetchall()
|
|
participantAdd_txhash_T = list(zip(*participantAdd_txhash))
|
|
|
|
if len(participantAdd_txhash) != 0 and transaction_data['txid'] in list(participantAdd_txhash_T[1]):
|
|
logger.warning(f"Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
pushData_SSEapi(f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
return 0
|
|
|
|
# if contractAddress was passed, then check if it matches the output address of this contract
|
|
if 'contractAddress' in parsed_data:
|
|
if parsed_data['contractAddress'] != outputlist[0]:
|
|
rejectComment = f"Contract participation at transaction {transaction_data['txid']} rejected as contractAddress specified in flodata, {parsed_data['contractAddress']}, doesnt not match with transaction's output address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
# Store transfer as part of RejectedContractTransactionHistory
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
# Pass information to SSE channel
|
|
pushData_SSEapi(f"Error| Mismatch in contract address specified in flodata and the output address of the transaction {transaction_data['txid']}")
|
|
return 0
|
|
|
|
# check the status of the contract
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
contractStatus = connection.execute(f"select status from activecontracts where contractName=='{parsed_data['contractName']}' and contractAddress='{outputlist[0]}'").fetchall()[0][0]
|
|
connection.close()
|
|
contractList = []
|
|
|
|
if contractStatus == 'closed':
|
|
rejectComment = f"Transaction {transaction_data['txid']} closed as Smart contract {parsed_data['contractName']} at the {outputlist[0]} is closed"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
else:
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
result = session.query(ContractStructure).filter_by(attribute='expiryTime').all()
|
|
session.close()
|
|
if result:
|
|
# now parse the expiry time in python
|
|
expirytime = result[0].value.strip()
|
|
expirytime_split = expirytime.split(' ')
|
|
parse_string = '{}/{}/{} {}'.format(expirytime_split[3], parsing.months[expirytime_split[1]], expirytime_split[2], expirytime_split[4])
|
|
expirytime_object = parsing.arrow.get(parse_string, 'YYYY/M/D HH:mm:ss').replace(tzinfo=expirytime_split[5][3:])
|
|
blocktime_object = parsing.arrow.get(transaction_data['blocktime']).to('Asia/Kolkata')
|
|
|
|
if blocktime_object > expirytime_object:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart contract {parsed_data['contractName']}-{outputlist[0]} has expired and will not accept any user participation"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check if user choice has been passed, to the wrong contract type
|
|
if 'userChoice' in parsed_data and 'exitconditions' not in contractStructure:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as userChoice, {parsed_data['userChoice']}, has been passed to Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} which doesn't accept any userChoice"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check if the right token is being sent for participation
|
|
if parsed_data['tokenIdentification'] != contractStructure['tokenIdentification']:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as the token being transferred, {parsed_data['tokenIdentidication'].upper()}, is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# Check if contractAmount is part of the contract structure, and enforce it if it is
|
|
if 'contractAmount' in contractStructure:
|
|
if float(contractStructure['contractAmount']) != float(parsed_data['tokenAmount']):
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as contractAmount being transferred is not part of the structure of Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
partialTransferCounter = 0
|
|
# Check if maximum subscription amount has reached
|
|
if 'maximumsubscriptionamount' in contractStructure:
|
|
# now parse the expiry time in python
|
|
maximumsubscriptionamount = float(contractStructure['maximumsubscriptionamount'])
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
amountDeposited = session.query(func.sum(ContractParticipants.tokenAmount)).all()[0][0]
|
|
session.close()
|
|
if amountDeposited is None:
|
|
amountDeposited = 0
|
|
|
|
if amountDeposited >= maximumsubscriptionamount:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as maximum subscription amount has been reached for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
elif ((float(amountDeposited) + float(parsed_data['tokenAmount'])) > maximumsubscriptionamount):
|
|
if 'contractAmount' in contractStructure:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as the contractAmount surpasses the maximum subscription amount, {contractStructure['maximumsubscriptionamount']} {contractStructure['tokenIdentification'].upper()}, for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
else:
|
|
partialTransferCounter = 1
|
|
|
|
# Check if exitcondition exists as part of contractstructure and is given in right format
|
|
if 'exitconditions' in contractStructure:
|
|
# This means the contract has an external trigger, ie. trigger coming from the contract committee
|
|
exitconditionsList = []
|
|
for condition in contractStructure['exitconditions']:
|
|
exitconditionsList.append(contractStructure['exitconditions'][condition])
|
|
|
|
if parsed_data['userChoice'] in exitconditionsList:
|
|
if partialTransferCounter == 0:
|
|
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
|
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'], inputlist[0], outputlist[0], transaction_data, parsed_data, blockinfo = blockinfo)
|
|
if returnval is not None:
|
|
# Store participant details in the smart contract's db
|
|
session.add(ContractParticipants(participantAddress=inputadd,
|
|
tokenAmount=parsed_data['tokenAmount'],
|
|
userChoice=parsed_data['userChoice'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.commit()
|
|
|
|
# Store transfer as part of ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(ContractTransactionHistory(transactionType='participation',
|
|
sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Store a mapping of participant address -> Contract participated in
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
|
tokenAmount=parsed_data['tokenAmount'],
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=outputlist[0],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.commit()
|
|
|
|
# If this is the first interaction of the outputlist's address with the given token name, add it to token mapping
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
firstInteractionCheck = connection.execute(f"select * from tokenAddressMapping where tokenAddress='{outputlist[0]}' and token='{parsed_data['tokenIdentification']}'").fetchall()
|
|
if len(firstInteractionCheck) == 0:
|
|
connection.execute(f"INSERT INTO tokenAddressMapping (tokenAddress, token, transactionHash, blockNumber, blockHash) VALUES ('{outputlist[0]}', '{parsed_data['tokenIdentification']}', '{transaction_data['txid']}', '{transaction_data['blockheight']}', '{transaction_data['blockhash']}')")
|
|
connection.close()
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{outputlist[0]}", transaction_type='ote-externaltrigger-participation')
|
|
return 1
|
|
|
|
else:
|
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
|
return 0
|
|
elif partialTransferCounter == 1:
|
|
# Transfer only part of the tokens users specified, till the time it reaches maximumamount
|
|
returnval = transferToken(parsed_data['tokenIdentification'], maximumsubscriptionamount - amountDeposited, inputlist[0], outputlist[0], transaction_data, parsed_data, blockinfo = blockinfo)
|
|
if returnval is not None:
|
|
# Store participant details in the smart contract's db
|
|
session.add(ContractParticipants(participantAddress=inputadd,
|
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
|
userChoice=parsed_data['userChoice'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Store a mapping of participant address -> Contract participated in
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
|
tokenAmount=maximumsubscriptionamount - amountDeposited,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=outputlist[0],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.commit()
|
|
session.close()
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{outputlist[0]}", transaction_type='ote-externaltrigger-participation')
|
|
return 1
|
|
|
|
else:
|
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as wrong userchoice entered for the Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
elif 'payeeAddress' in contractStructure:
|
|
# this means the contract is of the type internal trigger
|
|
if partialTransferCounter == 0:
|
|
transferAmount = parsed_data['tokenAmount']
|
|
elif partialTransferCounter == 1:
|
|
transferAmount = maximumsubscriptionamount - amountDeposited
|
|
|
|
# Check if the tokenAmount being transferred exists in the address & do the token transfer
|
|
returnval = transferToken(parsed_data['tokenIdentification'], transferAmount, inputlist[0], outputlist[0], transaction_data, parsed_data, blockinfo = blockinfo)
|
|
if returnval is not None:
|
|
# Store participant details in the smart contract's db
|
|
session.add(ContractParticipants(participantAddress=inputadd,
|
|
tokenAmount=transferAmount,
|
|
userChoice='-',
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
|
|
# Store transfer as part of ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(ContractTransactionHistory(transactionType='participation',
|
|
sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=transferAmount,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Store a mapping of participant address -> Contract participated in
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(ContractAddressMapping(address=inputadd, addressType='participant',
|
|
tokenAmount=transferAmount,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=outputlist[0],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.commit()
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{outputlist[0]}", transaction_type='ote-internaltrigger-participation')
|
|
return 1
|
|
|
|
else:
|
|
logger.info("Something went wrong in the smartcontract token transfer method")
|
|
return 0
|
|
|
|
elif contract_type == 'continuos-event':
|
|
contract_subtype = contract_session.query(ContractStructure.value).filter(ContractStructure.attribute == 'subtype').first()[0]
|
|
if contract_subtype == 'tokenswap':
|
|
# Check if the transaction hash already exists in the contract db (Safety check)
|
|
participantAdd_txhash = connection.execute('select participantAddress, transactionHash from contractparticipants').fetchall()
|
|
participantAdd_txhash_T = list(zip(*participantAdd_txhash))
|
|
|
|
if len(participantAdd_txhash) != 0 and transaction_data['txid'] in list(participantAdd_txhash_T[1]):
|
|
logger.warning(f"Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
pushData_SSEapi(f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
return 0
|
|
|
|
# if contractAddress was passed, then check if it matches the output address of this contract
|
|
if 'contractAddress' in parsed_data:
|
|
if parsed_data['contractAddress'] != outputlist[0]:
|
|
rejectComment = f"Contract participation at transaction {transaction_data['txid']} rejected as contractAddress specified in flodata, {parsed_data['contractAddress']}, doesnt not match with transaction's output address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
# Pass information to SSE channel
|
|
pushData_SSEapi(f"Error| Mismatch in contract address specified in flodata and the output address of the transaction {transaction_data['txid']}")
|
|
return 0
|
|
|
|
if contractStructure['pricetype'] in ['predetermined','determined']:
|
|
swapPrice = float(contractStructure['price'])
|
|
elif contractStructure['pricetype'] == 'dynamic':
|
|
pass
|
|
|
|
swapAmount = float(parsed_data['tokenAmount'])/swapPrice
|
|
|
|
# Check if the swap amount is available in the deposits of the selling token
|
|
# if yes do the transfers, otherwise reject the transaction
|
|
#
|
|
active_contract_deposits = contract_session.query(ContractDeposits).filter(ContractDeposits.status=='active').all()
|
|
|
|
consumed_deposit_ids = contract_session.query(ConsumedInfo.id_deposittable).all()
|
|
|
|
available_deposits = active_contract_deposits[:]
|
|
available_deposit_sum = 0
|
|
|
|
for entry in active_contract_deposits:
|
|
if entry.id in [consumed_deposit_ids] or arrow.get(entry.unix_expiryTime)<arrow.get(blockinfo['time']):
|
|
index = active_contract_deposits.index(entry)
|
|
del available_deposits[index]
|
|
else:
|
|
available_deposit_sum = available_deposit_sum + entry.depositBalance
|
|
|
|
if available_deposit_sum >= swapAmount:
|
|
# accepting token transfer from participant to smart contract address
|
|
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'], inputlist[0], outputlist[0], transaction_data=transaction_data, parsed_data=parsed_data, isInfiniteToken=None, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.info("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Particiaption")
|
|
return 0
|
|
|
|
# ContractDepositTable
|
|
# For each unique deposit( address, expirydate, blocknumber) there will be 2 entries added to the table
|
|
# the consumption of the deposits will start form the top of the table
|
|
deposit_counter = 0
|
|
remaining_amount = swapAmount
|
|
for a_deposit in available_deposits:
|
|
if a_deposit.depositBalance > remaining_amount:
|
|
# accepting token transfer from the contract to depositor's address
|
|
returnval = transferToken(contractStructure['accepting_token'], remaining_amount * swapPrice, contractStructure['contractAddress'], a_deposit.depositorAddress, transaction_data=transaction_data, parsed_data=parsed_data, isInfiniteToken=None, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.info("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Particiaption deposit swap operation")
|
|
return 0
|
|
|
|
contract_session.add(ContractDeposits( depositorAddress= a_deposit.depositorAddress,
|
|
depositAmount= 0 - remaining_amount,
|
|
status='deposit-honor',
|
|
transactionHash= a_deposit.transactionHash,
|
|
blockNumber= blockinfo['height'],
|
|
blockHash= blockinfo['hash']))
|
|
|
|
# if the total is consumsed then the following entry won't take place
|
|
contract_session.add(ContractDeposits( depositorAddress= a_deposit.depositorAddress,
|
|
depositBalance= a_deposit.depositBalance - remaining_amount,
|
|
expiryTime = a_deposit.expiryTime,
|
|
unix_expiryTime = a_deposit.unix_expiryTime,
|
|
status='active',
|
|
transactionHash= a_deposit.transactionHash,
|
|
blockNumber= blockinfo['height'],
|
|
blockHash= blockinfo['hash']))
|
|
|
|
# ConsumedInfoTable
|
|
contract_session.add(ConsumedInfo( id_deposittable= a_deposit.id,
|
|
transactionHash= a_deposit.transactionHash,
|
|
blockNumber= blockinfo['height']))
|
|
remaining_amount = remaining_amount - a_deposit.depositBalance
|
|
remaining_amount = 0
|
|
break
|
|
|
|
elif a_deposit.depositBalance <= remaining_amount:
|
|
# accepting token transfer from the contract to depositor's address
|
|
returnval = transferToken(contractStructure['accepting_token'], a_deposit.depositBalance * swapPrice, contractStructure['contractAddress'], a_deposit.depositorAddress, transaction_data=transaction_data, parsed_data=parsed_data, isInfiniteToken=None, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.info("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Particiaption deposit swap operation")
|
|
return 0
|
|
|
|
contract_session.add(ContractDeposits( depositorAddress= a_deposit.depositorAddress,
|
|
depositAmount= 0 - a_deposit.depositBalance,
|
|
status='deposit-honor',
|
|
transactionHash= a_deposit.transactionHash,
|
|
blockNumber= blockinfo['height'],
|
|
blockHash= blockinfo['hash']))
|
|
|
|
# ConsumedInfoTable
|
|
contract_session.add(ConsumedInfo( id_deposittable= a_deposit.id,
|
|
transactionHash= a_deposit.transactionHash,
|
|
blockNumber= blockinfo['height']))
|
|
remaining_amount = remaining_amount - a_deposit.depositBalance
|
|
|
|
|
|
# token transfer from the contract to participant's address
|
|
returnval = transferToken(contractStructure['selling_token'], swapAmount, outputlist[0], inputlist[0], transaction_data=transaction_data, parsed_data=parsed_data, isInfiniteToken=None, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.info("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Particiaption")
|
|
return 0
|
|
|
|
# ContractParticipationTable
|
|
contract_session.add(ContractParticipants( participantAddress = parsed_data['contractAddress'],
|
|
tokenAmount= parsed_data['tokenAmount'],
|
|
userChoice= swapPrice,
|
|
transactionHash= transaction_data['txid'],
|
|
blockNumber= blockinfo['height'],
|
|
blockHash= blockinfo['hash'],
|
|
winningAmount = swapAmount))
|
|
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
contract_session.add(ContractTransactionHistory( transactionType = 'participation',
|
|
transactionSubType = 'swap',
|
|
sourceFloAddress = inputlist[0],
|
|
destFloAddress = outputlist[0],
|
|
transferAmount = swapAmount,
|
|
blockNumber = blockinfo['height'],
|
|
blockHash = blockinfo['hash'],
|
|
time = blockinfo['time'],
|
|
transactionHash = transaction_data['txid'],
|
|
blockchainReference = blockchainReference,
|
|
jsonData = json.dumps(transaction_data),
|
|
parsedFloData = json.dumps(parsed_data)))
|
|
contract_session.commit()
|
|
contract_session.close()
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{outputlist[0]}", transaction_type='tokenswap-participation')
|
|
pushData_SSEapi(f"Token swap successfully performed at contract {parsed_data['contractName']}-{outputlist[0]} with the transaction {transaction_data['txid']}")
|
|
|
|
else:
|
|
# Reject the participation saying not enough deposit tokens are available
|
|
rejectComment = f"Swap participation at transaction {transaction_data['txid']} rejected as requested swap amount is {swapAmount} but {available_deposit_sum} is available"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as the participation doesn't belong to any valid contract type"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as a Smart Contract with the name {parsed_data['contractName']} at address {outputlist[0]} doesnt exist"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
elif parsed_data['transferType'] == 'nft':
|
|
pass
|
|
|
|
# todo Rule 47 - If the parsed data type is token incorporation, then check if the name hasn't been taken already
|
|
# if it has been taken then reject the incorporation. Else incorporate it
|
|
elif parsed_data['type'] == 'tokenIncorporation':
|
|
if not is_a_contract_address(inputlist[0]):
|
|
if not check_database_existence('token', {'token_name':f"{parsed_data['tokenIdentification']}"}):
|
|
session = create_database_session_orm('token', {'token_name': f"{parsed_data['tokenIdentification']}"}, TokenBase)
|
|
session.add(ActiveTable(address=inputlist[0], parentid=0, transferBalance=parsed_data['tokenAmount'], addressBalance=parsed_data['tokenAmount'], blockNumber=blockinfo['height']))
|
|
session.add(TransferLogs(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'], sourceId=0, destinationId=1,
|
|
blockNumber=transaction_data['blockheight'], time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid']))
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(TransactionHistory(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data), transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# add it to token address to token mapping db table
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute(f"INSERT INTO tokenAddressMapping (tokenAddress, token, transactionHash, blockNumber, blockHash) VALUES ('{inputadd}', '{parsed_data['tokenIdentification']}', '{transaction_data['txid']}', '{transaction_data['blockheight']}', '{transaction_data['blockhash']}');")
|
|
connection.execute(f"INSERT INTO databaseTypeMapping (db_name, db_type, keyword, object_format, blockNumber) VALUES ('{parsed_data['tokenIdentification']}', 'token', '', '', '{transaction_data['blockheight']}')")
|
|
connection.close()
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['tokenIdentification']}")
|
|
pushData_SSEapi(f"Token | Successfully incorporated token {parsed_data['tokenIdentification']} at transaction {transaction_data['txid']}")
|
|
return 1
|
|
else:
|
|
rejectComment = f"Token incorporation rejected at transaction {transaction_data['txid']} as token {parsed_data['tokenIdentification']} already exists"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
else:
|
|
rejectComment = f"Token incorporation at transaction {transaction_data['txid']} rejected as either the input address is part of a contract address"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# todo Rule 48 - If the parsed data type if smart contract incorporation, then check if the name hasn't been taken already
|
|
# if it has been taken then reject the incorporation.
|
|
elif parsed_data['type'] == 'smartContractIncorporation':
|
|
if not check_database_existence('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{parsed_data['contractAddress']}"}):
|
|
# todo Rule 49 - If the contract name hasn't been taken before, check if the contract type is an authorized type by the system
|
|
if parsed_data['contractType'] == 'one-time-event':
|
|
logger.info("Smart contract is of the type one-time-event")
|
|
|
|
# either userchoice or payeeAddress condition should be present. Check for it
|
|
if 'userchoices' not in parsed_data['contractConditions'] and 'payeeAddress' not in parsed_data['contractConditions']:
|
|
rejectComment = f"Either userchoice or payeeAddress should be part of the Contract conditions.\nSmart contract incorporation on transaction {transaction_data['txid']} rejected"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
# userchoice and payeeAddress conditions cannot come together. Check for it
|
|
if 'userchoices' in parsed_data['contractConditions'] and 'payeeAddress' in parsed_data['contractConditions']:
|
|
rejectComment = f"Both userchoice and payeeAddress provided as part of the Contract conditions.\nSmart contract incorporation on transaction {transaction_data['txid']} rejected"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
# todo Rule 50 - Contract address mentioned in flodata field should be same as the receiver FLO address on the output side
|
|
# henceforth we will not consider any flo private key initiated comment as valid from this address
|
|
# Unlocking can only be done through smart contract system address
|
|
if parsed_data['contractAddress'] == inputadd:
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{parsed_data['contractAddress']}"}, ContractBase)
|
|
session.add(ContractStructure(attribute='contractType', index=0, value=parsed_data['contractType']))
|
|
session.add(ContractStructure(attribute='contractName', index=0, value=parsed_data['contractName']))
|
|
session.add(ContractStructure(attribute='tokenIdentification', index=0, value=parsed_data['tokenIdentification']))
|
|
session.add(ContractStructure(attribute='contractAddress', index=0, value=parsed_data['contractAddress']))
|
|
session.add(ContractStructure(attribute='flodata', index=0, value=parsed_data['flodata']))
|
|
session.add(ContractStructure(attribute='expiryTime', index=0, value=parsed_data['contractConditions']['expiryTime']))
|
|
if 'contractAmount' in parsed_data['contractConditions'].keys():
|
|
session.add(ContractStructure(attribute='contractAmount', index=0, value=parsed_data['contractConditions']['contractAmount']))
|
|
|
|
if 'minimumsubscriptionamount' in parsed_data['contractConditions']:
|
|
session.add(ContractStructure(attribute='minimumsubscriptionamount', index=0, value=parsed_data['contractConditions']['minimumsubscriptionamount']))
|
|
if 'maximumsubscriptionamount' in parsed_data['contractConditions']:
|
|
session.add(ContractStructure(attribute='maximumsubscriptionamount', index=0, value=parsed_data['contractConditions']['maximumsubscriptionamount']))
|
|
if 'userchoices' in parsed_data['contractConditions']:
|
|
for key, value in literal_eval(parsed_data['contractConditions']['userchoices']).items():
|
|
session.add(ContractStructure(attribute='exitconditions', index=key, value=value))
|
|
|
|
if 'payeeAddress' in parsed_data['contractConditions']:
|
|
# in this case, expirydate( or maximumamount) is the trigger internally. Keep a track of expiry dates
|
|
session.add(ContractStructure(attribute='payeeAddress', index=0, value=json.dumps(parsed_data['contractConditions']['payeeAddress'])))
|
|
|
|
# Store transfer as part of ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(ContractTransactionHistory(transactionType='incorporation', sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=None,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# add Smart Contract name in token contract association
|
|
session = create_database_session_orm('token', {'token_name': f"{parsed_data['tokenIdentification']}"}, TokenBase)
|
|
session.add(TokenContractAssociation(tokenIdentification=parsed_data['tokenIdentification'],
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=parsed_data['contractAddress'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Store smart contract address in system's db, to be ignored during future transfers
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(ActiveContracts(contractName=parsed_data['contractName'],
|
|
contractAddress=parsed_data['contractAddress'], status='active',
|
|
tokenIdentification=parsed_data['tokenIdentification'],
|
|
contractType=parsed_data['contractType'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
incorporationDate=transaction_data['blocktime']))
|
|
session.commit()
|
|
|
|
session.add(ContractAddressMapping(address=inputadd, addressType='incorporation',
|
|
tokenAmount=None,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=inputadd,
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
|
|
session.add(DatabaseTypeMapping(db_name=f"{parsed_data['contractName']}-{inputadd}",
|
|
db_type='smartcontract',
|
|
keyword='',
|
|
object_format='',
|
|
blockNumber=transaction_data['blockheight']))
|
|
|
|
session.add(TimeActions(time=parsed_data['contractConditions']['expiryTime'],
|
|
activity='contract-time-trigger',
|
|
status='active',
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=inputadd,
|
|
contractType='one-time-event-trigger',
|
|
tokens_db=json.dumps([parsed_data['tokenIdentification']]),
|
|
parsed_data=json.dumps(parsed_data),
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight']))
|
|
|
|
session.commit()
|
|
session.close()
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{parsed_data['contractAddress']}")
|
|
|
|
pushData_SSEapi('Contract | Contract incorporated at transaction {} with name {}-{}'.format(transaction_data['txid'], parsed_data['contractName'], parsed_data['contractAddress']))
|
|
return 1
|
|
else:
|
|
rejectComment = f"Contract Incorporation on transaction {transaction_data['txid']} rejected as contract address in Flodata and input address are different"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(f"Error | Contract Incorporation rejected as address in Flodata and input address are different at transaction {transaction_data['txid']}")
|
|
return 0
|
|
|
|
if parsed_data['contractType'] == 'continuous-event' or parsed_data['contractType'] == 'continuos-event':
|
|
logger.debug("Smart contract is of the type continuous-event")
|
|
# Add checks to reject the creation of contract
|
|
if parsed_data['contractAddress'] == inputadd:
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{parsed_data['contractAddress']}"}, ContractBase)
|
|
session.add(ContractStructure(attribute='contractType', index=0, value=parsed_data['contractType']))
|
|
session.add(ContractStructure(attribute='contractName', index=0, value=parsed_data['contractName']))
|
|
session.add(ContractStructure(attribute='contractAddress', index=0, value=parsed_data['contractAddress']))
|
|
session.add(ContractStructure(attribute='flodata', index=0, value=parsed_data['flodata']))
|
|
|
|
if parsed_data['stateF'] != {}:
|
|
for key, value in parsed_data['stateF'].items():
|
|
session.add(ContractStructure(attribute=f'statef-{key}', index=0, value=value))
|
|
|
|
if 'subtype' in parsed_data['contractConditions']:
|
|
# todo: Check if the both the tokens mentioned exist if its a token swap
|
|
#if (parsed_data['contractConditions']['subtype'] == 'tokenswap') and (check_database_existence('token', {'token_name':f"{parsed_data['contractConditions']['accepting_token'].split('#')[0]}"})) and (check_database_existence('token', {'token_name':f"{parsed_data['contractConditions']['selling_token'].split('#')[0]}"})):
|
|
if (parsed_data['contractConditions']['subtype'] == 'tokenswap') and (check_database_existence('token', {'token_name':f"{parsed_data['contractConditions']['selling_token'].split('#')[0]}"})):
|
|
#if (parsed_data['contractConditions']['subtype'] == 'tokenswap'):
|
|
if parsed_data['contractConditions']['pricetype'] in ['predetermined','statef']:
|
|
session.add(ContractStructure(attribute='subtype', index=0, value=parsed_data['contractConditions']['subtype']))
|
|
session.add(ContractStructure(attribute='accepting_token', index=0, value=parsed_data['contractConditions']['accepting_token']))
|
|
session.add(ContractStructure(attribute='selling_token', index=0, value=parsed_data['contractConditions']['selling_token']))
|
|
# determine price
|
|
session.add(ContractStructure(attribute='pricetype', index=0, value=parsed_data['contractConditions']['pricetype']))
|
|
session.add(ContractStructure(attribute='price', index=0, value=parsed_data['contractConditions']['price']))
|
|
|
|
# Store transfer as part of ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(ContractTransactionHistory(transactionType='incorporation',
|
|
sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=None,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# add Smart Contract name in token contract association
|
|
accepting_sending_tokenlist = [parsed_data['contractConditions']['accepting_token'], parsed_data['contractConditions']['selling_token']]
|
|
for token_name in accepting_sending_tokenlist:
|
|
token_name = token_name.split('#')[0]
|
|
if token_name == 'bitcoin':
|
|
continue
|
|
session = create_database_session_orm('token', {'token_name': f"{token_name}"}, TokenBase)
|
|
session.add(TokenContractAssociation(tokenIdentification=token_name,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=parsed_data['contractAddress'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Store smart contract address in system's db, to be ignored during future transfers
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(ActiveContracts(contractName=parsed_data['contractName'],
|
|
contractAddress=parsed_data['contractAddress'], status='active',
|
|
tokenIdentification=str(accepting_sending_tokenlist),
|
|
contractType=parsed_data['contractType'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
incorporationDate=transaction_data['blocktime']))
|
|
session.commit()
|
|
|
|
# todo - Add a condition for rejected contract transaction on the else loop for this condition
|
|
session.add(ContractAddressMapping(address=inputadd, addressType='incorporation',
|
|
tokenAmount=None,
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=inputadd,
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash']))
|
|
session.add(DatabaseTypeMapping(db_name=f"{parsed_data['contractName']}-{inputadd}",
|
|
db_type='smartcontract',
|
|
keyword='',
|
|
object_format='',
|
|
blockNumber=transaction_data['blockheight']))
|
|
session.commit()
|
|
session.close()
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{parsed_data['contractAddress']}")
|
|
pushData_SSEapi('Contract | Contract incorporated at transaction {} with name {}-{}'.format(transaction_data['txid'], parsed_data['contractName'], parsed_data['contractAddress']))
|
|
return 1
|
|
else:
|
|
rejectComment = f"pricetype is not part of accepted parameters for a continuos event contract of the type token swap.\nSmart contract incorporation on transaction {transaction_data['txid']} rejected"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"No subtype provided || mentioned tokens do not exist for the Contract of type continuos event.\nSmart contract incorporation on transaction {transaction_data['txid']} rejected"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
session.commit()
|
|
session.close()
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as a Smart Contract with the name {parsed_data['contractName']} at address {parsed_data['contractAddress']} already exists"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'incorporation', inputadd, inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
elif parsed_data['type'] == 'smartContractPays':
|
|
logger.info(f"Transaction {transaction_data['txid']} is of the type smartContractPays")
|
|
# Check if input address is a committee address
|
|
if inputlist[0] in committeeAddressList:
|
|
# check if the contract exists
|
|
if check_database_existence('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"}):
|
|
# Check if the transaction hash already exists in the contract db (Safety check)
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"})
|
|
participantAdd_txhash = connection.execute(f"select sourceFloAddress, transactionHash from contractTransactionHistory where transactionType != 'incorporation'").fetchall()
|
|
participantAdd_txhash_T = list(zip(*participantAdd_txhash))
|
|
|
|
if len(participantAdd_txhash) != 0 and transaction_data['txid'] in list(participantAdd_txhash_T[1]):
|
|
logger.warning(f"Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
pushData_SSEapi(f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
return 0
|
|
|
|
# pull out the contract structure into a dictionary
|
|
contractStructure = extract_contractStructure(parsed_data['contractName'], outputlist[0])
|
|
|
|
# if contractAddress has been passed, check if output address is contract Incorporation address
|
|
if 'contractAddress' in contractStructure:
|
|
if outputlist[0] != contractStructure['contractAddress']:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} hasn't expired yet"
|
|
logger.warning(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check the type of smart contract ie. external trigger or internal trigger
|
|
if 'payeeAddress' in contractStructure:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} has an internal trigger"
|
|
logger.warning(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check the status of the contract
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
contractStatus = connection.execute(f"select status from activecontracts where contractName=='{parsed_data['contractName']}' and contractAddress='{outputlist[0]}'").fetchall()[0][0]
|
|
connection.close()
|
|
contractList = []
|
|
|
|
if contractStatus == 'closed':
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart contract {parsed_data['contractName']} at the {outputlist[0]} is closed"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
else:
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
result = session.query(ContractStructure).filter_by(attribute='expiryTime').all()
|
|
session.close()
|
|
if result:
|
|
# now parse the expiry time in python
|
|
expirytime = result[0].value.strip()
|
|
expirytime_split = expirytime.split(' ')
|
|
parse_string = '{}/{}/{} {}'.format(expirytime_split[3],
|
|
parsing.months[expirytime_split[1]],
|
|
expirytime_split[2], expirytime_split[4])
|
|
expirytime_object = parsing.arrow.get(parse_string, 'YYYY/M/D HH:mm:ss').replace(
|
|
tzinfo=expirytime_split[5][3:])
|
|
blocktime_object = parsing.arrow.get(transaction_data['blocktime']).to('Asia/Kolkata')
|
|
|
|
if blocktime_object <= expirytime_object:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart contract {parsed_data['contractName']}-{outputlist[0]} has not expired and will not trigger"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check if the user choice passed is part of the contract structure
|
|
tempchoiceList = []
|
|
for item in contractStructure['exitconditions']:
|
|
tempchoiceList.append(contractStructure['exitconditions'][item])
|
|
|
|
if parsed_data['triggerCondition'] not in tempchoiceList:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as triggerCondition, {parsed_data['triggerCondition']}, has been passed to Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} which doesn't accept any userChoice of the given name"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
# check if minimumsubscriptionamount exists as part of the contract structure
|
|
if 'minimumsubscriptionamount' in contractStructure:
|
|
# if it has not been reached, close the contract and return money
|
|
minimumsubscriptionamount = float(contractStructure['minimumsubscriptionamount'])
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
amountDeposited = session.query(func.sum(ContractParticipants.tokenAmount)).all()[0][0]
|
|
session.close()
|
|
|
|
if amountDeposited is None:
|
|
amountDeposited = 0
|
|
|
|
if amountDeposited < minimumsubscriptionamount:
|
|
# close the contract and return the money
|
|
logger.info('Minimum subscription amount hasn\'t been reached\n The token will be returned back')
|
|
# Initialize payback to contract participants
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"})
|
|
contractParticipants = connection.execute('select participantAddress, tokenAmount, transactionHash from contractparticipants').fetchall()[0][0]
|
|
|
|
for participant in contractParticipants:
|
|
tokenIdentification = connection.execute('select * from contractstructure where attribute="tokenIdentification"').fetchall()[0][0]
|
|
contractAddress = connection.execute(
|
|
'select * from contractstructure where attribute="contractAddress"').fetchall()[0][0]
|
|
returnval = transferToken(tokenIdentification, participant[1], contractAddress, participant[0], transaction_data, parsed_data, blockinfo = blockinfo)
|
|
if returnval is None:
|
|
logger.info("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
|
return 0
|
|
|
|
connection.execute(
|
|
'update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format(
|
|
(participant[1], participant[0], participant[4])))
|
|
|
|
# add transaction to ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
session.add(ContractTransactionHistory(transactionType='trigger',
|
|
transactionSubType='minimumsubscriptionamount-payback',
|
|
sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=None,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute(
|
|
'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format(
|
|
parsed_data['contractName'], outputlist[0]))
|
|
connection.execute(
|
|
'update activecontracts set status="{}" where contractName="{}" and contractAddress="{}"'.format(
|
|
transaction_data['blocktime'],
|
|
parsed_data['contractName'], outputlist[0]))
|
|
connection.close()
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['contractName']}-{outputlist[0]}")
|
|
|
|
pushData_SSEapi(
|
|
'Trigger | Minimum subscription amount not reached at contract {}-{} at transaction {}. Tokens will be refunded'.format(
|
|
parsed_data['contractName'], outputlist[0], transaction_data['txid']))
|
|
return 1
|
|
|
|
# Trigger the contract
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"})
|
|
tokenSum = connection.execute('select IFNULL(sum(tokenAmount), 0) from contractparticipants').fetchall()[0][0]
|
|
if tokenSum > 0:
|
|
contractWinners = connection.execute('select * from contractparticipants where userChoice="{}"'.format(parsed_data['triggerCondition'])).fetchall()
|
|
winnerSum = connection.execute('select sum(tokenAmount) from contractparticipants where userChoice="{}"'.format(parsed_data['triggerCondition'])).fetchall()[0][0]
|
|
tokenIdentification = connection.execute('select value from contractstructure where attribute="tokenIdentification"').fetchall()[0][0]
|
|
|
|
for winner in contractWinners:
|
|
winnerAmount = "%.8f" % ((winner[2] / winnerSum) * tokenSum)
|
|
returnval = transferToken(tokenIdentification, winnerAmount, outputlist[0], winner[1], transaction_data, parsed_data, blockinfo = blockinfo)
|
|
if returnval is None:
|
|
logger.critical("Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
|
return 0
|
|
|
|
connection.execute(f"INSERT INTO contractwinners (participantAddress, winningAmount, userChoice, transactionHash, blockNumber, blockHash) VALUES('{winner[1]}', {winnerAmount}, '{parsed_data['triggerCondition']}', '{transaction_data['txid']}','{blockinfo['height']}','{blockinfo['hash']}');")
|
|
|
|
# add transaction to ContractTransactionHistory
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(ContractTransactionHistory(transactionType='trigger',
|
|
transactionSubType='committee',
|
|
sourceFloAddress=inputadd,
|
|
destFloAddress=outputlist[0],
|
|
transferAmount=None,
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data),
|
|
parsedFloData=json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
'''connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute('INSERT INTO activecontracts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, contractStructure['contractName'], contractStructure['contractAddress'], 'closed', contractStructure['tokenIdentification'], contractStructure['contractType'], transaction_data['txid'], blockinfo['height'], blockinfo['hash'], 'query.incorporationDate', 'query.expiryDate', blockinfo['time']))
|
|
connection.execute('INSERT INTO time_actions VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', (None, blockinfo['time'], 'contract-time-trigger', 'closed', contractStructure['contractName'], contractStructure['contractAddress'], contractStructure['contractType'], 'query.tokens_db', 'query.parsed_data', transaction_data['txid'], blockinfo['height']))
|
|
connection.close()'''
|
|
|
|
close_expire_contract(contractStructure, 'closed', transaction_data['txid'], blockinfo['height'], blockinfo['hash'], 'query.incorporationDate', 'query.expiryDate', blockinfo['time'], blockinfo['time'], 'contract-time-trigger', contractStructure['contractName'], contractStructure['contractAddress'], contractStructure['contractType'], 'query.tokens_db', 'query.parsed_data', blockinfo['height'])
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{contractStructure['contractName']}-{contractStructure['contractAddress']}")
|
|
|
|
pushData_SSEapi('Trigger | Contract triggered of the name {}-{} is active currently at transaction {}'.format(parsed_data['contractName'], outputlist[0], transaction_data['txid']))
|
|
return 1
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as Smart Contract named {parsed_data['contractName']} at the address {outputlist[0]} doesn't exist"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'trigger', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as input address, {inputlist[0]}, is not part of the committee address list"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
elif parsed_data['type'] == 'smartContractDeposit':
|
|
if check_database_existence('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"}):
|
|
# Check if the transaction hash already exists in the contract db (Safety check)
|
|
connection = create_database_connection('smart_contract', {'contract_name':f"{parsed_data['contractName']}", 'contract_address':f"{outputlist[0]}"})
|
|
participantAdd_txhash = connection.execute('select participantAddress, transactionHash from contractparticipants').fetchall()
|
|
participantAdd_txhash_T = list(zip(*participantAdd_txhash))
|
|
|
|
if len(participantAdd_txhash) != 0 and transaction_data['txid'] in list(participantAdd_txhash_T[1]):
|
|
logger.warning(f"Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
pushData_SSEapi(f"Error | Transaction {transaction_data['txid']} rejected as it already exists in the Smart Contract db. This is unusual, please check your code")
|
|
return 0
|
|
|
|
# if contractAddress was passed, then check if it matches the output address of this contract
|
|
if 'contractAddress' in parsed_data:
|
|
if parsed_data['contractAddress'] != outputlist[0]:
|
|
rejectComment = f"Contract deposit at transaction {transaction_data['txid']} rejected as contractAddress specified in flodata, {parsed_data['contractAddress']}, doesnt not match with transaction's output address {outputlist[0]}"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'participation', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
# Pass information to SSE channel
|
|
pushData_SSEapi(f"Error| Mismatch in contract address specified in flodata and the output address of the transaction {transaction_data['txid']}")
|
|
return 0
|
|
|
|
# pull out the contract structure into a dictionary
|
|
contractStructure = extract_contractStructure(parsed_data['contractName'], outputlist[0])
|
|
|
|
# Transfer the token
|
|
returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['depositAmount'], inputlist[0], outputlist[0], transaction_data, parsed_data, blockinfo=blockinfo)
|
|
if returnval is None:
|
|
logger.info("Something went wrong in the token transfer method")
|
|
pushData_SSEapi(f"Error | Something went wrong while doing the internal db transactions for {transaction_data['txid']}")
|
|
return 0
|
|
|
|
# Push the deposit transaction into deposit database contract database
|
|
session = create_database_session_orm('smart_contract', {'contract_name': f"{parsed_data['contractName']}", 'contract_address': f"{outputlist[0]}"}, ContractBase)
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
old_depositBalance = session.query(ContractDeposits.depositBalance).order_by(ContractDeposits.id.desc()).first()
|
|
if old_depositBalance is None:
|
|
old_depositBalance = 0
|
|
else:
|
|
old_depositBalance = old_depositBalance[0]
|
|
session.add(ContractDeposits( depositorAddress = inputadd,
|
|
depositAmount = parsed_data['depositAmount'],
|
|
depositBalance = old_depositBalance + parsed_data['depositAmount'],
|
|
expiryTime = parsed_data['depositConditions']['expiryTime'],
|
|
unix_expiryTime = convert_datetime_to_arrowobject(parsed_data['depositConditions']['expiryTime']).timestamp,
|
|
status = 'active',
|
|
transactionHash = transaction_data['txid'],
|
|
blockNumber = transaction_data['blockheight'],
|
|
blockHash = transaction_data['blockhash']
|
|
))
|
|
session.add(ContractTransactionHistory(transactionType = 'smartContractDeposit',
|
|
transactionSubType = None,
|
|
sourceFloAddress = inputadd,
|
|
destFloAddress = outputlist[0],
|
|
transferAmount = parsed_data['depositAmount'],
|
|
blockNumber = transaction_data['blockheight'],
|
|
blockHash = transaction_data['blockhash'],
|
|
time = transaction_data['blocktime'],
|
|
transactionHash = transaction_data['txid'],
|
|
blockchainReference = blockchainReference,
|
|
jsonData = json.dumps(transaction_data),
|
|
parsedFloData = json.dumps(parsed_data)
|
|
))
|
|
session.commit()
|
|
session.close()
|
|
|
|
session = create_database_session_orm('system_dbs', {'db_name': f"system"}, SystemBase)
|
|
session.add(TimeActions(time=parsed_data['depositConditions']['expiryTime'],
|
|
activity='contract-deposit',
|
|
status='active',
|
|
contractName=parsed_data['contractName'],
|
|
contractAddress=outputlist[0],
|
|
contractType='continuos-event-swap',
|
|
tokens_db=f"{parsed_data['tokenIdentification']}",
|
|
parsed_data=json.dumps(parsed_data),
|
|
transactionHash=transaction_data['txid'],
|
|
blockNumber=transaction_data['blockheight']))
|
|
session.commit()
|
|
pushData_SSEapi(f"Deposit Smart Contract Transaction {transaction_data['txid']} for the Smart contract named {parsed_data['contractName']} at the address {outputlist[0]}")
|
|
updateLatestTransaction(transaction_data, parsed_data , f"{parsed_data['contractName']}-{outputlist[0]}")
|
|
return 1
|
|
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as a Smart Contract with the name {parsed_data['contractName']} at address {outputlist[0]} doesnt exist"
|
|
logger.info(rejectComment)
|
|
rejected_contract_transaction_history(transaction_data, parsed_data, 'smartContractDeposit', outputlist[0], inputadd, outputlist[0], rejectComment)
|
|
return 0
|
|
|
|
elif parsed_data['type'] == 'nftIncorporation':
|
|
'''
|
|
DIFFERENT BETWEEN TOKEN AND NFT
|
|
System.db will have a different entry
|
|
in creation nft word will be extra
|
|
NFT Hash must be present
|
|
Creation and transfer amount .. only integer parts will be taken
|
|
Keyword nft must be present in both creation and transfer
|
|
'''
|
|
if not is_a_contract_address(inputlist[0]):
|
|
if not check_database_existence('token', {'token_name':f"{parsed_data['tokenIdentification']}"}):
|
|
session = create_database_session_orm('token', {'token_name': f"{parsed_data['tokenIdentification']}"}, TokenBase)
|
|
session.add(ActiveTable(address=inputlist[0], parentid=0, transferBalance=parsed_data['tokenAmount'], blockNumber=blockinfo['height']))
|
|
session.add(TransferLogs(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'], sourceId=0, destinationId=1,
|
|
blockNumber=transaction_data['blockheight'], time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid']))
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
session.add(TransactionHistory(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data), transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# add it to token address to token mapping db table
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute(f"INSERT INTO tokenAddressMapping (tokenAddress, token, transactionHash, blockNumber, blockHash) VALUES ('{inputadd}', '{parsed_data['tokenIdentification']}', '{transaction_data['txid']}', '{transaction_data['blockheight']}', '{transaction_data['blockhash']}');")
|
|
nft_data = {'sha256_hash': f"{parsed_data['nftHash']}"}
|
|
connection.execute(f"INSERT INTO databaseTypeMapping (db_name, db_type, keyword, object_format, blockNumber) VALUES ('{parsed_data['tokenIdentification']}', 'nft', '', '{nft_data}', '{transaction_data['blockheight']}'")
|
|
connection.close()
|
|
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['tokenIdentification']}")
|
|
pushData_SSEapi(f"Token | Succesfully incorporated token {parsed_data['tokenIdentification']} at transaction {transaction_data['txid']}")
|
|
return 1
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as an NFT with the name {parsed_data['tokenIdentification']} has already been incorporated"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
else:
|
|
rejectComment = f"NFT incorporation at transaction {transaction_data['txid']} rejected as either the input address is part of a contract address"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
elif parsed_data['type'] == 'infiniteTokenIncorporation':
|
|
if not is_a_contract_address(inputlist[0]) and not is_a_contract_address(outputlist[0]):
|
|
if not check_database_existence('token', {'token_name':f"{parsed_data['tokenIdentification']}"}):
|
|
parsed_data['tokenAmount'] = 0
|
|
tokendb_session = create_database_session_orm('token', {'token_name': f"{parsed_data['tokenIdentification']}"}, TokenBase)
|
|
tokendb_session.add(ActiveTable(address=inputlist[0], parentid=0, transferBalance=parsed_data['tokenAmount'], blockNumber=blockinfo['height']))
|
|
tokendb_session.add(TransferLogs(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'], sourceId=0, destinationId=1,
|
|
blockNumber=transaction_data['blockheight'], time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid']))
|
|
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
|
tokendb_session.add(TransactionHistory(sourceFloAddress=inputadd, destFloAddress=outputlist[0],
|
|
transferAmount=parsed_data['tokenAmount'],
|
|
blockNumber=transaction_data['blockheight'],
|
|
blockHash=transaction_data['blockhash'],
|
|
time=transaction_data['blocktime'],
|
|
transactionHash=transaction_data['txid'],
|
|
blockchainReference=blockchainReference,
|
|
jsonData=json.dumps(transaction_data), transactionType=parsed_data['type'],
|
|
parsedFloData=json.dumps(parsed_data)))
|
|
|
|
# add it to token address to token mapping db table
|
|
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
|
connection.execute(f"INSERT INTO tokenAddressMapping (tokenAddress, token, transactionHash, blockNumber, blockHash) VALUES ('{inputadd}', '{parsed_data['tokenIdentification']}', '{transaction_data['txid']}', '{transaction_data['blockheight']}', '{transaction_data['blockhash']}');")
|
|
info_object = {'root_address': inputadd}
|
|
connection.execute("""INSERT INTO databaseTypeMapping (db_name, db_type, keyword, object_format, blockNumber) VALUES (?, ?, ?, ?, ?)""", (parsed_data['tokenIdentification'], 'infinite-token', '', json.dumps(info_object), transaction_data['blockheight']))
|
|
updateLatestTransaction(transaction_data, parsed_data, f"{parsed_data['tokenIdentification']}")
|
|
tokendb_session.commit()
|
|
connection.close()
|
|
tokendb_session.close()
|
|
pushData_SSEapi(f"Token | Succesfully incorporated token {parsed_data['tokenIdentification']} at transaction {transaction_data['txid']}")
|
|
return 1
|
|
else:
|
|
rejectComment = f"Transaction {transaction_data['txid']} rejected as a token with the name {parsed_data['tokenIdentification']} has already been incorporated"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
else:
|
|
rejectComment = f"Infinite token incorporation at transaction {transaction_data['txid']} rejected as either the input address is part of a contract address"
|
|
logger.info(rejectComment)
|
|
rejected_transaction_history(transaction_data, parsed_data, inputadd, outputlist[0], rejectComment)
|
|
pushData_SSEapi(rejectComment)
|
|
return 0
|
|
|
|
|
|
def scanBlockchain():
|
|
# Read start block no
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
startblock = int(session.query(SystemData).filter_by(attribute='lastblockscanned').all()[0].value) + 1
|
|
session.commit()
|
|
session.close()
|
|
|
|
# todo Rule 6 - Find current block height
|
|
# Rule 7 - Start analysing the block contents from starting block to current height
|
|
|
|
# Find current block height
|
|
current_index = -1
|
|
while(current_index == -1):
|
|
response = newMultiRequest('blocks?limit=1')
|
|
try:
|
|
current_index = response['blocks'][0]['height']
|
|
except:
|
|
logger.info('Latest block count response from multiRequest() is not in the right format. Displaying the data received in the log below')
|
|
logger.info(response)
|
|
logger.info('Program will wait for 1 seconds and try to reconnect')
|
|
time.sleep(1)
|
|
else:
|
|
logger.info("Current block height is %s" % str(current_index))
|
|
break
|
|
|
|
for blockindex in range(startblock, current_index):
|
|
if blockindex in IGNORE_BLOCK_LIST:
|
|
continue
|
|
processBlock(blockindex=blockindex)
|
|
|
|
# At this point the script has updated to the latest block
|
|
# Now we connect to flosight's websocket API to get information about the latest blocks
|
|
|
|
|
|
def switchNeturl(currentneturl):
|
|
neturlindex = serverlist.index(currentneturl)
|
|
if neturlindex+1 >= len(serverlist):
|
|
return serverlist[neturlindex+1 - len(serverlist)]
|
|
else:
|
|
return serverlist[neturlindex+1]
|
|
|
|
|
|
def reconnectWebsocket(socket_variable):
|
|
# Switch a to different flosight
|
|
# neturl = switchNeturl(neturl)
|
|
# Connect to Flosight websocket to get data on new incoming blocks
|
|
i=0
|
|
newurl = serverlist[0]
|
|
while(not socket_variable.connected):
|
|
logger.info(f"While loop {i}")
|
|
logger.info(f"Sleeping for 3 seconds before attempting reconnect to {newurl}")
|
|
time.sleep(3)
|
|
try:
|
|
scanBlockchain()
|
|
logger.info(f"Websocket endpoint which is being connected to {newurl}socket.io/socket.io.js")
|
|
socket_variable.connect(f"{newurl}socket.io/socket.io.js")
|
|
i=i+1
|
|
except:
|
|
logger.info(f"disconnect block: Failed reconnect attempt to {newurl}")
|
|
newurl = switchNeturl(newurl)
|
|
i=i+1
|
|
|
|
|
|
# MAIN EXECUTION STARTS
|
|
# Configuration of required variables
|
|
config = configparser.ConfigParser()
|
|
config.read('config.ini')
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
|
|
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
|
|
file_handler = logging.FileHandler(os.path.join(config['DEFAULT']['DATA_PATH'],'tracking.log'))
|
|
file_handler.setLevel(logging.INFO)
|
|
file_handler.setFormatter(formatter)
|
|
|
|
stream_handler = logging.StreamHandler()
|
|
stream_handler.setFormatter(formatter)
|
|
|
|
logger.addHandler(file_handler)
|
|
logger.addHandler(stream_handler)
|
|
|
|
|
|
# Rule 1 - Read command line arguments to reset the databases as blank
|
|
# Rule 2 - Read config to set testnet/mainnet
|
|
# Rule 3 - Set flo blockexplorer location depending on testnet or mainnet
|
|
# Rule 4 - Set the local flo-cli path depending on testnet or mainnet ( removed this feature | Flosights are the only source )
|
|
# Rule 5 - Set the block number to scan from
|
|
|
|
|
|
# Read command line arguments
|
|
parser = argparse.ArgumentParser(description='Script tracks RMT using FLO data on the FLO blockchain - https://flo.cash')
|
|
parser.add_argument('-r', '--reset', nargs='?', const=1, type=int, help='Purge existing db and rebuild it from scratch')
|
|
parser.add_argument('-rb', '--rebuild', nargs='?', const=1, type=int, help='Rebuild it')
|
|
args = parser.parse_args()
|
|
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'tokens')
|
|
if not os.path.isdir(dirpath):
|
|
os.mkdir(dirpath)
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'smartContracts')
|
|
if not os.path.isdir(dirpath):
|
|
os.mkdir(dirpath)
|
|
|
|
# Read configuration
|
|
|
|
# todo - write all assertions to make sure default configs are right
|
|
if (config['DEFAULT']['NET'] != 'mainnet') and (config['DEFAULT']['NET'] != 'testnet'):
|
|
logger.error("NET parameter in config.ini invalid. Options are either 'mainnet' or 'testnet'. Script is exiting now")
|
|
sys.exit(0)
|
|
|
|
# Specify mainnet and testnet server list for API calls and websocket calls
|
|
serverlist = None
|
|
if config['DEFAULT']['NET'] == 'mainnet':
|
|
serverlist = config['DEFAULT']['MAINNET_FLOSIGHT_SERVER_LIST']
|
|
elif config['DEFAULT']['NET'] == 'testnet':
|
|
serverlist = config['DEFAULT']['TESTNET_FLOSIGHT_SERVER_LIST']
|
|
serverlist = serverlist.split(',')
|
|
neturl = config['DEFAULT']['FLOSIGHT_NETURL']
|
|
tokenapi_sse_url = config['DEFAULT']['TOKENAPI_SSE_URL']
|
|
|
|
IGNORE_BLOCK_LIST = config['DEFAULT']['IGNORE_BLOCK_LIST'].split(',')
|
|
IGNORE_BLOCK_LIST = [int(s) for s in IGNORE_BLOCK_LIST]
|
|
IGNORE_TRANSACTION_LIST = config['DEFAULT']['IGNORE_TRANSACTION_LIST'].split(',')
|
|
|
|
# Delete database and smartcontract directory if reset is set to 1
|
|
if args.reset == 1:
|
|
logger.info("Resetting the database. ")
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'tokens')
|
|
if os.path.exists(dirpath):
|
|
shutil.rmtree(dirpath)
|
|
os.mkdir(dirpath)
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'smartContracts')
|
|
if os.path.exists(dirpath):
|
|
shutil.rmtree(dirpath)
|
|
os.mkdir(dirpath)
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'system.db')
|
|
if os.path.exists(dirpath):
|
|
os.remove(dirpath)
|
|
dirpath = os.path.join(config['DEFAULT']['DATA_PATH'], 'latestCache.db')
|
|
if os.path.exists(dirpath):
|
|
os.remove(dirpath)
|
|
|
|
# Read start block no
|
|
startblock = int(config['DEFAULT']['START_BLOCK'])
|
|
session = create_database_session_orm('system_dbs', {'db_name': "system"}, SystemBase)
|
|
session.add(SystemData(attribute='lastblockscanned', value=startblock - 1))
|
|
session.commit()
|
|
session.close()
|
|
|
|
# Initialize latest cache DB
|
|
session = create_database_session_orm('system_dbs', {'db_name': "latestCache"}, LatestCacheBase)
|
|
session.commit()
|
|
session.close()
|
|
|
|
|
|
# Determine API source for block and transaction information
|
|
if __name__ == "__main__":
|
|
# MAIN LOGIC STARTS
|
|
# scan from the latest block saved locally to latest network block
|
|
scanBlockchain()
|
|
|
|
# At this point the script has updated to the latest block
|
|
# Now we connect to flosight's websocket API to get information about the latest blocks
|
|
# Neturl is the URL for Flosight API whose websocket endpoint is being connected to
|
|
|
|
sio = socketio.Client()
|
|
# Connect to a websocket endpoint and wait for further events
|
|
reconnectWebsocket(sio)
|
|
#sio.connect(f"{neturl}socket.io/socket.io.js")
|
|
|
|
@sio.on('connect')
|
|
def token_connect():
|
|
current_time=datetime.now().strftime('%H:%M:%S')
|
|
logger.info(f"Token Tracker has connected to websocket endpoint. Time : {current_time}")
|
|
sio.emit('subscribe', 'inv')
|
|
|
|
@sio.on('disconnect')
|
|
def token_disconnect():
|
|
current_time = datetime.now().strftime('%H:%M:%S')
|
|
logger.info(f"disconnect block: Token Tracker disconnected from websocket endpoint. Time : {current_time}")
|
|
logger.info('disconnect block: Triggering client disconnect')
|
|
sio.disconnect()
|
|
logger.info('disconnect block: Finished triggering client disconnect')
|
|
reconnectWebsocket(sio)
|
|
|
|
@sio.on('connect_error')
|
|
def connect_error():
|
|
current_time = datetime.now().strftime('%H:%M:%S')
|
|
logger.info(f"connection error block: Token Tracker disconnected from websocket endpoint. Time : {current_time}")
|
|
logger.info('connection error block: Triggering client disconnect')
|
|
sio.disconnect()
|
|
logger.info('connection error block: Finished triggering client disconnect')
|
|
reconnectWebsocket(sio)
|
|
|
|
@sio.on('block')
|
|
def on_block(data):
|
|
logger.info('New block received')
|
|
logger.info(str(data))
|
|
processBlock(blockhash=data) |