eth_sendRawTransaction | Avalanche
Avalanche 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 Avalanche 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 an AVAX transfer on the Fuji testnet.
Additional libraries requirement
Note that web3.js requires installing two extra libraries:
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 AVAX 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 Avalanche using ethers.js:
Note that this code only shows how to execute the swapExactETHForTokens
function on SushiSwap on Avalanche mainnet, and not how to track the prices. You need to build a separate part for this.
This particular example shows how to swap AVAX for AAVE tokens.
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 AVAX 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.
Body
The signed transaction.