-
Notifications
You must be signed in to change notification settings - Fork 0
/
v4dydxget_all_fills.py
95 lines (90 loc) · 4.9 KB
/
v4dydxget_all_fills.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
import sys
from datetime import datetime
from dateutil.parser import isoparse
from os import path, popen
from random import randrange
from requests import get
from sys import argv, maxsize
INDEXERURL = 'https://indexer.dydx.trade/v4'
#counterlimit is the number of iterations it would pull records per subaccount per order-type (short-term and long-term). The indexer API limit is 100 records per iteration.
#counterlimit = 100 means '100'(counterlimit) x number of subaccounts x 2(types of orders) interactios x 100 maximum records per iteraction
counterlimit = 100
#walletaddress
def getsubaccounts():
subaccountslist = []
r = get(INDEXERURL+'/addresses/'+walletaddress)
try:
r.raise_for_status()
if r.status_code == 200:
for item in r.json()['subaccounts']:
subaccountslist.append(item['subaccountNumber'])
return subaccountslist
else:
print('Bad requests status code:', r.status_code)
except Exception as error:
print('getsubaccounts() Address not found', walletaddress)
return None
#walletaddress
def findfills():
counter = 0
subaccountlist = getsubaccounts()
for subaccountnumber in subaccountlist:
if len(subaccountlist) > 1:
print('Searching subaccount', str(subaccountnumber)+'...')
# height = maxsize
# height limited to 2147483647 for 32-bit OS, equivalent to 2038-01-19T03:14Z
height = 2147483647
newheight = 0
while newheight < height:
if counter > counterlimit:
#reached counterlimit, move to next subaccount
break
if market == 'all':
r = get(INDEXERURL+'/fills', params = {
'address': walletaddress,
'subaccountNumber': subaccountnumber,
'createdBeforeOrAtHeight': height,
})
else:
r = get(INDEXERURL+'/fills', params = {
'address': walletaddress,
'subaccountNumber': subaccountnumber,
'market': market,
'marketType': 'PERPETUAL',
'createdBeforeOrAtHeight': height,
})
try:
r.raise_for_status()
if r.status_code == 200:
if len(r.json()) > 0:
if len(r.json()['fills']) > 1:
topheight = r.json()['fills'][0]['createdAtHeight']
newheight = r.json()['fills'][-1]['createdAtHeight']
if topheight == newheight:
print('Error: more than 100 records with the same createdAtHeight', topheight)
#move to next subaccount
break
for item in r.json()['fills']:
print(item)
if len(r.json()['fills']) > 99 and int(newheight) < height:
height = int(newheight) + 1
newheight = 0
counter += 1
else:
#end of results, move to next subaccount
break
else:
#no results, move to next subaccount
break
else:
print('findorder2a() Bad requests status code:', r.status_code)
return None
except Exception as error:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "findorder2a() api query failed (%s)" % error)
return None
walletaddress = argv[1]
if len(argv) > 2:
market = argv[2]
else:
market = 'all'
findfills()