-
Notifications
You must be signed in to change notification settings - Fork 6
/
AccountBalance.py
executable file
·84 lines (67 loc) · 2.4 KB
/
AccountBalance.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
#!/usr/bin/env python3
import requests
import time
import hashlib
import hmac
import base64
import json
from collections import OrderedDict
from config import apikey_secret
from config import apikey_public
from config import domain
# Define Global Vars
uri = "/account/balance"
url = domain + uri
askey = apikey_secret.encode("utf-8")
pkey = apikey_public.encode("utf-8")
skey = base64.standard_b64decode(askey)
def build_headers(URL, PUBKEY, PRIVKEY):
"""Build timestamp, format and encode everything, and construct string to
sign with api key. Use HmacSHA512 algorithm in order to sign.
Lastly build the headers to send... In order to ensure the correct order
of key value pairs in the JSON payload, build an ordered dictionary out
of a list of tuples.
"""
# Build timestamp
tstamp = time.time()
ctstamp = int(tstamp * 1000) # or int(tstamp * 1000) or round(tstamp * 1000)
sctstamp = str(ctstamp)
# Optional timestamping method, not recommended as ticker results may
# be cached beyond the authentication window.
#
#urt = "/market/BTC/AUD/tick"
#turl = domain + urt
#rt = requests.get(turl, verify=True)
#tstamp = r.json()["timestamp"]
#ctstamp = tstamp * 1000
# Build and sign to construct body
sbody = uri + "\n" + sctstamp + "\n"
rbody = sbody.encode("utf-8")
rsig = hmac.new(skey, rbody, hashlib.sha512)
bsig = base64.standard_b64encode(rsig.digest()).decode("utf-8")
# Construct header list of key value pairs
headers_list = OrderedDict([("Accept", "application/json"),
("Accept-Charset", "UTF-8"),
("Content-Type", "application/json"),
("apikey", pkey),
("timestamp", sctstamp),
("signature", bsig)])
# Load list into dictionary
headers = dict(headers_list)
return headers
def main():
""" Build the request body by invoking header function with config
params specified as global variables at top and return the balances for
AUD, BTC and LTC.
TODO: Add in functionality to pass options for the CLI.
"""
res = build_headers(url, pkey, skey)
r = requests.get(url, headers=res, verify=True)
audbal = r.json()[0]
btcbal = r.json()[1]
ltcbal = r.json()[2]
print(audbal)
print(btcbal)
print(ltcbal)
if __name__ == "__main__":
main()