Test rebuild along with database changes
This commit is contained in:
parent
32c7494504
commit
29c6019c15
@ -225,7 +225,6 @@ class LatestTransactions(LatestCacheBase):
|
||||
transactionType = Column('transactionType', String)
|
||||
parsedFloData = Column('parsedFloData', String)
|
||||
|
||||
|
||||
class LatestBlocks(LatestCacheBase):
|
||||
__tablename__ = "latestBlocks"
|
||||
id = Column('id', Integer, primary_key=True)
|
||||
|
||||
12
parsing.py
12
parsing.py
@ -978,7 +978,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
|
||||
contract_address = extract_special_character_word(first_classification['wordlist'],'$')
|
||||
contract_address = find_original_case(contract_address, clean_text)
|
||||
if not check_flo_address(contract_address):
|
||||
if not check_flo_address(contract_address, is_testnet):
|
||||
return outputreturn('noise')
|
||||
|
||||
contract_conditions = extract_contract_conditions(processed_text, contract_type, contract_token, blocktime=blockinfo['time'])
|
||||
@ -1004,7 +1004,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
return outputreturn('one-time-event-userchoice-smartcontract-incorporation',f"{contract_token}", f"{contract_name}", f"{contract_address}", f"{clean_text}", f"{contract_conditions['contractAmount']}", f"{minimum_subscription_amount}" , f"{maximum_subscription_amount}", f"{contract_conditions['userchoices']}", f"{contract_conditions['expiryTime']}")
|
||||
elif 'payeeAddress' in contract_conditions.keys():
|
||||
contract_conditions['payeeAddress'] = find_word_index_fromstring(clean_text,contract_conditions['payeeAddress'])
|
||||
if not check_flo_address(contract_conditions['payeeAddress']):
|
||||
if not check_flo_address(contract_conditions['payeeAddress'], is_testnet):
|
||||
return outputreturn('noise')
|
||||
else:
|
||||
return outputreturn('one-time-event-time-smartcontract-incorporation',f"{contract_token}", f"{contract_name}", f"{contract_address}", f"{clean_text}", f"{contract_conditions['contractAmount']}", f"{minimum_subscription_amount}" , f"{maximum_subscription_amount}", f"{contract_conditions['payeeAddress']}", f"{contract_conditions['expiryTime']}")
|
||||
@ -1028,7 +1028,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
contract_address = ''
|
||||
else:
|
||||
contract_address = find_original_case(contract_address, clean_text)
|
||||
if not check_flo_address(contract_address):
|
||||
if not check_flo_address(contract_address, is_testnet):
|
||||
return outputreturn('noise')
|
||||
|
||||
if operation == 'category1':
|
||||
@ -1072,7 +1072,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
contract_address = ''
|
||||
else:
|
||||
contract_address = find_original_case(contract_address, clean_text)
|
||||
if not check_flo_address(contract_address):
|
||||
if not check_flo_address(contract_address, is_testnet):
|
||||
return outputreturn('noise')
|
||||
|
||||
return outputreturn('smart-contract-one-time-event-continuos-event-participation', f"{clean_text}", f"{tokenname}", tokenamount, f"{contract_name}", f"{contract_address}")
|
||||
@ -1106,7 +1106,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
|
||||
contract_address = extract_special_character_word(first_classification['wordlist'],'$')
|
||||
contract_address = find_original_case(contract_address, clean_text)
|
||||
if not check_flo_address(contract_address):
|
||||
if not check_flo_address(contract_address, is_testnet):
|
||||
return outputreturn('noise')
|
||||
|
||||
contract_conditions = extract_contract_conditions(processed_text, contract_type, contract_token, blocktime=blockinfo['time'])
|
||||
@ -1117,7 +1117,7 @@ def parse_flodata(text, blockinfo, net):
|
||||
assert contract_conditions['subtype'] == 'tokenswap'
|
||||
assert check_regex("^[A-Za-z][A-Za-z0-9_-]*[A-Za-z0-9]$", contract_conditions['accepting_token'])
|
||||
assert check_regex("^[A-Za-z][A-Za-z0-9_-]*[A-Za-z0-9]$", contract_conditions['accepting_token'])
|
||||
assert contract_conditions['priceType']=="'predetermined'" or contract_conditions['priceType']=='"predetermined"' or contract_conditions['priceType']=="predetermined" or check_flo_address(find_original_case(contract_conditions['priceType'], clean_text))
|
||||
assert contract_conditions['priceType']=="'predetermined'" or contract_conditions['priceType']=='"predetermined"' or contract_conditions['priceType']=="predetermined" or check_flo_address(find_original_case(contract_conditions['priceType'], clean_text), is_testnet)
|
||||
assert float(contract_conditions['price'])
|
||||
except AssertionError:
|
||||
return outputreturn('noise')
|
||||
|
||||
189
test_rebuild.py
Normal file
189
test_rebuild.py
Normal file
@ -0,0 +1,189 @@
|
||||
from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from models import SystemData, ActiveTable, ConsumedTable, TransferLogs, TransactionHistory, RejectedTransactionHistory, Base, ContractStructure, ContractBase, ContractParticipants, SystemBase, ActiveContracts, ContractAddressMapping, LatestCacheBase, ContractTransactionHistory, RejectedContractTransactionHistory, TokenContractAssociation, ContinuosContractBase, ContractStructure1, ContractParticipants1, ContractDeposits1, ContractTransactionHistory1, LatestCacheBase, LatestTransactions, LatestBlocks
|
||||
import json
|
||||
from tracktokens_smartcontracts import processTransaction
|
||||
import os
|
||||
import logging
|
||||
import argparse
|
||||
import configparser
|
||||
import pdb
|
||||
import shutil
|
||||
import sys
|
||||
|
||||
|
||||
# helper functions
|
||||
def check_database_existence(type, parameters):
|
||||
if type == 'token':
|
||||
return os.path.isfile(f"./tokens/{parameters['token_name']}.db")
|
||||
|
||||
if type == 'smart_contract':
|
||||
return os.path.isfile(f"./smartContracts/{parameters['contract_name']}-{parameters['contract_address']}.db")
|
||||
|
||||
|
||||
def create_database_connection(type, parameters):
|
||||
if type == 'token':
|
||||
engine = create_engine(f"sqlite:///tokens/{parameters['token_name']}.db", echo=True)
|
||||
elif type == 'smart_contract':
|
||||
engine = create_engine(f"sqlite:///smartContracts/{parameters['contract_name']}-{parameters['contract_address']}.db", echo=True)
|
||||
elif type == 'system_dbs':
|
||||
engine = create_engine(f"sqlite:///{parameters['db_name']}.db", echo=False)
|
||||
|
||||
connection = engine.connect()
|
||||
return connection
|
||||
|
||||
|
||||
def create_database_session_orm(type, parameters, base):
|
||||
if type == 'token':
|
||||
engine = create_engine(f"sqlite:///tokens/{parameters['token_name']}.db", echo=True)
|
||||
base.metadata.create_all(bind=engine)
|
||||
session = sessionmaker(bind=engine)()
|
||||
|
||||
elif type == 'smart_contract':
|
||||
engine = create_engine(f"sqlite:///smartContracts/{parameters['contract_name']}-{parameters['contract_address']}.db", echo=True)
|
||||
base.metadata.create_all(bind=engine)
|
||||
session = sessionmaker(bind=engine)()
|
||||
|
||||
elif type == 'system_dbs':
|
||||
engine = create_engine(f"sqlite:///{parameters['db_name']}.db", echo=False)
|
||||
base.metadata.create_all(bind=engine)
|
||||
session = sessionmaker(bind=engine)()
|
||||
|
||||
return session
|
||||
|
||||
|
||||
# rename all the old databases
|
||||
# system.db , latestCache.db, smartContracts, tokens
|
||||
if os.path.isfile('./system.db'):
|
||||
os.rename('system.db', 'system1.db')
|
||||
if os.path.isfile('./latestCache.db'):
|
||||
os.rename('latestCache.db', 'latestCache1.db')
|
||||
if os.path.isfile('./smartContracts'):
|
||||
os.rename('smartContracts', 'smartContracts1')
|
||||
if os.path.isfile('./tokens'):
|
||||
os.rename('tokens', 'tokens1')
|
||||
|
||||
|
||||
# MAIN EXECUTION STARTS
|
||||
# Configuration of required variables
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
formatter = logging.Formatter('%(asctime)s:%(name)s:%(message)s')
|
||||
|
||||
file_handler = logging.FileHandler('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()
|
||||
|
||||
apppath = os.path.dirname(os.path.realpath(__file__))
|
||||
dirpath = os.path.join(apppath, 'tokens')
|
||||
if not os.path.isdir(dirpath):
|
||||
os.mkdir(dirpath)
|
||||
dirpath = os.path.join(apppath, 'smartContracts')
|
||||
if not os.path.isdir(dirpath):
|
||||
os.mkdir(dirpath)
|
||||
|
||||
# Read configuration
|
||||
config = configparser.ConfigParser()
|
||||
config.read('config.ini')
|
||||
|
||||
# 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']
|
||||
|
||||
# Delete database and smartcontract directory if reset is set to 1
|
||||
#if args.reset == 1:
|
||||
logger.info("Resetting the database. ")
|
||||
apppath = os.path.dirname(os.path.realpath(__file__))
|
||||
dirpath = os.path.join(apppath, 'tokens')
|
||||
shutil.rmtree(dirpath)
|
||||
os.mkdir(dirpath)
|
||||
dirpath = os.path.join(apppath, 'smartContracts')
|
||||
shutil.rmtree(dirpath)
|
||||
os.mkdir(dirpath)
|
||||
dirpath = os.path.join(apppath, 'system.db')
|
||||
if os.path.exists(dirpath):
|
||||
os.remove(dirpath)
|
||||
dirpath = os.path.join(apppath, '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()
|
||||
|
||||
# get all blocks and transaction data
|
||||
latestCache_session = create_database_session_orm('system_dbs', {'db_name':'latestCache1'}, LatestCacheBase)
|
||||
lblocks = latestCache_session.query(LatestBlocks).all()
|
||||
ltransactions = latestCache_session.query(LatestTransactions).all()
|
||||
latestCache_session.close()
|
||||
|
||||
lblocks_dict = {}
|
||||
for block in lblocks:
|
||||
block_dict = block.__dict__
|
||||
lblocks_dict[block_dict['blockNumber']] = {'blockHash':f"{block_dict['blockHash']}", 'jsonData':f"{block_dict['jsonData']}"}
|
||||
|
||||
# process and rebuild all transactions
|
||||
for transaction in ltransactions:
|
||||
transaction_dict = transaction.__dict__
|
||||
transaction_data = json.loads(transaction_dict['jsonData'])
|
||||
parsed_flodata = json.loads(transaction_dict['parsedFloData'])
|
||||
block_info = json.loads(lblocks_dict[transaction_dict['blockNumber']]['jsonData'])
|
||||
processTransaction(transaction_data, parsed_flodata, block_info)
|
||||
|
||||
# copy the old block data
|
||||
old_latest_cache = create_database_connection('system_dbs', {'db_name':'latestCache1'})
|
||||
old_latest_cache.execute("ATTACH DATABASE 'latestCache.db' AS new_db")
|
||||
old_latest_cache.execute("INSERT INTO new_db.latestBlocks SELECT * FROM latestBlocks")
|
||||
old_latest_cache.close()
|
||||
|
||||
|
||||
# delete
|
||||
# system.db , latestCache.db, smartContracts, tokens
|
||||
if os.path.isfile('./system1.db'):
|
||||
os.remove('system1.db')
|
||||
if os.path.isfile('./latestCache1.db'):
|
||||
os.remove('latestCache1.db')
|
||||
if os.path.isfile('./smartContracts1'):
|
||||
shutil.rmtree('smartContracts1')
|
||||
if os.path.isfile('./tokens1'):
|
||||
shutil.rmtree('tokens1')
|
||||
@ -13,10 +13,10 @@ from sqlalchemy import create_engine, func
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
import time
|
||||
import parsing
|
||||
from config import *
|
||||
from datetime import datetime
|
||||
import pdb
|
||||
from models import SystemData, ActiveTable, ConsumedTable, TransferLogs, TransactionHistory, RejectedTransactionHistory, Base, ContractStructure, ContractBase, ContractParticipants, SystemBase, ActiveContracts, ContractAddressMapping, LatestCacheBase, ContractTransactionHistory, RejectedContractTransactionHistory, TokenContractAssociation, ContinuosContractBase, ContractStructure1, ContractParticipants1, ContractDeposits1, ContractTransactionHistory1
|
||||
from config import *
|
||||
from datetime import datetime
|
||||
import pdb
|
||||
from models import SystemData, ActiveTable, ConsumedTable, TransferLogs, TransactionHistory, RejectedTransactionHistory, Base, ContractStructure, ContractBase, ContractParticipants, SystemBase, ActiveContracts, ContractAddressMapping, LatestCacheBase, ContractTransactionHistory, RejectedContractTransactionHistory, TokenContractAssociation, ContinuosContractBase, ContractStructure1, ContractParticipants1, ContractDeposits1, ContractTransactionHistory1, DatabaseAddressMapping
|
||||
|
||||
|
||||
goodblockset = {}
|
||||
@ -126,12 +126,11 @@ def processBlock(blockindex=None, blockhash=None):
|
||||
|
||||
# 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'])
|
||||
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)
|
||||
returnval = processTransaction(transaction_data, parsed_data, blockinfo)
|
||||
|
||||
if returnval == 1:
|
||||
acceptedTxList.append(transaction)
|
||||
@ -177,18 +176,17 @@ def updateLatestBlock(blockData):
|
||||
conn.close()
|
||||
|
||||
|
||||
def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress, transaction_data=None, parsed_data=None, isInfiniteToken=None):
|
||||
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}"}, Base)
|
||||
|
||||
if isInfiniteToken == True:
|
||||
# Make new entry
|
||||
session.add(ActiveTable(address=outputAddress, consumedpid='1', transferBalance=float(tokenAmount)))
|
||||
|
||||
block_data = newMultiRequest('block/{}'.format(transaction_data['blockhash']))
|
||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||
session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
||||
transferAmount=tokenAmount, blockNumber=block_data['height'],
|
||||
blockHash=block_data['hash'], time=block_data['time'],
|
||||
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'],
|
||||
@ -212,7 +210,6 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
|
||||
|
||||
elif availableTokens >= commentTransferAmount:
|
||||
table = session.query(ActiveTable).filter(ActiveTable.address == inputAddress).all()
|
||||
block_data = newMultiRequest('block/{}'.format(transaction_data['blockhash']))
|
||||
|
||||
pidlst = []
|
||||
checksum = 0
|
||||
@ -233,7 +230,7 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
|
||||
session.add(TransferLogs(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
||||
transferAmount=entry[0].transferBalance, sourceId=piditem[0],
|
||||
destinationId=lastid + 1,
|
||||
blockNumber=block_data['height'], time=block_data['time'],
|
||||
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
||||
transactionHash=transaction_data['txid']))
|
||||
entry[0].transferBalance = 0
|
||||
|
||||
@ -276,7 +273,7 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
|
||||
session.add(TransferLogs(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
||||
transferAmount=entry[0].transferBalance, sourceId=piditem[0],
|
||||
destinationId=lastid + 1,
|
||||
blockNumber=block_data['height'], time=block_data['time'],
|
||||
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
||||
transactionHash=transaction_data['txid']))
|
||||
entry[0].transferBalance = 0
|
||||
consumedpid_string = consumedpid_string + '{},'.format(piditem[0])
|
||||
@ -285,7 +282,7 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
|
||||
transferAmount=piditem[1] - (checksum - commentTransferAmount),
|
||||
sourceId=piditem[0],
|
||||
destinationId=lastid + 1,
|
||||
blockNumber=block_data['height'], time=block_data['time'],
|
||||
blockNumber=blockinfo['height'], time=blockinfo['time'],
|
||||
transactionHash=transaction_data['txid']))
|
||||
entry[0].transferBalance = checksum - commentTransferAmount
|
||||
|
||||
@ -317,12 +314,10 @@ def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress,
|
||||
session.commit()
|
||||
session.commit()
|
||||
|
||||
block_data = newMultiRequest('block/{}'.format(transaction_data['blockhash']))
|
||||
|
||||
blockchainReference = neturl + 'tx/' + transaction_data['txid']
|
||||
session.add(TransactionHistory(sourceFloAddress=inputAddress, destFloAddress=outputAddress,
|
||||
transferAmount=tokenAmount, blockNumber=block_data['height'],
|
||||
blockHash=block_data['hash'], time=block_data['time'],
|
||||
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'],
|
||||
@ -383,17 +378,12 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
|
||||
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])
|
||||
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")
|
||||
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(
|
||||
connection.execute('update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format(
|
||||
(participant[1], participant[0], participant[2])))
|
||||
|
||||
# add transaction to ContractTransactionHistory
|
||||
@ -440,7 +430,7 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
payeeAddress = contractStructure['payeeAddress']
|
||||
tokenIdentification = contractStructure['tokenIdentification']
|
||||
contractAddress = contractStructure['contractAddress']
|
||||
returnval = transferToken(tokenIdentification, tokenAmount_sum, contractAddress, payeeAddress)
|
||||
returnval = transferToken(tokenIdentification, tokenAmount_sum, contractAddress, payeeAddress, blockinfo = blockinfo)
|
||||
if returnval is None:
|
||||
logger.critical(
|
||||
"Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
||||
@ -490,9 +480,7 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
connection.execute('select sum(tokenAmount) 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]
|
||||
contractParticipants = connection.execute('select participantAddress, tokenAmount, transactionHash from contractparticipants').fetchall()[0][0]
|
||||
|
||||
for participant in contractParticipants:
|
||||
tokenIdentification = connection.execute(
|
||||
@ -502,8 +490,7 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
contractAddress = connection.execute(
|
||||
'select * from contractstructure where attribute="contractAddress"').fetchall()[0][
|
||||
0]
|
||||
returnval = transferToken(tokenIdentification, participant[1], contractAddress,
|
||||
participant[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")
|
||||
@ -524,14 +511,11 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
session.close()
|
||||
|
||||
connection = create_database_connection('system_dbs', {'db_name':'system'})
|
||||
connection.execute(
|
||||
'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format(
|
||||
connection.execute('update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format(
|
||||
contract[0], contract[1]))
|
||||
connection.execute(
|
||||
'update activecontracts set closeDate="{}" where contractName="{}" and contractAddress="{}"'.format(
|
||||
connection.execute('update activecontracts set closeDate="{}" where contractName="{}" and contractAddress="{}"'.format(
|
||||
blockinfo['time'], contract[0], contract[1]))
|
||||
connection.execute(
|
||||
'update activecontracts set expiryDate="{}" where contractName="{}" and contractAddress="{}"'.format(
|
||||
connection.execute('update activecontracts set expiryDate="{}" where contractName="{}" and contractAddress="{}"'.format(
|
||||
blockinfo['time'], contract[0], contract[1]))
|
||||
connection.close()
|
||||
return
|
||||
@ -541,12 +525,10 @@ def checkLocaltriggerContracts(blockinfo):
|
||||
tokenIdentification = contractStructure['tokenIdentification']
|
||||
contractAddress = contractStructure['contractAddress']
|
||||
connection = create_database_connection('smart_contract', {'contract_name':f"{contract[0]}", 'contract_address':f"{contract[1]}"})
|
||||
tokenAmount_sum = \
|
||||
connection.execute('select sum(tokenAmount) from contractparticipants').fetchall()[0][0]
|
||||
returnval = transferToken(tokenIdentification, tokenAmount_sum, contractAddress, payeeAddress)
|
||||
tokenAmount_sum = connection.execute('select sum(tokenAmount) from contractparticipants').fetchall()[0][0]
|
||||
returnval = transferToken(tokenIdentification, tokenAmount_sum, contractAddress, payeeAddress, blockinfo = blockinfo)
|
||||
if returnval is None:
|
||||
logger.critical(
|
||||
"Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
||||
logger.critical("Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
||||
return
|
||||
connection.execute('update contractparticipants set winningAmount="{}"'.format(0))
|
||||
|
||||
@ -581,7 +563,7 @@ def checkReturnDeposits(blockinfo):
|
||||
pass
|
||||
|
||||
|
||||
def processTransaction(transaction_data, parsed_data):
|
||||
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.
|
||||
@ -598,22 +580,28 @@ def processTransaction(transaction_data, parsed_data):
|
||||
querylist = []
|
||||
|
||||
# todo Rule 39 - Create a list of vins for a given transaction id
|
||||
for obj in transaction_data["vin"]:
|
||||
'''for obj in transaction_data["vin"]:
|
||||
if 'coinbase' in obj.keys():
|
||||
return 0
|
||||
querylist.append([obj["txid"], obj["vout"]])
|
||||
querylist.append([obj["txid"], obj["vout"]])'''
|
||||
|
||||
totalinputval = 0
|
||||
inputadd = ''
|
||||
#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 query in querylist:
|
||||
'''for query in querylist:
|
||||
content = newMultiRequest('tx/{}'.format(str(query[0])))
|
||||
for objec in content["vout"]:
|
||||
if objec["n"] == query[1]:
|
||||
inputadd = objec["scriptPubKey"]["addresses"][0]
|
||||
pdb.set_trace()
|
||||
totalinputval = totalinputval + float(objec["value"])
|
||||
vinlist.append([inputadd, objec["value"]])
|
||||
vinlist.append([inputadd, objec["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):
|
||||
@ -625,6 +613,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
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:
|
||||
@ -682,7 +671,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
isInfiniteToken = False
|
||||
|
||||
# Check if the transaction hash already exists in the token db
|
||||
connection = create_database_connection('token', {'token_name':f"{create_database_connection}"})
|
||||
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))
|
||||
@ -692,7 +681,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
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)
|
||||
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']}")
|
||||
@ -1048,7 +1037,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
# 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)
|
||||
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,
|
||||
@ -1109,7 +1098,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
# 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)
|
||||
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,
|
||||
@ -1171,7 +1160,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
# 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)
|
||||
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,
|
||||
@ -1222,7 +1211,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
# 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)
|
||||
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,
|
||||
@ -1363,7 +1352,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
elif contractStructure['priceType'] == 'dynamic':
|
||||
pass
|
||||
|
||||
returnval = transferToken(contractStructure['accepting_token'], swapPrice, inputlist[0],outputlist[0], transaction_data, parsed_data)
|
||||
returnval = transferToken(contractStructure['accepting_token'], swapPrice, 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']}")
|
||||
@ -1384,7 +1373,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
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)
|
||||
|
||||
returnval = transferToken(contractStructure['selling_token'], swapPrice, outputlist[0], inputlist[0], transaction_data, parsed_data)
|
||||
returnval = transferToken(contractStructure['selling_token'], swapPrice, outputlist[0], inputlist[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']}")
|
||||
@ -2132,8 +2121,7 @@ def processTransaction(transaction_data, parsed_data):
|
||||
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)
|
||||
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")
|
||||
@ -2194,11 +2182,9 @@ def processTransaction(transaction_data, parsed_data):
|
||||
|
||||
for winner in contractWinners:
|
||||
winnerAmount = "%.8f" % ((winner[2] / winnerSum) * tokenSum)
|
||||
returnval = transferToken(tokenIdentification, winnerAmount,
|
||||
outputlist[0], winner[1], transaction_data, parsed_data)
|
||||
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")
|
||||
logger.critical("Something went wrong in the token transfer method while doing local Smart Contract Trigger")
|
||||
return 0
|
||||
connection.execute(
|
||||
f"update contractparticipants set winningAmount='{winnerAmount}' where participantAddress='{winner[1]}' and transactionHash='{winner[4]}'")
|
||||
@ -2619,7 +2605,8 @@ logger.addHandler(stream_handler)
|
||||
|
||||
# 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')
|
||||
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()
|
||||
|
||||
apppath = os.path.dirname(os.path.realpath(__file__))
|
||||
@ -2679,45 +2666,46 @@ if args.reset == 1:
|
||||
session.close()
|
||||
|
||||
|
||||
# MAIN LOGIC STARTS
|
||||
# scan from the latest block saved locally to latest network block
|
||||
scanBlockchain()
|
||||
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
|
||||
# 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')
|
||||
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_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('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('block')
|
||||
def on_block(data):
|
||||
logger.info('New block received')
|
||||
logger.info(str(data))
|
||||
processBlock(blockhash=data)
|
||||
@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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user