This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Transactions.py
184 lines (165 loc) · 6.11 KB
/
Transactions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sys, os, threading, traceback, time
import dogecoinrpc, dogecoinrpc.connection, psycopg2
import Config, Logger, Blocknotify
def database():
return psycopg2.connect(database = Config.config["database"])
def daemon():
return dogecoinrpc.connect_to_local()
cur = database().cursor()
cur.execute("SELECT block FROM lastblock")
lastblock = cur.fetchone()[0]
del cur
class NotEnoughMoney(Exception):
pass
InsufficientFunds = dogecoinrpc.exceptions.InsufficientFunds
unconfirmed = {}
# Monkey-patching dogecoinrpc
def patchedlistsinceblock(self, block_hash, minconf=1):
res = self.proxy.listsinceblock(block_hash, minconf)
res['transactions'] = [dogecoinrpc.connection.TransactionInfo(**x) for x in res['transactions']]
return res
try:
daemon().listsinceblock("0", 1)
except TypeError:
dogecoinrpc.connection.DogecoinConnection.listsinceblock = patchedlistsinceblock
# End of monkey-patching
def txlog(cursor, token, amt, tx = None, address = None, src = None, dest = None):
cursor.execute("INSERT INTO txlog VALUES (%s, %s, %s, %s, %s, %s, %s)", (time.time(), token, src, dest, amt, tx, address))
def notify_block():
global lastblock, unconfirmed
lb = daemon().listsinceblock(lastblock, Config.config["confirmations"])
db = database()
cur = db.cursor()
txlist = [(int(tx.amount), tx.address) for tx in lb["transactions"] if tx.category == "receive" and tx.confirmations >= Config.config["confirmations"]]
if len(txlist):
addrlist = [(tx[1],) for tx in txlist]
cur.executemany("UPDATE accounts SET balance = balance + %s FROM address_account WHERE accounts.account = address_account.account AND address_account.address = %s", txlist)
cur.executemany("UPDATE address_account SET used = '1' WHERE address = %s", addrlist)
unconfirmed = {}
for tx in lb["transactions"]:
if tx.category == "receive":
cur.execute("SELECT account FROM address_account WHERE address = %s", (tx.address,))
if cur.rowcount:
account = cur.fetchone()[0]
if tx.confirmations < Config.config["confirmations"]:
unconfirmed[account] = unconfirmed.get(account, 0) + int(tx.amount)
else:
txlog(cur, Logger.token(), int(tx.amount), tx = tx.txid.encode("ascii"), address = tx.address, dest = account)
cur.execute("UPDATE lastblock SET block = %s", (lb["lastblock"],))
db.commit()
lastblock = lb["lastblock"]
def balance(account):
cur = database().cursor()
cur.execute("SELECT balance FROM accounts WHERE account = %s", (account,))
if cur.rowcount:
return cur.fetchone()[0]
else:
return 0
def balance_unconfirmed(account):
return unconfirmed.get(account, 0)
def tip(token, source, target, amount):
db = database()
cur = db.cursor()
cur.execute("SELECT * FROM accounts WHERE account = ANY(%s) FOR UPDATE", (sorted([target, source]),))
try:
cur.execute("UPDATE accounts SET balance = balance - %s WHERE account = %s", (amount, source))
except psycopg2.IntegrityError as e:
raise NotEnoughMoney()
if not cur.rowcount:
raise NotEnoughMoney()
cur.execute("UPDATE accounts SET balance = balance + %s WHERE account = %s", (amount, target))
if not cur.rowcount:
cur.execute("INSERT INTO accounts VALUES (%s, %s)", (target, amount))
txlog(cur, token, amount, src = source, dest = target)
db.commit()
def tip_multiple(token, source, dict):
db = database()
cur = db.cursor()
cur.execute("SELECT * FROM accounts WHERE account = ANY(%s) FOR UPDATE", (sorted(dict.keys() + [source]),))
spent = 0
for target in dict:
amount = dict[target]
try:
cur.execute("UPDATE accounts SET balance = balance - %s WHERE account = %s", (amount, source))
except psycopg2.IntegrityError as e:
raise NotEnoughMoney()
if not cur.rowcount:
raise NotEnoughMoney()
spent += amount
cur.execute("UPDATE accounts SET balance = balance + %s WHERE account = %s", (amount, target))
if not cur.rowcount:
cur.execute("INSERT INTO accounts VALUES (%s, %s)", (target, amount))
for target in dict:
txlog(cur, token, dict[target], src = source, dest = target)
db.commit()
def withdraw(token, account, address, amount):
db = database()
cur = db.cursor()
try:
cur.execute("UPDATE accounts SET balance = balance - %s WHERE account = %s", (amount + 1, account))
except psycopg2.IntegrityError as e:
raise NotEnoughMoney()
if not cur.rowcount:
raise NotEnoughMoney()
try:
tx = daemon().sendtoaddress(address, amount, comment = "sent with Doger")
except InsufficientFunds:
raise
except:
Logger.irclog("Emergency lock on account '%s'" % (account))
lock(account, True)
raise
db.commit()
txlog(cur, token, amount + 1, tx = tx.encode("ascii"), address = address, src = account)
db.commit()
return tx.encode("ascii")
def deposit_address(account):
db = database()
cur = db.cursor()
cur.execute("SELECT address FROM address_account WHERE used = '0' AND account = %s LIMIT 1", (account,))
if cur.rowcount:
return cur.fetchone()[0]
addr = daemon().getnewaddress()
try:
cur.execute("SELECT * FROM accounts WHERE account = %s", (account,))
if not cur.rowcount:
cur.execute("INSERT INTO accounts VALUES (%s, 0)", (account,))
cur.execute("INSERT INTO address_account VALUES (%s, %s, '0')", (addr, account))
db.commit()
except:
pass
return addr.encode("ascii")
def verify_address(address):
if address.isalnum():
return daemon().validateaddress(address).isvalid
else:
return False
def ping():
daemon().getbalance()
def balances():
cur = database().cursor()
cur.execute("SELECT SUM(balance) FROM accounts")
db = float(cur.fetchone()[0])
dogecoind = float(daemon().getbalance(minconf = Config.config["confirmations"]))
return (db, dogecoind)
def get_info():
info = daemon().getinfo()
return (info, daemon().getblockhash(info.blocks).encode("ascii"))
def lock(account, state = None):
if state == None:
cur = database().cursor()
cur.execute("SELECT * FROM locked WHERE account = %s", (account,))
return not not cur.rowcount
elif state == True:
db = database()
cur = db.cursor()
try:
cur.execute("INSERT INTO locked VALUES (%s)", (account,))
db.commit()
except psycopg2.IntegrityError as e:
pass
elif state == False:
db = database()
cur = db.cursor()
cur.execute("DELETE FROM locked WHERE account = %s", (account,))
db.commit()