eth_sendRawTransaction | Polygon

Recipes
βš–οΈ
How to transfer the entire account balance using web3.js
Open Recipe

Polygon API method that allows submitting a signed transaction to the network. Once a transaction is signed, you can use the eth_sendRawTransaction method to submit the signed transaction to the Polygon network for processing.

🚧

Note that the interactive example in this page will not work, due to the fact that eth_sendRawTransaction is used to modify the state of the blockchain, it is not possible to duplicate the same request.

πŸ‘

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • data β€” the signed transaction. The serialized transaction data, which includes the 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, signed with a library such as web3.py, web3.js, or ethers.js using the sender's private key.

Response

  • result β€” the unique hash identifying the transaction or the zero hash if the transaction is not available yet. The zero hash is a special value that represents an invalid or null hash.

eth_sendRawTransaction code examples

The following examples demonstrate how to use Web3 libraries to make a MATIC transfer on the Mumbai testnet.

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function sendMatic(value) {

    // Define the sender and receiver addresses, and the private key
    const sender = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
    const receiver = '0xe9ba4b4d84d7a3c80245514213b88d50ed937114';
    const privateKey = Buffer.from('1111111111111111111111111111111111111111111111111111111111111111', 'hex');

    // Define the gas limit
    const gasLimit = await web3.eth.estimateGas({
        from: sender,
        to: receiver,
    });

    // Get the transaction count for the sender address
    const nonce = await web3.eth.getTransactionCount(sender);

    // Define the transaction object
    const transactionObject = {
        to: receiver,
        gasPrice: web3.utils.toHex(web3.utils.toWei('50', 'gwei')),
        gasLimit: web3.utils.toHex(gasLimit),
        nonce: web3.utils.toHex(nonce),
        value: web3.utils.toHex(web3.utils.toWei(value, 'ether')),
    };

    // Define the chain configuration
    const common = Common.forCustomChain(
        'mainnet', {
            name: 'polygon-mumbai',
            networkId: 80001,
            chainId: 80001,
        },
        'petersburg',
    );

    // Create a new transaction object to sign 
    const tx = new Transaction(transactionObject, {
        common
    });

    // Sign the transaction using the private key  
    const signedTx = tx.sign(privateKey);

    // Serialize the signed transaction and send it to the blockchain 
    const serializedTx = tx.serialize();
    const rawTransaction = '0x' + serializedTx.toString('hex');
    console.log(`Raw transaction: ${rawTransaction}\n`)

    try {
        console.log(`Sending transaction...`)
        const result = await web3.eth.sendSignedTransaction(rawTransaction);
        console.log(`Transaction hash: ${result.transactionHash}`);

    } catch (error) {
        console.error(error);
    }
}

sendMatic("2");
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

async function sendEth(value) {

  // Define the sender and receiver addresses, and the private key
	const sender = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a';
  const receiver = '0xe9ba4b4d84d7a3c80245514213b88d50ed937114';
  const privateKey = '1111111111111111111111111111111111111111111111111111111111111111';
  
  // Define the gas limit
  const gasLimit = await provider.estimateGas({
    from: sender,
    to: receiver,
    value: ethers.parseEther(value),
  });

  // Get the transaction count for the sender address
  const nonce = await provider.getTransactionCount(sender);

  // Define the transaction object
  const transactionObject = {
    to: receiver,
    gasPrice: ethers.parseUnits('50', 'gwei'),
    gasLimit: gasLimit.toString(),
    nonce: nonce,
    value: ethers.parseEther(value),
  };

  // Sign the transaction using the private key  
  const wallet = new ethers.Wallet(privateKey, provider);
  console.log(`Sending transaction...`)
  
  const transaction = await wallet.sendTransaction(transactionObject);
  console.log('Transaction hash:', transaction.hash);
}

sendEth("2");
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL"

# Define the sender and receiver addresses, and the private key
sender = '0x19e7e376e7c213b7e7e7e46cc70a5dd086daff2a' 
receiver = '0xe9ba4b4d84d7a3c80245514213b88d50ed937114' 
private_key = '1111111111111111111111111111111111111111111111111111111111111111'   

# Estimate gas limit
gas_limit = web3.eth.estimate_gas({
  'from': sender,
  'to': receiver
})

# Build the transaction object
transaction = {
  'nonce': web3.eth.get_transaction_count(sender),
  'to': receiver,
  'value': web3.to_wei(1, 'ether'), # value to send 
  'gas': gas_limit,
  'gasPrice': web3.eth.gas_price,
  'chainId': web3.eth.chain_id
}

# Sign the transaction
signed_tx = web3.eth.account.sign_transaction(transaction, private_key)
print(f'Signed transaction: {signed_tx}')

# Send the transaction
print('Sending transaction...')
tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction)
print(f'Transaction hash: {web3.to_hex(tx_hash)}')

πŸ“˜

Note that you will need to edit the sender, receiver, and privateKey fields to be able to use this code.

Use case

One potential use case for eth_sendRawTransaction is to automatically swap MATIC for a token when its price reaches a certain level. eth_sendRawTransaction will allow specifying custom transaction parameters, such as gas price or gas limit, 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 that are optimized for the current network conditions.

The following code shows how to call the swapExactETHForTokens function from the SushiSwap exchange on Polygon using ethers.js:

πŸ“˜

Note that this code only shows how to execute the swapExactETHForTokens function on SushiSwap on Polygon mainnet, and not how to track the prices. You need to build a separate part for this.

This particular example shows how to swap MATIC for AAVE tokens.

const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.providers.JsonRpcProvider(NODE_URL);

// ABI for the swapExactETHForTokens function
const sushiRouterABI = [
  {
     "inputs":[
        {
           "internalType":"uint256",
           "name":"amountOutMin",
           "type":"uint256"
        },
        {
           "internalType":"address[]",
           "name":"path",
           "type":"address[]"
        },
        {
           "internalType":"address",
           "name":"to",
           "type":"address"
        },
        {
           "internalType":"uint256",
           "name":"deadline",
           "type":"uint256"
        }
     ],
     "name":"swapExactETHForTokens",
     "outputs":[
        {
           "internalType":"uint256[]",
           "name":"amounts",
           "type":"uint256[]"
        }
     ],
     "stateMutability":"payable",
     "type":"function"
  }
]

async function swapETHForToken(value, token, privateKey) {
  // Create a new wallet using the private key
  const wallet = new ethers.Wallet(privateKey, provider);

  // Get the SushiSwap Router contract and create a contract instance
  const sushiRouterAddress = '0x1b02dA8Cb0d097eB8D57A175b88c7D8b47997506'; // SushiSwap Router address on Polygon Mainnet
  const sushiRouterContract = new ethers.Contract(sushiRouterAddress, sushiRouterABI, wallet);

  // Prepare the swap parameters
  const tokenToBuy = token;
  const amountOfETH = ethers.utils.parseEther(value); // Convert the input value to Wei
  const minAmountOfTokens = ethers.utils.parseUnits('0'); // Set the minimum output token amount to 0, this means that you will always receive something. 
  const deadline = Math.floor(Date.now() / 1000) + 60 * 10; // Set the deadline to 10 minutes from now

  // Define the token path for the swap
  const path = ['0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', tokenToBuy]; // WMATIC to token to buy

  // Parameters for the transaction object
  const nonce = await wallet.getTransactionCount();
  const gasPrice = await provider.getGasPrice();
  const transactionData = sushiRouterContract.interface.encodeFunctionData('swapExactETHForTokens', [
    minAmountOfTokens,
    path,
    wallet.address,
    deadline
  ]);

  // Calculate the gas limit based on the parameters
  const gasLimit = await provider.estimateGas({to: sushiRouterAddress, value: amountOfETH, data: transactionData});

  // Get the chain ID for replay-protected (EIP-155) transactions
  const chainId = await provider.getNetwork().then(network => network.chainId);

  // Build the transaction object
  const transactionObject = {
    to: sushiRouterAddress,
    value: amountOfETH,
    nonce: nonce,
    gasPrice: gasPrice,
    gasLimit: gasLimit,
    data: transactionData,
    chainId: chainId
  };

  // Sign and send the transaction
  console.log(`Swapping tokens...`);
  const signedTx = await wallet.signTransaction(transactionObject);
  const transaction = await provider.sendTransaction(signedTx);
  console.log(`Transaction sent; transaction hash: ${transaction.hash}\n`);

  // Wait for the transaction to be confirmed
  console.log(`Validating transaction...`);
  const receipt = await transaction.wait();
  return receipt
}

async function main() {
  try {
    // Set the swap parameters and private key
    const quantity = '0.2'; // Amount of MATIC to swap
    const addressToBuy = '0xD6DF932A45C0f255f85145f286eA0b292B21C90B'; // Address of the token to buy (AAVE in this case)
    const privateKey = '1111111111111111111111111111111111111111111111111111111111111111'; // Replace with your actual private key

    // Execute the swap and check if successful
    const receipt = await swapETHForToken(quantity, addressToBuy, privateKey);
    
    if (receipt.status === 1) {
      console.log(`The swap was successful!`);
      console.log(`Transaction receipt`, receipt);
      
    } else {
      console.log(`The swap failed with status ${receipt.status}.`);
    }
  } catch (error) {
    console.error(`An error occurred during the swap: ${error.message}`);
  }
}


main();

🚧

Security notice

You need your private key to sign the transaction; never share your private key with anyone.

On a side note, the private key in this case must be imported without '0x' at the beginning of the string.

In the swapETHForToken function, a new wallet is created using the provided privateKey. Then, the instance of the SushiSwap Router contract is obtained using its address and the ABI (Application Binary Interface) for the swapExactETHForTokens function. This function swaps a specified amount of MATIC for a token at the current market price.

The tokenToBuy, amountOfETH, minAmountOfTokens, and deadline variables are used to prepare the swap parameters. The path variable defines the input and output token path for the swap. The nonce, gasPrice, and the transactionData variables define the parameters for the transaction object and encode the swapExactETHForTokens function call with the specified parameters.

The gasLimit variable is calculated based on the parameters, and the chainId variable is obtained for replay-protected (EIP-155) transactions. Then, the transactionObject variable is built, including the to address, the value, the nonce, the gasPrice, the gasLimit, the data, and the chainId.

Finally, the transaction is signed and sent, and the receipt is returned. If the status of the receipt is equal to 1, the swap was successful, and the receipt object is logged to the console. If the status is not equal to 1, the swap failed, and an error message is logged to the console.

Try the eth_sendRawTransaction RPC method yourself

Language
Click Try It! to start a request and see the response here!