-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoinmate.js
214 lines (200 loc) · 7.96 KB
/
coinmate.js
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
"use strict";
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class coinmate extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'coinmate',
'name': 'CoinMate',
'countries': [ 'GB', 'CZ', 'EU' ], // UK, Czech Republic
'rateLimit': 1000,
'has': {
'CORS': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27811229-c1efb510-606c-11e7-9a36-84ba2ce412d8.jpg',
'api': 'https://coinmate.io/api',
'www': 'https://coinmate.io',
'doc': [
'http://docs.coinmate.apiary.io',
'https://coinmate.io/developers',
],
},
'requiredCredentials': {
'apiKey': true,
'secret': true,
'uid': true,
},
'api': {
'public': {
'get': [
'orderBook',
'ticker',
'transactions',
],
},
'private': {
'post': [
'balances',
'bitcoinWithdrawal',
'bitcoinDepositAddresses',
'buyInstant',
'buyLimit',
'cancelOrder',
'cancelOrderWithInfo',
'createVoucher',
'openOrders',
'redeemVoucher',
'sellInstant',
'sellLimit',
'transactionHistory',
'unconfirmedBitcoinDeposits',
],
},
},
'markets': {
'BTC/EUR': { 'id': 'BTC_EUR', 'symbol': 'BTC/EUR', 'base': 'BTC', 'quote': 'EUR', 'precision': { 'amount': 4, 'price': 2 }},
'BTC/CZK': { 'id': 'BTC_CZK', 'symbol': 'BTC/CZK', 'base': 'BTC', 'quote': 'CZK', 'precision': { 'amount': 4, 'price': 2 }},
'LTC/BTC': { 'id': 'LTC_BTC', 'symbol': 'LTC/BTC', 'base': 'LTC', 'quote': 'BTC', 'precision': { 'amount': 4, 'price': 5 }},
},
'fees': {
'trading': {
'maker': 0.0005,
'taker': 0.0035,
},
},
});
}
async fetchBalance (params = {}) {
let response = await this.privatePostBalances ();
let balances = response['data'];
let result = { 'info': balances };
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balances) {
account['free'] = balances[currency]['available'];
account['used'] = balances[currency]['reserved'];
account['total'] = balances[currency]['balance'];
}
result[currency] = account;
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, params = {}) {
let response = await this.publicGetOrderBook (this.extend ({
'currencyPair': this.marketId (symbol),
'groupByPriceLimit': 'False',
}, params));
let orderbook = response['data'];
let timestamp = orderbook['timestamp'] * 1000;
return this.parseOrderBook (orderbook, timestamp, 'bids', 'asks', 'price', 'amount');
}
async fetchTicker (symbol, params = {}) {
let response = await this.publicGetTicker (this.extend ({
'currencyPair': this.marketId (symbol),
}, params));
let ticker = response['data'];
let timestamp = ticker['timestamp'] * 1000;
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': parseFloat (ticker['high']),
'low': parseFloat (ticker['low']),
'bid': parseFloat (ticker['bid']),
'ask': parseFloat (ticker['ask']),
'vwap': undefined,
'open': undefined,
'close': undefined,
'first': undefined,
'last': parseFloat (ticker['last']),
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': parseFloat (ticker['amount']),
'quoteVolume': undefined,
'info': ticker,
};
}
parseTrade (trade, market = undefined) {
if (!market)
market = this.markets_by_id[trade['currencyPair']];
return {
'id': trade['transactionId'],
'info': trade,
'timestamp': trade['timestamp'],
'datetime': this.iso8601 (trade['timestamp']),
'symbol': market['symbol'],
'type': undefined,
'side': undefined,
'price': trade['price'],
'amount': trade['amount'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
let market = this.market (symbol);
let response = await this.publicGetTransactions (this.extend ({
'currencyPair': market['id'],
'minutesIntoHistory': 10,
}, params));
return this.parseTrades (response['data'], market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
let method = 'privatePost' + this.capitalize (side);
let order = {
'currencyPair': this.marketId (symbol),
};
if (type == 'market') {
if (side == 'buy')
order['total'] = amount; // amount in fiat
else
order['amount'] = amount; // amount in fiat
method += 'Instant';
} else {
order['amount'] = amount; // amount in crypto
order['price'] = price;
method += this.capitalize (type);
}
let response = await this[method] (this.extend (order, params));
return {
'info': response,
'id': response['data'].toString (),
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.privatePostCancelOrder ({ 'orderId': id });
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + path;
if (api == 'public') {
if (Object.keys (params).length)
url += '?' + this.urlencode (params);
} else {
this.checkRequiredCredentials ();
let nonce = this.nonce ().toString ();
let auth = nonce + this.uid + this.apiKey;
let signature = this.hmac (this.encode (auth), this.encode (this.secret));
body = this.urlencode (this.extend ({
'clientId': this.uid,
'nonce': nonce,
'publicKey': this.apiKey,
'signature': signature.toUpperCase (),
}, params));
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
};
}
return { 'url': url, 'method': method, 'body': body, 'headers': headers };
}
async request (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let response = await this.fetch2 (path, api, method, params, headers, body);
if ('error' in response)
if (response['error'])
throw new ExchangeError (this.id + ' ' + this.json (response));
return response;
}
}