Sign and Broadcast Transaction

To complete any staking or withdrawal operation on Ethereum, sign the unsigned transaction provided by the Pooled 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/deposit, /staking/unstake or /staking/withdraw.

2. Sign the transaction

Sign the unsigned transaction using your Ethereum private key. It can be done with any of the signing libraries, or you can use the following ethers.js code:

import { Wallet, providers, TransactionRequest } from "ethers";

// Example input values (replace with your data)
const PRIVATE_KEY = "<PRIVATE_KEY>";
const RPC_URL = "<ETHEREUM_RPC_URL>";

const unsignedTx: TransactionRequest = {
  to: "0xba447498dc4c169f2b4f427b2c4d532320457e89", // recipient address for this transaction
  data: "0xf9609f08...", // transaction data payload in the hexadecimal format.
  value: "10000000000000000" // amount this transaction is sending in Wei; as string, convert if needed
};

async function signTransaction() {
  const provider = new providers.JsonRpcProvider(RPC_URL);
  const wallet = new Wallet(PRIVATE_KEY, provider);
  const tx = await wallet.populateTransaction(unsignedTx);
  // Optionally set gas/gasPrice here
  const signedTx = await wallet.signTransaction(tx);
  console.log(signedTx);
}

signTransaction(); // the result is a raw signed hex-encoded transaction ready for broadcasting
npm install [email protected]
node signTransaction.js

3. Broadcast the transaction

Broadcast the signed transaction to the Ethereum network by sending a POST request to /api/v1/staking/pool/{network}/transaction/send.

Example request (for hoodi network):

curl --request POST \
     --url https://api-test.p2p.org/api/v1/staking/pool/hoodi/transaction/send \
     --header 'accept: application/json' \
     --header 'authorization: Bearer <token>' \
     --header 'content-type: application/json' \
     --data '
{
    "signedTransaction": "0x02f8b983088bb00e8459682f0084cf5f645482cb2694ba447498dc4c169f2b4f427b2c4d532320457e89872386f26fc10000b844f9609f08000000000000000000000000092af80778ff3c3d27fd2744c39f6e9326d9aaee0000000000000000000000000000000000000000000000000000000000000000c080a0709f764a37d31ed49fa84913082568b241b57a3cc7e660a597868f9d39bb4055a018ce04abc90fcceec21bf084c8966426ad7eda33a1cb541078b8a597ea7340a8"
}'
  • signedTransaction — signed transaction in the hexadecimal format which needs to be broadcasted to the network.

Example response:

{
    "error": null,
    "result": {
        "hash": "0x9945ee6bbbdb7c34565b8833d37d302da9c76d63262d12a36f7c8f822553fa84",
        "status": "success",
        "blockNumber": 791360,
        "transactionIndex": 0,
        "gasUsed": "51613",
        "cumulativeGasUsed": "51613",
        "effectiveGasPrice": "2606634082",
        "from": "0x092Af80778ff3c3D27Fd2744C39f6e9326d9AaEe",
        "to": "0xba447498DC4c169f2b4f427B2c4D532320457E89"
    }
}
  • hash — hash of the Ethereum transaction.
  • status — transaction status: pending, confirmed, or failed.
  • blockNumber — block number in which the transaction has been included; null if the transaction has not been confirmed yet.
  • transactionIndex — value of the position of the transaction in the block in which it has been included.
  • gasUsed — amount of gas spent for the transaction.
  • cumulativeGasUsed — total amount of gas used by all transactions in a block up to this transaction.
  • effectiveGasPrice — actual price per gas unit.
  • from — sender address of this transaction.
  • to — recipient address for this transaction.

What's next?