From 8c611853872d1a468a79bfdaa9795ffbc9a5083e Mon Sep 17 00:00:00 2001 From: Kay Kurokawa Date: Thu, 9 Mar 2017 11:12:49 -0500 Subject: [PATCH] handle decimal encoded string output from lbryum --- lbrynet/core/Wallet.py | 2 +- lbrynet/lbrynet_daemon/Daemon.py | 39 ++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lbrynet/core/Wallet.py b/lbrynet/core/Wallet.py index bc123ad22..f61c213c8 100644 --- a/lbrynet/core/Wallet.py +++ b/lbrynet/core/Wallet.py @@ -635,7 +635,7 @@ class Wallet(object): if my_claim: log.info("Updating claim") - if self.get_balance() < bid - my_claim['amount']: + if self.get_balance() < bid - Decimal(my_claim['amount']): raise InsufficientFundsError() new_metadata = yield self.update_metadata(_metadata, my_claim['value']) old_claim_outpoint = ClaimOutpoint(my_claim['txid'], my_claim['nout']) diff --git a/lbrynet/lbrynet_daemon/Daemon.py b/lbrynet/lbrynet_daemon/Daemon.py index 7e2460294..e62004650 100644 --- a/lbrynet/lbrynet_daemon/Daemon.py +++ b/lbrynet/lbrynet_daemon/Daemon.py @@ -8,7 +8,6 @@ import requests import urllib import simplejson as json from requests import exceptions as requests_exceptions -from decimal import Decimal import random from twisted.web import server @@ -1492,15 +1491,8 @@ class Daemon(AuthJSONRPCServer): txid, amount, value, n, height """ - def _convert_amount_to_float(r): - if not r: - return False - else: - r['amount'] = float(r['amount']) / 10 ** 8 - return r - d = self.session.wallet.get_claim_info(name, txid, nout) - d.addCallback(_convert_amount_to_float) + d.addCallback(format_json_out_amount_as_float) d.addCallback(lambda r: self._render_response(r)) return d @@ -1826,15 +1818,8 @@ class Daemon(AuthJSONRPCServer): list of name claims """ - def _clean(claims): - for c in claims: - for k in c.keys(): - if isinstance(c[k], Decimal): - c[k] = float(c[k]) - return defer.succeed(claims) - d = self.session.wallet.get_name_claims() - d.addCallback(_clean) + d.addCallback(format_json_out_amount_as_float) d.addCallback(lambda claims: self._render_response(claims)) return d @@ -1861,6 +1846,7 @@ class Daemon(AuthJSONRPCServer): """ d = self.session.wallet.get_claims_for_name(name) + d.addCallback(format_json_out_amount_as_float) d.addCallback(lambda r: self._render_response(r)) return d @@ -2562,3 +2548,22 @@ def get_version_from_tag(tag): return match.group(1) else: raise Exception('Failed to parse version from tag {}'.format(tag)) + + +# lbryum returns json loadeable object with amounts as decimal encoded string, +# convert them into floats for the daemon +# TODO: daemon should also use decimal encoded string +def format_json_out_amount_as_float(obj): + if isinstance(obj, dict): + for k, v in obj.iteritems(): + if k == 'amount' or k == 'effective_amount': + obj[k] = float(obj[k]) + if isinstance(v, dict) or isinstance(v, list): + obj[k] = format_json_out_amount_as_float(v) + + elif isinstance(obj, list): + obj = [format_json_out_amount_as_float(o) for o in obj] + return obj + + +