-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddresswatcher.py
73 lines (64 loc) · 2.43 KB
/
addresswatcher.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
import datetime
import gevent
import requests
import base58
class AddressWatcher(gevent.Greenlet):
addresses = {}
def __init__(self, testnet=True):
gevent.Greenlet.__init__(self)
self.transfer_tx_callback = None
self.testnet = testnet
if testnet:
self.url_base = "https://api-test.wavesplatform.com/v0"
self.asset_id = "CgUrFtinLXEbJwJVjwwcppk4Vpz1nMmR3H5cQaDcUcfe"
else:
self.url_base = "https://api.wavesplatform.com/v0"
self.asset_id = "9R3iLi4qGLVWKc16Tg98gmRvgg1usGEYd7SgC1W5D6HB"
def _run(self):
print("running AddressWatcher...")
dt = datetime.datetime.utcnow()
js_datestring = dt.strftime("%Y-%m-%dT%H:%M:%SZ")
after = None
last = True
while 1:
# poll for more transactions
url = self.url_base + "/transactions/transfer"
params = {"assetId": self.asset_id, "timeStart": js_datestring, "sort": "asc"}
if after:
params["after"] = after
r = requests.get(url, params=params)
if r.status_code == 200:
body = r.json()
for tx in body["data"]:
tx = tx["data"]
if tx["recipient"] in self.addresses:
if tx["attachment"]:
tx["attachment"] = base58.b58decode(tx["attachment"]).decode("utf-8")
tokens = self.addresses[tx["recipient"]]
if self.transfer_tx_callback:
self.transfer_tx_callback(tokens, tx)
if "lastCursor" in body:
after = body["lastCursor"]
if "isLastPage" in body:
last = body["isLastPage"]
else:
#TODO log error
print(r)
# sleep
gevent.sleep(5)
def watch(self, address, token):
if not address in self.addresses:
self.addresses[address] = tokens = []
else:
tokens = self.addresses[address]
if token not in tokens:
tokens.append(token)
def watched(self):
return self.addresses
def transfer_tx(self, txid):
url = self.url_base + "/transactions/transfer/" + txid
r = requests.get(url)
if r.status_code == 200:
body = r.json()
return body["data"]
return None