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

WIP: Feature / Handle Swap & Bridge active route "time-out"s #1315

Draft
wants to merge 9 commits into
base: v2
Choose a base branch
from
46 changes: 26 additions & 20 deletions src/controllers/swapAndBridge/swapAndBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getTokenAmount } from '../../libs/portfolio/helpers'
import {
convertPortfolioTokenToSocketAPIToken,
getActiveRoutesForAccount,
getActiveRoutesLowestServiceTime,
getIsBridgeTxn,
getIsTokenEligibleForSwapAndBridge,
getQuoteRouteSteps,
Expand Down Expand Up @@ -1130,6 +1131,19 @@ export class SwapAndBridgeController extends EventEmitter {
if (broadcastedButNotConfirmed) return
if (activeRoute.routeStatus === 'completed') return

const serviceTimeThresholdInMs = getActiveRoutesLowestServiceTime([activeRoute]) * 3
const prevAttemptedToGetTheStatusAt = activeRoute.lastAttemptedToGetTheStatusAt || Date.now()
const shouldTimeout = prevAttemptedToGetTheStatusAt > Date.now() + serviceTimeThresholdInMs
if (shouldTimeout) {
// TODO: Put in more details about the funds in the error message
return this.updateActiveRoute(activeRoute.activeRouteId, {
routeStatus: 'timed-out',
error:
'Unable to get the route status after multiple attempts. The transaction may take longer than expected. The funds might have arrived on the destination chain. Please attempt to check the status manually.'
})
}

const lastAttemptedToGetTheStatusAt = Date.now()
try {
status = await this.#socketAPI.getRouteStatus({
activeRouteId: activeRoute.activeRouteId,
Expand All @@ -1138,36 +1152,28 @@ export class SwapAndBridgeController extends EventEmitter {
})
} catch (e: any) {
const { message } = getHumanReadableSwapAndBridgeError(e)
this.updateActiveRoute(activeRoute.activeRouteId, { error: message })
this.updateActiveRoute(activeRoute.activeRouteId, {
error: message,
lastAttemptedToGetTheStatusAt
})
return
}

const route = this.activeRoutes.find((r) => r.activeRouteId === activeRoute.activeRouteId)
if (route?.error) {
this.updateActiveRoute(activeRoute.activeRouteId, {
error: undefined
error: undefined,
lastAttemptedToGetTheStatusAt
})
}

if (status === 'completed') {
this.updateActiveRoute(
activeRoute.activeRouteId,
{
routeStatus: 'completed',
error: undefined
},
true
)
} else if (status === 'ready') {
this.updateActiveRoute(
activeRoute.activeRouteId,
{
routeStatus: 'ready',
error: undefined
},
true
)
const activeRoutePropsToUpdate: Partial<ActiveRoute> = { lastAttemptedToGetTheStatusAt }
if (status && ['completed, ready'].includes(status)) {
activeRoutePropsToUpdate.routeStatus = status
activeRoutePropsToUpdate.error = undefined
}

this.updateActiveRoute(activeRoute.activeRouteId, activeRoutePropsToUpdate, true)
}

await Promise.all(
Expand Down
9 changes: 8 additions & 1 deletion src/interfaces/swapAndBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ export type ActiveRoute = {
transactionData: { txHash: string }[] | null
userAddress: string
}
routeStatus: 'waiting-approval-to-resolve' | 'in-progress' | 'ready' | 'completed' | 'failed'
routeStatus:
| 'waiting-approval-to-resolve'
| 'in-progress'
| 'timed-out'
| 'ready'
| 'completed'
| 'failed'
lastAttemptedToGetTheStatusAt?: number // timestamp
error?: string
}

Expand Down
Loading