Skip to content

Commit

Permalink
Attempt to search for signature transaction status if confirm fails
Browse files Browse the repository at this point in the history
  • Loading branch information
mliu committed Nov 20, 2024
1 parent d333896 commit 2da17d8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function buildOptimalTransactionImpl(
0,
)
averagePrioritizationFee = Math.ceil(
(averagePrioritizationFee / recentPrioritizationFees.length) * 1.5,
(averagePrioritizationFee / recentPrioritizationFees.length) * 2000,
)

tx.instructions.unshift(
Expand Down
35 changes: 28 additions & 7 deletions packages/huma-widget/src/components/SolanaTxSendModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SolanaChainEnum } from '@huma-finance/shared'
import { sleep, SolanaChainEnum } from '@huma-finance/shared'
import React, { useEffect, useState } from 'react'
import {
buildOptimalTransactionFromConnection,
Expand Down Expand Up @@ -34,6 +34,7 @@ export function SolanaTxSendModal({
return
}

let signatureResult = ''
try {
// Optimize transaction
const lockedWritableAccounts = extractWritableAccounts(tx)
Expand All @@ -42,20 +43,40 @@ export function SolanaTxSendModal({
lockedWritableAccounts,
connection,
)
const signature = await sendTransaction(optimizedTx, connection, {
maxRetries: 5,
signatureResult = await sendTransaction(optimizedTx, connection, {
preflightCommitment: 'confirmed',
skipPreflight: true,
})
setSignature(signature)
dispatch(setSolanaSignature(signature))
setSignature(signatureResult)
dispatch(setSolanaSignature(signatureResult))
await connection.confirmTransaction({
blockhash: optimizedTx.recentBlockhash!,
lastValidBlockHeight: optimizedTx.lastValidBlockHeight!,
signature,
signature: signatureResult,
})
handleSuccess({ signature })
handleSuccess({ signature: signatureResult })
} catch (error: unknown) {
let signatureStatusRetries = 0

while (signatureStatusRetries < 5) {
// Attempt to load the signature status using transaction history
// eslint-disable-next-line no-await-in-loop
await sleep(1000)
// eslint-disable-next-line no-await-in-loop
const result = await connection.getSignatureStatus(signatureResult, {
searchTransactionHistory: true,
})
if (
result?.value?.confirmationStatus === 'finalized' ||
result?.value?.confirmationStatus === 'confirmed'
) {
handleSuccess({ signature: signatureResult })
return
}

signatureStatusRetries += 1
}

const err = error as Error
dispatch(setError({ errorMessage: err?.message || '' }))
}
Expand Down

0 comments on commit 2da17d8

Please sign in to comment.