Skip to content

Commit 6221e13

Browse files
author
Pedro Pablo Bustamante Barrera
committed
Merge branch 'develop'
2 parents 562c5e1 + c5fcece commit 6221e13

19 files changed

+335
-36
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,39 @@ This sdk makes use of the [api version 3](https://api.exchange.cryptomkt.com) of
2020

2121
## rest client
2222

23-
```javascript
23+
```typescript
2424
const { Client } = require("cryptomarket");
2525

2626
// instance a client
27-
let apiKey = "AB32B3201";
28-
let apiSecret = "21b12401";
29-
let client = new Client(apiKey, apiSecret);
27+
const apiKey = "AB32B3201";
28+
const apiSecret = "21b12401";
29+
const client = new Client(apiKey, apiSecret);
3030

3131
// get currencies
32-
let currencies = await client.getCurrencies();
32+
const currencies = await client.getCurrencies();
3333

3434
// get order books
35-
let orderBook = await client.getOrderBook("EOSETH");
35+
const orderBook = await client.getOrderBook("EOSETH");
3636

3737
// get your account balances
38-
let accountBalances = await client.getWalletBalances();
38+
const accountBalances = await client.getWalletBalances();
3939

4040
// get your trading balances
41-
let tradingBalances = await client.getSpotTradingBalances();
41+
const tradingBalances = await client.getSpotTradingBalances();
4242

4343
// move balance from wallet to spot trading
44-
let result = await client.transferBetweenWalletAndExchange({
44+
const result = await client.transferBetweenWalletAndExchange({
4545
currency: "EOS",
4646
amount: "3.2",
4747
source: Account.Wallet,
4848
destination: Account.Spot,
4949
});
5050

5151
// get your active orders
52-
let orders = await client.getAllActiveSpotOrders("EOSETH");
52+
const activeOrders = await client.getAllActiveSpotOrders("EOSETH");
5353

5454
// create a new order
55-
let newOrder = await client.createOrder({
55+
const newOrder = await client.createOrder({
5656
symbol: "EOSETH",
5757
side: "buy",
5858
quantity: "10",

lib/client.ts

Lines changed: 175 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import {
3737
Trade,
3838
Transaction,
3939
} from "./models";
40+
import { ConvertedCandlesBySymbol } from "./models/ConvertedCandle";
41+
import { ConvertedCandles } from "./models/ConvertedCandles";
4042
import { fromCamelCaseToSnakeCase, fromSnakeCaseToCamelCase } from "./paramStyleConverter"
4143

4244

@@ -86,6 +88,102 @@ export class Client {
8688
return fromSnakeCaseToCamelCase(response);
8789
}
8890

91+
92+
//////////////
93+
// ALIASES //
94+
/////////////
95+
// MARKET DATA
96+
97+
/**
98+
* alias of {@link getTicker}
99+
*/
100+
getTickerBySymbol = this.getTicker;
101+
/**
102+
* alias of {@link getTicker}
103+
*/
104+
getTickerOfSymbol = this.getTicker;
105+
/**
106+
* alias of {@link getTickerLastPrice}
107+
*/
108+
getTickerPriceBySymbol = this.getTickerLastPrice;
109+
/**
110+
* alias of {@link getTickerLastPrice}
111+
*/
112+
getTickerPriceOfSymbol = this.getTickerLastPrice;
113+
/**
114+
* Alias of {@link getTradesBySymbol}
115+
*/
116+
getTradesOfSymbol = this.getTradesBySymbol;
117+
/**
118+
* alias of {@link getOrderBook}
119+
*/
120+
getOrderbookBySymbol = this.getOrderBook;
121+
/**
122+
* alias of {@link getOrderBook}
123+
*/
124+
getOrderbookOfSymbol = this.getOrderBook;
125+
/**
126+
* alias of {@link getOrderBookVolume}
127+
*/
128+
getOrderBookVolumeBySymbol = this.getOrderBookVolume;
129+
/**
130+
* alias of {@link getOrderBookVolume}
131+
*/
132+
getOrderBookVolumeOfSymbol = this.getOrderBookVolume;
133+
/**
134+
* alias of {@link getCandlesBySymbol}
135+
*/
136+
getCandlesOfSymbol = this.getCandlesBySymbol;
137+
/**
138+
* alias of {@link getConvertedCandlesBySymbol}
139+
*/
140+
getConvertedCandlesOfSymbol = this.getConvertedCandlesBySymbol;
141+
142+
// SPOT TRADING
143+
144+
/**
145+
* alias of {@link getSpotTradingBalance}
146+
*/
147+
getSpotTradingBalanceOfCurrency = this.getSpotTradingBalance;
148+
/**
149+
* alias of {@link getSpotTradingBalance}
150+
*/
151+
getSpotTradingBalanceByCurrency = this.getSpotTradingBalance;
152+
153+
/**
154+
* alias of {@link getAllTradingCommissions}
155+
*/
156+
getTradingCommissions = this.getAllTradingCommissions;
157+
/**
158+
* alias of {@link getTradingCommission}
159+
*/
160+
getTradingCommissionOfSymbol = this.getTradingCommission;
161+
/**
162+
* alias of {@link getTradingCommission}
163+
*/
164+
getTradingCommissionBySymbol = this.getTradingCommission;
165+
166+
// WALLET MANAGEMENT
167+
168+
/**
169+
* alias of {@link getWalletBalance}
170+
*/
171+
getWalletBalanceOfCurrency = this.getWalletBalance;
172+
/**
173+
* alias of {@link getWalletBalance}
174+
*/
175+
getWalletBalanceByCurrency = this.getWalletBalance;
176+
/**
177+
* alais of {@link getDepositCryptoAddress}
178+
*/
179+
getDepositCryptoAddressByCurrency = this.getDepositCryptoAddress;
180+
/**
181+
* alias of {@link getDepositCryptoAddress}
182+
*/
183+
getDepositCryptoAddressOfCurrency = this.getDepositCryptoAddress;
184+
185+
186+
89187
//////////////////
90188
// PUBLIC CALLS //
91189
//////////////////
@@ -394,9 +492,9 @@ export class Client {
394492
/**
395493
* Get candles for all symbols or for specified symbols
396494
*
397-
* Candels are used for OHLC representation
495+
* Candles are used for OHLC representation
398496
*
399-
* The result contains candles with non-zero volume only (no trades = no candles).
497+
* The result contains candles with non-zero volume only (no trades = no candles).
400498
*
401499
* Requires no API key Access Rights.
402500
*
@@ -426,9 +524,9 @@ export class Client {
426524
/**
427525
* Get candles for a symbol
428526
*
429-
* Candels are used for OHLC representation
527+
* Candles are used for OHLC representation
430528
*
431-
* The result contains candles with non-zero volume only (no trades = no candles).
529+
* The result contains candles with non-zero volume only (no trades = no candles).
432530
*
433531
* Requires no API key Access Rights.
434532
*
@@ -459,6 +557,79 @@ export class Client {
459557
return this.get(`public/candles/${symbol}`, params);
460558
}
461559

560+
/**
561+
* Gets candles regarding the last price converted to the target currency for all symbols or for the specified symbols
562+
*
563+
* Candles are used for OHLC representation
564+
*
565+
* The result contains candles with non-zero volume only (no trades = no candles).
566+
*
567+
* Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid or ask price, the last price is returned.
568+
*
569+
* Requires no API key Access Rights.
570+
*
571+
* https://api.exchange.cryptomkt.com/#candles
572+
*
573+
* @param {object} [params]
574+
* @param {string[]} [params.targetCurrency] Target currency for conversion
575+
* @param {string[]} [params.symbols] Optional. A list of symbol ids
576+
* @param {PERIOD} [params.period] Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30'
577+
* @param {SORT} [params.sort] Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
578+
* @param {string} [params.from] Optional. Initial value of the queried interval. As Datetime
579+
* @param {string} [params.till] Optional. Last value of the queried interval. As Datetime
580+
* @param {number} [params.limit] Optional. Candles per query. Defaul is 10. Min is 1. Max is 1000
581+
*
582+
* @return A class with the targetCurrency and data with a map with a list of candles for each symbol of the query. indexed by symbol.
583+
*/
584+
getConvertedCandles(params?: {
585+
targetCurrency: string;
586+
symbols?: string[];
587+
period?: PERIOD;
588+
sort?: SORT;
589+
from?: string;
590+
till?: string;
591+
limit?: number;
592+
}): Promise<ConvertedCandles> {
593+
return this.get("public/converted/candles", params);
594+
}
595+
596+
/**
597+
* Gets candles regarding the last price converted to the target currency for the specified symbol
598+
*
599+
* Candles are used for OHLC representation
600+
*
601+
* The result contains candles with non-zero volume only (no trades = no candles).
602+
*
603+
* Conversion from the symbol quote currency to the target currency is the mean of "best" bid price and "best" ask price in the order book. If there is no "best" bid or ask price, the last price is returned.
604+
*
605+
* Requires no API key Access Rights.
606+
*
607+
* https://api.exchange.cryptomkt.com/#candles
608+
*
609+
* @param {string[]} symbol A symbol id
610+
* @param {object} params
611+
* @param {string[]} params.targetCurrency Target currency for conversion
612+
* @param {PERIOD} [params.period] Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30'
613+
* @param {SORT} [params.sort] Optional. Sort direction. 'ASC' or 'DESC'. Default is 'DESC'
614+
* @param {string} [params.from] Optional. Initial value of the queried interval. As Datetime
615+
* @param {string} [params.till] Optional. Last value of the queried interval. As Datetime
616+
* @param {number} [params.limit] Optional. Candles per query. Defaul is 10. Min is 1. Max is 1000
617+
*
618+
* @return An object with the targetCurrency and data with a list of candles for the symbol of the query.
619+
*/
620+
getConvertedCandlesBySymbol(
621+
symbol: string,
622+
params?: {
623+
targetCurrency: string;
624+
period?: PERIOD;
625+
sort?: SORT;
626+
from?: string;
627+
till?: string;
628+
limit?: number;
629+
}): Promise<ConvertedCandlesBySymbol> {
630+
return this.get(`public/converted/candles/${symbol}`, params);
631+
}
632+
462633
/////////////////////////
463634
// AUTHENTICATED CALLS //
464635
/////////////////////////

lib/models/ConvertedCandle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Candle } from "./Candle";
2+
3+
export interface ConvertedCandlesBySymbol {
4+
targetCurrency: string;
5+
data: Candle[];
6+
}

lib/models/ConvertedCandles.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Candle } from "./Candle";
2+
3+
export interface ConvertedCandles {
4+
targetCurrency: string;
5+
data: { [key: string]: Candle[] };
6+
}

lib/paramStyleConverter.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ const convertObjectKeysToCamelCase = (obj: { [x: string]: any }): { [x: string]:
3939
}
4040

4141

42-
const camelCaseToSnakeCase = (camelCase: string) => {
43-
return camelCase.replace(/([A-Z])/g, letter => `_${letter.toLowerCase()}`);
44-
}
42+
const camelCaseToSnakeCase = (camelCase: string) => camelCase
43+
.replace(/([A-Z])/g, letter => `_${letter.toLowerCase()}`)
44+
4545

46-
const snakeCaseToCamelCase = (value: string) =>
47-
value.replace(/([_][a-z])/g, _letter => _letter.replace('_', '').toUpperCase()
48-
);
46+
const snakeCaseToCamelCase = (value: string) => value
47+
.replace(/([_][a-z])/g, _letter => _letter.replace('_', '').toUpperCase());
4948

5049
const isObject = (value: any) => {
5150
return typeof (value) == "object";

lib/websocket/clientBase.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ export class WSClientBase {
109109
this.ws.send(JSON.stringify(payload));
110110
return withTimeout(this.requestTimeoutMs, promise);
111111
}
112+
112113
protected requestList({ method, params: paramsRaw = {}, responseCount = 1 }: { method: any; params?: {}; responseCount?: number; }): Promise<unknown> {
113114
const params = fromCamelCaseToSnakeCase(paramsRaw)
114115
const { id, promise } = this.multiResponsePromiseFactory.newMultiResponsePromise(responseCount);

lib/websocket/marketDataClient.ts

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class MarketDataClient extends WSClientBase {
7474
const promise = new Promise(function (resolve, reject) {
7575
emitter.once(ID.toString(), function (response) {
7676
if ("error" in response) {
77-
reject(new CryptomarketAPIException(response));
77+
reject(new CryptomarketAPIException(response.error));
7878
return;
7979
}
8080
resolve(response.result);
@@ -159,7 +159,7 @@ export class MarketDataClient extends WSClientBase {
159159
/**
160160
* subscribe to a feed of candles
161161
*
162-
* subscription is for all symbols or for the specified symbols
162+
* subscription is for the specified symbols
163163
*
164164
* normal subscriptions have one update message per symbol
165165
*
@@ -171,9 +171,9 @@ export class MarketDataClient extends WSClientBase {
171171
* https://api.exchange.cryptomkt.com/#subscribe-to-candles
172172
*
173173
* @param {function} callback a function that recieves notifications as a dict of candles indexed by symbol, and the type of notification (either SNAPSHOT or UPDATE)
174-
* @param {PERIOD} [params.period] Optional. A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month). Default is 'M30'
175-
* @param {string[]} [params.symbols] Optional. A list of symbol ids
176-
* @param {number} params.limit Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0
174+
* @param {PERIOD} params.period A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month).
175+
* @param {string[]} params.symbols A list of symbol ids
176+
* @param {number} [params.limit] Optional. Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0
177177
* @return A promise that resolves when subscribed with a list of the successfully subscribed symbols
178178
*/
179179
subscribeToCandles({
@@ -193,6 +193,41 @@ export class MarketDataClient extends WSClientBase {
193193
});
194194
}
195195

196+
/**
197+
* subscribes to a feed of candles regarding the last price converted to the target currency * for the specified symbols
198+
*
199+
* subscription is only for the specified symbols
200+
*
201+
* normal subscriptions have one update message per symbol
202+
*
203+
* Requires no API key Access Rights
204+
*
205+
* https://api.exchange.cryptomkt.com/#subscribe-to-converted-candles
206+
*
207+
* @param {function} callback a function that recieves notifications as a dict of candles indexed by symbol, and the type of notification (either SNAPSHOT or UPDATE)
208+
* @param {function} params.targetCurrency Target currency for conversion
209+
* @param {PERIOD} params.period A valid tick interval. 'M1' (one minute), 'M3', 'M5', 'M15', 'M30', 'H1' (one hour), 'H4', 'D1' (one day), 'D7', '1M' (one month).
210+
* @param {string[]} params.symbols A list of symbol ids
211+
* @param {number} [params.limit] Optional. Number of historical entries returned in the first feed. Min is 0. Max is 1000. Default is 0
212+
* @return A promise that resolves when subscribed with a list of the successfully subscribed symbols
213+
*/
214+
subscribeToConvertedCandles({
215+
callback,
216+
params: { period, ...params },
217+
}: {
218+
callback: (
219+
notification: { [x: string]: WSCandle[] },
220+
type: NOTIFICATION_TYPE
221+
) => any;
222+
params: { targetCurrency: string, period: PERIOD; symbols: string[]; limit?: number };
223+
}): Promise<string[]> {
224+
return this.makeSubscription({
225+
channel: `converted/candles/${period}`,
226+
callback: parseMapListInterceptor(callback, parseWSCandle),
227+
params,
228+
});
229+
}
230+
196231
/**
197232
* subscribe to a feed of mini tickers
198233
*

0 commit comments

Comments
 (0)