From 3ba6b95b72d0546585e9ec48ab8a5e1ac4a1f49d Mon Sep 17 00:00:00 2001 From: Vivek Teega Date: Wed, 12 Jun 2019 18:01:08 +0530 Subject: [PATCH] Keeping a map of address's first associtiation with a token --- models.py | 8 ++++++++ tracktokens-smartcontracts.py | 26 +++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/models.py b/models.py index 0320959..734d831 100644 --- a/models.py +++ b/models.py @@ -103,4 +103,12 @@ class ContractParticipantMapping(SystemBase): tokenAmount = Column('tokenAmount', Float) transactionHash = Column('transactionHash', String) +class TokenAddressMapping(SystemBase): + __tablename__ = "tokenAddressMapping" + + id = Column('id', Integer, primary_key=True) + tokenAddress = Column('tokenAddress', String) + token = Column('token', String) + transactionHash = Column('transactionHash', String) + diff --git a/tracktokens-smartcontracts.py b/tracktokens-smartcontracts.py index 76bfe7a..b9655ce 100755 --- a/tracktokens-smartcontracts.py +++ b/tracktokens-smartcontracts.py @@ -17,10 +17,9 @@ import pybtc def pushData_SSEapi(message): - url = 'https://ranchimallflo.duckdns.org/' signature = pybtc.sign_message(message, privKey) headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'Signature': signature} - r = requests.post(url, json={'message': '{}'.format(message)}, headers=headers) + r = requests.post(sseAPI_url, json={'message': '{}'.format(message)}, headers=headers) def transferToken(tokenIdentification, tokenAmount, inputAddress, outputAddress): @@ -401,11 +400,12 @@ def startWorking(transaction_data, parsed_data, blockinfo): connection = engine.connect() 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]): print('Transaction {} already exists in the token db. This is unusual, please check your code'.format(transaction_data['txid'])) - pushData_SSEapi('Error | Transaction {} already exists in the token db. This is unusual, please check your code'.format(transaction_data['txid']) + pushData_SSEapi('Error | Transaction {} already exists in the token db. This is unusual, please check your code'.format(transaction_data['txid'])) return returnval = transferToken(parsed_data['tokenIdentification'], parsed_data['tokenAmount'], inputlist[0], outputlist[0]) @@ -414,6 +414,17 @@ def startWorking(transaction_data, parsed_data, blockinfo): pushData_SSEapi('Error | Something went wrong while doing the internal db transactions for {}'.format(transaction_data['txid'])) return + # If this is the first interaction of the outputlist's address with the given token name, add it to token mapping + engine = create_engine('sqlite:///system.db'.format(parsed_data['tokenIdentification']), echo=True) + connection = engine.connect() + firstInteractionCheck = connection.execute('select * from tokenAddressMapping where tokenAddress="{}" and token="{}"'.format(outputlist[0], parsed_data['tokenIdentification'])).fetchall() + + if len(firstInteractionCheck) == 0: + connection.execute('INSERT INTO tokenAddressMapping [(tokenAddress, token, transactionHash)] VALUES ({}, {}, {});'.format(outputlist[0], parsed_data['tokenIdentification'])) + + connection.close() + + # Pass information to SSE channel url = 'https://ranchimallflo.duckdns.org/' headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} @@ -629,6 +640,15 @@ def startWorking(transaction_data, parsed_data, blockinfo): session.commit() session.close() + # If this is the first interaction of the outputlist's address with the given token name, add it to token mapping + engine = create_engine('sqlite:///system.db'.format(parsed_data['tokenIdentification']), echo=True) + connection = engine.connect() + connection.execute( + 'INSERT INTO tokenAddressMapping [(tokenAddress, token, transactionHash)] VALUES ({}, {}, {});'.format( + inputadd, parsed_data['tokenIdentification'], transaction_data['txid'])) + + connection.close() + pushData_SSEapi('Token | Succesfully incorporated token {} at transaction {}'.format( parsed_data['tokenIdentification'], transaction_data['txid'])) else: