-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaccount.py
162 lines (132 loc) · 4.98 KB
/
account.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import requests
from sys import stderr
class Account:
# static use only
cmc = None
currency = ""
name = ""
addr = ""
balance = 0.0
balance_usd = 0.0
wallet_addr = ""
def __init__(self, crypto):
self.currency = crypto['currency']
if 'name' in crypto:
self.name = crypto['name']
if 'addr' in crypto:
self.addr = crypto['addr']
if 'balance' in crypto:
self.balance = crypto['balance']
if 'wallet_addr' in crypto:
self.wallet_addr = crypto['wallet_addr']
def factory(crypto):
type = crypto['currency']
if type == "bitcoin":
return BitcoinAccount(crypto)
elif type == "zcash":
return ZcashAccount(crypto)
elif type == "ethereum":
return EthereumAccount(crypto)
elif type == "aeon":
return AeonAccount(crypto)
elif type == "monero":
return MoneroAccount(crypto)
elif type == "litecoin":
return LitecoinAccount(crypto)
else:
return OtherAccount(crypto)
factory = staticmethod(factory)
def fill_balance(self):
raise NotImplementedError()
def fill_usd_balance(self):
if Account.cmc is None:
url = 'https://api.coinmarketcap.com/v1/ticker/'
Account.cmc = requests.get(url)
if not Account.cmc.ok:
stderr.write('Error getting USD values for %s\n' % self.name)
return
for curr in Account.cmc.json():
if curr['id'] == self.currency:
self.balance_usd = float(curr['price_usd']) * self.balance
class BitcoinAccount(Account):
def fill_balance(self):
if self.addr is not "":
url = 'https://blockchain.info/rawaddr/%s' % self.addr
btc_rsp = requests.get(url)
try:
self.balance = float(btc_rsp.json()['final_balance'])
# from satoshi to btc
self.balance *= 0.00000001
except:
stderr.write('BTC fill_balance error: addr=%s\n' % self.addr)
class ZcashAccount(Account):
def fill_balance(self):
if self.addr is not "":
url = 'https://api.zcha.in/v2/mainnet/accounts/%s' % self.addr
zec_rsp = requests.get(url)
try:
self.balance = float(zec_rsp.json()['balance'])
except:
stderr.write('ZEC fill_balance error: addr=%s\n' % self.addr)
class EthereumAccount(Account):
def fill_balance(self):
if self.addr is not "":
url = 'https://api.etherscan.io/api?' \
'module=account&action=balance&address=%s' % self.addr
eth_rsp = requests.get(url)
try:
self.balance = float(eth_rsp.json()['result'])
self.balance *= 0.000000000000000001
except:
stderr.write('ETH fill_balance error: addr=%s\n' % self.addr)
class AeonAccount(Account):
def fill_balance(self):
if self.balance != 0.0:
return self.balance
if self.wallet_addr is not "":
jsoncontent = b'{"jsonrpc":"2.0","id":"0","method":"getbalance"}'
headers = {'Content-Type': 'application/json'}
try:
aeon_rsp = requests.post(
self.wallet_addr + '/json_rpc',
headers=headers,
data=jsoncontent)
if aeon_rsp.json():
self.balance = float(
aeon_rsp.json().get(
"result", 0).get(
'balance', 0))
self.balance *= 0.000000000001
except requests.exceptions.ConnectionError as e:
msg = (
"Getting {currency}'s balance have failed with message:"
"\n{message}\n").format(
currency=self.currency, message=e)
print(msg)
class MoneroAccount(AeonAccount):
pass
class LitecoinAccount(BitcoinAccount):
def fill_balance(self):
if self.addr is not "":
url = "https://api.blockchair.com/litecoin/dashboards/address/%s" % self.addr
try:
chair_rsp = requests.get(url)
if chair_rsp.json():
self.balance = float(chair_rsp.json().get(
"data", 0
).get(
self.addr, 0
).get(
"address", 0
).get("balance", 0))
self.balance *= 0.000000000001
except requests.exceptions.ConnectionError as e:
msg = (
"Getting {currency}'s balance have failed with message:"
"\n{message}\n").format(
currency=self.currency, message=e)
print(msg)
return 0.0
class OtherAccount(Account):
def fill_balance(self):
return