add payment verifier and confirmations to merchant script
This commit is contained in:
parent
03199dcf2e
commit
a6e7470b11
|
@ -51,18 +51,19 @@ omg_addresses = {}
|
||||||
|
|
||||||
def electrum_input_thread(in_queue):
|
def electrum_input_thread(in_queue):
|
||||||
while True:
|
while True:
|
||||||
addr, amount = in_queue.get(True,1000000000)
|
addr, amount, confirmations = in_queue.get(True,1000000000)
|
||||||
if addr in omg_addresses:
|
if addr in omg_addresses:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
print "subscribing to ", addr
|
print "subscribing to ", addr
|
||||||
omg_addresses[addr] = amount
|
omg_addresses[addr] = {'requested':float(amount), 'confirmations':int(confirmations)}
|
||||||
interface.send([('blockchain.address.subscribe',[addr])])
|
interface.send([('blockchain.address.subscribe',[addr])])
|
||||||
|
|
||||||
|
|
||||||
def electrum_output_thread(out_queue):
|
def electrum_output_thread(out_queue):
|
||||||
while True:
|
while True:
|
||||||
r = interface.responses.get(True, 100000000000)
|
r = interface.get_response()
|
||||||
|
print r
|
||||||
method = r.get('method')
|
method = r.get('method')
|
||||||
|
|
||||||
if method == 'blockchain.address.subscribe':
|
if method == 'blockchain.address.subscribe':
|
||||||
|
@ -71,32 +72,32 @@ def electrum_output_thread(out_queue):
|
||||||
|
|
||||||
elif method == 'blockchain.address.get_history':
|
elif method == 'blockchain.address.get_history':
|
||||||
addr = r.get('params')[0]
|
addr = r.get('params')[0]
|
||||||
#print "received history for", addr
|
|
||||||
confirmed = unconfirmed = 0
|
|
||||||
h = r.get('result')
|
h = r.get('result')
|
||||||
if h is None:
|
if h is None: continue
|
||||||
continue
|
omg_addresses[addr]['history'] = h
|
||||||
|
|
||||||
for item in h:
|
for item in h:
|
||||||
tx_hash = item.get('tx_hash')
|
tx_hash = item.get('tx_hash')
|
||||||
verifier.add(tx_hash)
|
verifier.add(tx_hash)
|
||||||
|
|
||||||
|
elif method == 'blockchain.numblocks.subscribe':
|
||||||
|
for addr in omg_addresses:
|
||||||
|
h = omg_addresses[addr].get('history',[])
|
||||||
|
amount = omg_addresses[addr].get('requested')
|
||||||
|
confs = omg_addresses[addr].get('confirmations')
|
||||||
|
val = 0
|
||||||
|
|
||||||
|
for item in h:
|
||||||
|
tx_hash = item.get('tx_hash')
|
||||||
v = item['value']
|
v = item['value']
|
||||||
if v<0: continue
|
if v<0: continue
|
||||||
if item['height']:
|
if verifier.get_confirmations(tx_hash) >= conf:
|
||||||
confirmed += v
|
val += v
|
||||||
else:
|
|
||||||
unconfirmed += v
|
|
||||||
|
|
||||||
s = (confirmed+unconfirmed)/1.e8
|
s = (val)/1.e8
|
||||||
print "balance for %s:"%addr, s
|
print "balance for %s:"%addr, s
|
||||||
amount = float(omg_addresses.get(addr))
|
|
||||||
if s>=amount:
|
if s>=amount:
|
||||||
out_queue.put( ('payment',addr) )
|
out_queue.put( ('payment',addr) )
|
||||||
|
|
||||||
elif method == 'blockchain.numblocks.subscribe':
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
stopping = False
|
stopping = False
|
||||||
|
|
||||||
|
@ -107,7 +108,7 @@ def do_stop():
|
||||||
def do_create(conn):
|
def do_create(conn):
|
||||||
# creation
|
# creation
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute("CREATE TABLE electrum_payments (id INT PRIMARY KEY, address VARCHAR(40), amount FLOAT, received_at TIMESTAMP, expires_at TIMESTAMP, paid INT(1), processed INT(1));")
|
cur.execute("CREATE TABLE electrum_payments (id INT PRIMARY KEY, address VARCHAR(40), amount FLOAT, confirmations INT(8), received_at TIMESTAMP, expires_at TIMESTAMP, paid INT(1), processed INT(1));")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
def process_request(i, amount, confirmations, expires_in, password):
|
def process_request(i, amount, confirmations, expires_in, password):
|
||||||
|
@ -167,7 +168,7 @@ if __name__ == '__main__':
|
||||||
interface.start()
|
interface.start()
|
||||||
interface.send([('blockchain.numblocks.subscribe',[])])
|
interface.send([('blockchain.numblocks.subscribe',[])])
|
||||||
|
|
||||||
verifier = WalletVerifier(interface, config)
|
verifier = WalletVerifier(interface, {})
|
||||||
verifier.start()
|
verifier.start()
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,7 +185,7 @@ if __name__ == '__main__':
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
|
|
||||||
# get a list of addresses to watch
|
# get a list of addresses to watch
|
||||||
cur.execute("SELECT address, amount FROM electrum_payments WHERE paid IS NULL;")
|
cur.execute("SELECT address, amount, confirmations FROM electrum_payments WHERE paid IS NULL;")
|
||||||
data = cur.fetchall()
|
data = cur.fetchall()
|
||||||
for item in data:
|
for item in data:
|
||||||
in_queue.put(item)
|
in_queue.put(item)
|
||||||
|
@ -201,9 +202,9 @@ if __name__ == '__main__':
|
||||||
id = cur.fetchone()[0]
|
id = cur.fetchone()[0]
|
||||||
cur.execute("update electrum_payments set paid=1 where id=%d;"%(id))
|
cur.execute("update electrum_payments set paid=1 where id=%d;"%(id))
|
||||||
elif cmd == 'request':
|
elif cmd == 'request':
|
||||||
i, addr, amount, hours = params
|
i, addr, amount, confs, hours = params
|
||||||
sql = "INSERT INTO electrum_payments (id, address, amount, received_at, expires_at, paid, processed)"\
|
sql = "INSERT INTO electrum_payments (id, address, amount, confirmations, received_at, expires_at, paid, processed)"\
|
||||||
+ " VALUES (%d, '%s', %f, CURRENT_TIMESTAMP, ADDTIME(CURRENT_TIMESTAMP, '0 %d:0:0'), NULL, NULL);"%(i, addr, amount, hours)
|
+ " VALUES (%d, '%s', %f, %d, CURRENT_TIMESTAMP, ADDTIME(CURRENT_TIMESTAMP, '0 %d:0:0'), NULL, NULL);"%(i, addr, amount, confs, hours)
|
||||||
cur.execute(sql)
|
cur.execute(sql)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue