eth_sendTransaction
and eth_sendRawTransaction
?eth_sendTransaction
and eth_sendRawTransaction
are both Ethereum API functions which broadcast a transaction to the Ethereum network so it will be added to a future block. They differ in how they handle signing of the transactions.eth_sendTransaction
is used for sending unsigned transactions, which means the node you are sending to must manage your private key so it can sign the transaction before broadcasting it to the chain. Since Alchemy doesn't hold user's private keys, we do not support this method.eth_sendRawTransaction
is used to broadcast transactions that have already been signed. This means you first have to usesignTransaction(tx, private_key)
, then pass in the result into eth_sendRawTransaction
.eth_sendRawTransaction
is accessed by calling the function web3.eth.sendSignedTransaction. This is what we will be using in our tutorial.cd
into itdotenv
actually works in the context of a conventional NodeJS server file, check out this helpful video!.env
, nothing more) in your project directory and add the following (replacing your-api-key
and your-private-key
, keeping both within the quotation marks):sendTx.js
filesendTx.js
file, which is where we will configure and send our example transaction, and add the following lines of code to it:nonce
: The nonce specification is used to keep track of the number of transactions sent from your address. We need this for security purposes and to prevent replay attacks. To get the number of transactions sent from your address we use getTransactionCount.transaction
: The transaction object has a few aspects we need to specifyto
: This is the address we want to send Eth to. In this case, we are sending Eth back to the Rinkeby faucet we initially requested from.value
: This is the amount we wish to send, specified in wei where 10^18 wei = 1 ETHgas
: There are many ways to determine the right amount of gas to include with your transaction. Alchemy even has a gas price webhook to notify you when the gas price falls within a certain threshold. For mainnet transactions, it's good practice to check a gas estimator like Eth Gas Station to determine the right amount of gas to include. 21000 is the minimum amount of gas an operation on Ethereum will use, so to ensure our transaction will be executed we put 30000 here.maxFeePerGas
: This is the amount you are willing to pay per gas for the transaction to execute. Since EIP 1559, this field or the maxPriorityFeePerGas
field is required.nonce
: see above nonce definition. Nonce starts counting from zero.data
: Used for sending additional information with your transfer, or calling a smart contract, not required for balance transfers, check out the note below.signedTx
: To sign our transaction object we will use the signTransaction
method with our PRIVATE_KEY
sendSignedTransaction
: Once we have a signed transaction, we can send it off to be included in a subsequent block by using sendSignedTransaction
data
data
field required, however, if you'd like to send additional information alongside your transaction, you can include that information in HEX format in this field.data: web3.utils.toHex(‘IPFS hash‘)
. And now anyone can query the chain and see when that document was added.data
field should contain the smart function you wish to execute, alongside any parameters.node sendTx.js
0x31b98d14007bdee637298086988a0bbd31184523