From fa97bbc98212d65c98cc92029d15b97dae4d362b Mon Sep 17 00:00:00 2001 From: c4605 Date: Tue, 18 Mar 2025 22:36:53 +0100 Subject: [PATCH] feat: add dumpable cache object for evm onchain config --- src/XLinkSDK.ts | 18 +++++++++++- src/utils/DumpableCache.ts | 43 +++++++++++++++++++++++++++++ src/xlinkSdkUtils/types.internal.ts | 1 + 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/utils/DumpableCache.ts diff --git a/src/XLinkSDK.ts b/src/XLinkSDK.ts index d39239d..7d76ee4 100644 --- a/src/XLinkSDK.ts +++ b/src/XLinkSDK.ts @@ -118,6 +118,7 @@ import { evmNativeCurrencyAddress, } from "./xlinkSdkUtils/types" import { SDKGlobalContext } from "./xlinkSdkUtils/types.internal" +import { DumpableCache, getCacheInside } from "./utils/DumpableCache" export { GetSupportedRoutesFn_Conditions, @@ -183,6 +184,7 @@ export { GetTimeLockedAssetsInput, GetTimeLockedAssetsOutput, } from "./xlinkSdkUtils/timelockFromEVM" +export type { DumpableCache } from "./utils/DumpableCache" export interface XLinkSDKOptions { __experimental?: { @@ -198,6 +200,9 @@ export interface XLinkSDKOptions { runes?: { ignoreValidateResult?: boolean } + evm?: { + onChainConfigCachePrepared?: (cache: DumpableCache) => void + } } evm?: { /** @@ -231,6 +236,17 @@ export class XLinkSDK { const cacheEVMOnChainConfig = options.evm?.cacheOnChainConfig ?? defaultConfig.evm?.cacheOnChainConfig + let onChainConfigCache: + | undefined + | SDKGlobalContext["evm"]["onChainConfigCache"] + if (cacheEVMOnChainConfig) { + const onChainConfigDumpableCache = new DumpableCache() + options.__experimental?.evm?.onChainConfigCachePrepared?.( + onChainConfigDumpableCache, + ) + onChainConfigCache = getCacheInside(onChainConfigDumpableCache) + } + this.sdkContext = { routes: { detectedCache: new Map(), @@ -262,7 +278,7 @@ export class XLinkSDK { evm: { routesConfigCache: new Map(), feeRateCache: new Map(), - onChainConfigCache: cacheEVMOnChainConfig ? new Map() : undefined, + onChainConfigCache, viemClients: { ...defaultEvmClients, ...options.evm?.viemClients, diff --git a/src/utils/DumpableCache.ts b/src/utils/DumpableCache.ts new file mode 100644 index 0000000..4b1a344 --- /dev/null +++ b/src/utils/DumpableCache.ts @@ -0,0 +1,43 @@ +import { props } from "./promiseHelpers" +import { GeneralCacheInterface } from "./types/GeneralCacheInterface" + +const dumpableCacheKey = Symbol("dumpableCacheKey") + +export const getCacheInside = ( + obj: DumpableCache, +): GeneralCacheInterface => { + return obj[dumpableCacheKey] as any +} + +export class DumpableCache { + readonly [dumpableCacheKey]: Map + + constructor() { + this[dumpableCacheKey] = new Map() + } + + async dump(): Promise { + return JSON.stringify({ + ["**NOTICE**"]: + "This is a dumped cache, DO NOT use it or modify it, only for SDK internal usage.", + version: 1, + data: await props(Object.fromEntries(this[dumpableCacheKey])), + }) + } + + async load(data: string): Promise { + try { + const parsed = JSON.parse(data) + if (parsed.version !== 1) { + throw new Error("Unsupported cache version") + } + + this[dumpableCacheKey].clear() + for (const [key, value] of Object.entries(parsed.data)) { + this[dumpableCacheKey].set(key, value) + } + } catch (error) { + console.trace("Failed to load cache", error) + } + } +} diff --git a/src/xlinkSdkUtils/types.internal.ts b/src/xlinkSdkUtils/types.internal.ts index 09e4c0e..8d83e54 100644 --- a/src/xlinkSdkUtils/types.internal.ts +++ b/src/xlinkSdkUtils/types.internal.ts @@ -13,6 +13,7 @@ import { EVMAddress } from "./types" export interface SDKGlobalContextCache extends GeneralCacheInterface {} + export function withGlobalContextCache( cache: undefined | SDKGlobalContextCache>, cacheKey: K,