diff --git a/src/client/createSorokitClient.ts b/src/client/createSorokitClient.ts index b02e620..82e780f 100644 --- a/src/client/createSorokitClient.ts +++ b/src/client/createSorokitClient.ts @@ -706,9 +706,16 @@ export function createSorokitClient( errorHandler, { functionName: "account.get", params: { publicKey } }, () => - withLogging(logger, "account.get", { publicKey }, () => - getAccount(horizonUrl, publicKey), - ), + withLogging(logger, "account.get", { publicKey }, async () => { + const cacheKey = `account:get:${horizonUrl}:${publicKey}`; + if (cache) { + const cachedVal = cache.get(cacheKey); + if (cachedVal) return ok(cachedVal as AccountInfo); + } + const res = await getAccount(horizonUrl, publicKey); + if (cache && res.status === "ok") cache.set(cacheKey, res.data); + return res; + }), ).then(applyTx), getAccountsBatch: (publicKeys, timeoutMs) => withErrorHandling( @@ -724,18 +731,32 @@ export function createSorokitClient( errorHandler, { functionName: "account.getBalances", params: { publicKey } }, () => - withLogging(logger, "account.getBalances", { publicKey }, () => - getBalances(horizonUrl, publicKey), - ), + withLogging(logger, "account.getBalances", { publicKey }, async () => { + const cacheKey = `account:balances:${horizonUrl}:${publicKey}`; + if (cache) { + const cachedVal = cache.get(cacheKey); + if (cachedVal) return ok(cachedVal as AssetBalance[]); + } + const res = await getBalances(horizonUrl, publicKey); + if (cache && res.status === "ok") cache.set(cacheKey, res.data); + return res; + }), ).then(applyTx), getAssetBalances: (publicKey, filter, timeoutMs) => withErrorHandling( errorHandler, { functionName: "account.getAssetBalances", params: { publicKey, filter } }, () => - withLogging(logger, "account.getAssetBalances", { publicKey, filter }, () => - getAssetBalances(horizonUrl, publicKey, filter), - ), + withLogging(logger, "account.getAssetBalances", { publicKey, filter }, async () => { + const cacheKey = `account:assetBalances:${horizonUrl}:${publicKey}:${JSON.stringify(filter ?? {})}`; + if (cache) { + const cachedVal = cache.get(cacheKey); + if (cachedVal) return ok(cachedVal as AssetBalance[]); + } + const res = await getAssetBalances(horizonUrl, publicKey, filter); + if (cache && res.status === "ok") cache.set(cacheKey, res.data); + return res; + }), ).then(applyTx), stream: (publicKey, streamConfig, signal) => streamAccount(horizonUrl, publicKey, streamConfig, signal, logger),