Signing Raw Transactions
Last updated
Last updated
The closest real world example of a raw transaction — probably a bank cheque 💸
Some ways we can utilize raw transactions include signing a transaction offline on a secure machine and then broadcasting it separately or to send a transaction at a later point of time.
You will need Node.js installed and a valid Gnosisscan API Key.
In a new folder, initiate a new Node.js project with the command npm init -y
to accept all default project parameters.
A package.json
file will be created for you, which contains all your packages and project information.
We'll need to integrate a helpful JavaScript library known as Ethers.js that will help with interacting with the Ethereum blockchain.
We can do so using the command npm i ethers
from a terminal within this project, which will add ethers
as a dependency.
We'll then create a new file named transaction.js
where we'll write the rest of the code in JavaScript and import ethers
at the top of the file.
One of the useful classes that Ethers.js provides is a Wallet
, which represents a regular Ethereum address that you can use to store and send xDAI.
We can initiate a new Wallet
by specifying a private key which we can generate or grab one from an existing wallet like MetaMask.
A transactions is made up of several parameters that have to be defined, for this example we'll be making a simple xDAI transfer.
to
the address
to send xDAI to
value
the amount of xDAI to send
gasLimit
the maximum units of gas to consume in this transaction, set to 21000
for basic xDAI transfers
maxPriorityFeePerGas
the tip paid to miners, introduced in EIP-1559
maxFeePerGas
the maximum price paid per unit of gas, introduced in EIP-1559
nonce
the number of transactions sent from the address
type
set to 0x2
, to denote EIP-1559 type transactions
chainId
the chain ID to send the transaction, for example 100
for Gnosis
The sample code to send 1 xDAI
to address 0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd
on the Gnosis mainnet will be as follows.
After detailing the contents of the transaction, we'll need to sign it — using the wallet's private key we created in Step 3 to prove we are allowed to spend funds in the wallet address.
With the transaction signed, we have just one more step to serialize, or simply converting our transaction into a hexadecimal format.
With the signed raw transaction, we can now pass it to the "eth_sendRawTransaction" endpoint to be broadcasted to the Gnosis network.
A successfully broadcasted transaction will return a transaction hash, which you can use the "eth_getTransactionbyHash" endpoint or look it up on Gnosisscan!
You can run this code using the command node transaction.js
The full sample code for this tutorial is on Github, feel free to fork and extend the functionality of it ! 🤖
If you are following this article, feel free to get some xDAI from a faucet to fund your address.