Sign and Broadcast Transaction
To complete a staking or withdrawal operation on Ethereum, sign the unsigned transaction provided by the Staking API and broadcast it to the network.
1. Prepare the transaction
Retrieve an unsigned serialized transaction from the relevant API endpoint, e.g., staking/direct/tx/deposit or staking/direct/tx/withdrawal.
2. Sign and send the transaction
Use the P2P Signer SDK to sign the unsignedTransactionData following protocol specific configuration.
Broadcast the signed transaction on to the chain using the code below:
import { ethers } from 'ethers';
/**
* Broadcasts a signed transaction hex to the network.
* @param signedTx - The hex string returned by Signer SDK
*/
async function broadcast(signedTx: string) {
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
try {
const response = await provider.broadcastTransaction(signedTx);
console.log("Transaction Hash:", response.hash);
return response;
} catch (error) {
console.error("Broadcast failed:", error);
}
}
What's next?
- Getting Started
- Withdrawal
- Staking API reference
Updated 1 day ago