diff --git a/CHANGELOG.md b/CHANGELOG.md index 15ef6fe6d..ee26a594f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- added: Make swap timeouts adjustable. + ## 2.0.1 (2024-01-08) - added: Missing asset action types (claim, claimOrder, swapNetworkFee, and transferNetworkFee). diff --git a/src/core/swap/swap-api.ts b/src/core/swap/swap-api.ts index 42f6d0674..9ba157860 100644 --- a/src/core/swap/swap-api.ts +++ b/src/core/swap/swap-api.ts @@ -14,7 +14,7 @@ import { EdgeSwapRequest, EdgeSwapRequestOptions } from '../../types/types' -import { fuzzyTimeout } from '../../util/promise' +import { fuzzyTimeout, timeout } from '../../util/promise' import { ApiInput } from '../root-pixie' /** @@ -26,7 +26,13 @@ export async function fetchSwapQuotes( request: EdgeSwapRequest, opts: EdgeSwapRequestOptions = {} ): Promise { - const { disabled = {}, preferPluginId, promoCodes = {} } = opts + const { + disabled = {}, + noResponseMs, + preferPluginId, + promoCodes = {}, + slowResponseMs = 20000 + } = opts const { log } = ai.props const account = ai.props.state.accounts[accountId] @@ -76,7 +82,7 @@ export async function fetchSwapQuotes( } // Wait for the results, with error handling: - return await fuzzyTimeout(promises, 20000).then( + const promise = fuzzyTimeout(promises, slowResponseMs).then( ({ results: quotes, errors }) => { for (const pluginId of pendingIds) { log.warn(`${pluginId} gave swap timeout`) @@ -98,6 +104,9 @@ export async function fetchSwapQuotes( throw pickBestError(errors) } ) + + if (noResponseMs == null) return await promise + return await timeout(promise, noResponseMs) } function wrapQuote( diff --git a/src/types/types.ts b/src/types/types.ts index b741c681e..a2091c1bf 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1452,6 +1452,18 @@ export interface EdgeSwapRequestOptions { preferType?: EdgeSwapPluginType disabled?: EdgePluginMap promoCodes?: EdgePluginMap + + /** + * If we have some quotes already, how long should we wait + * for stragglers before we give up? Defaults to 20000ms. + */ + slowResponseMs?: number + + /** + * If don't have any quotes yet, how long should we wait + * before we give up? Defaults to Infinity. + */ + noResponseMs?: number } // edge login ---------------------------------------------------------- diff --git a/src/util/promise.ts b/src/util/promise.ts index 055464eb2..dc08e8850 100644 --- a/src/util/promise.ts +++ b/src/util/promise.ts @@ -26,12 +26,12 @@ export function timeout( const timer = setTimeout(() => reject(error), ms) promise.then( ok => { - resolve(ok) clearTimeout(timer) + resolve(ok) }, error => { - reject(error) clearTimeout(timer) + reject(error) } ) })