1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 14:28:07 +00:00

Parse json floats as string

With python-2.7 the float values are sometimes rounded to unacceptable
levels, e.g. stripping the last two digits for values over 100k BTC.
This change parses floats as strings to avoid rounding.

Refactored get_url out of fetch_json to make it easier to add
new tx_api with a different url scheme.
This commit is contained in:
Jochen Hoenicke 2017-11-25 14:47:41 +01:00 committed by Pavol Rusnak
parent ffeb94f792
commit 69067c9280

View File

@ -32,19 +32,23 @@ class TxApi(object):
self.network = network self.network = network
self.url = url self.url = url
def get_url(self, resource, resourceid):
url = '%s%s/%s' % (self.url, resource, resourceid)
return url
def fetch_json(self, resource, resourceid): def fetch_json(self, resource, resourceid):
global cache_dir global cache_dir
if cache_dir: if cache_dir:
cache_file = '%s/%s_%s_%s.json' % (cache_dir, self.network, resource, resourceid) cache_file = '%s/%s_%s_%s.json' % (cache_dir, self.network, resource, resourceid)
try: # looking into cache first try: # looking into cache first
j = json.load(open(cache_file)) j = json.load(open(cache_file), parse_float=str)
return j return j
except: except:
pass pass
try: try:
url = '%s%s/%s' % (self.url, resource, resourceid) url = self.get_url(resource, resourceid)
r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'}) r = requests.get(url, headers={'User-agent': 'Mozilla/5.0'})
j = r.json() j = r.json(parse_float=str)
except: except:
raise RuntimeError('URL error: %s' % url) raise RuntimeError('URL error: %s' % url)
if cache_dir and cache_file: if cache_dir and cache_file:
@ -88,7 +92,7 @@ class TxApiInsight(TxApi):
for vout in data['vout']: for vout in data['vout']:
o = t.bin_outputs.add() o = t.bin_outputs.add()
o.amount = int(Decimal(str(vout['value'])) * 100000000) o.amount = int(Decimal(vout['value']) * 100000000)
o.script_pubkey = binascii.unhexlify(vout['scriptPubKey']['hex']) o.script_pubkey = binascii.unhexlify(vout['scriptPubKey']['hex'])
if self.zcash: if self.zcash: