Skip to content
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

- fixed: (0x Gasless) Save swaps under the transaction hash that mines, not a submitted one the relayer can replace and leave pending

## 2.62.0 (2026-11-13)

- added: (Houdini) Pay a pasted Zcash unified address out to its transparent receiver
Expand Down
125 changes: 94 additions & 31 deletions src/swap/defi/0x/0xGasless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { div, mul } from 'biggystring'
import {
EdgeAssetAction,
EdgeCorePluginFactory,
EdgeLog,
EdgeSwapApproveOptions,
EdgeSwapInfo,
EdgeSwapQuote,
Expand All @@ -13,7 +14,12 @@ import {

import { due } from '../../../util/due'
import { snooze } from '../../../util/utils'
import { EXPIRATION_MS } from './constants'
import {
EXPIRATION_MS,
STATUS_MAX_BACKOFF_MS,
STATUS_POLL_MS,
STATUS_TIMEOUT_MS
} from './constants'
import { asInitOptions } from './types'
import { getCurrencyCode, getZeroXAsset, makeSignatureStruct } from './util'
import { ZeroXApi } from './ZeroXApi'
Expand All @@ -30,7 +36,7 @@ const swapInfo: EdgeSwapInfo = {
}

export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
const { io } = opts
const { io, log } = opts
const initOptions = asInitOptions(opts.initOptions)

const api = new ZeroXApi(io, initOptions.apiKey)
Expand Down Expand Up @@ -157,39 +163,19 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
trade: tradeData
})

let apiSwapStatus: GaslessSwapStatusResponse
do {
// Wait before checking
await snooze(500)
apiSwapStatus = await api.gaslessSwapStatus(
chainId,
apiSwapSubmission.tradeHash
)
if (
apiSwapStatus.status === 'succeeded' &&
apiSwapStatus.transactions.length > 1
) {
throw new Error(
`Swap failed: Unexpected multiple transactions for 'succeeded' status: ${apiSwapStatus.transactions.length}`
)
}
} while (
apiSwapStatus.status === 'pending' ||
// If status==='submitted' there may be multiple transaction
// competing. Wait until there's only one (status is 'succeeded'
// or 'confirmed')
apiSwapStatus.transactions.length !== 1
)

if (apiSwapStatus.status === 'failed') {
throw new Error(`Swap failed: ${apiSwapStatus.reason ?? 'unknown'}`)
}
const txid = await waitForMinedTxid({
getStatus: async () =>
await api.gaslessSwapStatus(chainId, apiSwapSubmission.tradeHash),
log,
pollMs: STATUS_POLL_MS,
timeoutMs: STATUS_TIMEOUT_MS
})

const assetAction: EdgeAssetAction = {
assetActionType: 'swap'
}

const orderId: string = apiSwapStatus.transactions[0].hash
const orderId = txid

let orderUri: string | undefined
const { transactionExplorer } = swapRequest.fromWallet.currencyInfo
Expand Down Expand Up @@ -244,7 +230,7 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
savedAction,
signedTx: '', // Signing is done by the tx-relay server
tokenId: swapRequest.fromTokenId,
txid: apiSwapStatus.transactions[0].hash,
txid,
walletId: swapRequest.fromWallet.id
}

Expand Down Expand Up @@ -273,3 +259,80 @@ export const make0xGaslessPlugin: EdgeCorePluginFactory = opts => {
}
}
}

const TXID_REGEX = /^0x[0-9a-f]{64}$/i

interface WaitForMinedTxidOptions {
getStatus: () => Promise<GaslessSwapStatusResponse>
log: EdgeLog
pollMs: number
timeoutMs: number
}

/**
* Polls a submitted gasless trade until 0x reports it mined, and returns
* the hash of the mined transaction.
*
* A "submitted" trade can list transactions the relayer later replaces,
* so only "succeeded" (in a block) or "confirmed" names the hash to save.
* A failed status request is retried with a doubling delay until the
* deadline, because 0x already holds the signed trade and may still mine it.
*/
export async function waitForMinedTxid(
opts: WaitForMinedTxidOptions
): Promise<string> {
const { getStatus, log, pollMs, timeoutMs } = opts
const deadline = Date.now() + timeoutMs

let delayMs = pollMs
let lastStatus = 'none'
while (Date.now() < deadline) {
await snooze(delayMs)

let apiSwapStatus: GaslessSwapStatusResponse
try {
apiSwapStatus = await getStatus()
} catch (error: unknown) {
log.warn(`0x gasless status check failed: ${String(error)}`)
delayMs = Math.min(delayMs * 2, STATUS_MAX_BACKOFF_MS)
continue
}
delayMs = pollMs

const hashes = apiSwapStatus.transactions.map(tx => tx.hash)
const status = `${apiSwapStatus.status} [${hashes.join(', ')}]`
if (status !== lastStatus) {
log.warn(`0x gasless status: ${status}`)
lastStatus = status
}

if (apiSwapStatus.status === 'failed') {
throw new Error(`Swap failed: ${apiSwapStatus.reason}`)
}
if (
apiSwapStatus.status === 'succeeded' ||
apiSwapStatus.status === 'confirmed'
) {
if (hashes.length > 1) {
throw new Error(
`Swap failed: Unexpected multiple transactions for '${apiSwapStatus.status}' status: ${hashes.length}`
)
}
if (hashes.length === 1) {
const [hash] = hashes
if (!TXID_REGEX.test(hash)) {
throw new Error(
`Swap failed: Invalid transaction hash for '${apiSwapStatus.status}' status: ${hash}`
)
}
return hash
}
}
}

throw new Error(
`Swap timed out after ${
timeoutMs / 1000
} seconds waiting for 0x to mine it (last status: ${lastStatus}). It may still complete.`
)
}
6 changes: 6 additions & 0 deletions src/swap/defi/0x/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export const EXPIRATION_MS = 1000 * 60
/** How often to poll a submitted gasless trade's status */
export const STATUS_POLL_MS = 500
/** Longest wait between status polls while the status request keeps failing */
export const STATUS_MAX_BACKOFF_MS = 1000 * 16
/** How long to wait for a submitted gasless trade to mine */
export const STATUS_TIMEOUT_MS = 1000 * 60 * 5
/** [The ERC-7528: ETH (Native Asset) Address Convention](https://eips.ethereum.org/EIPS/eip-7528) */
export const NATIVE_TOKEN_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
Loading
Loading