· 7 years ago · Mar 27, 2018, 05:16 AM
1api_base = 'https://api.gdax.com'
2 passphrase = ''
3 api_key = ''
4 api_secret = ''
5
6 class GDAXRequestAuth(AuthBase):
7def __init__(self, api_key, secret_key, passphrase):
8 self.api_key = api_key
9 self.secret_key = secret_key
10 self.passphrase = passphrase
11
12def __call__(self, request):
13 timestamp = str(time.time())
14 message = timestamp + request.method + request.path_url + (request.body or '')
15 hmac_key = base64.b64decode(self.secret_key)
16 signature = hmac.new(hmac_key, message.encode('utf-8'), hashlib.sha256)
17 signature_b64 = base64.b64encode(signature.digest())
18 request.headers.update({
19 'CB-ACCESS-SIGN': signature_b64,
20 'CB-ACCESS-TIMESTAMP': timestamp,
21 'CB-ACCESS-KEY': self.api_key,
22 'CB-ACCESS-PASSPHRASE': self.passphrase,
23 'Content-Type': 'application/json'
24 })
25 return request
26
27 def buy_limit(product_id, size, price):
28auth = GDAXRequestAuth(api_key, api_secret, passphrase)
29order_data = {
30'type': 'limit',
31'side': 'buy',
32'product_id': product_id,
33'size': size,
34'price': price,
35'post_only': True
36}
37response = requests.post(api_base + '/orders', data=json.dumps(order_data), auth=auth)
38if response.status_code is not 200:
39 raise Exception('Invalid GDAX Status Code: %d' % response.status_code)
40return response.json()