|
1 | 1 | # CryptoMarket-javascript |
2 | | -[main page](https://www.cryptomkt.com/) |
3 | 2 |
|
| 3 | +[main page](https://www.cryptomkt.com/) |
4 | 4 |
|
5 | 5 | [sign up in CryptoMarket](https://www.cryptomkt.com/account/register). |
6 | 6 |
|
7 | 7 | # Installation |
| 8 | + |
8 | 9 | To install Cryptomarket use npm |
| 10 | + |
9 | 11 | ``` |
10 | 12 | npm install cryptomarket |
11 | 13 | ``` |
| 14 | + |
12 | 15 | # Documentation |
13 | 16 |
|
14 | | -This sdk makes use of the [api version 2](https://api.exchange.cryptomkt.com/v2) of cryptomarket. |
| 17 | +This sdk makes use of the [api version 3](https://api.exchange.cryptomkt.com) of cryptomarket. |
15 | 18 |
|
16 | 19 | # Quick Start |
17 | 20 |
|
18 | 21 | ## rest client |
| 22 | + |
19 | 23 | ```javascript |
20 | | -const { Client } = require('cryptomarket') |
| 24 | +const { Client } = require("cryptomarket"); |
21 | 25 |
|
22 | 26 | // instance a client |
23 | | -let apiKey='AB32B3201' |
24 | | -let api_secret='21b12401' |
25 | | -let client = new Client(apiKey, api_secret) |
| 27 | +let apiKey = "AB32B3201"; |
| 28 | +let apiSecret = "21b12401"; |
| 29 | +let client = new Client(apiKey, apiSecret); |
26 | 30 |
|
27 | 31 | // get currencies |
28 | | -let currencies = await client.getCurrencies() |
| 32 | +let currencies = await client.getCurrencies(); |
29 | 33 |
|
30 | 34 | // get order books |
31 | | -let orderBook = await client.getOrderBook('EOSETH') |
| 35 | +let orderBook = await client.getOrderBook("EOSETH"); |
32 | 36 |
|
33 | 37 | // get your account balances |
34 | | -let accountBalance = await client.getAccountBalance() |
| 38 | +let accountBalances = await client.getWalletBalances(); |
35 | 39 |
|
36 | 40 | // get your trading balances |
37 | | -let tradingBalance = await client.getTradingBalance() |
| 41 | +let tradingBalances = await client.getSpotTradingBalances(); |
38 | 42 |
|
39 | | -// move balance from account to trading |
40 | | -let result = await client.transferMoneyFromAccountBalanceToTradingBalance('ETH', '3.2') |
| 43 | +// move balance from wallet to spot trading |
| 44 | +let result = await client.transferBetweenWalletAndExchange({ |
| 45 | + currency: "EOS", |
| 46 | + amount: "3.2", |
| 47 | + source: Account.Wallet, |
| 48 | + destination: Account.Spot, |
| 49 | +}); |
41 | 50 |
|
42 | 51 | // get your active orders |
43 | | -let orders = await client.getActiveOrders('EOSETH') |
| 52 | +let orders = await client.getAllActiveSpotOrders("EOSETH"); |
44 | 53 |
|
45 | 54 | // create a new order |
46 | | -let newOrder = await client.createOrder({'symbol':'EOSETH', 'side':'buy', 'quantity':'10', 'price':'10'}) |
| 55 | +let newOrder = await client.createOrder({ |
| 56 | + symbol: "EOSETH", |
| 57 | + side: "buy", |
| 58 | + quantity: "10", |
| 59 | + price: "10", |
| 60 | +}); |
47 | 61 | ``` |
48 | 62 |
|
49 | 63 | ## websocket client |
50 | | -There are three websocket clients, one for public request (WSPublicClient), one for trading request (WSTradingClient) and one for the account requests (WSAccountClient). |
51 | 64 |
|
52 | | -Clients work with promises |
| 65 | +There are three websocket clients, the market data client, the spot trading client and the wallet client. |
| 66 | +The market data client requires no authentication, while the spot trading client and the wallet client do require it. |
53 | 67 |
|
54 | | -Clients must be connected before any request. Authentication for WSTradingClient and WSAccountClient is automatic. |
| 68 | +All websocket methods return promises. Subscriptions also take in a function of two parameters, the notification data, and the notification type. The notification type is of type NOTIFICATION_TYPE, and is either SNAPSHOT, NOTIFICATION or DATA. |
55 | 69 |
|
56 | | -```javascript |
57 | | -const { WSPublicClient, WSTradingClient, WSAccountClient} = require('cryptomarket') |
| 70 | +The documentation of a specific subscriptions explains with of this types of |
| 71 | +notification uses. |
58 | 72 |
|
59 | | -let publicClient = new WSPublicClient() |
60 | | -await publicClient.connect() |
| 73 | +### MarketDataClient |
61 | 74 |
|
62 | | -// get currencies |
63 | | -await publicClient.getCurrencies() |
| 75 | +Example of use of the market data client |
64 | 76 |
|
65 | | -let apiKey='AB32B3201' |
66 | | -let apiSecret='21b12401' |
67 | | -let tradingClient = new WSTradingClient(apiKey, apiSecret) |
| 77 | +```typescript |
| 78 | +// instantiate a market data client |
| 79 | +const wsclient = new WSMarketDataClient(); |
68 | 80 |
|
69 | | -await tradingClient.connect() |
| 81 | +// make a partial orderbook subscription |
70 | 82 |
|
71 | | -// get your trading balance |
72 | | -let balance = await tradingClient.getTradingBalance() |
| 83 | +// make subscription |
| 84 | +await marketDataClient.subscribeToPartialOrderBook( |
| 85 | + callback:(notification, type) => { |
| 86 | + if (type === NOTIFICATION_TYPE.DATA) { |
| 87 | + System.out.println("this subscription only recieves data notifications"); |
| 88 | + } |
| 89 | + for (const symbol in notification) { |
| 90 | + console.log(symbol); |
| 91 | + const orderbook = notification[symbol]; |
| 92 | + console.log(orderbook); |
| 93 | + } |
| 94 | + }, |
| 95 | + params: { |
| 96 | + speed: ORDER_BOOK_SPEED._100_MS, |
| 97 | + depth: DEPTH._5, |
| 98 | + symbols: ["EOSETH", "ETHBTC"], |
| 99 | + } |
| 100 | +); |
73 | 101 |
|
74 | | -// get your active orders |
75 | | -let activeOrders = await tradingClient.getActiveOrders() |
76 | 102 |
|
77 | | -await tradingClient.createOrder({ |
78 | | - clientOrderId:"qqwe123qwe", |
79 | | - symbol:'EOSETH', |
80 | | - side:'buy', |
81 | | - quantity:'10', |
82 | | - price:'10' |
83 | | -}) |
| 103 | +``` |
84 | 104 |
|
85 | | -let accountClient = new WSAccountClient(apiKey, apiSecret) |
86 | | -await accountClient.connect() |
| 105 | +### SpotTradingClient |
87 | 106 |
|
88 | | -// get your account balance |
89 | | -let accBalance = await accountCilent.getAccountBalance() |
90 | | -``` |
91 | | -### subscriptions |
| 107 | +Example of use of the spot trading client |
92 | 108 |
|
93 | | -all subscriptions take a callback argument to call with each feed of the subscription |
| 109 | +```typescript |
| 110 | +const apiKey = "AB32B3201"; |
| 111 | +const apiSecret= "21b12401"; |
94 | 112 |
|
95 | | -```javascript |
| 113 | +// instantiate a spot trading websocket client with a window of 10 seconds |
| 114 | +const wsclient = new WSTradingClient(apiKey, apiSecret, 10_000); |
96 | 115 |
|
97 | | -// callback is for the subscription feed |
98 | | -function callback(feed) { |
99 | | - // handle feed |
100 | | - console.log(feed) |
| 116 | +// connect the client (and authenticate it automatically) |
| 117 | +await wsclient.connect(); |
| 118 | + |
| 119 | +// get all the spot trading balances |
| 120 | +const balanceList = await tradingClient.getSpotTradingBalances() |
| 121 | +console.log(balanceList); |
| 122 | + |
| 123 | + |
| 124 | +let clientOrderID = Math.floor(Date.now() / 1000).toString(); |
| 125 | +// make a spot order |
| 126 | +const report = await tradingClient.createSpotOrder({ |
| 127 | + clientOrderId: clientOrderID, |
| 128 | + symbol: "EOSETH", |
| 129 | + side: "sell", |
| 130 | + quantity: "0.01", |
| 131 | + price: "1000", |
101 | 132 | } |
| 133 | +console.log(report); |
| 134 | +``` |
| 135 | +
|
| 136 | +### WalletClient |
| 137 | +
|
| 138 | +Example of use of the wallet client |
102 | 139 |
|
103 | | -await publicClient.subscribeToOrderBook('EOSETH', callabck) |
| 140 | +```typescript |
| 141 | +// instantiate a wallet websocket client with a default window of 10 seconds |
| 142 | +const wsclient = new WSWalletClient(keys.apiKey, keys.apiSecret); |
104 | 143 |
|
| 144 | +// get a list of transactions |
| 145 | +const transactionList = await walletClient.getTransactions({ |
| 146 | + currencies: ["EOS"], |
| 147 | + limit: 3, |
| 148 | +}); |
| 149 | +console.log(transactionList); |
105 | 150 |
|
106 | | -await accountClient.subscribeToTransactions(callback) |
| 151 | +// subscribe to a feed of transactions |
| 152 | +await walletClient.subscribeToTransactions((notification, type) => { |
| 153 | + if (type === NOTIFICATION_TYPE.UPDATE) { |
| 154 | + console.log("this subscription only recieves update notifications"); |
| 155 | + } |
| 156 | + console.log(notification); |
| 157 | +}); |
107 | 158 | ``` |
108 | 159 |
|
109 | 160 | ## error handling |
110 | 161 |
|
111 | | -```javascript |
| 162 | +```typescript |
112 | 163 |
|
113 | | -{ CryptomarketSDKException, Client, WSPublicClient } = require('cryptomarket') |
| 164 | +{ CryptomarketSDKException, Client, WSMarketDataClient } = require('cryptomarket') |
114 | 165 | // exceptions derive from the CryptomarketSDKException class. |
115 | 166 |
|
116 | | -client = new Client() |
| 167 | +const client = new Client() |
117 | 168 | // catch a failed transaction |
118 | 169 | try { |
119 | | - order = client.createOrder({ |
| 170 | + const order = await client.createOrder({ |
120 | 171 | 'symbol':'EOSETHH', // non existant symbol |
121 | 172 | 'side':'sell', |
122 | | - 'quantity':'10', |
| 173 | + 'quantity':'10', |
123 | 174 | 'price':'10'}) |
124 | 175 | } catch (error) { |
125 | 176 | console.log(error) |
126 | 177 | } |
127 | 178 |
|
128 | | -wsclient = new WSPublicClient() |
129 | | -await wsclient.connect() |
| 179 | +const wsclient = new WSMarketDataClient() |
| 180 | +await wsclient.connect(); |
130 | 181 |
|
131 | | -// catch a missing argument |
| 182 | +// catch missing arguments |
132 | 183 | try { |
133 | | - await wsclient.subscribeToOrderbook('ETHBTC') // needs a callback |
| 184 | + await wsclient.subscribeToMiniTickers() |
134 | 185 | } catch (error) { |
135 | 186 | console.log(error) |
136 | 187 | } |
137 | | - |
138 | | -// also catches forwarded errors from the cryptomarket exchange server |
139 | | -try { |
140 | | - let trades = await wsclient.getTrades("abcde", myCallback) // not a real symbol |
141 | | -} catch (err) { |
142 | | - console.log(err) |
143 | | -} |
144 | 188 | ``` |
145 | 189 |
|
| 190 | +# Constants |
| 191 | +
|
| 192 | +All constants used for calls are in the `constants` module. |
| 193 | +
|
146 | 194 | # Checkout our other SDKs |
147 | 195 |
|
148 | 196 | [python sdk](https://github.com/cryptomkt/cryptomkt-python) |
|
0 commit comments