eth_sendRawTransaction - Broadcast Signed T...
Submit signed transactions to the Neuroweb Mainnet network. Essential for sending ETH, interacting with contracts, and deploying smart contracts.
Submits a pre-signed transaction to the network for broadcast and execution. Returns the transaction hash if successful.
When to Use This Method
eth_sendRawTransaction is required for:
- Sending ETH - Transfer value between accounts
- Token Transfers - Execute ERC20/NFT transfers
- Contract Interactions - Call state-changing functions
- Contract Deployment - Deploy new smart contracts
Common Use Cases
1. Safe Transaction Broadcasting
// Broadcast with safety checks
async function safeBroadcast(wallet, transaction) {
// Pre-flight checks
const balance = await wallet.provider.getBalance(wallet.address);
const estimatedCost = transaction.gasLimit * transaction.maxFeePerGas;
if (balance < estimatedCost + transaction.value) {
throw new Error('Insufficient balance for transaction');
}
// Check nonce
const expectedNonce = await wallet.provider.getTransactionCount(wallet.address);
if (transaction.nonce && transaction.nonce !== expectedNonce) {
console.warn(`Nonce mismatch: expected ${expectedNonce}, got ${transaction.nonce}`);
}
// Sign transaction
const signedTx = await wallet.signTransaction(transaction);
// Broadcast with retry
let attempts = 0;
while (attempts < 3) {
try {
const txHash = await wallet.provider.send('eth_sendRawTransaction', [signedTx]);
return txHash;
} catch (error) {
if (error.message.includes('already known')) {
// Transaction already sent
return ethers.Transaction.from(signedTx).hash;
}
attempts++;
if (attempts === 3) throw error;
await new Promise(r => setTimeout(r, 1000 * attempts));
}
}
}2. Optimized Gas Strategy
// Dynamic gas price management
async function optimizedSend(wallet, to, value) {
const provider = wallet.provider;
// Get network conditions
const [block, feeData, gasPrice] = await Promise.all([
provider.getBlock('latest'),
provider.getFeeData(),
provider.getGasPrice()
]);
// Calculate optimal fees
const baseFee = block.baseFeePerGas;
const priorityFee = feeData.maxPriorityFeePerGas;
// Adjust based on network congestion
const congestionFactor = Number(baseFee) / Number(parseUnits('10', 'gwei'));
const adjustedPriorityFee = priorityFee * BigInt(Math.ceil(congestionFactor));
const transaction = {
to: to,
value: parseEther(value),
type: 2,
chainId: 1,
maxFeePerGas: baseFee * 2n + adjustedPriorityFee,
maxPriorityFeePerGas: adjustedPriorityFee,
gasLimit: 21000n
};
const signedTx = await wallet.signTransaction(transaction);
return await provider.send('eth_sendRawTransaction', [signedTx]);
}3. Transaction Queue Management
// Manage transaction queue
class TransactionQueue {
constructor(wallet) {
this.wallet = wallet;
this.queue = [];
this.processing = false;
}
async add(transaction) {
this.queue.push(transaction);
if (!this.processing) {
this.process();
}
}
async process() {
this.processing = true;
while (this.queue.length > 0) {
const tx = this.queue.shift();
try {
// Add nonce
tx.nonce = await this.wallet.provider.getTransactionCount(this.wallet.address);
// Sign and send
const signedTx = await this.wallet.signTransaction(tx);
const hash = await this.wallet.provider.send('eth_sendRawTransaction', [signedTx]);
console.log(`Transaction sent: ${hash}`);
// Wait for confirmation before next
await this.wallet.provider.waitForTransaction(hash);
} catch (error) {
console.error('Transaction failed:', error);
// Optionally re-queue or handle error
}
}
this.processing = false;
}
}