Skip to content

Commit 888af9f

Browse files
committed
external api testnet support
1 parent bc19296 commit 888af9f

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

blockchain_utils.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@
55
from config import BTAPIKEY
66
from rpcclient import gettxout
77
from cacher import *
8+
import config
9+
10+
BLOCKCHAININFO_API_URL = "https://blockchain.info/"
11+
BLOCKR_API_URL = "http://btc.blockr.io/api/v1"
12+
BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/btc/"
13+
BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/main/"
14+
BITGO_API_URL = "https://www.bitgo.com/api/v1/"
15+
BLOCKONOMICS_API_URL = "https://www.blockonomics.co/api"
16+
17+
# note: "blockchain.info" does not support testnet
18+
# note: "blockonomics" does not appear to support testnet
19+
TESTNET_BLOCKR_API_URL = "http://tbtc.blockr.io/api/v1"
20+
TESTNET_BLOCKTRAIL_API_URL = "https://api.blocktrail.com/v1/tbtc/"
21+
TESTNET_BLOCKCYPHER_API_URL = "https://api.blockcypher.com/v1/btc/test3/"
22+
TESTNET_BITGO_API_URL = "https://test.bitgo.com/api/v1/"
823

924
try:
1025
expTime=config.BTCBAL_CACHE
@@ -15,7 +30,8 @@ def bc_getutxo(address, ramount, page=1, retval=None, avail=0):
1530
if retval==None:
1631
retval=[]
1732
try:
18-
r = requests.get('https://api.blocktrail.com/v1/btc/address/'+address+'/unspent-outputs?api_key='+str(BTAPIKEY)+'&limit=200&page='+str(page))
33+
apiUrl = BLOCKTRAIL_API_URL if not config.TESTNET else TESTNET_BLOCKTRAIL_API_URL
34+
r = requests.get(apiUrlr + '/address/'+address+'/unspent-outputs?api_key='+str(BTAPIKEY)+'&limit=200&page='+str(page))
1935
if r.status_code == 200:
2036
response = r.json()
2137
unspents = response['data']
@@ -40,7 +56,8 @@ def bc_getutxo(address, ramount, page=1, retval=None, avail=0):
4056

4157
def bc_getutxo_blockcypher(address, ramount):
4258
try:
43-
r = requests.get('https://api.blockcypher.com/v1/btc/main/addrs/'+address+'?unspentOnly=true')
59+
apiUrl = BLOCKCYPHER_API_URL if not config.TESTNET else TESTNET_BLOCKCYPHER_API_URL
60+
r = requests.get(apiUrl + '/addrs/'+address+'?unspentOnly=true')
4461

4562
if r.status_code == 200:
4663
unspents = r.json()['txrefs']
@@ -65,7 +82,8 @@ def bc_getutxo_blockcypher(address, ramount):
6582

6683
def bc_getutxo_blockr(address, ramount):
6784
try:
68-
r = requests.get('http://btc.blockr.io/api/v1/address/unspent/'+address+'?unconfirmed=1')
85+
apiUrl = BLOCKR_API_URL if not config.TESTNET else TESTNET_BLOCKR_API_URL
86+
r = requests.get(apiUrl + '/address/unspent/'+address+'?unconfirmed=1')
6987

7088
if r.status_code == 200:
7189
#Process and format response from blockr.io
@@ -92,8 +110,12 @@ def bc_getutxo_blockr(address, ramount):
92110

93111

94112
def bc_getpubkey(address):
113+
# note: only supports mainnet
95114
try:
96-
r = requests.get('https://blockchain.info/q/pubkeyaddr/'+address)
115+
if config.TESTNET:
116+
return "error: tried using blockchain.info api with testnet enabled"
117+
118+
r = requests.get(BLOCKCHAININFO_API_URL + '/q/pubkeyaddr/'+address)
97119

98120
if r.status_code == 200:
99121
return str(r.text)
@@ -117,7 +139,8 @@ def bc_getbalance(address):
117139

118140
def bc_getbalance_bitgo(address):
119141
try:
120-
r= requests.get('https://www.bitgo.com/api/v1/address/'+address)
142+
apiUrl = BITGO_API_URL if not config.TESTNET else TESTNET_BITGO_API_URL
143+
r= requests.get(apiUrl + '/address/'+address)
121144
if r.status_code == 200:
122145
balance = int(r.json()['balance'])
123146
return {"bal":balance , "error": None}
@@ -128,7 +151,8 @@ def bc_getbalance_bitgo(address):
128151

129152
def bc_getbalance_blockcypher(address):
130153
try:
131-
r= requests.get('https://api.blockcypher.com/v1/btc/main/addrs/'+address+'/balance')
154+
apiUrl = BLOCKCYPHER_API_URL if not config.TESTNET else TESTNET_BLOCKCYPHER_API_URL
155+
r= requests.get(apiUrl + '/addrs/'+address+'/balance')
132156
if r.status_code == 200:
133157
balance = int(r.json()['balance'])
134158
return {"bal":balance , "error": None}
@@ -139,7 +163,8 @@ def bc_getbalance_blockcypher(address):
139163

140164
def bc_getbalance_blockr(address):
141165
try:
142-
r= requests.get('http://btc.blockr.io/api/v1/address/balance/'+address)
166+
apiUrl = BLOCKR_API_URL if not config.TESTNET else TESTNET_BLOCKR_API_URL
167+
r= requests.get(apiUrl + '/address/balance/'+address)
143168
if r.status_code == 200:
144169
balance = int(r.json()['data']['balance']*1e8)
145170
return {"bal":balance , "error": None}
@@ -222,7 +247,10 @@ def bc_getbulkbalance_blockonomics(addresses):
222247
formatted=formatted+" "+address
223248

224249
try:
225-
r = requests.post('https://www.blockonomics.co/api/balance',json.dumps({"addr":formatted}))
250+
if config.TESTNET:
251+
return "error: tried using blockonomics api with testnet enabled"
252+
253+
r = requests.post(apiUrl + '/balance',json.dumps({"addr":formatted}))
226254
if r.status_code == 200:
227255
balances = r.json()['response']
228256
retval = {}
@@ -243,7 +271,8 @@ def bc_getbulkbalance_blockr(addresses):
243271
formatted=formatted+","+address
244272

245273
try:
246-
r= requests.get('http://btc.blockr.io/api/v1/address/balance/'+formatted)
274+
apiUrl = BLOCKR_API_URL if not config.TESTNET else TESTNET_BLOCKR_API_URL
275+
r= requests.get(apiUrl + '/address/balance/'+formatted)
247276
if r.status_code == 200:
248277
balances = r.json()['data']
249278
retval = {}
@@ -263,7 +292,10 @@ def bc_getbulkbalance_blockchain(addresses):
263292
else:
264293
formatted=formatted+"|"+address
265294
try:
266-
r= requests.get('https://blockchain.info/balance?active='+formatted)
295+
if config.TESTNET:
296+
return "error: tried using blockchain.info api with testnet enabled"
297+
298+
r= requests.get(BLOCKCHAININFO_API_URL + '/balance?active='+formatted)
267299
if r.status_code == 200:
268300
balances = r.json()
269301
retval = {}

config.py.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ REDIS_DB=0
99
#How long, in seconds, to cache BTC balance info for new addresses, Default 10min (600)
1010
BTCBAL_CACHE=600
1111

12+
# Set to True to switch to processing testnet
13+
TESTNET = False

omniEngine.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import sys
44
from datetime import datetime
55
from cacher import *
6+
import config
67

78
USER=getpass.getuser()
89
lockFile='/tmp/omniEngine.lock.'+str(USER)
910
now=datetime.now()
10-
testnet=0
1111
sys.argv.pop(0)
1212

1313
if os.path.isfile(lockFile):
@@ -176,7 +176,7 @@
176176
#check any active crowdsales and update json if the endtime has passed (based on block time)
177177
expireCrowdsales(block_data['result']['time'], Protocol)
178178
#exodus address generates dev msc, sync our balance to match the generated balanace
179-
if testnet:
179+
if config.TESTNET:
180180
syncAddress('mpexoDuSkGGqvqrkrjiFng38QPkJQVFyqv', Protocol)
181181
#upadate temp orderbook
182182
#updateorderblob()

0 commit comments

Comments
 (0)