From 7a986f475ed31b2c7886872e38b21c13a443d124 Mon Sep 17 00:00:00 2001 From: David May Date: Thu, 26 Feb 2026 09:00:27 +0100 Subject: [PATCH] fix: send & refactoring --- .../services/scrypt-websocket-connection.ts | 18 +++- .../exchange/services/scrypt.service.ts | 96 +++++++------------ 2 files changed, 50 insertions(+), 64 deletions(-) diff --git a/src/integration/exchange/services/scrypt-websocket-connection.ts b/src/integration/exchange/services/scrypt-websocket-connection.ts index 610f519154..9948a92b4c 100644 --- a/src/integration/exchange/services/scrypt-websocket-connection.ts +++ b/src/integration/exchange/services/scrypt-websocket-connection.ts @@ -82,8 +82,8 @@ export class ScryptWebSocketConnection { // --- PUBLIC METHODS --- // - async send(request: ScryptRequest): Promise { - return this.request(request); + async send(type: ScryptMessageType, data: any[]): Promise { + return this.notify({ type, data }); } async fetch(streamName: ScryptMessageType, filters?: Record): Promise { @@ -98,7 +98,8 @@ export class ScryptWebSocketConnection { } async requestAndWaitForUpdate( - request: ScryptRequest, + type: ScryptMessageType, + data: any[], streamName: ScryptMessageType, matcher: (data: T[]) => T | null, timeoutMs: number, @@ -118,7 +119,7 @@ export class ScryptWebSocketConnection { } }); - this.request(request, timeoutMs).catch((error) => { + this.request({ type, data }, timeoutMs).catch((error) => { clearTimeout(timeoutId); unsubscribe(); reject(error); @@ -247,6 +248,15 @@ export class ScryptWebSocketConnection { // --- REQUEST/RESPONSE --- // + private async notify(message: ScryptRequest): Promise { + const ws = await this.ensureConnected(); + + const reqId = ++this.reqIdCounter; + const request: ScryptRequest = { ...message, reqid: reqId }; + + ws.send(JSON.stringify(request)); + } + private async request(message: ScryptRequest, timeoutMs = 30000): Promise { const ws = await this.ensureConnected(); diff --git a/src/integration/exchange/services/scrypt.service.ts b/src/integration/exchange/services/scrypt.service.ts index cbbb11f0ae..a39e593346 100644 --- a/src/integration/exchange/services/scrypt.service.ts +++ b/src/integration/exchange/services/scrypt.service.ts @@ -98,25 +98,21 @@ export class ScryptService extends PricingProvider { ): Promise { const clReqId = randomUUID(); - const withdrawRequest = { - type: ScryptMessageType.NEW_WITHDRAW_REQUEST, - data: [ - { - Quantity: amount.toString(), - Currency: currency, - MarketAccount: 'default', - RoutingInfo: { - WalletAddress: address, - Memo: memo ?? '', - DestinationTag: '', - }, - ClReqID: clReqId, - }, - ], + const withdrawData = { + Quantity: amount.toString(), + Currency: currency, + MarketAccount: 'default', + RoutingInfo: { + WalletAddress: address, + Memo: memo ?? '', + DestinationTag: '', + }, + ClReqID: clReqId, }; const transaction = await this.connection.requestAndWaitForUpdate( - withdrawRequest, + ScryptMessageType.NEW_WITHDRAW_REQUEST, + [withdrawData], ScryptMessageType.BALANCE_TRANSACTION, (transactions) => transactions.find((t) => t.ClReqID === clReqId && t.TransactionType === ScryptTransactionType.WITHDRAWAL) ?? @@ -163,21 +159,15 @@ export class ScryptService extends PricingProvider { timeStamp: Date; txHashes?: string[]; }): Promise { - const request = { - type: ScryptMessageType.NEW_DEPOSIT_REQUEST, - reqid: Date.now(), - data: [ - { - Currency: params.currency, - ClReqID: params.reqId, - Quantity: params.amount.toString(), - TransactTime: params.timeStamp.toISOString(), - TxHashes: (params.txHashes?.length ? params.txHashes : [params.reqId]).map((hash) => ({ TxHash: hash })), - }, - ], + const depositData = { + Currency: params.currency, + ClReqID: params.reqId, + Quantity: params.amount.toString(), + TransactTime: params.timeStamp.toISOString(), + TxHashes: (params.txHashes?.length ? params.txHashes : [params.reqId]).map((hash) => ({ TxHash: hash })), }; - await this.connection.send(request); + await this.connection.send(ScryptMessageType.NEW_DEPOSIT_REQUEST, [depositData]); } // --- TRANSACTIONS --- // @@ -406,13 +396,9 @@ export class ScryptService extends PricingProvider { orderData.Price = price.toString(); } - const orderRequest = { - type: ScryptMessageType.NEW_ORDER_SINGLE, - data: [orderData], - }; - const report = await this.connection.requestAndWaitForUpdate( - orderRequest, + ScryptMessageType.NEW_ORDER_SINGLE, + [orderData], ScryptMessageType.EXECUTION_REPORT, (reports) => reports.find((r) => r.ClOrdID === clOrdId) ?? null, 60000, @@ -430,24 +416,19 @@ export class ScryptService extends PricingProvider { private async cancelOrder(clOrdId: string, from: string, to: string): Promise { const { symbol } = await this.getTradePair(from, to); - const origClOrdId = clOrdId; const newClOrdId = randomUUID(); - const cancelRequest = { - type: ScryptMessageType.ORDER_CANCEL_REQUEST, - data: [ - { - OrigClOrdID: origClOrdId, - ClOrdID: newClOrdId, - Symbol: symbol, - }, - ], + const cancelData = { + OrigClOrdID: clOrdId, + ClOrdID: newClOrdId, + Symbol: symbol, }; const report = await this.connection.requestAndWaitForUpdate( - cancelRequest, + ScryptMessageType.ORDER_CANCEL_REQUEST, + [cancelData], ScryptMessageType.EXECUTION_REPORT, - (reports) => reports.find((r) => r.OrigClOrdID === origClOrdId || r.ClOrdID === newClOrdId) ?? null, + (reports) => reports.find((r) => r.OrigClOrdID === clOrdId || r.ClOrdID === newClOrdId) ?? null, 60000, ); @@ -462,24 +443,19 @@ export class ScryptService extends PricingProvider { newPrice: number, ): Promise { const { symbol } = await this.getTradePair(from, to); - const origClOrdId = clOrdId; const newClOrdId = randomUUID(); - const replaceRequest = { - type: ScryptMessageType.ORDER_CANCEL_REPLACE_REQUEST, - data: [ - { - OrigClOrdID: origClOrdId, - ClOrdID: newClOrdId, - Symbol: symbol, - OrderQty: newQuantity.toString(), - Price: newPrice.toString(), - }, - ], + const editData = { + OrigClOrdID: clOrdId, + ClOrdID: newClOrdId, + Symbol: symbol, + OrderQty: newQuantity.toString(), + Price: newPrice.toString(), }; const report = await this.connection.requestAndWaitForUpdate( - replaceRequest, + ScryptMessageType.ORDER_CANCEL_REPLACE_REQUEST, + [editData], ScryptMessageType.EXECUTION_REPORT, (reports) => reports.find((r) => r.ClOrdID === newClOrdId) ?? null, 60000,