@dydxprotocol/v4-client-js 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dydxprotocol/v4-client-js",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "General client library for the new dYdX system (v4 decentralized)",
5
5
  "main": "build/cjs/src/index.js",
6
6
  "module": "build/esm/src/index.js",
@@ -62,6 +62,7 @@
62
62
  "cosmjs-types": "^0.9.0",
63
63
  "ethereum-cryptography": "^2.0.0",
64
64
  "ethers": "^6.6.1",
65
+ "https-proxy-agent": "^7.0.6",
65
66
  "long": "^4.0.0",
66
67
  "protobufjs": ">=6.11.4",
67
68
  "ws": "^8.14.2"
@@ -1,4 +1,5 @@
1
1
  import { PageRequest } from '@dydxprotocol/v4-proto/src/codegen/cosmos/base/query/v1beta1/pagination';
2
+ import { AxiosProxyConfig } from 'axios';
2
3
  import Long from 'long';
3
4
 
4
5
  import { BroadcastOptions, DenomConfig } from './types';
@@ -243,10 +244,12 @@ export const PAGE_REQUEST: PageRequest = {
243
244
  export class IndexerConfig {
244
245
  public restEndpoint: string;
245
246
  public websocketEndpoint: string;
247
+ public proxy?: AxiosProxyConfig;
246
248
 
247
- constructor(restEndpoint: string, websocketEndpoint: string) {
249
+ constructor(restEndpoint: string, websocketEndpoint: string, proxy?: AxiosProxyConfig) {
248
250
  this.restEndpoint = restEndpoint;
249
251
  this.websocketEndpoint = websocketEndpoint;
252
+ this.proxy = proxy;
250
253
  }
251
254
  }
252
255
 
@@ -19,10 +19,10 @@ export class IndexerClient {
19
19
  this.config = config;
20
20
  this.apiTimeout = apiTimeout ?? DEFAULT_API_TIMEOUT;
21
21
 
22
- this._markets = new MarketsClient(config.restEndpoint);
23
- this._account = new AccountClient(config.restEndpoint);
24
- this._utility = new UtilityClient(config.restEndpoint);
25
- this._vault = new VaultClient(config.restEndpoint);
22
+ this._markets = new MarketsClient(config.restEndpoint, apiTimeout, config.proxy);
23
+ this._account = new AccountClient(config.restEndpoint, apiTimeout, config.proxy);
24
+ this._utility = new UtilityClient(config.restEndpoint, apiTimeout, config.proxy);
25
+ this._vault = new VaultClient(config.restEndpoint, apiTimeout, config.proxy);
26
26
  }
27
27
 
28
28
  /**
@@ -1,24 +1,30 @@
1
+ import axios, { AxiosInstance, AxiosProxyConfig } from 'axios';
2
+
1
3
  import { DEFAULT_API_TIMEOUT } from '../constants';
2
4
  import { generateQueryPath } from '../helpers/request-helpers';
3
- import { RequestMethod, Response, request } from '../lib/axios';
5
+ import { Response } from '../lib/axios';
4
6
  import { Data } from '../types';
5
7
 
6
8
  export default class RestClient {
7
9
  readonly host: string;
8
10
  readonly apiTimeout: Number;
11
+ readonly axiosInstance: AxiosInstance;
9
12
 
10
- constructor(host: string, apiTimeout?: Number | null) {
13
+ constructor(host: string, apiTimeout?: Number | null, proxy?: AxiosProxyConfig) {
11
14
  if (host.endsWith('/')) {
12
15
  this.host = host.slice(0, -1);
13
16
  } else {
14
17
  this.host = host;
15
18
  }
16
19
  this.apiTimeout = apiTimeout || DEFAULT_API_TIMEOUT;
20
+ this.axiosInstance = axios.create({
21
+ proxy,
22
+ });
17
23
  }
18
24
 
19
25
  async get(requestPath: string, params: {} = {}): Promise<Data> {
20
26
  const url = `${this.host}${generateQueryPath(requestPath, params)}`;
21
- const response = await request(url);
27
+ const response = await this.axiosInstance.get(url);
22
28
  return response.data;
23
29
  }
24
30
 
@@ -29,6 +35,6 @@ export default class RestClient {
29
35
  headers: {} = {},
30
36
  ): Promise<Response> {
31
37
  const url = `${this.host}${generateQueryPath(requestPath, params)}`;
32
- return request(url, RequestMethod.POST, body, headers);
38
+ return this.axiosInstance.post(url, body, { headers });
33
39
  }
34
40
  }
@@ -1,5 +1,7 @@
1
+ import { HttpsProxyAgent } from 'https-proxy-agent';
1
2
  import WebSocket, { ErrorEvent, MessageEvent } from 'ws';
2
3
 
4
+ import { getProxyAgent } from '../lib/utils';
3
5
  import { IndexerConfig } from './constants';
4
6
 
5
7
  enum OutgoingMessageTypes {
@@ -37,6 +39,7 @@ export enum CandlesResolution {
37
39
  export class SocketClient {
38
40
  private url: string;
39
41
  private ws?: WebSocket;
42
+ private proxyAgent?: HttpsProxyAgent<string>;
40
43
  private onOpenCallback?: () => void;
41
44
  private onCloseCallback?: () => void;
42
45
  private onMessageCallback?: (event: MessageEvent) => void;
@@ -51,6 +54,7 @@ export class SocketClient {
51
54
  onErrorCallback: (event: ErrorEvent) => void,
52
55
  ) {
53
56
  this.url = config.websocketEndpoint;
57
+ this.proxyAgent = config.proxy ? getProxyAgent(config.proxy) : undefined;
54
58
  this.onOpenCallback = onOpenCallback;
55
59
  this.onCloseCallback = onCloseCallback;
56
60
  this.onMessageCallback = onMessageCallback;
@@ -58,7 +62,9 @@ export class SocketClient {
58
62
  }
59
63
 
60
64
  connect(): void {
61
- this.ws = new WebSocket(this.url);
65
+ this.ws = new WebSocket(this.url, {
66
+ agent: this.proxyAgent,
67
+ });
62
68
  this.ws.addEventListener('open', this.handleOpen.bind(this));
63
69
  this.ws.addEventListener('close', this.handleClose.bind(this));
64
70
  this.ws.addEventListener('message', this.handleMessage.bind(this));
package/src/lib/utils.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { AxiosProxyConfig } from 'axios';
2
+ import { HttpsProxyAgent } from 'https-proxy-agent';
3
+
1
4
  import { MAX_UINT_32 } from '../clients/constants';
2
5
 
3
6
  /**
@@ -74,3 +77,8 @@ export function calculateClockOffsetFromFetchDateHeader(
74
77
 
75
78
  return Math.floor(offset);
76
79
  }
80
+
81
+ export function getProxyAgent(proxy: AxiosProxyConfig): HttpsProxyAgent<string> {
82
+ const auth = proxy.auth ? `${proxy.auth.username}:${proxy.auth.password}@` : '';
83
+ return new HttpsProxyAgent(`${proxy.protocol}://${auth}${proxy.host}:${proxy.port}`);
84
+ }