From 8652661b2a7adb6934bd8b18f9535d0ceab77e7f Mon Sep 17 00:00:00 2001 From: Matej Lubej Date: Wed, 19 Jun 2024 10:16:04 +0200 Subject: [PATCH] Add transaction detail API cache - as /chain/transaction/{hash} endpoint response won't change, cache it --- .changelog/1979.internal.md | 1 + src/vendors/oasisscan.ts | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changelog/1979.internal.md diff --git a/.changelog/1979.internal.md b/.changelog/1979.internal.md new file mode 100644 index 0000000000..4b58f50302 --- /dev/null +++ b/.changelog/1979.internal.md @@ -0,0 +1 @@ +Add transaction detail API cache diff --git a/src/vendors/oasisscan.ts b/src/vendors/oasisscan.ts index bcc6cbc73c..e38c60f143 100644 --- a/src/vendors/oasisscan.ts +++ b/src/vendors/oasisscan.ts @@ -22,6 +22,8 @@ import { import { throwAPIErrors } from './helpers' +const getTransactionCacheMap: Record = {} + export function getOasisscanAPIs(url: string | 'https://api.oasisscan.com/mainnet') { const explorerConfig = new Configuration({ basePath: url, @@ -54,11 +56,20 @@ export function getOasisscanAPIs(url: string | 'https://api.oasisscan.com/mainne } async function getTransaction({ hash }: { hash: string }) { + if (hash in getTransactionCacheMap) { + return getTransactionCacheMap[hash] + } + const transaction = await operationsEntity.getTransaction({ hash, }) - const [parsedTx] = parseTransactionsList([transaction.data]) + const [parsedTx] = parseTransactionsList([transaction.data ?? {}]) + + if (transaction.code === 0 && !!transaction.data) { + getTransactionCacheMap[hash] = parsedTx + } + return parsedTx }