-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtcx.js
177 lines (164 loc) · 6.34 KB
/
btcx.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
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class btcx extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'btcx',
'name': 'BTCX',
'countries': [ 'IS', 'US', 'EU' ],
'rateLimit': 1500, // support in english is very poor, unable to tell rate limits
'version': 'v1',
'has': {
'CORS': false,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27766385-9fdcc98c-5ed6-11e7-8f14-66d5e5cd47e6.jpg',
'api': 'https://btc-x.is/api',
'www': 'https://btc-x.is',
'doc': 'https://btc-x.is/custom/api-document.html',
},
'api': {
'public': {
'get': [
'depth/{id}/{limit}',
'ticker/{id}',
'trade/{id}/{limit}',
],
},
'private': {
'post': [
'balance',
'cancel',
'history',
'order',
'redeem',
'trade',
'withdraw',
],
},
},
'markets': {
'BTC/USD': { 'id': 'btc/usd', 'symbol': 'BTC/USD', 'base': 'BTC', 'quote': 'USD' },
'BTC/EUR': { 'id': 'btc/eur', 'symbol': 'BTC/EUR', 'base': 'BTC', 'quote': 'EUR' },
},
});
}
async fetchBalance (params = {}) {
let balances = await this.privatePostBalance ();
let result = { 'info': balances };
let currencies = Object.keys (balances);
for (let c = 0; c < currencies.length; c++) {
let currency = currencies[c];
let uppercase = currency.toUpperCase ();
let account = {
'free': balances[currency],
'used': 0.0,
'total': balances[currency],
};
result[uppercase] = account;
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
let request = {
'id': this.marketId (symbol),
};
if (typeof limit !== 'undefined')
request['limit'] = limit; // 1000
let orderbook = await this.publicGetDepthIdLimit (this.extend (request, params));
return this.parseOrderBook (orderbook, undefined, 'bids', 'asks', 'price', 'amount');
}
async fetchTicker (symbol, params = {}) {
let ticker = await this.publicGetTickerId (this.extend ({
'id': this.marketId (symbol),
}, params));
let timestamp = ticker['time'] * 1000;
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': parseFloat (ticker['high']),
'low': parseFloat (ticker['low']),
'bid': parseFloat (ticker['sell']),
'ask': parseFloat (ticker['buy']),
'vwap': undefined,
'open': undefined,
'close': undefined,
'first': undefined,
'last': parseFloat (ticker['last']),
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': undefined,
'quoteVolume': parseFloat (ticker['volume']),
'info': ticker,
};
}
parseTrade (trade, market) {
let timestamp = parseInt (trade['date']) * 1000;
let side = (trade['type'] === 'ask') ? 'sell' : 'buy';
return {
'id': trade['id'],
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': market['symbol'],
'type': undefined,
'side': side,
'price': trade['price'],
'amount': trade['amount'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
let market = this.market (symbol);
let response = await this.publicGetTradeIdLimit (this.extend ({
'id': market['id'],
'limit': 1000,
}, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
let response = await this.privatePostTrade (this.extend ({
'type': side.toUpperCase (),
'market': this.marketId (symbol),
'amount': amount,
'price': price,
}, params));
return {
'info': response,
'id': response['order']['id'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.privatePostCancel ({ 'order': id });
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + this.version + '/';
if (api === 'public') {
url += this.implodeParams (path, params);
} else {
this.checkRequiredCredentials ();
let nonce = this.nonce ();
url += api;
body = this.urlencode (this.extend ({
'Method': path.toUpperCase (),
'Nonce': nonce,
}, params));
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Key': this.apiKey,
'Signature': this.hmac (this.encode (body), this.encode (this.secret), 'sha512'),
};
}
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)
throw new ExchangeError (this.id + ' ' + this.json (response));
return response;
}
};