· 7 years ago · Oct 21, 2018, 11:08 PM
1def save_address(new_address):
2 conn = sqlite3.connect("./db.sqlite")
3
4 #open db connection
5 c = conn.cursor()
6
7 #create address tables if not exist
8 c.execute("""CREATE TABLE IF NOT EXISTS addresses (
9 id integer PRIMARY KEY AUTOINCREMENT,
10 address text NOT NULL,
11 balance decimal(24, 2) DEFAULT 0.00,
12 locked decimal(24, 2) DEFAULT 0.00,
13 blockIndex integer NOT NULL,
14 scanIndex integer NOT NULL,
15 created integer DEFAULT CURRENT_TIMESTAMP
16 );""")
17
18 #create transaction table if not exist
19 c.execute("""CREATE TABLE IF NOT EXISTS transactions (
20 id integer PRIMARY KEY AUTOINCREMENT,
21 address text NOT NULL,
22 amount decimal(24, 2) DEFAULT 0.00,
23 fee decimal(24, 2) DEFAULT 0.00,
24 sfee decimal(24, 2) DEFAULT 0.00,
25 blockIndex integer NOT NULL,
26 transactionHash text NOT NULL,
27 paymendId text NOT NULL,
28 extra text NOT NULL,
29 timestamp integer NOT NULL,
30 confirms integer DEFAULT 0,
31 created integer DEFAULT CURRENT_TIMESTAMP
32 );""")
33
34 #prepare db insert
35 payload = ( new_address['address'], new_address['blockIndex'], new_address['blockIndex'])
36 c.execute('INSERT INTO addresses (address, blockIndex, scanIndex) VALUES (?, ? , ?)', payload)
37 print('[' + new_address['address'] + '] stored in db.')
38
39
40 #select and loop through addresses stored in sqlite
41 for addresses in c.execute('SELECT * from addresses;'):
42
43 address = addresses[1]
44 blockIndex = addresses[4]
45 scanIndex = addresses[5]
46 newIndex = scanIndex + 100
47
48 #check if addresses needs to be scanned
49 getStatus = ts.getStatus()
50
51 knownBlockCount = getStatus[1]['blockIndex']
52 heightDiff = knownBlockCount - scanIndex
53
54 if scanIndex >= knownBlockCount:
55 print('Reached top of chain.')
56
57 if heightDiff < 100:
58 newIndex = knownBlockCount
59
60 #scan each address for transactions
61 incoming_txs = ts.scanAddress(address, int(scanIndex))
62
63 if not incoming_txs:
64
65 print('[' + address + '] no transactions found between height: ' + str(scanIndex) + ' - ' + str(newIndex))
66 else:
67 #loop through each found tx and insert it
68 for tx in incoming_txs:
69
70 print(tx)
71 #store transactons, sfee = 0 for incoming
72 payload = (address, int(tx.amount), int(tx.fee), tx.blockIndex, tx.transactionHash, tx.paymendId, tx.extra, tx.timestamp, tx.confirms)
73 c.execute('INSERT INTO transactions (address, amount, fee, blockIndex, transactionHash, paymentId, extra, timestamp, confirms) VALUES (?, ? , ?, ?, ?, ? , ?, ?, ?)', payload)
74
75 #update scanIndex of the scanned account.
76 payload = (newIndex, address)
77 c.execute('UPDATE addresses SET scanIndex = ? WHERE address = ?')
78
79 conn.commit()
80 print('[' + address + '] stored ' + len(incoming_tx) + ' transactions found between height: ' + str(scanIndex) + ' - ' + str(newIndex))
81
82
83 #load transactions
84 for txs in c.execute('SELECT * from transactions;'):
85 print(txs)
86
87
88 #close connection
89 conn.close()
90
91class Login(Screen):
92 """
93 def prins(self, login, passw):
94 print(login)
95 print(passw)
96 """
97
98 def do_login(self, login, passes):
99 global correct
100 app = App.get_running_app()
101
102 clogin = "henlo"
103 cpasses = "print"
104
105 if login == clogin and passes == cpasses:
106 correct = True
107 self.manager.transition = SlideTransition(direction="left")
108
109 new_address = ts.createAddress()
110 save_address(new_address)
111
112 print('saved file to sql')