usdc private test

This commit is contained in:
tiagosiebler
2022-09-16 14:09:01 +01:00
parent dd047326bf
commit d16dee8caa
6 changed files with 183 additions and 39 deletions

View File

@@ -29,6 +29,8 @@ interface WsStoredState {
activePingTimer?: ReturnType<typeof setTimeout> | undefined;
/** A timer tracking that an upstream heartbeat was sent, expecting a reply before it expires */
activePongTimer?: ReturnType<typeof setTimeout> | undefined;
/** If a reconnection is in progress, this will have the timer for the delayed reconnect */
activeReconnectTimer?: ReturnType<typeof setTimeout> | undefined;
/**
* All the topics we are expected to be subscribed to (and we automatically resubscribe to if the connection drops)
*/

View File

@@ -97,7 +97,11 @@ export const WS_KEY_MAP = {
usdcPerpPublic: 'usdcPerpPublic',
} as const;
export const WS_AUTH_ON_CONNECT_KEYS: WsKey[] = [WS_KEY_MAP.spotV3Private];
export const WS_AUTH_ON_CONNECT_KEYS: WsKey[] = [
WS_KEY_MAP.spotV3Private,
WS_KEY_MAP.usdcOptionPrivate,
WS_KEY_MAP.usdcPerpPrivate,
];
export const PUBLIC_WS_KEYS = [
WS_KEY_MAP.linearPublic,
@@ -202,7 +206,9 @@ export function getUsdcWsKeyForTopic(
export const WS_ERROR_ENUM = {
NOT_AUTHENTICATED_SPOT_V3: '-1004',
BAD_API_KEY_SPOT_V3: '10003',
API_ERROR_GENERIC: '10001',
API_SIGN_AUTH_FAILED: '10003',
USDC_OPTION_AUTH_FAILED: '3303006',
};
export function neverGuard(x: never, msg: string): Error {

View File

@@ -40,7 +40,7 @@ export type WsClientEvent =
| 'open'
| 'update'
| 'close'
| 'error'
| 'errorEvent'
| 'reconnect'
| 'reconnected'
| 'response';
@@ -52,7 +52,7 @@ interface WebsocketClientEvents {
close: (evt: { wsKey: WsKey; event: any }) => void;
response: (response: any) => void;
update: (response: any) => void;
error: (response: any) => void;
errorEvent: (response: any) => void;
}
// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
@@ -141,7 +141,6 @@ export class WebsocketClient extends EventEmitter {
this.options.restOptions,
this.options.requestOptions
);
this.connectPublic();
break;
}
case 'usdcOption': {
@@ -152,7 +151,6 @@ export class WebsocketClient extends EventEmitter {
this.options.restOptions,
this.options.requestOptions
);
this.connectPublic();
break;
}
case 'usdcPerp': {
@@ -163,7 +161,6 @@ export class WebsocketClient extends EventEmitter {
this.options.restOptions,
this.options.requestOptions
);
this.connectPublic();
break;
}
default: {
@@ -179,23 +176,6 @@ export class WebsocketClient extends EventEmitter {
return this.options.testnet === true;
}
public isLinear(): boolean {
return this.options.market === 'linear';
}
public isSpot(): boolean {
return this.options.market === 'spot';
}
public isInverse(): boolean {
return this.options.market === 'inverse';
}
/** USDC, spot v3, unified margin, account asset */
// public isV3(): boolean {
// return this.options.market === 'v3';
// }
public close(wsKey: WsKey) {
this.logger.info('Closing connection', { ...loggerCategory, wsKey });
this.setWsState(wsKey, WsConnectionStateEnum.CLOSING);
@@ -333,7 +313,7 @@ export class WebsocketClient extends EventEmitter {
private parseWsError(context: string, error: any, wsKey: WsKey) {
if (!error.message) {
this.logger.error(`${context} due to unexpected error: `, error);
this.emit('error', error);
this.emit('errorEvent', error);
return;
}
@@ -352,7 +332,7 @@ export class WebsocketClient extends EventEmitter {
);
break;
}
this.emit('error', error);
this.emit('errorEvent', error);
}
/**
@@ -443,7 +423,7 @@ export class WebsocketClient extends EventEmitter {
this.setWsState(wsKey, WsConnectionStateEnum.RECONNECTING);
}
setTimeout(() => {
this.wsStore.get(wsKey, true).activeReconnectTimer = setTimeout(() => {
this.logger.info('Reconnecting to websocket', {
...loggerCategory,
wsKey,
@@ -458,7 +438,7 @@ export class WebsocketClient extends EventEmitter {
this.logger.silly('Sending ping', { ...loggerCategory, wsKey });
this.tryWsSend(wsKey, JSON.stringify({ op: 'ping' }));
this.wsStore.get(wsKey, true)!.activePongTimer = setTimeout(() => {
this.wsStore.get(wsKey, true).activePongTimer = setTimeout(() => {
this.logger.info('Pong timeout - closing socket to reconnect', {
...loggerCategory,
wsKey,
@@ -470,6 +450,10 @@ export class WebsocketClient extends EventEmitter {
private clearTimers(wsKey: WsKey) {
this.clearPingTimer(wsKey);
this.clearPongTimer(wsKey);
const wsState = this.wsStore.get(wsKey);
if (wsState?.activeReconnectTimer) {
clearTimeout(wsState.activeReconnectTimer);
}
}
// Send a ping at intervals
@@ -636,9 +620,11 @@ export class WebsocketClient extends EventEmitter {
// spot v1
msg?.code ||
// spot v3
msg?.type === 'error'
msg?.type === 'error' ||
// usdc options
msg?.success === false
) {
return this.emit('error', msg);
return this.emit('errorEvent', msg);
}
this.logger.warning('Unhandled/unrecognised ws event message', {
@@ -662,7 +648,7 @@ export class WebsocketClient extends EventEmitter {
if (
this.wsStore.isConnectionState(wsKey, WsConnectionStateEnum.CONNECTED)
) {
this.emit('error', error);
this.emit('errorEvent', error);
}
}
@@ -814,7 +800,7 @@ export class WebsocketClient extends EventEmitter {
/** @deprecated use "market: 'spotv3" client */
public subscribePublicSpotTrades(symbol: string, binary?: boolean) {
if (!this.isSpot()) {
if (this.options.market !== 'spot') {
throw this.wrongMarketError('spot');
}
@@ -833,7 +819,7 @@ export class WebsocketClient extends EventEmitter {
/** @deprecated use "market: 'spotv3" client */
public subscribePublicSpotTradingPair(symbol: string, binary?: boolean) {
if (!this.isSpot()) {
if (this.options.market !== 'spot') {
throw this.wrongMarketError('spot');
}
@@ -856,7 +842,7 @@ export class WebsocketClient extends EventEmitter {
candleSize: KlineInterval,
binary?: boolean
) {
if (!this.isSpot()) {
if (this.options.market !== 'spot') {
throw this.wrongMarketError('spot');
}
@@ -884,7 +870,7 @@ export class WebsocketClient extends EventEmitter {
dumpScale?: number,
binary?: boolean
) {
if (!this.isSpot()) {
if (this.options.market !== 'spot') {
throw this.wrongMarketError('spot');
}