This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoinspot.py
255 lines (206 loc) · 6.52 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
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:443/api/"
"""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("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
})
"""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
})
"""Send Coins
Args:
cointype: the coin shortname, example value 'BTC', 'LTC', 'DOGE'
address: the address to send coins to
amount: the amount of coins to send
Returns:
status: ok, error
"""
def my_coin_send(self, cointype, address, amount):
return self._request("my/coin/send", {
"cointype": cointype,
"address": address,
"amount": amount
})
"""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
})