From 1b59475c9bb81696d415b30c686002b6793dd273 Mon Sep 17 00:00:00 2001 From: Michael Liu Date: Wed, 20 Nov 2024 18:13:18 -0500 Subject: [PATCH] Attempt to search for signature transaction status if confirm fails (#369) --- .../utils/solana/buildOptimalTransaction.ts | 2 +- .../src/components/SolanaTxSendModal.tsx | 35 +++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts b/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts index 9e64ad04..544415f0 100644 --- a/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts +++ b/packages/huma-sdk/src/utils/solana/buildOptimalTransaction.ts @@ -38,7 +38,7 @@ async function buildOptimalTransactionImpl( 0, ) averagePrioritizationFee = Math.ceil( - (averagePrioritizationFee / recentPrioritizationFees.length) * 1.5, + (averagePrioritizationFee / recentPrioritizationFees.length) * 2000, ) tx.instructions.unshift( diff --git a/packages/huma-widget/src/components/SolanaTxSendModal.tsx b/packages/huma-widget/src/components/SolanaTxSendModal.tsx index 48a19e29..1e7b644d 100644 --- a/packages/huma-widget/src/components/SolanaTxSendModal.tsx +++ b/packages/huma-widget/src/components/SolanaTxSendModal.tsx @@ -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, @@ -34,6 +34,7 @@ export function SolanaTxSendModal({ return } + let signatureResult = '' try { // Optimize transaction const lockedWritableAccounts = extractWritableAccounts(tx) @@ -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 || '' })) }