From a02d38acfa61f4da5c6c4331afcb832191c2da6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezin=CC=81ski?= Date: Fri, 19 Apr 2024 17:52:57 +0200 Subject: [PATCH 1/3] cleanup fixes --- package.json | 7 ++-- src/transactions.ts | 45 +++++++++++++++----------- yarn.lock | 79 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 95 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index ec3c7db..43965e0 100644 --- a/package.json +++ b/package.json @@ -30,10 +30,10 @@ }, "homepage": "https://github.com/blockworks-foundation/mangolana#readme", "dependencies": { - "@solana/web3.js": "^1.88.0", + "@solana/web3.js": "^1.91.6", "bs58": "^5.0.0", "isomorphic-ws": "^5.0.0", - "node-fetch": "2.6.11", + "node-fetch": "3.3.2", "ws": "^8.16.0" }, "devDependencies": { @@ -41,6 +41,7 @@ "tslint": "^6.1.3", "tslint-config-prettier": "^1.18.0", "typedoc": "^0.23.16", - "typescript": "^4.8.4" + "typescript": "5.4.5", + "@types/node": "20.12.7" } } diff --git a/src/transactions.ts b/src/transactions.ts index e2f17f2..8e06f58 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -119,14 +119,13 @@ const confirmWithSignatureStatuses = ( return new Promise>(async (resolve, reject) => { try { const retryTimer = timeoutConfig.getSignatureStatusesPoolIntervalMs || 4000; - let intervalTimeout: NodeJS.Timer | null = null; - const cleanUp = () => { + let intervalTimeout: NodeJS.Timeout | null = null; + const onAbort = () => { if (intervalTimeout) { clearInterval(intervalTimeout); } - }; - const onAbort = () => { - cleanUp(); + internalSignal.removeEventListener('abort', onAbort); + externalSignal?.removeEventListener('abort', onAbort); reject(ConfirmationReject.Aborted); }; internalSignal.addEventListener('abort', onAbort); @@ -174,36 +173,44 @@ const confirmWithWebSockets = ( return new Promise>(async (resolve, reject) => { try { let subscriptionId: number | undefined; + let tempConnection = new Connection(connection.rpcEndpoint, connection.commitment); + let websocket: Websocket | null = null; const onAbort = () => { cleanup(); reject(ConfirmationReject.Aborted); }; - const cleanup = () => { - if ( - subscriptionId && - //@ts-ignore - Object.values(connection._subscriptionsByHash).find((x) => x.serverSubscriptionId === subscriptionId) - ) { - connection.removeSignatureListener(subscriptionId).catch((e) => { + const cleanup = async () => { + //@ts-ignore + if (subscriptionId && tempConnection._subscriptionDisposeFunctionsByClientSubscriptionId[subscriptionId]) { + try { + await tempConnection.removeSignatureListener(subscriptionId); + } catch (e) { logger.log('WS error in cleanup', e); - }); + } + } + if (websocket && websocket.removeAllListeners) { + websocket.removeAllListeners(); + websocket = null; } + subscriptionId = undefined; + internalSignal.removeEventListener('abort', onAbort); + externalSignal?.removeEventListener('abort', onAbort); }; internalSignal.addEventListener('abort', onAbort); externalSignal?.addEventListener('abort', onAbort); try { - logger.log('on signature', connection); + logger.log('on signature', txid); //In native websockets of web3 there is retry infinity so to prevent connecting to //broken rpc we check if websockets are working - const websocket = new Websocket(connection.rpcEndpoint.replace(/^http(s?):\/\//, 'ws$1://')); + websocket = new Websocket(tempConnection.rpcEndpoint.replace(/^http(s?):\/\//, 'ws$1://')); websocket.onerror = function error() { - websocket.close(); + websocket?.close(); }; websocket.onopen = async () => { - websocket.close(); + websocket?.close(); await sleep(100); - subscriptionId = connection.onSignature( + subscriptionId = tempConnection.onSignature( txid, (result, context) => { subscriptionId = undefined; @@ -260,6 +267,8 @@ const timeoutCheck = ( if (setTimeoutTimer) { clearTimeout(setTimeoutTimer); } + internalSignal.removeEventListener('abort', onAbort); + externalSignal?.removeEventListener('abort', onAbort); reject(ConfirmationReject.Aborted); }; internalSignal.addEventListener('abort', onAbort); diff --git a/yarn.lock b/yarn.lock index 376f524..3dfa495 100644 --- a/yarn.lock +++ b/yarn.lock @@ -44,11 +44,16 @@ dependencies: "@noble/hashes" "1.3.3" -"@noble/hashes@1.3.3", "@noble/hashes@^1.3.2": +"@noble/hashes@1.3.3": version "1.3.3" resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.3.tgz#39908da56a4adc270147bb07968bf3b16cfe1699" integrity sha512-V7/fPHgl+jsVPXqqeOzT8egNj2iBIVt+ECeMMG8TdcnTikP3oaBtUVqpT/gYCR68aEBJSF+XbYUxStjbFMqIIA== +"@noble/hashes@^1.3.3": + version "1.4.0" + resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.4.0.tgz#45814aa329f30e4fe0ba49426f49dfccdd066426" + integrity sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg== + "@solana/buffer-layout@^4.0.1": version "4.0.1" resolved "https://registry.yarnpkg.com/@solana/buffer-layout/-/buffer-layout-4.0.1.tgz#b996235eaec15b1e0b5092a8ed6028df77fa6c15" @@ -56,14 +61,14 @@ dependencies: buffer "~6.0.3" -"@solana/web3.js@^1.88.0": - version "1.88.0" - resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.88.0.tgz#24e1482f63ac54914430b4ce5ab36eaf433ecdb8" - integrity sha512-E4BdfB0HZpb66OPFhIzPApNE2tG75Mc6XKIoeymUkx/IV+USSYuxDX29sjgE/KGNYxggrOf4YuYnRMI6UiPL8w== +"@solana/web3.js@^1.91.6": + version "1.91.6" + resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.91.6.tgz#c090661c344cbc61e6cdeb0da67d3ea80d5848e1" + integrity sha512-dm20nN6HQvXToo+kM51nxHdtaa2wMSRdeK37p+WIWESfeiVHqV8XbV4XnWupq6ngt5vIckhGFG7ZnTBxUgLzDA== dependencies: "@babel/runtime" "^7.23.4" "@noble/curves" "^1.2.0" - "@noble/hashes" "^1.3.2" + "@noble/hashes" "^1.3.3" "@solana/buffer-layout" "^4.0.1" agentkeepalive "^4.5.0" bigint-buffer "^1.1.5" @@ -89,6 +94,13 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.8.3.tgz#ce750ab4017effa51aed6a7230651778d54e327c" integrity sha512-0os9vz6BpGwxGe9LOhgP/ncvYN5Tx1fNcd2TM3rD/aCGBkysb+ZWpXEocG24h6ZzOi13+VB8HndAQFezsSOw1w== +"@types/node@20.12.7": + version "20.12.7" + resolved "https://registry.yarnpkg.com/@types/node/-/node-20.12.7.tgz#04080362fa3dd6c5822061aa3124f5c152cff384" + integrity sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg== + dependencies: + undici-types "~5.26.4" + "@types/node@^12.12.54": version "12.20.55" resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240" @@ -260,6 +272,11 @@ concat-map@0.0.1: resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg== +data-uri-to-buffer@^4.0.0: + version "4.0.1" + resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e" + integrity sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A== + delay@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/delay/-/delay-5.0.0.tgz#137045ef1b96e5071060dd5be60bf9334436bd1d" @@ -307,11 +324,26 @@ fast-stable-stringify@^1.0.0: resolved "https://registry.yarnpkg.com/fast-stable-stringify/-/fast-stable-stringify-1.0.0.tgz#5c5543462b22aeeefd36d05b34e51c78cb86d313" integrity sha512-wpYMUmFu5f00Sm0cj2pfivpmawLZ0NKdviQ4w9zJeR8JVtOpOxHmLaJuj0vxvGqMJQWyP/COUkF75/57OKyRag== +fetch-blob@^3.1.2, fetch-blob@^3.1.4: + version "3.2.0" + resolved "https://registry.yarnpkg.com/fetch-blob/-/fetch-blob-3.2.0.tgz#f09b8d4bbd45adc6f0c20b7e787e793e309dcce9" + integrity sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ== + dependencies: + node-domexception "^1.0.0" + web-streams-polyfill "^3.0.3" + file-uri-to-path@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw== +formdata-polyfill@^4.0.10: + version "4.0.10" + resolved "https://registry.yarnpkg.com/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz#24807c31c9d402e002ab3d8c720144ceb8848423" + integrity sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g== + dependencies: + fetch-blob "^3.1.2" + fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" @@ -475,12 +507,19 @@ ms@^2.0.0: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== -node-fetch@2.6.11: - version "2.6.11" - resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.11.tgz#cde7fc71deef3131ef80a738919f999e6edfff25" - integrity sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w== +node-domexception@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5" + integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ== + +node-fetch@3.3.2: + version "3.3.2" + resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-3.3.2.tgz#d1e889bacdf733b4ff3b2b243eb7a12866a0b78b" + integrity sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA== dependencies: - whatwg-url "^5.0.0" + data-uri-to-buffer "^4.0.0" + fetch-blob "^3.1.4" + formdata-polyfill "^4.0.10" node-fetch@^2.7.0: version "2.7.0" @@ -650,10 +689,15 @@ typedoc@^0.23.16: minimatch "^5.1.0" shiki "^0.11.1" -typescript@^4.8.4: - version "4.8.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.4.tgz#c464abca159669597be5f96b8943500b238e60e6" - integrity sha512-QCh+85mCy+h0IGff8r5XWzOVSbBO+KfeYrMQh7NJ58QujwcE22u+NUSmUxqF+un70P9GXKxa2HCNiTTMJknyjQ== +typescript@5.4.5: + version "5.4.5" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.5.tgz#42ccef2c571fdbd0f6718b1d1f5e6e5ef006f611" + integrity sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ== + +undici-types@~5.26.4: + version "5.26.5" + resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" + integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== utf-8-validate@^5.0.2: version "5.0.9" @@ -677,6 +721,11 @@ vscode-textmate@^6.0.0: resolved "https://registry.yarnpkg.com/vscode-textmate/-/vscode-textmate-6.0.0.tgz#a3777197235036814ac9a92451492f2748589210" integrity sha512-gu73tuZfJgu+mvCSy4UZwd2JXykjK9zAZsfmDeut5dx/1a7FeTk0XwJsSuqQn+cuMCGVbIBfl+s53X4T19DnzQ== +web-streams-polyfill@^3.0.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz#2073b91a2fdb1fbfbd401e7de0ac9f8214cecb4b" + integrity sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw== + webidl-conversions@^3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871" From 21641680190b1350c292282257f4ce0382f6b229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezin=CC=81ski?= Date: Sun, 21 Apr 2024 16:04:14 +0200 Subject: [PATCH 2/3] fix --- src/transactions.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/transactions.ts b/src/transactions.ts index 8e06f58..c34f1dc 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -213,7 +213,6 @@ const confirmWithWebSockets = ( subscriptionId = tempConnection.onSignature( txid, (result, context) => { - subscriptionId = undefined; if (result.err) { cleanup(); logger.log('WS reject', txid, result); @@ -282,9 +281,6 @@ const timeoutCheck = ( const currentBlockHeight = await connection.getBlockHeight(confirmLevel); if (typeof currentBlockHeight !== undefined && timeoutBlockHeight <= currentBlockHeight!) { logger.log('Timed out for txid: ', txid); - if (intervalTimer) { - clearInterval(intervalTimer); - } reject(ConfirmationReject.Timeout); } } catch (e) { @@ -372,6 +368,7 @@ export const sendAndConfirmSignedTransaction = async ({ if (config?.resendTxUntilConfirmed) { config.resendPoolTimeMs ||= 2000; } + const rawTransaction = signedTransaction.serialize(); let txid = bs58.encode(signedTransaction.signatures[0].signature!); const startTime = getUnixTs(); From 8021636c2852b77426bc9a17aacbe368fe16d31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Brzezin=CC=81ski?= Date: Sun, 21 Apr 2024 16:06:23 +0200 Subject: [PATCH 3/3] lint fix --- src/transactions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transactions.ts b/src/transactions.ts index c34f1dc..e4f786a 100644 --- a/src/transactions.ts +++ b/src/transactions.ts @@ -173,7 +173,7 @@ const confirmWithWebSockets = ( return new Promise>(async (resolve, reject) => { try { let subscriptionId: number | undefined; - let tempConnection = new Connection(connection.rpcEndpoint, connection.commitment); + const tempConnection = new Connection(connection.rpcEndpoint, connection.commitment); let websocket: Websocket | null = null; const onAbort = () => { cleanup();