Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make swap timeouts adjustable #582

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
15 changes: 12 additions & 3 deletions src/core/swap/swap-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
Expand All @@ -26,7 +26,13 @@ export async function fetchSwapQuotes(
request: EdgeSwapRequest,
opts: EdgeSwapRequestOptions = {}
): Promise<EdgeSwapQuote[]> {
const { disabled = {}, preferPluginId, promoCodes = {} } = opts
const {
disabled = {},
noResponseMs,
preferPluginId,
promoCodes = {},
slowResponseMs = 20000
} = opts
const { log } = ai.props

const account = ai.props.state.accounts[accountId]
Expand Down Expand Up @@ -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`)
Expand All @@ -98,6 +104,9 @@ export async function fetchSwapQuotes(
throw pickBestError(errors)
}
)

if (noResponseMs == null) return await promise
return await timeout(promise, noResponseMs)
}

function wrapQuote(
Expand Down
12 changes: 12 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,18 @@ export interface EdgeSwapRequestOptions {
preferType?: EdgeSwapPluginType
disabled?: EdgePluginMap<true>
promoCodes?: EdgePluginMap<string>

/**
* 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 ----------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/util/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ export function timeout<T>(
const timer = setTimeout(() => reject(error), ms)
promise.then(
ok => {
resolve(ok)
clearTimeout(timer)
resolve(ok)
},
error => {
reject(error)
clearTimeout(timer)
reject(error)
}
)
})
Expand Down
Loading