From afe6b042f15e82e00c3553ef4e83ecc239a8fccb Mon Sep 17 00:00:00 2001 From: Vivek Teega Date: Mon, 24 Dec 2018 02:33:58 +0530 Subject: [PATCH] Cleaning the code --- .gitignore | 1 + comment_withtable.py | 271 -------------------------- config.ini | 9 + temp.py | 4 - update.py => track-tokens.py | 208 ++++++++++++-------- untitled.py | 364 ----------------------------------- 6 files changed, 143 insertions(+), 714 deletions(-) delete mode 100644 comment_withtable.py create mode 100644 config.ini delete mode 100644 temp.py rename update.py => track-tokens.py (62%) delete mode 100644 untitled.py diff --git a/.gitignore b/.gitignore index b7e822b..f59eb20 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ tree.db .idea/ +flo_addresses.txt diff --git a/comment_withtable.py b/comment_withtable.py deleted file mode 100644 index dd3fc44..0000000 --- a/comment_withtable.py +++ /dev/null @@ -1,271 +0,0 @@ -import requests -import json -import sqlite3 - -conn = sqlite3.connect('tree.db') -c = conn.cursor() - -c.execute("DROP TABLE IF EXISTS transactiontable") -c.execute("""CREATE TABLE transactiontable ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - address TEXT, - parentid INT, - transferBalance INT - )""") - -c.execute("DROP TABLE IF EXISTS transferlogs") -c.execute("""CREATE TABLE transferlogs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - primaryIDReference INTEGER, - transferDescription TEXT, - transferIDConsumed INT, - blockchainReference TEXT - )""") - -conn.commit() - - -#take in root address -root_address = "oaL5KH7UzkN8kGv2fWsrE2JFYYP5uLtHvz" -root_init_value = 1000 - -c.execute("INSERT INTO transactiontable ( address, parentid, transferBalance) VALUES (?,?,?)", (root_address, 0, root_init_value )) -conn.commit() - -transferDescription = "Root address = " + str(root_address) + " has been initialized with "+ str(root_init_value)+ " tokens" -blockchainReference = 'https://testnet.florincoin.info/tx/' -c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( 1, transferDescription, 0, blockchainReference)) - -#find root address's block -string = "https://testnet.florincoin.info/ext/getaddress/" + str(root_address) -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_trans_hash = '' -for cur in content["last_txs"]: - if cur["type"] == "vout": - root_trans_hash = cur["addresses"] - break - - -string = "https://testnet.florincoin.info/api/getrawtransaction?txid=" + str(root_trans_hash) +"&decrypt=1" -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_block_hash = content["blockhash"] - -string = "https://testnet.florincoin.info/api/getblock?hash=" + str(root_block_hash) -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_block_index = content["height"] - -print("root_block_index = " + str(root_block_index)) - -# get current block count -response = requests.get("https://testnet.florincoin.info/api/getblockcount") -current_index = json.loads(response.content.decode("utf-8")) - -def dothemagic(blockindex): - string = "https://testnet.florincoin.info/api/getblockhash?index=" + str(blockindex) - response = requests.get(string) - blockhash = response.content.decode("utf-8") - - string = "https://testnet.florincoin.info/api/getblock?hash=" + str(blockhash) - response = requests.get(string) - blockinfo = json.loads(response.content.decode("utf-8")) - - for transaction in blockinfo["tx"]: - string = "https://testnet.florincoin.info/api/getrawtransaction?txid="+ str(transaction) +"&decrypt=1" - response = requests.get(string) - data = json.loads(response.content.decode("utf-8")) - text = data["tx-comment"] - text = text[5:] - comment_list = text.split("#") - - if comment_list[0] == 'ranchimalltest': - - commentTransferAmount = int(comment_list[1]) - - outputlist = [] - for obj in data["vout"]: - if obj["scriptPubKey"]["type"] == "pubkeyhash": - temp = [] - temp.append(obj["scriptPubKey"]["addresses"][0]) - temp.append(obj["value"]) - - outputlist.append(temp) - - inputlist = [] - querylist = [] - - for obj in data["vin"]: - querylist.append([obj["txid"], obj["vout"]]) - - inputval = 0 - inputadd = '' - - for query in querylist: - string = "https://testnet.florincoin.info/api/getrawtransaction?txid="+ str(query[0]) +"&decrypt=1" - response = requests.get(string) - content = json.loads(response.content.decode("utf-8")) - - for objec in content["vout"]: - if objec["n"] == query[1]: - inputadd = objec["scriptPubKey"]["addresses"][0] - inputval = inputval + objec["value"] - - inputlist = [[inputadd, inputval]] - - - print("\n\nInput List") - print(inputlist) - print("\nOutput List") - print(outputlist) - - if len(inputlist) > 1: - print("Program has detected more than one input address ") - print("This transaction will be discarded") - continue - - c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?" , (inputlist[0][0],)) - availableTokens = c.fetchall() - availableTokens = availableTokens[0][0] - - if availableTokens is None: - print("The input address dosen't exist in our database ") - - elif availableTokens < commentTransferAmount: - print("\nThe transfer amount passed in the comments is more than the user owns\nHence we will be transfer all the user has\n") - - commentTransferAmount = availableTokens - - for output in outputlist: - if output[0] == inputlist[0][0]: - continue - - c.execute("SELECT * FROM transactiontable WHERE address=?",(inputlist[0][0],)) - table = c.fetchall() - - pidlst = [] - checksum = 0 - for row in table: - if checksum >= outputlist[0][1]: - break - pidlst.append(row[0]) - checksum = checksum + row[3] - - balance = commentTransferAmount - - for pid in pidlst: - c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,)) - temp = c.fetchall() - temp = temp[0][0] - - if balance <= temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0],pid,balance)) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp-balance, pid)) - - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = "$$ " +str(balance) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(temp-balance) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - VALUES (?,?)""", ( pid, transferDescription)) - - balance = 0 - conn.commit() - elif balance > temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0], pid, temp )) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (0, pid)) - - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = "$$ " + str(temp) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(0) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - VALUES (?,?)""", ( pid, transferDescription)) - - balance = balance - temp - conn.commit() - - elif availableTokens >= commentTransferAmount: - for output in outputlist: - if output[0] == inputlist[0][0]: - continue - - c.execute("SELECT * FROM transactiontable WHERE address=?",(inputlist[0][0],)) - table = c.fetchall() - - pidlst = [] - checksum = 0 - for row in table: - if checksum >= outputlist[0][1]: - break - pidlst.append(row[0]) - checksum = checksum + row[3] - - balance = commentTransferAmount - - for pid in pidlst: - c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,)) - temp = c.fetchall() - temp = temp[0][0] - - if balance <= temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0],pid,balance)) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp-balance, pid)) - - - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = str(balance) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = "balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(temp-balance) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - VALUES (?,?)""", ( pid, transferDescription)) - - balance = 0 - conn.commit() - elif balance > temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0], pid, temp )) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (0, pid)) - - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = str(temp) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = "balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(0) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - VALUES (?,?)""", ( pid, transferDescription)) - - - balance = balance - temp - conn.commit() - - -#current_index = 19431 -root_block_index = 19428 -# run loop and pass blockno -for blockindex in range(root_block_index + 1, current_index): - print(blockindex) - dothemagic(blockindex) - -conn.commit() -conn.close() \ No newline at end of file diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..6e17076 --- /dev/null +++ b/config.ini @@ -0,0 +1,9 @@ +[DEFAULT] +DB_NAME = tree.db +ROOT_ADDRESS = oPounjEbJxY7YCBaVBm61Lf2ym9DgFnAdu +INIT_TOKEN_NO = 21000000 + +;if 'ranchimalltest#100' is on blockchain, set value to 'ranchimalltest' +PREFIX = ranchimalltest + + diff --git a/temp.py b/temp.py deleted file mode 100644 index 1985a10..0000000 --- a/temp.py +++ /dev/null @@ -1,4 +0,0 @@ -import os -basedir = os.path.abspath(os.path.dirname(__file__)) - -print(basedir) diff --git a/update.py b/track-tokens.py similarity index 62% rename from update.py rename to track-tokens.py index db76488..bd099b1 100644 --- a/update.py +++ b/track-tokens.py @@ -1,6 +1,8 @@ import requests import json import sqlite3 +import argparse +import configparser def listcheck(lst): @@ -12,7 +14,7 @@ def listcheck(lst): return 1 return 0 -def dothemagic(blockindex): +def startTracking(blockindex, config, conn, c): string = "https://testnet.florincoin.info/api/getblockhash?index=" + str(blockindex) response = requests.get(string) blockhash = response.content.decode("utf-8") @@ -25,12 +27,12 @@ def dothemagic(blockindex): string = "https://testnet.florincoin.info/api/getrawtransaction?txid="+ str(transaction) +"&decrypt=1" response = requests.get(string) data = json.loads(response.content.decode("utf-8")) - text = data["tx-comment"] + text = data["floData"] text = text[5:] comment_list = text.split("#") - if comment_list[0] == 'ranchimalltest': - + if comment_list[0] == config['DEFAULT']['PREFIX']: + print("I just saw "+config['DEFAULT']['PREFIX']) commentTransferAmount = comment_list[1:] #if not all(isinstance(x, (int, float)) for x in commentTransferAmount): @@ -106,67 +108,6 @@ def dothemagic(blockindex): print("\nThe transfer amount passed in the comments is more than the user owns\nThis transaction will be discarded\n") continue - # commentTransferAmount = availableTokens - - # for output in outputlist: - # if output[0] == inputlist[0][0]: - # continue - - # c.execute("SELECT * FROM transactiontable WHERE address=?",(inputlist[0][0],)) - # table = c.fetchall() - - # pidlst = [] - # checksum = 0 - # for row in table: - # if checksum >= outputlist[0][1]: - # break - # pidlst.append(row[0]) - # checksum = checksum + row[3] - - # balance = commentTransferAmount - - # for pid in pidlst: - # c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,)) - # temp = c.fetchall() - # temp = temp[0][0] - - # if balance <= temp: - # c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0],pid,balance)) - # c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp-balance, pid)) - - # c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - # lastid = c.fetchall()[0][0] - # transferDescription = "$$ " +str(balance) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - # VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - # transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(temp-balance) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - # VALUES (?,?)""", ( pid, transferDescription)) - - # balance = 0 - # conn.commit() - # elif balance > temp: - # c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0], pid, temp )) - # c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (0, pid)) - - # c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - # lastid = c.fetchall()[0][0] - # transferDescription = "$$ " + str(temp) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - # VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - # transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(0) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - # VALUES (?,?)""", ( pid, transferDescription)) - - # balance = balance - temp - # conn.commit() - elif availableTokens >= sum(commentTransferAmount): if len(commentTransferAmount) != len(outputlist): print("The parameters in the comments aren't enough") @@ -219,6 +160,11 @@ def dothemagic(blockindex): c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, blockchainReference) VALUES (?,?,?)""", ( pid, transferDescription, blockchainReference)) + ## transaction history table ## + c.execute( + '''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', + (blockindex, inputlist[0][0], outputlist[0][0], str(balance), blockchainReference)) + ##webpage table section ## transferDescription = str(commentTransferAmount[i]) + " tokens transferred from " + str(inputlist[0][0]) + " to " + str(outputlist[i][0]) c.execute("""INSERT INTO webtable (transferDescription, blockchainReference) @@ -251,27 +197,139 @@ def dothemagic(blockindex): c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, blockchainReference) VALUES (?,?,?)""", ( pid, transferDescription, blockchainReference)) + ## transaction history table ## + c.execute( + '''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', + (blockindex, inputlist[0][0], outputlist[0][0], str(balance), blockchainReference)) balance = balance - temp conn.commit() -def update(): - conn = sqlite3.connect('tree.db') +def resetDatabase(c, conn): + c.execute("DROP TABLE IF EXISTS transactiontable") + c.execute("""CREATE TABLE transactiontable ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + address TEXT, + parentid INT, + transferBalance REAL + )""") + + c.execute("DROP TABLE IF EXISTS transferlogs") + c.execute("""CREATE TABLE transferlogs ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + primaryIDReference INTEGER, + transferDescription TEXT, + transferIDConsumed INT, + blockchainReference TEXT + )""") + + c.execute("DROP TABLE IF EXISTS webtable") + c.execute("""CREATE TABLE webtable ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + transferDescription TEXT, + blockchainReference TEXT + )""") + + c.execute("DROP TABLE IF EXISTS transactionHistory") + c.execute("""CREATE TABLE transactionHistory ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + blockno INT, + fromAddress TEXT, + toAddress TEXT, + amount REAL, + blockchainReference TEXT + )""") + + c.execute("DROP TABLE IF EXISTS extra") + c.execute("CREATE TABLE extra ( id INTEGER PRIMARY KEY , lastblockscanned INT)") + conn.commit() + + +def main(): + # Read configuration + config = configparser.ConfigParser() + config.read('config.ini') + + + # 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. 0 -> Keep db. 1 -> Purge db ') + args = parser.parse_args() + + # Connect to db + conn = sqlite3.connect(config['DEFAULT']['DB_NAME']) c = conn.cursor() - # get current block count + + # get current block height response = requests.get("https://testnet.florincoin.info/api/getblockcount") current_index = json.loads(response.content.decode("utf-8")) + print("current_block_height : " + str(current_index)) - c.execute("SELECT lastblockscanned FROM extra WHERE id=1") - lastblockscanned = c.fetchall()[0][0] + if args.reset == 1: + resetDatabase(c, conn) + # Read root address + root_address = config['DEFAULT']['ROOT_ADDRESS'] + root_init_value = int(config['DEFAULT']['INIT_TOKEN_NO']) - #print("last scanned block " + str(lastblockscanned)) + # Find root address's block no + string = "https://testnet.florincoin.info/ext/getaddress/" + str(root_address) + response = requests.get(string) + content = json.loads(response.content.decode("utf-8")) + root_trans_hash = '' + for cur in content["last_txs"]: + if cur["type"] == "vout": + root_trans_hash = cur["addresses"] + break + + string = "https://testnet.florincoin.info/api/getrawtransaction?txid=" + str(root_trans_hash) + "&decrypt=1" + response = requests.get(string) + content = json.loads(response.content.decode("utf-8")) + root_block_hash = content["blockhash"] + + string = "https://testnet.florincoin.info/api/getblock?hash=" + str(root_block_hash) + response = requests.get(string) + content = json.loads(response.content.decode("utf-8")) + root_block_index = content["height"] + # root_block_index = 26066 + + print("root_block_index = " + str(root_block_index)) + + c.execute("INSERT INTO transactiontable ( address, parentid, transferBalance) VALUES (?,?,?)", + (root_address, 0, root_init_value)) + conn.commit() + + transferDescription = "Root address = " + str(root_address) + " has been initialized with " + str( + root_init_value) + " tokens" + blockchainReference = 'https://testnet.florincoin.info/tx/' + c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) + VALUES (?,?,?,?)""", + (1, transferDescription, 0, blockchainReference)) + + c.execute( + '''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', + (root_block_index, '', root_address, root_init_value, blockchainReference)) + + c.execute("INSERT INTO extra (id, lastblockscanned) VALUES (?,?)", (1, root_block_index)) + conn.commit() + lastblockscanned = root_block_index + + else: + response = requests.get("https://testnet.florincoin.info/api/getblockcount") + current_index = json.loads(response.content.decode("utf-8")) + + c.execute("SELECT lastblockscanned FROM extra WHERE id=1") + lastblockscanned = c.fetchall()[0][0] + + + # run loop and pass blockno for blockindex in range(lastblockscanned + 1, current_index+1): - print(blockindex) - #dothemagic(blockindex) - #c.execute("UPDATE extra SET lastblockscanned=? WHERE id=?", (blockindex,1)) + print(blockindex) + startTracking(blockindex, config, conn, c) + c.execute("UPDATE extra SET lastblockscanned=? WHERE id=?", (blockindex,1)) + conn.commit() conn.commit() conn.close() -update() \ No newline at end of file +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/untitled.py b/untitled.py deleted file mode 100644 index 24a3c13..0000000 --- a/untitled.py +++ /dev/null @@ -1,364 +0,0 @@ -import requests -import json -import sqlite3 - -conn = sqlite3.connect('tree.db') -c = conn.cursor() - -c.execute("DROP TABLE IF EXISTS transactiontable") -c.execute("""CREATE TABLE transactiontable ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - address TEXT, - parentid INT, - transferBalance REAL - )""") - -c.execute("DROP TABLE IF EXISTS transferlogs") -c.execute("""CREATE TABLE transferlogs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - primaryIDReference INTEGER, - transferDescription TEXT, - transferIDConsumed INT, - blockchainReference TEXT - )""") - -c.execute("DROP TABLE IF EXISTS webtable") -c.execute("""CREATE TABLE webtable ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - transferDescription TEXT, - blockchainReference TEXT - )""") - -c.execute("DROP TABLE IF EXISTS transactionHistory") -c.execute("""CREATE TABLE transactionHistory ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - blockno INT, - fromAddress TEXT, - toAddress TEXT, - amount REAL, - blockchainReference TEXT - )""") - -c.execute("DROP TABLE IF EXISTS extra") -c.execute("CREATE TABLE extra ( id INTEGER PRIMARY KEY , lastblockscanned INT)") -conn.commit() - - -#take in root address -root_address = "oPounjEbJxY7YCBaVBm61Lf2ym9DgFnAdu" -root_init_value = 21000 - -#find root address's block -string = "https://testnet.florincoin.info/ext/getaddress/" + str(root_address) -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_trans_hash = '' -for cur in content["last_txs"]: - if cur["type"] == "vout": - root_trans_hash = cur["addresses"] - break - - -string = "https://testnet.florincoin.info/api/getrawtransaction?txid=" + str(root_trans_hash) +"&decrypt=1" -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_block_hash = content["blockhash"] - -string = "https://testnet.florincoin.info/api/getblock?hash=" + str(root_block_hash) -response = requests.get(string) -content = json.loads(response.content.decode("utf-8")) -root_block_index = content["height"] -#root_block_index = 26066 - -print("root_block_index = " + str(root_block_index)) - -c.execute("INSERT INTO transactiontable ( address, parentid, transferBalance) VALUES (?,?,?)", (root_address, 0, root_init_value )) -conn.commit() - -transferDescription = "Root address = " + str(root_address) + " has been initialized with "+ str(root_init_value)+ " tokens" -blockchainReference = 'https://testnet.florincoin.info/tx/' -c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( 1, transferDescription, 0, blockchainReference)) - -c.execute('''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', (root_block_index, '',root_address, root_init_value, blockchainReference)) - - -# get current block count -response = requests.get("https://testnet.florincoin.info/api/getblockcount") -current_index = json.loads(response.content.decode("utf-8")) -print("current_block_index : " + str(current_index)) - -def listcheck(lst): - for element in lst: - try: - float(element) - except ValueError: - print("values to be transferred aren't in the right format") - return 1 - return 0 - -def dothemagic(blockindex): - string = "https://testnet.florincoin.info/api/getblockhash?index=" + str(blockindex) - response = requests.get(string) - blockhash = response.content.decode("utf-8") - - string = "https://testnet.florincoin.info/api/getblock?hash=" + str(blockhash) - response = requests.get(string) - blockinfo = json.loads(response.content.decode("utf-8")) - - for transaction in blockinfo["tx"]: - string = "https://testnet.florincoin.info/api/getrawtransaction?txid="+ str(transaction) +"&decrypt=1" - response = requests.get(string) - data = json.loads(response.content.decode("utf-8")) - text = data["floData"] - text = text[5:] - comment_list = text.split("#") - - if comment_list[0] == 'ranchimalltest': - print("I just saw ranchimalltest") - commentTransferAmount = comment_list[1:] - - #if not all(isinstance(x, (int, float)) for x in commentTransferAmount): - # print("Values to be transffered aren't in the right format") - # continue - - if len(commentTransferAmount) == 0: - print("Value for token transfer has not been specified") - continue - - returnval = listcheck(commentTransferAmount) - if returnval == 1: - continue - - commentTransferAmount = list(map(float, commentTransferAmount)) - - inputlist = [] - querylist = [] - - for obj in data["vin"]: - querylist.append([obj["txid"], obj["vout"]]) - - inputval = 0 - inputadd = '' - - for query in querylist: - string = "https://testnet.florincoin.info/api/getrawtransaction?txid="+ str(query[0]) +"&decrypt=1" - response = requests.get(string) - content = json.loads(response.content.decode("utf-8")) - - for objec in content["vout"]: - if objec["n"] == query[1]: - inputadd = objec["scriptPubKey"]["addresses"][0] - inputval = inputval + objec["value"] - - inputlist = [[inputadd, inputval]] - - if len(inputlist) > 1: - print("Program has detected more than one input address ") - print("This transaction will be discarded") - continue - - outputlist = [] - for obj in data["vout"]: - if obj["scriptPubKey"]["type"] == "pubkeyhash": - if inputlist[0][0] == obj["scriptPubKey"]["addresses"][0]: - continue - temp = [] - temp.append(obj["scriptPubKey"]["addresses"][0]) - temp.append(obj["value"]) - - outputlist.append(temp) - - - print("\n\nInput List") - print(inputlist) - print("\nOutput List") - print(outputlist) - - if len(inputlist) > 1: - print("Program has detected more than one input address ") - print("This transaction will be discarded") - continue - - c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?" , (inputlist[0][0],)) - availableTokens = c.fetchall() - availableTokens = availableTokens[0][0] - - if availableTokens is None: - print("The input address dosen't exist in our database ") - - elif availableTokens < sum(commentTransferAmount): - print("\nThe transfer amount passed in the comments is more than the user owns\nThis transaction will be discarded\n") - continue - - # commentTransferAmount = availableTokens - - # for output in outputlist: - # if output[0] == inputlist[0][0]: - # continue - - # c.execute("SELECT * FROM transactiontable WHERE address=?",(inputlist[0][0],)) - # table = c.fetchall() - - # pidlst = [] - # checksum = 0 - # for row in table: - # if checksum >= outputlist[0][1]: - # break - # pidlst.append(row[0]) - # checksum = checksum + row[3] - - # balance = commentTransferAmount - - # for pid in pidlst: - # c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,)) - # temp = c.fetchall() - # temp = temp[0][0] - - # if balance <= temp: - # c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0],pid,balance)) - # c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp-balance, pid)) - - # c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - # lastid = c.fetchall()[0][0] - # transferDescription = "$$ " +str(balance) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - # VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - # transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(temp-balance) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - # VALUES (?,?)""", ( pid, transferDescription)) - - # balance = 0 - # conn.commit() - # elif balance > temp: - # c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (output[0], pid, temp )) - # c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (0, pid)) - - # c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - # lastid = c.fetchall()[0][0] - # transferDescription = "$$ " + str(temp) + " tokens transferred to " + str(output[0]) + " from pid = " + str(pid) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - # VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - # transferDescription = "$$ balance in id = " + str(pid) + " UPDATED from " + str(temp) + " to " + str(0) - # blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - # c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription) - # VALUES (?,?)""", ( pid, transferDescription)) - - # balance = balance - temp - # conn.commit() - - elif availableTokens >= sum(commentTransferAmount): - if len(commentTransferAmount) != len(outputlist): - print("The parameters in the comments aren't enough") - print("This transaction will be discarded") - continue - - for i in range(len(commentTransferAmount)): - # if output[0] == inputlist[0][0]: - # continue - - c.execute("SELECT * FROM transactiontable WHERE address=?",(inputlist[0][0],)) - table = c.fetchall() - - pidlst = [] - checksum = 0 - for row in table: - if checksum >= commentTransferAmount[i]: - break - pidlst.append(row[0]) - checksum = checksum + row[3] - - balance = commentTransferAmount[i] - c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?", ( outputlist[i][0],)) - opbalance = c.fetchall()[0][0] - if opbalance is None: - opbalance = 0 - - c.execute("SELECT sum(transferBalance) FROM transactiontable WHERE address=?", ( inputlist[0][0],)) - ipbalance = c.fetchall()[0][0] - - for pid in pidlst: - c.execute("SELECT transferBalance FROM transactiontable WHERE id=?", (pid,)) - temp = c.fetchall() - temp = temp[0][0] - - if balance <= temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (outputlist[i][0],pid,balance)) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (temp-balance, pid)) - - ## transaction logs section ## - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = str(balance) + " tokens transferred to " + str(outputlist[i][0]) + " from " + str(inputlist[0][0]) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = str(inputlist[0][0]) + " balance UPDATED from " + str(temp) + " to " + str(temp-balance) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, blockchainReference) - VALUES (?,?,?)""", ( pid, transferDescription, blockchainReference)) - - ## transaction history table ## - c.execute( - '''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', - (blockindex, inputlist[0][0], outputlist[0][0], str(balance), blockchainReference)) - - ##webpage table section ## - transferDescription = str(commentTransferAmount[i]) + " tokens transferred from " + str(inputlist[0][0]) + " to " + str(outputlist[i][0]) - c.execute("""INSERT INTO webtable (transferDescription, blockchainReference) - VALUES (?,?)""", ( transferDescription, blockchainReference)) - - transferDescription = "UPDATE " + str(outputlist[i][0]) + " balance from " + str(opbalance) + " to " + str(opbalance + commentTransferAmount[i]) - c.execute("""INSERT INTO webtable (transferDescription, blockchainReference) - VALUES (?,?)""", ( transferDescription, blockchainReference)) - - transferDescription = "UPDATE " + str(inputlist[0][0]) + " balance from " + str(ipbalance) + " to " + str(ipbalance - commentTransferAmount[i]) - c.execute("""INSERT INTO webtable (transferDescription, blockchainReference) - VALUES (?,?)""", ( transferDescription, blockchainReference)) - - balance = 0 - conn.commit() - elif balance > temp: - c.execute("INSERT INTO transactiontable (address, parentid, transferBalance) VALUES (?,?,?)", (outputlist[i][0], pid, temp )) - c.execute("UPDATE transactiontable SET transferBalance=? WHERE id=?", (0, pid)) - - ##transaction logs section ## - c.execute("SELECT id FROM transactiontable ORDER BY id DESC LIMIT 1") - lastid = c.fetchall()[0][0] - transferDescription = str(temp) + " tokens transferred to " + str(outputlist[i][0]) + " from " + str(inputlist[0][0]) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, transferIDConsumed, blockchainReference) - VALUES (?,?,?,?)""", ( lastid, transferDescription, pid, blockchainReference)) - - transferDescription = str() + " balance UPDATED from " + str(temp) + " to " + str(0) - blockchainReference = 'https://testnet.florincoin.info/tx/' + str(transaction) - c.execute("""INSERT INTO transferlogs (primaryIDReference, transferDescription, blockchainReference) - VALUES (?,?,?)""", ( pid, transferDescription, blockchainReference)) - - ## transaction history table ## - c.execute( - '''INSERT INTO transactionHistory (blockno, fromAddress, toAddress, amount, blockchainReference) VALUES (?,?,?,?,?)''', - (blockindex, inputlist[0][0], outputlist[0][0], str(balance), blockchainReference)) - - balance = balance - temp - conn.commit() - - -#current_index = 19431 -#root_block_index = 19428 - -c.execute("INSERT INTO extra (id, lastblockscanned) VALUES (?,?)", (1, root_block_index)) -# run loop and pass blockno -for blockindex in range(root_block_index + 1, current_index+1): - print(blockindex) - dothemagic(blockindex) - c.execute("UPDATE extra SET lastblockscanned=? WHERE id=?", (blockindex,1)) - -conn.commit() -conn.close() \ No newline at end of file