Skip to content

useTransaction

The useTransaction hook provides a high-level interface for executing bridge transactions. It handles chain switching, token approvals (ERC-20), and transaction submission automatically.

Usage

import { useTransaction, useQuote } from '@silentswap/react';
import { useAccount, useWalletClient } from 'wagmi';
 
function BridgeAction({ quote }) {
  const { address, connector } = useAccount();
  const { data: walletClient } = useWalletClient();
 
  const { executeTransaction, isLoading, currentStep, error } = useTransaction({
    address: address!,
    walletClient,
    connector,
  });
 
  const handleBridge = async () => {
    if (!quote) return;
    
    const result = await executeTransaction(quote);
    if (result) {
      console.log('Bridge transaction submitted:', result.requestId);
    }
  };
 
  return (
    <div>
      {isLoading && <p>Current Step: {currentStep}</p>}
      <button onClick={handleBridge} disabled={isLoading || !quote}>
        Execute Bridge
      </button>
      {error && <p style={{ color: 'red' }}>{error.message}</p>}
    </div>
  );
}

Options

PropertyTypeDescription
addressstringUser's EVM address (required).
walletClientWalletClientViem wallet client for signing (required for EVM transactions).
connectorConnectorWagmi connector for chain switching (required for EVM transactions).
solanaConnectorSolanaWalletConnectorSolana wallet connector (required for Solana transactions).
solanaConnectionSolanaConnectionSolana RPC connection (required for Solana transactions).
solanaRpcUrlstringOptional custom Solana RPC URL.
setCurrentStepFunctionOptional callback to set current step externally.
onStatusFunctionOptional callback for status updates.

Return Values

PropertyTypeDescription
executeTransactionFunctionAsync function to execute the bridge based on a BridgeQuote. Signature: (quote: BridgeQuote) => Promise<BridgeStatus | null>. Supports both EVM and Solana transactions.
executeSwapTransactionFunctionExecute a deposit transaction from an OrderResponse. Signature: (orderResponse: OrderResponse) => Promise<SwapTransaction>.
approveTokenSpendingFunctionApprove token spending for a given allowance target. Signature: (chainId, tokenAddress, allowanceTarget, amount, userAddress) => Promise<Hex | null>. Returns transaction hash if approval was needed, null otherwise.
getStatusFunctionFetch the current status of a bridge request. Signature: (requestId: string, provider: BridgeProvider) => Promise<BridgeStatus | null>.
isLoadingbooleanWhether a transaction is being processed.
currentStepstringHuman-readable description of the current step (e.g., "Approving token", "Switching chain").
errorError | nullError object if the transaction failed.