forked from OmniLayer/omniEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain_utils.py
328 lines (305 loc) · 10.9 KB
/
blockchain_utils.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import simplejson
import requests
import decimal
import json, re
from rpcclient import gettxout
from cacher import *
import config
try:
expTime=config.BTCBAL_CACHE
except:
expTime=600
try:
TESTNET = config.TESTNET
except:
TESTNET = False
if TESTNET:
# neither blockchain.info nor blockonomics support testnet
BLOCKCHAININFO_API_URL = "https://api.blockchain.info/haskoin-store/btc-testnet"
BLOCKONOMICS_API_URL = "https://www.blockonomics.co/api"
BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/tbtc"
BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/test3"
BITGO_API_URL = "https://test.bitgo.com/api/v1"
BTCCOM_API_URL = "https://chain.api.btc.com/v3"
else:
BLOCKCHAININFO_API_URL = "https://api.blockchain.info/haskoin-store/btc"
BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/btc"
BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/main"
BITGO_API_URL = "https://www.bitgo.com/api/v1"
BTCCOM_API_URL = "https://tchain.api.btc.com/v3"
BLOCKONOMICS_API_URL = "https://www.blockonomics.co/api"
def bc_getutxo(address, ramount):
try:
#r = requests.get(BLOCKCHAININFO_API_URL + '/unspent?active='+address)
r = requests.get(BLOCKCHAININFO_API_URL + '/address/'+address+'/unspent')
if r.status_code == 200:
avail=0
retval=[]
response = r.json()
#unspents = response['unspent_outputs']
unspents = response
print "got unspent list (blockchain)", response
for tx in sorted(unspents, key = lambda i: i['value'],reverse=True):
#txUsed=gettxout(tx['tx_hash_big_endian'],tx['tx_output_n'])['result']
txUsed=gettxout(tx['txid'],tx['index'])['result']
isUsed = txUsed==None
if not isUsed:
coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100)
multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig")
if not coinbaseHold and txUsed['confirmations'] > 0 and not multisigSkip:
avail += tx['value']
#retval.append([ tx['tx_hash_big_endian'], tx['tx_output_n'], tx['value'] ])
retval.append([ tx['txid'], tx['index'], tx['value'] ])
if avail >= ramount:
return {"avail": avail, "utxos": retval, "error": "none"}
if ('notice' in response and 'Ignoring' in response['notice']):
return bc_getutxo_btccom(address, ramount)
else:
return {"avail": avail, "error": "Low balance error"}
else:
return bc_getutxo_btccom(address, ramount)
except:
return bc_getutxo_btccom(address, ramount)
def bc_getutxo_btccom(address, ramount, page=1, retval=None, avail=0):
if retval==None:
retval=[]
try:
r = requests.get(BTCCOM_API_URL + '/address/'+address+'/unspent?pagesize=50&page='+str(page), timeout=2)
if r.status_code == 200:
response = r.json()['data']
unspents = response['list']
print "got unspent list (btc)", response
for tx in unspents:
txUsed=gettxout(tx['tx_hash'],tx['tx_output_n'])['result']
isUsed = txUsed==None
coinbaseHold = (txUsed['coinbase'] and txUsed['confirmations'] < 100)
multisigSkip = ("scriptPubKey" in txUsed and txUsed['scriptPubKey']['type'] == "multisig")
if not isUsed and not coinbaseHold and txUsed['confirmations'] > 0 and not multisigSkip:
avail += tx['value']
retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ])
if avail >= ramount:
return {"avail": avail, "utxos": retval, "error": "none"}
if int(response['total_count'])-(int(response['pagesize'])*page ) > 0:
return bc_getutxo(address, ramount, page+1, retval, avail)
return {"avail": avail, "error": "Low balance error"}
else:
return bc_getutxo_blockcypher(address, ramount)
except:
return bc_getutxo_blockcypher(address, ramount)
def bc_getutxo_blockcypher(address, ramount):
try:
r = requests.get(BLOCKCYPHER_API_URL + '/addrs/'+address+'?unspentOnly=true', timeout=2)
if r.status_code == 200:
try:
unspents = r.json()['txrefs']
except Exception as e:
print "no txrefs in bcypher json response"
unspents = []
print "got unspent list (bcypher)", unspents
retval = []
avail = 0
for tx in unspents:
txUsed=gettxout(tx['tx_hash'],tx['tx_output_n'])
isUsed = ('result' in txUsed and txUsed['result']==None)
if tx['confirmations'] > 0 and not isUsed:
avail += tx['value']
retval.append([ tx['tx_hash'], tx['tx_output_n'], tx['value'] ])
if avail >= ramount:
return {"avail": avail, "utxos": retval, "error": "none"}
return {"avail": avail, "error": "Low balance error"}
else:
return {"error": "Connection error", "code": r.status_code}
except Exception as e:
if 'call' in e.message:
msg=e.message.split("call: ")[1]
ret=re.findall('{.+',str(msg))
try:
msg=json.loads(ret[0])
except TypeError:
msg=ret[0]
except ValueError:
#reverse the single/double quotes and strip leading u in output to make it json compatible
msg=json.loads(ret[0].replace("'",'"').replace('u"','"'))
return {"error": "Connection error", "code": msg['message']}
else:
return {"error": "Connection error", "code": e.message}
#def bc_getpubkey(address):
# try:
# r = requests.get(BLOCKCHAININFO_API_URL + '/q/pubkeyaddr/'+address)
#
# if r.status_code == 200:
# return str(r.text)
# else:
# return "error"
# except:
# return "error"
def bc_getbalance(address):
try:
balance=rGet("omniwallet:balances:address:"+str(address))
balance=json.loads(balance)
if balance['error']:
raise LookupError("Not cached")
except Exception as e:
balance = bc_getbalance_bitgo(address)
#cache btc balance for 2.5 minutes
rSet("omniwallet:balances:address:"+str(address),json.dumps(balance))
rExpire("omniwallet:balances:address:"+str(address),expTime)
return balance
def bc_getbalance_bitgo(address):
try:
r= requests.get(BITGO_API_URL + '/address/'+address, timeout=2)
if r.status_code == 200:
#balance = int(r.json()['confirmedBalance'])
rt = r.json()
balance = int(rt['spendableBalance'])
pending = int(rt['unconfirmedReceives'])
return {"bal":balance, "pending": pending, "error": None}
else:
return bc_getbalance_blockcypher(address)
except:
return bc_getbalance_blockcypher(address)
def bc_getbalance_blockcypher(address):
try:
r= requests.get(BLOCKCYPHER_API_URL + '/addrs/'+address+'/balance', timeout=2)
if r.status_code == 200:
balance = int(r.json()['balance'])
return {"bal":balance , "error": None}
else:
return {"bal": 0 , "error": "Couldn't get balance"}
except:
return {"bal": 0 , "error": "Couldn't get balance"}
def bc_getbulkbalance(addresses):
split=[]
recurse=[]
counter=0
retval={}
cbdata={}
for a in addresses:
try:
cb=rGet("omniwallet:balances:address:"+str(a))
cb=json.loads(cb)
if cb['error']:
raise LookupError("Not cached")
else:
cbdata[a]=cb['bal']
except Exception as e:
if counter < 20:
split.append(a)
else:
recurse.append(a)
counter+=1
if len(split)==0:
if len(cbdata) > 0:
retval={'bal':cbdata, 'fresh':None}
else:
retval={'bal':{}, 'fresh':None}
else:
if TESTNET:
try:
data=bc_getbulkbalance_blockchain(split)
if data['error']:
raise Exception("issue getting blockchain baldata",data)
else:
retval={'bal':dict(data['bal'],**cbdata), 'fresh':split}
except Exception as e:
print e
try:
data=bc_getbulkbalance_btccom(split)
if data['error']:
raise Exception("issue getting btccom baldata","data",data,"split",split)
else:
retval={'bal':dict(data['bal'],**cbdata), 'fresh':split}
except Exception as e:
print e
if len(cbdata) > 0:
retval={'bal':cbdata, 'fresh':None}
else:
retval={'bal':{}, 'fresh':None}
else:
try:
data=bc_getbulkbalance_blockonomics(split)
if data['error']:
raise Exception("issue getting blockonomics baldata",data)
else:
retval={'bal':dict(data['bal'],**cbdata), 'fresh':split}
except Exception as e:
print e
try:
data=bc_getbulkbalance_blockchain(split)
if data['error']:
raise Exception("issue getting blockchain baldata",data)
else:
retval={'bal':dict(data['bal'],**cbdata), 'fresh':split}
except Exception as e:
print e
if len(cbdata) > 0:
retval={'bal':cbdata, 'fresh':None}
else:
retval={'bal':{}, 'fresh':None}
rSetNotUpdateBTC(retval)
if len(recurse)>0:
rdata=bc_getbulkbalance(recurse)
else:
rdata={}
return dict(retval['bal'],**rdata)
def bc_getbulkbalance_blockonomics(addresses):
formatted=""
for address in addresses:
if formatted=="":
formatted=address
else:
formatted=formatted+" "+address
try:
r = requests.post(BLOCKONOMICS_API_URL + '/balance',json.dumps({"addr":formatted}))
if r.status_code == 200:
balances = r.json()['response']
retval = {}
for entry in balances:
retval[entry['addr']] = int(entry['confirmed'])+int(entry['unconfirmed'])
return {"bal": retval, "error": None}
else:
return {"bal": None , "error": True}
except:
return {"bal": None , "error": True}
def bc_getbulkbalance_blockchain(addresses):
formatted=""
for address in addresses:
if formatted=="":
formatted=address
else:
#formatted=formatted+"|"+address
formatted=formatted+","+address
try:
#r= requests.get(BLOCKCHAININFO_API_URL + '/balance?active='+formatted)
r= requests.get(BLOCKCHAININFO_API_URL + '/address/balances?addresses='+formatted)
if r.status_code == 200:
balances = r.json()
retval = {}
for entry in balances:
#retval[entry] = int(balances[entry]['final_balance'])
retval[entry['address']] = int(entry['confirmed'])
return {"bal": retval, "error": None}
else:
return {"bal": None , "error": True}
except:
return {"bal": None , "error": True}
def bc_getbulkbalance_btccom(addresses):
formatted=""
for address in addresses:
if formatted=="":
formatted=address
else:
formatted=formatted+","+address
try:
r = requests.get(BTCCOM_API_URL + '/address/'+formatted, timeout=2)
if r.status_code == 200:
balances = r.json()
retval = {}
for entry in balances["data"]:
retval[entry["address"]] = int(entry['balance'])
return {"bal": retval, "error": None}
else:
return {"bal": None , "error": True}
except Exception as e:
print "error getting btccom bulk",e
return {"bal": None , "error": True}