-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddresswatcher.py
72 lines (62 loc) · 2.34 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
import datetime
import gevent
import requests
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 start_watching(self, transfer_tx_callback):
self.transfer_tx_callback = transfer_tx_callback
self.start()
def _run(self):
print("running AddressWatcher...")
dt = datetime.datetime.utcnow()
js_datestring = dt.strftime("%Y-%m-%dT%H:%M:%SZ")
after = None
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
#print(params)
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:
api_keys = self.addresses[tx["recipient"]]
if self.transfer_tx_callback:
self.transfer_tx_callback(api_keys, tx)
if "lastCursor" in body:
after = body["lastCursor"]
else:
#TODO log error
print(r)
# sleep
gevent.sleep(5)
def watch(self, address, api_key):
if not address in self.addresses:
self.addresses[address] = api_keys = []
else:
api_keys = self.addresses[address]
if api_key not in api_keys:
api_keys.append(api_key)
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