From 09fa370d0406bc5c68597b3844b4a7b6b52a774f Mon Sep 17 00:00:00 2001 From: Vivek Teega Date: Fri, 31 May 2019 15:03:49 +0530 Subject: [PATCH] Adding more columns for sqlite3 db --- models.py | 12 ++++++ tracktokens-smartcontracts.py | 81 +++++++++++++++++++++++++++-------- 2 files changed, 76 insertions(+), 17 deletions(-) diff --git a/models.py b/models.py index 9fe813e..0320959 100644 --- a/models.py +++ b/models.py @@ -14,6 +14,7 @@ class ActiveTable(Base): consumedpid = Column('consumedpid', String) transferBalance = Column('transferBalance', Float) + class ConsumedTable(Base): __tablename__ = "consumedTable" @@ -24,6 +25,7 @@ class ConsumedTable(Base): consumedpid = Column('consumedpid', String) transferBalance = Column('transferBalance', Float) + class TransferLogs(Base): __tablename__ = "transferlogs" @@ -37,6 +39,7 @@ class TransferLogs(Base): time = Column('time', Integer) transactionHash = Column('transactionHash', String) + class TransactionHistory(Base): __tablename__ = "transactionHistory" @@ -49,6 +52,7 @@ class TransactionHistory(Base): transactionHash = Column('transactionHash', String) blockchainReference = Column('blockchainReference', String) + class ContractStructure(ContractBase): __tablename__ = "contractstructure" @@ -57,6 +61,7 @@ class ContractStructure(ContractBase): index = Column('index', Integer) value = Column('value', String) + class ContractParticipants(ContractBase): __tablename__ = "contractparticipants" @@ -65,6 +70,8 @@ class ContractParticipants(ContractBase): tokenAmount = Column('tokenAmount', Float) userChoice = Column('userChoice', String) transactionHash = Column('transactionHash', String) + winningAmount = Column('winningAmount', Float) + class ActiveContracts(SystemBase): __tablename__ = "activecontracts" @@ -74,6 +81,10 @@ class ActiveContracts(SystemBase): contractAddress = Column('contractAddress', String) status = Column('status', String) transactionHash = Column('transactionHash', String) + incorporationDate = Column('incorporationDate', String) + expiryDate = Column('expiryDate', String) + closeDate = Column('closeDate', String) + class SystemData(SystemBase): __tablename__ = "systemData" @@ -90,5 +101,6 @@ class ContractParticipantMapping(SystemBase): contractName = Column('contractName', String) contractAddress = Column('contractAddress', String) tokenAmount = Column('tokenAmount', Float) + transactionHash = Column('transactionHash', String) diff --git a/tracktokens-smartcontracts.py b/tracktokens-smartcontracts.py index 851e2bd..430df97 100755 --- a/tracktokens-smartcontracts.py +++ b/tracktokens-smartcontracts.py @@ -175,8 +175,7 @@ def checkLocaltriggerContracts(blockinfo): 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:]) + expirytime_object = parsing.arrow.get(parse_string, 'YYYY/M/D HH:mm:ss').replace(tzinfo=expirytime_split[5][3:]) blocktime_object = parsing.arrow.get(blockinfo['time']).to('IST') if blocktime_object > expirytime_object: @@ -185,20 +184,37 @@ def checkLocaltriggerContracts(blockinfo): tokenAmount_sum = 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 from contractparticipants').fetchall()[0][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]) if returnval is None: - print("Something went wrong in the token transfer method while doing local Smart Contract Trigger") + print("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]))) engine = create_engine('sqlite:///system.db', echo=True) connection = engine.connect() 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(block_data['time'], + contract[0], contract[1])) connection.close() + engine = create_engine('sqlite:///system.db', echo=True) + connection = engine.connect() + connection.execute( + 'update activecontracts set status="expired" where contractName="{}" and contractAddress="{}"'.format( + contract[0], contract[1])) + connection.execute( + 'update activecontracts set expirydate="{}" where contractName="{}" and contractAddress="{}"'.format(block_data['time'], + contract[0], contract[1])) + connection.close() + else: # This is a blockchain trigger contract @@ -214,11 +230,16 @@ def checkLocaltriggerContracts(blockinfo): if returnval is None: print("Something went wrong in the token transfer method while doing local Smart Contract Trigger") return + connection.execute( + 'update contractparticipants set winningAmount="{}"'.format( + (0))) engine = create_engine('sqlite:///system.db', echo=False) connection = engine.connect() connection.execute( - 'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format( - contract[0], contract[1])) + '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( + block_data['time'], contract[0], contract[1])) connection.close() expiryTime = connection.execute('select value from contractstructure where attribute=="expiryTime"').fetchall()[0][0] @@ -235,7 +256,7 @@ def checkLocaltriggerContracts(blockinfo): if tokenAmount_sum < minimumsubscriptionamount: # Initialize payback to contract participants contractParticipants = connection.execute( - 'select participantAddress, tokenAmount from contractparticipants').fetchall()[0][0] + 'select participantAddress, tokenAmount, transactionHash from contractparticipants').fetchall()[0][0] for participant in contractParticipants: tokenIdentification = connection.execute( @@ -249,11 +270,15 @@ def checkLocaltriggerContracts(blockinfo): print( "Something went wrong in the token transfer method while doing local Smart Contract Trigger") return + connection.execute('update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format((participant[1], participant[0], participant[2]))) engine = create_engine('sqlite:///system.db', echo=False) connection = engine.connect() 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( + block_data['time'],contract[0], contract[1])) connection.close() # Trigger the contract @@ -264,11 +289,15 @@ def checkLocaltriggerContracts(blockinfo): if returnval is None: print("Something went wrong in the token transfer method while doing local Smart Contract Trigger") return + connection.execute('update contractparticipants set winningAmount="{}"'.format(0)) engine = create_engine('sqlite:///system.db', echo=False) connection = engine.connect() 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( + block_data['time'], contract[0], contract[1])) connection.close() @@ -649,7 +678,7 @@ def startWorking(transaction_data, parsed_data, blockinfo): SystemBase.metadata.create_all(bind=engine) session = sessionmaker(bind=engine)() session.add(ActiveContracts(contractName=parsed_data['contractName'], - contractAddress=parsed_data['contractAddress'], status='active')) + contractAddress=parsed_data['contractAddress'], status='active', transactionHash=transaction_data['txid'], incorporationDate=blockinfo['time'])) session.commit() session.close() else: @@ -669,7 +698,7 @@ def startWorking(transaction_data, parsed_data, blockinfo): connection = engine.connect() # todo : Get only activeContracts which have non-local trigger ie. committee triggers them - contractDetails = connection.execute('select contractName, contractAddress from activecontracts where status=="active"').fetchall() + contractDetails = connection.execute('select contractName, contractAddress from activecontracts where status=="expired"').fetchall() connection.close() contractList = [] @@ -703,7 +732,6 @@ def startWorking(transaction_data, parsed_data, blockinfo): parsed_data['triggerCondition'])).fetchall()[0][0] tokenIdentification = connection.execute( 'select value from contractstructure where attribute="tokenIdentification"').fetchall()[0][0] - connection.close() for winner in contractWinners: returnval = transferToken(tokenIdentification, (winner[2] / winnerSum) * tokenSum, @@ -711,10 +739,16 @@ def startWorking(transaction_data, parsed_data, blockinfo): if returnval is None: print("CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Trigger") return + connection.execute( + 'update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format( + ((winner[2] / winnerSum) * tokenSum, winner[1], winner[4]))) engine = create_engine('sqlite:///system.db', echo=True) connection = engine.connect() connection.execute( 'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format(parsed_data['contractName'], outputlist[0])) + connection.execute( + 'update activecontracts set closeDate="{}" where contractName="{}" and contractAddress="{}"'.format(block_data['time'], + parsed_data['contractName'], outputlist[0])) connection.close() return @@ -756,7 +790,7 @@ def startWorking(transaction_data, parsed_data, blockinfo): echo=True) connection = engine.connect() contractParticipants = connection.execute( - 'select participantAddress, tokenAmount from contractparticipants').fetchall()[0][0] + 'select participantAddress, tokenAmount, transactionHash from contractparticipants').fetchall()[0][0] for participant in contractParticipants: tokenIdentification = connection.execute( @@ -770,12 +804,20 @@ def startWorking(transaction_data, parsed_data, blockinfo): print( "CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Trigger") return - engine = create_engine('sqlite:///system.db', echo=True) - connection = engine.connect() + connection.execute( - 'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format( - parsed_data['contractName'], outputlist[0])) - connection.close() + 'update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format( + (participant[1], participant[0], participant[4]))) + + engine = create_engine('sqlite:///system.db', echo=True) + connection = engine.connect() + 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(block_data['time'], + parsed_data['contractName'], outputlist[0])) + connection.close() return engine = create_engine('sqlite:///smartContracts/{}-{}.db'.format(parsed_data['contractName'], outputlist[0]), echo=True) @@ -784,7 +826,6 @@ def startWorking(transaction_data, parsed_data, blockinfo): tokenSum = connection.execute('select sum(tokenAmount) from contractparticipants').fetchall()[0][0] 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] - connection.close() for winner in contractWinners: winner = list(winner) @@ -793,11 +834,17 @@ def startWorking(transaction_data, parsed_data, blockinfo): print( "CRITICAL ERROR | Something went wrong in the token transfer method while doing local Smart Contract Trigger") return + connection.execute('update contractparticipants set winningAmount="{}" where participantAddress="{}" and transactionHash="{}"'.format((winner[2]/winnerSum)*tokenSum, winner[1], winner[4])) + connection.close() + engine = create_engine('sqlite:///system.db', echo=True) connection = engine.connect() connection.execute( 'update activecontracts set status="closed" where contractName="{}" and contractAddress="{}"'.format( parsed_data['contractName'], outputlist[0])) + connection.execute( + 'update activecontracts set closeDate="{}" where contractName="{}" and contractAddress="{}"'.format(block_data['time'], + parsed_data['contractName'], outputlist[0])) connection.close() else: print('Input address is not part of the committee address list. This trigger is rejected')