From 0cdc68587903ec7b34736356dfd11d9b45cab744 Mon Sep 17 00:00:00 2001 From: Mohammad Aghamir Date: Tue, 23 Jun 2020 13:01:16 +0430 Subject: [PATCH 1/2] TronscanApi: Using decimal.Decimal instead of float. --- blockapi/api/tronscan.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/blockapi/api/tronscan.py b/blockapi/api/tronscan.py index 0b353394..6cfc614a 100644 --- a/blockapi/api/tronscan.py +++ b/blockapi/api/tronscan.py @@ -1,3 +1,4 @@ +from decimal import Decimal from blockapi.services import ( BlockchainAPI, on_failure_return_none @@ -16,7 +17,7 @@ class TronscanAPI(BlockchainAPI): symbol = 'TRX' base_url = 'https://apilist.tronscan.org/api' rate_limit = 0 - coef = 1e-6 + coef = "1e-6" max_items_per_page = None page_offset_step = None confirmed_num = None @@ -37,27 +38,31 @@ def get_balance(self): trc10_tokenlist = self.request('get_trc10_tokenlist')['data'] for token in trc10_tokenlist: - token_map[token['tokenID']] = {'abbr': token['abbr'], 'precision': token['precision'] } + token_map[token['tokenID']] = {'abbr': token['abbr'], 'precision': token['precision']} balances = [] - for coin in response['tokenBalances']: if coin['name'] == '_': symbol = self.symbol owner_address = self.address - coin_coef = 1e-6 + coin_coef = "1e-6" else: - symbol = token_map[int(coin['name'])]['abbr'] + token = token_map.get(int(coin['name'])) + # Test address has token with 1002001 which doesn't return in 10000 tokens in token list. + # So we simply ignore this + if token is None: + continue + symbol = token.get('abbr', '') owner_address = coin['owner_address'] - coin_coef = 1/pow(10,int(token_map[int(coin['name'])]['precision'])) + + coin_coef = "1e-{}".format(token.get('precision', 1)) balances.append({'symbol': symbol, - 'amount': float(coin['balance']) * coin_coef, + 'amount': Decimal(coin['balance']) * Decimal(coin_coef), 'address': owner_address}) - for coin in response['trc20token_balances']: balances.append({'symbol': coin['symbol'], - 'amount': float(coin['balance']) * (1/pow(10,int(coin['decimals']))), - 'address': None }) + 'amount': Decimal(coin['balance']) * Decimal("1e-{}".format(coin['decimals'])), + 'address': None}) return balances From 0a8fea379753a2128f9e1ecc66c5792c86ae92bc Mon Sep 17 00:00:00 2001 From: Mohammad Aghamir Date: Tue, 23 Jun 2020 13:05:15 +0430 Subject: [PATCH 2/2] Test: Fix test file name --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a48f09ad..c1095153 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ dist: python3 -m twine upload dist/* test: - python3 blockapi/test.py + python3 blockapi/test_init.py -.PHONY: dist \ No newline at end of file +.PHONY: dist