Skip to content

Commit

Permalink
Await tx receipt for transaction steps
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-palmer committed May 14, 2024
1 parent e4879e0 commit 8bd32ae
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/sdk/src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export async function sendTransactionSafely(
step: Execute['steps'][0],
wallet: AdaptedWallet,
setTxHashes: (
tx: NonNullable<Execute['steps'][0]['items']>[0]['txHashes'],
tx: NonNullable<Execute['steps'][0]['items']>[0]['txHashes']
) => void,
setInternalTxHashes: (
tx: NonNullable<Execute['steps'][0]['items']>[0]['internalTxHashes'],
tx: NonNullable<Execute['steps'][0]['items']>[0]['internalTxHashes']
) => void,
request: AxiosRequestConfig,
headers?: AxiosRequestHeaders,
crossChainIntentChainId?: number,
crossChainIntentChainId?: number
) {
const client = getClient()
let txHash = await wallet.handleSendTransactionStep(chainId, item, step)
Expand All @@ -54,7 +54,7 @@ export async function sendTransactionSafely(
setTxHashes([{ txHash: txHash, chainId: chainId }])

// Handle transaction replacements and cancellations
viemClient
const receipt = await viemClient
.waitForTransactionReceipt({
hash: txHash,
onReplaced: (replacement) => {
Expand All @@ -70,21 +70,21 @@ export async function sendTransactionSafely(
attemptCount = 0 // reset attempt count
getClient()?.log(
['Transaction replaced', replacement],
LogLevel.Verbose,
LogLevel.Verbose
)
},
})
.catch((error) => {
getClient()?.log(
['Error in waitForTransactionReceipt', error],
LogLevel.Error,
LogLevel.Error
)
})

const validate = (res: AxiosResponse) => {
getClient()?.log(
['Execute Steps: Polling for confirmation', res],
LogLevel.Verbose,
LogLevel.Verbose
)
if (res.status === 200 && res.data && res.data.status === 'failure') {
throw Error('Transaction failed')
Expand Down Expand Up @@ -125,6 +125,13 @@ export async function sendTransactionSafely(

if (!res || validate(res)) {
waitingForConfirmation = false // transaction confirmed
} else if (
// rely on tx confirmation if there is no check endpoint
!item?.check?.endpoint &&
receipt &&
receipt.status === 'success'
) {
waitingForConfirmation = false
} else if (res) {
if (res.data.status !== 'pending') {
attemptCount++
Expand Down

0 comments on commit 8bd32ae

Please sign in to comment.