forked from OmniLayer/omniEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalancehelper.py
202 lines (185 loc) · 8.77 KB
/
balancehelper.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import json, re
from sqltools import *
from blockchain_utils import *
def get_balancedata(address):
addr = re.sub(r'\W+', '', address) #check alphanumeric
ROWS=dbSelect("""select
f1.propertyid, sp.propertytype, f1.balanceavailable, f1.pendingpos, f1.pendingneg
from
(select
COALESCE(s1.propertyid,s2.propertyid) as propertyid, COALESCE(s1.balanceavailable,0) as balanceavailable,
COALESCE(s2.pendingpos,0) as pendingpos,COALESCE(s2.pendingneg,0) as pendingneg
from
(select propertyid,balanceavailable
from addressbalances
where address=%s) s1
full join
(SELECT atx.propertyid,
sum(CASE WHEN atx.balanceavailablecreditdebit > 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingpos,
sum(CASE WHEN atx.balanceavailablecreditdebit < 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingneg
from
addressesintxs atx, transactions tx
where
atx.txdbserialnum=tx.txdbserialnum
and tx.txstate='pending'
and tx.txdbserialnum<-1
and atx.address=%s
group by
atx.propertyid) s2
on s1.propertyid=s2.propertyid) f1
inner join smartproperties sp
on f1.propertyid=sp.propertyid and (sp.protocol='Omni' or sp.protocol='Mastercoin' or sp.protocol='Bitcoin')
order by f1.propertyid""",(addr,addr))
balance_data = { 'balance': [] }
ret = bc_getbalance(addr)
out = ret['bal']
err = ret['error']
for balrow in ROWS:
cID = str(int(balrow[0])) #currency id
sym_t = ('BTC' if cID == '0' else ('OMNI' if cID == '1' else ('T-OMNI' if cID == '2' else 'SP' + cID) ) ) #symbol template
#1 = new indivisible property, 2=new divisible property (per spec)
divi = True if int(balrow[1]) == 2 else False
res = { 'symbol' : sym_t, 'divisible' : divi, 'id' : cID }
res['pendingpos'] = str(long(balrow[3]))
res['pendingneg'] = str(long(balrow[4]))
if cID == '0':
#get btc balance from bc api's
if err != None or out == '':
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
try:
#if balrow[4] < 0:
# res['value'] = str(long( out ) + long(balrow[4]))
#else:
res['value'] = str(long( out ))
except ValueError:
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
#get regular balance from db
if balrow[4] < 0:
#update the 'available' balance immediately when the sender sent something. prevent double spend
res['value'] = str(long(balrow[2]+balrow[4]))
else:
res['value'] = str(long(balrow[2]))
#res['reserved_balance'] = ('%.8f' % float(balrow[5])).rstrip('0').rstrip('.')
balance_data['balance'].append(res)
#check if we got BTC data from DB, if not trigger manually add
addbtc=True
for x in balance_data['balance']:
if "BTC" in x['symbol']:
addbtc=False
if addbtc:
btc_balance = { 'symbol': 'BTC', 'divisible': True, 'id' : 0 }
if err != None or out == '':
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
try:
#btc_balance[ 'value' ] = str(long( json.loads( out )[0][ 'paid' ]))
#btc_balance[ 'value' ] = str(long( json.loads( out )['data']['balance']*1e8 ))
btc_balance[ 'value' ] = str(long( out ))
except ValueError:
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
btc_balance['pendingpos'] = str(long(0))
btc_balance['pendingneg'] = str(long(0))
balance_data['balance'].append(btc_balance)
return balance_data
def get_bulkbalancedata(addresses):
btclist=bc_getbulkbalance(addresses)
retval = {}
for address in addresses:
addr = re.sub(r'\W+', '', address) #check alphanumeric
ROWS=dbSelect("""select
f1.propertyid, sp.propertytype, f1.balanceavailable, f1.pendingpos, f1.pendingneg
from
(select
COALESCE(s1.propertyid,s2.propertyid) as propertyid, COALESCE(s1.balanceavailable,0) as balanceavailable,
COALESCE(s2.pendingpos,0) as pendingpos,COALESCE(s2.pendingneg,0) as pendingneg
from
(select propertyid,balanceavailable
from addressbalances
where address=%s) s1
full join
(SELECT atx.propertyid,
sum(CASE WHEN atx.balanceavailablecreditdebit > 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingpos,
sum(CASE WHEN atx.balanceavailablecreditdebit < 0 THEN atx.balanceavailablecreditdebit ELSE 0 END) AS pendingneg
from
addressesintxs atx, transactions tx
where
atx.txdbserialnum=tx.txdbserialnum
and tx.txstate='pending'
and tx.txdbserialnum<-1
and atx.address=%s
group by
atx.propertyid) s2
on s1.propertyid=s2.propertyid) f1
inner join smartproperties sp
on f1.propertyid=sp.propertyid and (sp.protocol='Omni' or sp.protocol='Mastercoin' or sp.protocol='Bitcoin')
order by f1.propertyid""",(addr,addr))
balance_data = { 'balance': [] }
try:
if address in btclist:
out = btclist[address]
err = None
else:
out = ''
err = "Missing"
except TypeError:
out = ''
err = "Missing"
for balrow in ROWS:
cID = str(int(balrow[0])) #currency id
sym_t = ('BTC' if cID == '0' else ('OMNI' if cID == '1' else ('T-OMNI' if cID == '2' else 'SP' + cID) ) ) #symbol template
#1 = new indivisible property, 2=new divisible property (per spec)
divi = True if int(balrow[1]) == 2 else False
res = { 'symbol' : sym_t, 'divisible' : divi, 'id' : cID }
res['pendingpos'] = str(long(balrow[3]))
res['pendingneg'] = str(long(balrow[4]))
if cID == '0':
#get btc balance from bc api's
if err != None or out == '':
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
try:
#if balrow[4] < 0:
# res['value'] = str(long( out ) + long(balrow[4]))
#else:
res['value'] = str(long( out ))
except ValueError:
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
#get regular balance from db
if balrow[4] < 0:
#update the 'available' balance immediately when the sender sent something. prevent double spend
res['value'] = str(long(balrow[2]+balrow[4]))
else:
res['value'] = str(long(balrow[2]))
#res['reserved_balance'] = ('%.8f' % float(balrow[5])).rstrip('0').rstrip('.')
balance_data['balance'].append(res)
#check if we got BTC data from DB, if not trigger manually add
addbtc=True
for x in balance_data['balance']:
if "BTC" in x['symbol']:
addbtc=False
if addbtc:
btc_balance = { 'symbol': 'BTC', 'divisible': True, 'id' : 0 }
if err != None or out == '':
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
else:
try:
#btc_balance[ 'value' ] = str(long( json.loads( out )[0][ 'paid' ]))
btc_balance[ 'value' ] = str(long( out ))
except ValueError:
#btc_balance[ 'value' ] = str(long(-555))
btc_balance[ 'value' ] = str(long(0))
btc_balance['pendingpos'] = str(long(0))
btc_balance['pendingneg'] = str(long(0))
balance_data['balance'].append(btc_balance)
retval[address]=balance_data
return retval