-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoingi.js
213 lines (197 loc) · 8.24 KB
/
coingi.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
"use strict";
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange')
const { ExchangeError } = require ('./base/errors')
// ---------------------------------------------------------------------------
module.exports = class coingi extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'coingi',
'name': 'Coingi',
'rateLimit': 1000,
'countries': [ 'PA', 'BG', 'CN', 'US' ], // Panama, Bulgaria, China, US
'hasFetchTickers': true,
'hasCORS': false,
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/28619707-5c9232a8-7212-11e7-86d6-98fe5d15cc6e.jpg',
'api': 'https://api.coingi.com',
'www': 'https://coingi.com',
'doc': 'http://docs.coingi.apiary.io/',
},
'api': {
'current': {
'get': [
'order-book/{pair}/{askCount}/{bidCount}/{depth}',
'transactions/{pair}/{maxCount}',
'24hour-rolling-aggregation',
],
},
'user': {
'post': [
'balance',
'add-order',
'cancel-order',
'orders',
'transactions',
'create-crypto-withdrawal',
],
},
},
'markets': {
'LTC/BTC': { 'id': 'ltc-btc', 'symbol': 'LTC/BTC', 'base': 'LTC', 'quote': 'BTC' },
'PPC/BTC': { 'id': 'ppc-btc', 'symbol': 'PPC/BTC', 'base': 'PPC', 'quote': 'BTC' },
'DOGE/BTC': { 'id': 'doge-btc', 'symbol': 'DOGE/BTC', 'base': 'DOGE', 'quote': 'BTC' },
'VTC/BTC': { 'id': 'vtc-btc', 'symbol': 'VTC/BTC', 'base': 'VTC', 'quote': 'BTC' },
'FTC/BTC': { 'id': 'ftc-btc', 'symbol': 'FTC/BTC', 'base': 'FTC', 'quote': 'BTC' },
'NMC/BTC': { 'id': 'nmc-btc', 'symbol': 'NMC/BTC', 'base': 'NMC', 'quote': 'BTC' },
'DASH/BTC': { 'id': 'dash-btc', 'symbol': 'DASH/BTC', 'base': 'DASH', 'quote': 'BTC' },
},
});
}
async fetchBalance (params = {}) {
let currencies = [];
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c].toLowerCase ();
currencies.push (currency);
}
let balances = await this.userPostBalance ({
'currencies': currencies.join (',')
});
let result = { 'info': balances };
for (let b = 0; b < balances.length; b++) {
let balance = balances[b];
let currency = balance['currency']['name'];
currency = currency.toUpperCase ();
let account = {
'free': balance['available'],
'used': balance['blocked'] + balance['inOrders'] + balance['withdrawing'],
'total': 0.0,
};
account['total'] = this.sum (account['free'], account['used']);
result[currency] = account;
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, params = {}) {
let market = this.market (symbol);
let orderbook = await this.currentGetOrderBookPairAskCountBidCountDepth (this.extend ({
'pair': market['id'],
'askCount': 512, // maximum returned number of asks 1-512
'bidCount': 512, // maximum returned number of bids 1-512
'depth': 32, // maximum number of depth range steps 1-32
}, params));
return this.parseOrderBook (orderbook, undefined, 'bids', 'asks', 'price', 'baseAmount');
}
parseTicker (ticker, market = undefined) {
let timestamp = this.milliseconds ();
let symbol = undefined;
if (market)
symbol = market['symbol'];
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': ticker['high'],
'low': ticker['low'],
'bid': ticker['highestBid'],
'ask': ticker['lowestAsk'],
'vwap': undefined,
'open': undefined,
'close': undefined,
'first': undefined,
'last': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': ticker['baseVolume'],
'quoteVolume': ticker['counterVolume'],
'info': ticker,
};
return ticker;
}
async fetchTickers (symbols = undefined, params = {}) {
let response = await this.currentGet24hourRollingAggregation (params);
let result = {};
for (let t = 0; t < response.length; t++) {
let ticker = response[t];
let base = ticker['currencyPair']['base'].toUpperCase ();
let quote = ticker['currencyPair']['counter'].toUpperCase ();
let symbol = base + '/' + quote;
let market = this.markets[symbol];
result[symbol] = this.parseTicker (ticker, market);
}
return result;
}
async fetchTicker (symbol, params = {}) {
let tickers = await this.fetchTickers (undefined, params);
if (symbol in tickers)
return tickers[symbol];
throw new ExchangeError (this.id + ' return did not contain ' + symbol);
}
parseTrade (trade, market = undefined) {
if (!market)
market = this.markets_by_id[trade['currencyPair']];
return {
'id': trade['id'],
'info': trade,
'timestamp': trade['timestamp'],
'datetime': this.iso8601 (trade['timestamp']),
'symbol': market['symbol'],
'type': undefined,
'side': undefined, // type
'price': trade['price'],
'amount': trade['amount'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
let market = this.market (symbol);
let response = await this.currentGetTransactionsPairMaxCount (this.extend ({
'pair': market['id'],
'maxCount': 128,
}, params));
return this.parseTrades (response, market);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
let order = {
'currencyPair': this.marketId (symbol),
'volume': amount,
'price': price,
'orderType': (side == 'buy') ? 0 : 1,
};
let response = await this.userPostAddOrder (this.extend (order, params));
return {
'info': response,
'id': response['result'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.userPostCancelOrder ({ 'orderId': id });
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + api + '/' + this.implodeParams (path, params);
let query = this.omit (params, this.extractParams (path));
if (api == 'current') {
if (Object.keys (query).length)
url += '?' + this.urlencode (query);
} else {
let nonce = this.nonce ();
let request = this.extend ({
'token': this.apiKey,
'nonce': nonce,
}, query);
let auth = nonce.toString () + '$' + this.apiKey;
request['signature'] = this.hmac (this.encode (auth), this.encode (this.secret));
body = this.json (request);
headers = {
'Content-Type': 'application/json',
};
}
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 ('errors' in response)
throw new ExchangeError (this.id + ' ' + this.json (response));
return response;
}
}