Skip to content

Commit 413ef2e

Browse files
authored
Merge pull request sieblyio#177 from JJ-Cro/updateexamples04022025
feat: add new examples for REST and WebSocket API usage, including private trading and public API calls, and restructure example directories
2 parents 28196c5 + e5eac09 commit 413ef2e

7 files changed

Lines changed: 4826 additions & 4640 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RestClient, WebsocketClient } from '../src/index.js';
1+
import { RestClient, WebsocketClient } from '../../src/index.js';
22

33
// or
44
// import { RestClient } from 'okx-api';

examples/rest-private-trade-market.ts renamed to examples/Rest/rest-private-trade-market.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OrderRequest, RestClient } from '../src/index.js';
1+
import { OrderRequest, RestClient } from '../../src/index.js';
22

33
// or
44
// import { RestClient, OrderRequest } from 'okx-api';

examples/Rest/rest-private-trade.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ const client = new RestClient({
4242
// apiPass: API_PASS,
4343
// });
4444

45-
function logWSEvent(type, data) {
45+
function logWSEvent(type: string, data: any) {
4646
console.log(new Date(), `WS ${type} event: `, data);
4747
}
4848

4949
// simple sleep function
50-
function promiseSleep(milliseconds) {
50+
function promiseSleep(milliseconds: number) {
5151
return new Promise((resolve) => setTimeout(resolve, milliseconds));
5252
}
5353

5454
// WARNING: for sensitive math you should be using a library such as decimal.js!
55-
function roundDown(value, decimals) {
55+
function roundDown(value: any, decimals: number) {
5656
return Number(
5757
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals,
5858
);

examples/Websocket/WS-API/ws-api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { DefaultLogger, WebsocketAPIClient } from '../../../src/index.js';
2929
(async () => {
3030
const logger = {
3131
...DefaultLogger,
32-
trace: (...params) => console.log('trace', ...params),
32+
trace: (...params: any[]) => console.log('trace', ...params),
3333
};
3434

3535
// For private events, all 3 of the following are required (per account):

examples/Websocket/WS-API/ws-api-trade-raw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
(async () => {
1515
const logger = {
1616
...DefaultLogger,
17-
trace: (...params) => console.log('trace', ...params),
17+
trace: (...params: any[]) => console.log('trace', ...params),
1818
};
1919

2020
// For private events, all 3 of the following are required (per account):

examples/Websocket/app.okx.com.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { RestClient, WebsocketClient } from '../../src/index.js';
2+
3+
// or
4+
// import { RestClient } from 'okx-api';
5+
6+
const client = new RestClient({
7+
/**
8+
* To use "app.okx.com", set market to "US"
9+
*
10+
* Note: your available functionality may differ, refer to the corresponding API docs for more details:
11+
* https://app.okx.com/docs-v5/en/#overview-production-trading-services
12+
*/
13+
market: 'US',
14+
});
15+
16+
const wsClient = new WebsocketClient({
17+
market: 'US',
18+
});
19+
20+
/**
21+
* This is a simple script wrapped in a immediately invoked function expression, designed to make public API calls without credentials
22+
*/
23+
(async () => {
24+
try {
25+
const results = await client.getInstruments({ instType: 'SPOT' });
26+
27+
console.log(
28+
'result: ',
29+
results.filter((row) => row.baseCcy === 'SUI'),
30+
);
31+
} catch (e) {
32+
console.error('request failed: ', e);
33+
}
34+
35+
// Raw data will arrive on the 'update' event
36+
wsClient.on('update', (data) => {
37+
// console.log(
38+
// new Date(),
39+
// 'ws update (raw data received)',
40+
// JSON.stringify(data, null, 2),
41+
// );
42+
// console.log('ws update (raw data received)', JSON.stringify(data, null, 2));
43+
console.log(
44+
new Date(),
45+
'ws update (raw data received)',
46+
JSON.stringify(data),
47+
);
48+
});
49+
wsClient.on('open', (data) => {
50+
console.log('ws connection opened open:', data.wsKey);
51+
});
52+
wsClient.on('reconnected', (data) => {
53+
console.log('ws has reconnected ', data?.wsKey);
54+
});
55+
wsClient.on('exception', (data) => {
56+
console.error('ws exception: ', data);
57+
});
58+
wsClient.subscribe([
59+
{
60+
channel: 'instruments',
61+
instType: 'SPOT',
62+
},
63+
{
64+
channel: 'tickers',
65+
instId: 'ETH-BTC',
66+
},
67+
]);
68+
})();

0 commit comments

Comments
 (0)