curl --request POST \
--url https://fantom-mainnet.core.chainstack.com/4ab982aa70a7baead515ffdb5915df3f \
--header 'Content-Type: application/json' \
--data '{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_sendRawTransaction",
"params": [
"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
]
}'
{
"jsonrpc": "<string>",
"id": 123,
"result": {}
}
curl --request POST \
--url https://fantom-mainnet.core.chainstack.com/4ab982aa70a7baead515ffdb5915df3f \
--header 'Content-Type: application/json' \
--data '{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_sendRawTransaction",
"params": [
"0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675"
]
}'
{
"jsonrpc": "<string>",
"id": 123,
"result": {}
}
eth_sendRawTransaction
method to submit the signed transaction to the Fantom network for processing.
eth_sendRawTransaction
is used to modify the state of the blockchain, it is not possible to duplicate the same request.data
—the signed transaction. The serialized transaction data includes transaction metadata such as the nonce, gas price, gas limit, recipient address, and data payload, as well as the digital signature generated using the private key associated with the address that is sending the transaction. Typically, the transaction is signed with a library such as web3.py, web3.js, or ethers.js using the sender’s private key.result
— the unique hash identifying the transaction or the zero hash if the transaction is unavailable. The zero hash is a special value representing an invalid or null hash.eth_sendRawTransaction
code examplesconst { Web3 } = require("web3");
// Initialize Web3 instance using a provider
const web3 = new Web3(
new Web3.providers.HttpProvider("YOUR_CHAINSTACK_RPC_NODE")
);
/**
* Sends a specified amount from a given account to another.
*
* @param {string} secretKey The private key of the sender's account.
* @param {string} to The recipient address.
* @param {string} amount The amount to send in Ether.
*/
async function sendAmount(secretKey, to, amount) {
const account = web3.eth.accounts.privateKeyToAccount(secretKey);
web3.eth.accounts.wallet.add(account);
const senderAddress = account.address;
console.log(
`Attempting to send ${amount} ETH from ${senderAddress} to ${to}`
);
const MAX_RETRIES = 3; // Maximum number of retries
const COOLDOWN = 5000; // Time waited between retries in ms
let retries = 0; // Initialize retry counter
async function sendTransaction() {
try {
const balance = await web3.eth.getBalance(senderAddress);
console.log(
`Current balance: ${web3.utils.fromWei(balance, "ether")} ETH`
);
const gasPrice = await web3.eth.getGasPrice();
console.log(
`Current gas price: ${web3.utils.fromWei(gasPrice, "gwei")} Gwei`
);
const gasLimit = await web3.eth.estimateGas({
from: senderAddress,
to: to,
value: web3.utils.toWei(amount, "ether"),
});
console.log(`Estimated gas limit: ${gasLimit}`);
const gasCost = BigInt(gasPrice) * BigInt(gasLimit);
console.log(
`Estimated gas cost: ${web3.utils.fromWei(
gasCost.toString(),
"ether"
)} ETH`
);
const amountToSend = web3.utils.toWei(amount, "ether");
const totalCost = BigInt(amountToSend) + gasCost;
if (BigInt(balance) >= totalCost) {
console.log(`Amount to send: ${amount} ETH`);
const transaction = {
to: to,
value: amountToSend,
gas: gasLimit,
gasPrice: gasPrice,
nonce: await web3.eth.getTransactionCount(senderAddress, "latest"),
};
console.log("Signing transaction...");
const signedTx = await account.signTransaction(transaction);
console.log("Transaction signed. Sending...");
const receipt = await web3.eth.sendSignedTransaction(
signedTx.rawTransaction
);
console.log(
`Transaction successful with hash: ${receipt.transactionHash}`
);
console.log(
`Find the transaction in the explorer: https://sepolia.etherscan.io/tx/${receipt.transactionHash}`
);
} else {
console.log(
"Not enough balance to cover the transaction cost. Transaction aborted."
);
}
} catch (error) {
console.error(`Failed to send transaction: ${error.message}`);
if (retries < MAX_RETRIES) {
retries++;
console.log(`Retrying... (${retries}/${MAX_RETRIES})`);
await new Promise((resolve) => setTimeout(resolve, COOLDOWN)); // Wait for 5 seconds before retrying
await sendTransaction(); // Retry the transaction
} else {
console.error("Maximum retries reached. Giving up.");
}
}
}
await sendTransaction();
}
// Replace with your secret key, recipient address, and the amount to send
const secretKey = "0x_YOUR_PRIVATE_KEY";
const recipientAddress = "DESTINATION_ADDRESS";
const amountToSend = "1.0"; // Amount in Ether
sendAmount(secretKey, recipientAddress, amountToSend);
amountToSend
, recipientAddress
, and secretKey
fields to be able to use this code.eth_sendRawTransaction
is to automatically swap FTM for a token when its price reaches a certain level. eth_sendRawTransaction
will allow custom transaction parameters, such as gas price or gas limit, to be specified to optimize the speed and cost of the transaction.
For instance, you could build a program monitoring the price of a specific token and execute a swapExactETHForTokens
transaction when the token reaches a certain price level, it could use eth_sendRawTransaction
to submit a raw transaction with a custom gas price and limit optimized for the current network conditions.The transaction hash.
The response is of type object
.
Was this page helpful?