UNPKG

@metamask/multichain-account-service

Version:
1 lines 4.31 kB
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,6BAA6B,EAC9B,qCAAqC;AAEtC,qBAAqB;AACrB,MAAM,OAAO,YAAa,SAAQ,KAAK;IACrC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,OAAO,KAAK,YAAY,YAAY,CAAC;AACvC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,8BAA8B,CAAC,KAAc;IAC3D,OAAO,CACL,KAAK,YAAY,sBAAsB;QACvC,KAAK,CAAC,OAAO,KAAK,6BAA6B,CAAC,gBAAgB,CACjE,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,WAA6B,EAC7B,EACE,WAAW,GAAG,CAAC,EACf,SAAS,GAAG,GAAG,MACiC,EAAE;IAEpD,IAAI,SAAS,CAAC;IACd,IAAI,OAAO,GAAG,SAAS,CAAC;IACxB,KAAK,IAAI,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,WAAW,EAAE,OAAO,EAAE,EAAE,CAAC;QACxD,IAAI,CAAC;YACH,OAAO,MAAM,WAAW,EAAE,CAAC;QAC7B,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,SAAS,GAAG,KAAK,CAAC;YAClB,IAAI,OAAO,IAAI,WAAW,EAAE,CAAC;gBAC3B,MAAM;YACR,CAAC;YACD,MAAM,KAAK,GAAG,OAAO,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3D,OAAO,IAAI,CAAC,CAAC;QACf,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,EAAoB,EACpB,YAAoB,GAAG;IAEvB,IAAI,KAAK,CAAC;IACV,IAAI,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAI;YAC3B,EAAE,EAAE;YACJ,IAAI,OAAO,CAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,EAAE;gBAClC,KAAK,GAAG,UAAU,CAChB,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,YAAY,CAAC,oBAAoB,SAAS,IAAI,CAAC,CAAC,EACjE,SAAS,CACV,CAAC;YACJ,CAAC,CAAC;SACH,CAAC,CAAC;IACL,CAAC;YAAS,CAAC;QACT,IAAI,KAAK,EAAE,CAAC;YACV,YAAY,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import {\n KeyringControllerError,\n KeyringControllerErrorMessage,\n} from '@metamask/keyring-controller';\n\n/** Timeout error. */\nexport class TimeoutError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'TimeoutError';\n }\n}\n\n/**\n * Check if an error is a `TimeoutError`.\n *\n * @param error - The error to check.\n * @returns `true` if the error is a `TimeoutError`, otherwise `false`.\n */\nexport function isTimeoutError(error: unknown): error is TimeoutError {\n return error instanceof TimeoutError;\n}\n\n/**\n * Check if an error is a `KeyringControllerLockedError`.\n *\n * @param error - The error to check.\n * @returns `true` if the error is a `KeyringControllerLockedError`, otherwise `false`.\n */\nexport function isKeyringControllerLockedError(error: unknown): boolean {\n return (\n error instanceof KeyringControllerError &&\n error.message === KeyringControllerErrorMessage.ControllerLocked\n );\n}\n\n/**\n * Execute a function with exponential backoff on transient failures.\n *\n * @param fnToExecute - The function to execute.\n * @param options - The options for the retry.\n * @param options.maxAttempts - The maximum number of attempts.\n * @param options.backOffMs - The backoff in milliseconds.\n * @throws An error if the transaction count cannot be retrieved.\n * @returns The result of the function.\n */\nexport async function withRetry<T>(\n fnToExecute: () => Promise<T>,\n {\n maxAttempts = 3,\n backOffMs = 500,\n }: { maxAttempts?: number; backOffMs?: number } = {},\n): Promise<T> {\n let lastError;\n let backOff = backOffMs;\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n return await fnToExecute();\n } catch (error) {\n lastError = error;\n if (attempt >= maxAttempts) {\n break;\n }\n const delay = backOff;\n await new Promise((resolve) => setTimeout(resolve, delay));\n backOff *= 2;\n }\n }\n throw lastError;\n}\n\n/**\n * Execute a promise with a timeout.\n *\n * @param fn - A callback that returns the promise to execute.\n * @param timeoutMs - The timeout in milliseconds.\n * @returns The result of the promise.\n */\nexport async function withTimeout<T>(\n fn: () => Promise<T>,\n timeoutMs: number = 500,\n): Promise<T> {\n let timer;\n try {\n return await Promise.race<T>([\n fn(),\n new Promise<T>((_resolve, reject) => {\n timer = setTimeout(\n () => reject(new TimeoutError(`Timed out after: ${timeoutMs}ms`)),\n timeoutMs,\n );\n }),\n ]);\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n }\n}\n"]}