From 0db35808fc80a9e51e5c1daefddf5994b8ea8b69 Mon Sep 17 00:00:00 2001 From: wphan Date: Fri, 15 Dec 2023 16:23:42 -0800 Subject: [PATCH 1/3] example --- src/dlob-subscriber/DLOBSubscriberIO.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/dlob-subscriber/DLOBSubscriberIO.ts b/src/dlob-subscriber/DLOBSubscriberIO.ts index 1f6faf1..b277304 100644 --- a/src/dlob-subscriber/DLOBSubscriberIO.ts +++ b/src/dlob-subscriber/DLOBSubscriberIO.ts @@ -144,6 +144,13 @@ export class DLOBSubscriberIO extends DLOBSubscriber { asks: l2Formatted.asks.slice(0, 5), }); + const orderSubscriberSlot = this.slotSource.getSlot(); + const dlobOracleSlot = l2Formatted['oracle']['slot']; + if (Math.abs(orderSubscriberSlot - dlobOracleSlot) > 50) { + noticeSlack('dlobsubscirber kiling itstelf slot breakokren') + process.exit(420); + } + this.redisClient.client.publish( `orderbook_${marketType}_${l2Args.marketIndex}`, JSON.stringify(l2Formatted) From 9d857385c45b5c58455b4a6b0b514f178b4eafb4 Mon Sep 17 00:00:00 2001 From: wphan Date: Fri, 15 Dec 2023 16:44:26 -0800 Subject: [PATCH 2/3] add implementation --- src/dlob-subscriber/DLOBSubscriberIO.ts | 15 +++++-- src/utils/webhook.ts | 57 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/utils/webhook.ts diff --git a/src/dlob-subscriber/DLOBSubscriberIO.ts b/src/dlob-subscriber/DLOBSubscriberIO.ts index b277304..7dd331f 100644 --- a/src/dlob-subscriber/DLOBSubscriberIO.ts +++ b/src/dlob-subscriber/DLOBSubscriberIO.ts @@ -19,6 +19,7 @@ import { l2WithBNToStrings, } from '../utils/utils'; import { logger } from '../utils/logger'; +import { webhookMessage } from '../utils/webhook'; type wsMarketL2Args = { marketIndex: number; @@ -144,12 +145,18 @@ export class DLOBSubscriberIO extends DLOBSubscriber { asks: l2Formatted.asks.slice(0, 5), }); + // START HACK FOR DEBUGGING FAILURE WEBSOCKET CONNECTIONS const orderSubscriberSlot = this.slotSource.getSlot(); - const dlobOracleSlot = l2Formatted['oracle']['slot']; - if (Math.abs(orderSubscriberSlot - dlobOracleSlot) > 50) { - noticeSlack('dlobsubscirber kiling itstelf slot breakokren') - process.exit(420); + const dlobOracleSlot = parseInt(l2Formatted['oracleData']['slot']); + const slotDiff = Math.abs(orderSubscriberSlot - dlobOracleSlot); + if (slotDiff > 50) { + const msg = `DlobPublisher bad slot in ${l2Formatted['marketType']} market ${l2Formatted['marketName']}! abs(oracleSlot (${dlobOracleSlot}) - orderSubscriberSlot (${orderSubscriberSlot})) = ${slotDiff} > 50`; + logger.error(msg); + webhookMessage(msg).then(() => { + process.exit(1); + }); } + // END HACK FOR DEBUGGING FAILURE WEBSOCKET CONNECTIONS this.redisClient.client.publish( `orderbook_${marketType}_${l2Args.marketIndex}`, diff --git a/src/utils/webhook.ts b/src/utils/webhook.ts new file mode 100644 index 0000000..7ecdedc --- /dev/null +++ b/src/utils/webhook.ts @@ -0,0 +1,57 @@ +import axios from 'axios'; +require('dotenv').config(); +import { logger } from './logger'; + +// enum for webhook type (slack, telegram, ...) +export enum WebhookType { + Slack = 'slack', + Discord = 'discord', + Telegram = 'telegram', +} + +export async function webhookMessage( + message: string, + webhookUrl?: string, + prefix?: string, + webhookType?: WebhookType +): Promise { + if (process.env.WEBHOOK_TYPE) { + webhookType = process.env.WEBHOOK_TYPE as WebhookType; + } + if (!webhookType) { + webhookType = WebhookType.Discord; + } + if (webhookUrl || process.env.WEBHOOK_URL) { + const webhook = webhookUrl || process.env.WEBHOOK_URL; + const fullMessage = prefix ? prefix + message : message; + if (fullMessage && webhook) { + try { + let data; + switch (webhookType) { + case WebhookType.Slack: + data = { + text: fullMessage, + }; + break; + case WebhookType.Discord: + data = { + content: fullMessage, + }; + break; + case WebhookType.Telegram: + data = { + text: fullMessage, + }; + break; + default: + logger.error(`webhookMessage: unknown webhookType: ${webhookType}`); + return; + } + + await axios.post(webhook, data); + } catch (err) { + logger.info('webhook error'); + } + } + } +} From 9fac4adf1999a9fd812fb946c3e6c36eef418419 Mon Sep 17 00:00:00 2001 From: wphan Date: Fri, 15 Dec 2023 16:48:22 -0800 Subject: [PATCH 3/3] update deps --- package.json | 1 + yarn.lock | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 990ef00..f0091a3 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@types/redis": "^4.0.11", "@types/ws": "^8.5.8", "async-mutex": "^0.4.0", + "axios": "^1.6.2", "commander": "^9.4.0", "compression": "^1.7.4", "cors": "^2.8.5", diff --git a/yarn.lock b/yarn.lock index a64b3b4..7fc16da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1791,6 +1791,11 @@ async@^3.2.3: resolved "https://registry.yarnpkg.com/async/-/async-3.2.4.tgz#2d22e00f8cddeb5fde5dd33522b56d1cf569a81c" integrity sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ== +asynckit@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79" + integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q== + atomic-sleep@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/atomic-sleep/-/atomic-sleep-1.0.0.tgz#eb85b77a601fc932cfe432c5acd364a9e2c9075b" @@ -1811,6 +1816,15 @@ avvio@^7.1.2: fastq "^1.6.1" queue-microtask "^1.1.2" +axios@^1.6.2: + version "1.6.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-1.6.2.tgz#de67d42c755b571d3e698df1b6504cde9b0ee9f2" + integrity sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A== + dependencies: + follow-redirects "^1.15.0" + form-data "^4.0.0" + proxy-from-env "^1.1.0" + balanced-match@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" @@ -2080,6 +2094,13 @@ colorspace@1.1.x: color "^3.1.3" text-hex "1.0.x" +combined-stream@^1.0.8: + version "1.0.8" + resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" + integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== + dependencies: + delayed-stream "~1.0.0" + commander@^2.20.3: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" @@ -2213,6 +2234,11 @@ delay@^5.0.0: resolved "https://registry.yarnpkg.com/delay/-/delay-5.0.0.tgz#137045ef1b96e5071060dd5be60bf9334436bd1d" integrity sha512-ReEBKkIfe4ya47wlPYf/gu5ib6yUG0/Aez0JQZQz94kiWtRQvZIQbTiehsnwHvLSWJnQdhVeqYue7Id1dKr0qw== +delayed-stream@~1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" + integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== + denque@^1.5.0: version "1.5.1" resolved "https://registry.yarnpkg.com/denque/-/denque-1.5.1.tgz#07f670e29c9a78f8faecb2566a1e2c11929c5cbf" @@ -2717,6 +2743,11 @@ fn.name@1.x.x: resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc" integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw== +follow-redirects@^1.15.0: + version "1.15.3" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a" + integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q== + for-each@^0.3.3: version "0.3.3" resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e" @@ -2724,6 +2755,15 @@ for-each@^0.3.3: dependencies: is-callable "^1.1.3" +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + forwarded@0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811" @@ -3250,7 +3290,7 @@ mime-db@1.52.0, "mime-db@>= 1.43.0 < 2": resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.52.0.tgz#bbabcdc02859f4987301c856e3387ce5ec43bf70" integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg== -mime-types@~2.1.24, mime-types@~2.1.34: +mime-types@^2.1.12, mime-types@~2.1.24, mime-types@~2.1.34: version "2.1.35" resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a" integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw== @@ -3626,6 +3666,11 @@ proxy-addr@^2.0.7, proxy-addr@~2.0.7: forwarded "0.2.0" ipaddr.js "1.9.1" +proxy-from-env@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2" + integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg== + punycode@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec"