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
| Property | Type | Description |
|---|---|---|
address | string | User's EVM address (required). |
walletClient | WalletClient | Viem wallet client for signing (required for EVM transactions). |
connector | Connector | Wagmi connector for chain switching (required for EVM transactions). |
solanaConnector | SolanaWalletConnector | Solana wallet connector (required for Solana transactions). |
solanaConnection | SolanaConnection | Solana RPC connection (required for Solana transactions). |
solanaRpcUrl | string | Optional custom Solana RPC URL. |
setCurrentStep | Function | Optional callback to set current step externally. |
onStatus | Function | Optional callback for status updates. |
Return Values
| Property | Type | Description |
|---|---|---|
executeTransaction | Function | Async function to execute the bridge based on a BridgeQuote. Signature: (quote: BridgeQuote) => Promise<BridgeStatus | null>. Supports both EVM and Solana transactions. |
executeSwapTransaction | Function | Execute a deposit transaction from an OrderResponse. Signature: (orderResponse: OrderResponse) => Promise<SwapTransaction>. |
approveTokenSpending | Function | Approve 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. |
getStatus | Function | Fetch the current status of a bridge request. Signature: (requestId: string, provider: BridgeProvider) => Promise<BridgeStatus | null>. |
isLoading | boolean | Whether a transaction is being processed. |
currentStep | string | Human-readable description of the current step (e.g., "Approving token", "Switching chain"). |
error | Error | null | Error object if the transaction failed. |