-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtronscan.py
More file actions
87 lines (68 loc) · 2.73 KB
/
tronscan.py
File metadata and controls
87 lines (68 loc) · 2.73 KB
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
from blockapi.services import BlockchainAPI
class TronscanAPI(BlockchainAPI):
"""
coins: tron
API docs: https://github.com/tronscan/tronscan-frontend/blob/dev2019
/document/api.md
Explorer: https://tronscan.org
"""
active = True
symbol = 'TRX'
base_url = 'https://apilist.tronscan.org/api'
rate_limit = 0
coef = 1e-6
max_items_per_page = None
page_offset_step = None
confirmed_num = None
supported_requests = {
'get_balance': '/account?address={address}',
'get_trc10_tokenlist': '/token?limit=10000',
'transfers': '/token_trc20/transfers?limit={limit}&start=0&sort=-timestamp&count=true&relatedAddress={address}'
}
def get_balance(self):
response = self.request('get_balance',
address=self.address)
if not response:
return None
token_map = {}
trc10_tokenlist = self.request('get_trc10_tokenlist')['data']
for token in trc10_tokenlist:
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
else:
if int(coin['name']) not in token_map:
# It has been found out that some coins are not
# present in token map
continue
symbol = token_map[int(coin['name'])]['abbr']
owner_address = coin['owner_address']
coin_coef = pow(-10,
int(token_map[int(coin['name'])]['precision']))
balances.append({'symbol': symbol,
'amount': float(coin['balance']) * coin_coef,
'address': owner_address})
for coin in response['trc20token_balances']:
balances.append(
{
'symbol': coin['symbol'],
'amount': float(coin['balance'])
* pow(-10, int(coin['decimals'])),
'address': None
})
return balances
def transfers(self, limit=50):
response = self.request('transfers', address=self.address, limit=limit)
transfers = []
for transfer in response["token_transfers"]:
sum_decimal = int(transfer["tokenInfo"]["tokenDecimal"])
transfer["quant"] = "{}.{}".format(transfer["quant"][:-sum_decimal], transfer["quant"][sum_decimal:])
transfers.append(transfer)
return transfers