forked from clugg/coinspot-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinspot.py
318 lines (252 loc) · 8.31 KB
/
coinspot.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import hashlib
import hmac
import json
from time import time
from hashlib import sha512
import requests
class CoinSpot(object):
# The endpoint for the API
API_ENDPOINT = "https://www.coinspot.com.au/api/"
#API_ENDPOINT = "https://www.coinspot.com.au/api/ro"
"""Sets up the user's API key and secret.
Args:
key: Your API key generated from the settings page.
sign: The POST data is to be signed using your secret key
according to HMAC-SHA512 method.
"""
def __init__(self, key, secret):
self.key = key
self.secret = secret.encode()
"""Yields the input, resulting in Transfer-Encoding: chunked while
using the requests library.
Args:
data: Data to make generator for.
Returns:
A generator containing the data provided.
"""
def _chunker(self, data):
yield data
"""Forms a request to the private API using the path and data provided,
automatically including a nonce and signature for the data.
Args:
path: API endpoint to interact with.
data (optional): Data to send with request.
Returns:
A Response instance.
"""
def _request(self, path, data=None):
if data is None:
data = {}
data["nonce"] = int(time() * 1000)
json_data = json.dumps(data, separators=(',', ':')).encode()
return requests.post(
self.API_ENDPOINT + path,
data=self._chunker(json_data),
headers={
"Content-Type": "application/json",
"sign": hmac.new(self.secret, json_data, sha512).hexdigest(),
"key": self.key,
}
).json()
"""Latest Prices
Returns:
status: ok, error
prices: object containing one property for each coin
with the latest prices for that coin
"""
@staticmethod
def latest():
return requests.get("https://www.coinspot.com.au/pubapi/latest").json()
"""List Open Orders
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
Returns:
status: ok, error
buyorders: array containing all the open buy orders
sellorders: array containing all the open sell orders
"""
def orders(self, cointype):
return self._request("orders", {
"cointype": cointype
})
"""List Order History
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
Returns:
status: ok, error
orders: list of the last 1000 completed orders
"""
def orders_history(self, cointype):
return self._request("orders/history", {
"cointype": cointype
})
"""List My Balances
Returns:
status: ok, error
balances: object containing one property for each coin
with your balance for that coin.
"""
def my_balances(self):
return self._request("ro/my/balances")
"""List My Orders
A list of your open orders by coin type, it will
return a maximum of 100 results
Returns:
status: ok, error
buyorders: array containing all your buy orders
sellorders: array containing all your sell orders
"""
def my_orders(self):
return self._request("my/orders")
"""Place Buy Order
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
amount: the amount of coins you want to buy,
max precision 8 decimal places
rate: the rate in AUD you are willing to pay,
max precision 6 decimal places
Returns:
status: ok, error
"""
def my_buy(self, cointype, amount, rate):
return self._request("my/buy", {
"cointype": cointype,
"amount": amount,
"rate": rate
})
"""Cancel Buy Order
Args:
id: the id of the order to cancel
Returns:
status: ok, error
"""
def my_buy_cancel(self, _id):
return self._request("my/buy/cancel", {
"id": _id
})
"""Place Sell Order
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
amount: the amount of coins you want to sell,
max precision 8 decimal places
rate: the rate in AUD you are willing to sell for,
max precision 6 decimal places
Returns:
status: ok, error
"""
def my_sell(self, cointype, amount, rate):
return self._request("my/sell", {
"cointype": cointype,
"amount": amount,
"rate": rate
})
"""Display Coin Balance
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
Returns:
status: ok, error
balance - object containing one property with your balance, AUD value and rate for that coin
"""
def my_coin_balance(self, cointype):
return self._request("api/ro/my/balances/:cointype", {
"cointype": cointype
})
"""Cancel Sell Order
Args:
id: the id of the order to cancel
Returns:
status: ok, error
"""
def my_sell_cancel(self, _id):
return self._request("my/sell/cancel", {
"id": _id
})
"""Deposit Coins
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
Returns:
status: ok, error
address: your deposit address for the coin
"""
def my_coin_deposit(self, cointype):
return self._request("my/coin/deposit", {
"cointype": cointype
})
"""Quick Buy Quote
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
amount: the amount of coins to buy
Returns:
status: ok, error
quote: the rate per coin
timeframe: estimate hours to wait for trade to complete
(0 = immediate trade)
"""
def quote_buy(self, cointype, amount):
return self._request("quote/buy", {
"cointype": cointype,
"amount": amount
})
"""Quick Sell Quote
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
amount: the amount of coins to sell
Returns:
status: ok, error
quote: the rate per coin
timeframe: estimate hours to wait for trade to complete
(0 = immediate trade)
"""
def quote_sell(self, cointype, amount):
return self._request("quote/sell", {
"cointype": cointype,
"amount": amount
})
"""Display Coin Transactions
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
startdate - (optional) format 'YYYY-MM-DD'
enddate - (optional) format 'YYYY-MM-DD'
Returns:
status - ok, error
buyorders - array containing your coins buy order history
sellorders - array containing your coins sell order history
"""
def coin_transactions(self, cointype, startdate, enddate):
return self._request("ro/my/transactions/:cointype", {
"cointype": cointype,
"startdate": startdate,
"enddate": enddate
})
"""Display Transactions
Args:
startdate - (optional) format 'YYYY-MM-DD'
enddate - (optional) format 'YYYY-MM-DD'
Returns:
status - ok, error
buyorders - array containing your coins buy order history
sellorders - array containing your coins sell order history
"""
def my_transactions(self, startdate, enddate):
return self._request("ro/my/transactions", {
"startdate": startdate,
"enddate": enddate
})
"""Display Coin Balance
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
Response
status - ok, error
balance - object containing one property with your balance, AUD value and rate for that coin
"""
def my_coin_balance(self,cointype):
return self._request("api/ro/my/balances/:cointype", {
"cointype": cointype
})
""" Display Affiliate Payments
Response:
an object containing a list of all your affiliate payments
"""
def affiliate_payments(self):
return self._request("ro/my/affiliatepayments", {
})