Gnosisscan
HomeTwitterKnowledge Base
  • Introduction
  • ✨Getting Started
    • Creating an Account
    • Getting an API key
    • Endpoint URLs
  • 🎯API Endpoints
    • Accounts
    • Contracts
    • Transactions
    • Blocks
    • Logs
    • Geth Proxy
    • Tokens
    • Stats
  • 📖Tutorials
    • Signing Raw Transactions
    • Read/Write Smart Contracts
    • Verifying Contracts Programmatically
  • 🏆API PRO
    • API PRO
    • Using API PRO
  • 🤝Support
    • FAQ
    • Rate Limits
    • Common Error Messages
    • Getting Help
  • Visit Gnosisscan.io
Powered by GitBook
On this page
  • 1. Setting up a Node.js project
  • 2. Integrating Ethers.js
  • 3. Creating a wallet
  • 4. Crafting the transaction
  • 5. Sign & serialize
  • 6. Send via API
  • Wrapping Up
  1. Tutorials

Signing Raw Transactions

PreviousStatsNextRead/Write Smart Contracts

Last updated 1 year ago

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 installed and a valid .

1. Setting up a Node.js project

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.

2. Integrating Ethers.js

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.

const ethers = require('ethers');

async function main() {

  // rest of code goes here

}

main();

3. Creating a wallet

const ethers = require('ethers');

async function main() {

  // defining the wallet private key
  let privatekey = 'CE75F1A875F2DB7FB064F5DBD302B0C77FFEAA18CC4C314167A5111A04F79AFA';
  let wallet = new ethers.Wallet(privatekey);

  // print the wallet address
  console.log('Using wallet address ' + wallet.address);
  
}

main();

4. Crafting the transaction

A transactions is made up of several parameters that have to be defined, for this example we'll be making a simple xDAI transfer.

Parameter
Description

to

the address to send xDAI to

value

the amount of xDAI to send

gasLimit

maxPriorityFeePerGas

maxFeePerGas

nonce

type

set to 0x2, to denote EIP-1559 type transactions

chainId

The sample code to send 1 xDAI to address 0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd on the Gnosis mainnet will be as follows.

const ethers = require('ethers');

async function main() {

  let privatekey = 'CE75F1A875F2DB7FB064F5DBD302B0C77FFEAA18CC4C314167A5111A04F79AFA';
  let wallet = new ethers.Wallet(privatekey);

  console.log('Using wallet address ' + wallet.address);

  let transaction = {
    to: '0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd',
    value: ethers.utils.parseEther('1'),
    gasLimit: '21000',
    maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
    maxFeePerGas: ethers.utils.parseUnits('20', 'gwei'),
    nonce: 1,
    type: 2,
    chainId: 100
  };

}

main();

5. Sign & serialize

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.

const ethers = require('ethers');

async function main() {

  let privatekey = 'CE75F1A875F2DB7FB064F5DBD302B0C77FFEAA18CC4C314167A5111A04F79AFA';
  let wallet = new ethers.Wallet(privatekey);

  console.log('Using wallet address ' + wallet.address);

  let transaction = {
    to: '0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd',
    value: ethers.utils.parseEther('1'),
    gasLimit: '21000',
    maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
    maxFeePerGas: ethers.utils.parseUnits('20', 'gwei'),
    nonce: 1,
    type: 2,
    chainId: 100
  };

  // sign and serialize the transaction 
  let rawTransaction = await wallet.signTransaction(transaction).then(ethers.utils.serializeTransaction(transaction));
  
  // print the raw transaction hash
  console.log('Raw txhash string ' + rawTransaction);

}

main();

6. Send via API

You can run this code using the command node transaction.js

const ethers = require('ethers');

async function main() {

  let privatekey = '<privatekey>';
  let wallet = new ethers.Wallet(privatekey);

  console.log('Using wallet address ' + wallet.address);

  let transaction = {
    to: '0xa238b6008Bc2FBd9E386A5d4784511980cE504Cd',
    value: ethers.utils.parseEther('1'),
    gasLimit: '21000',
    maxPriorityFeePerGas: ethers.utils.parseUnits('5', 'gwei'),
    maxFeePerGas: ethers.utils.parseUnits('20', 'gwei'),
    nonce: 2,
    type: 2,
    chainId: 100,
  };

  let rawTransaction = await wallet.signTransaction(transaction).then(ethers.utils.serializeTransaction(transaction));
  console.log('Raw txhash string ' + rawTransaction);

  // pass the raw transaction hash to the "eth_sendRawTransaction" endpoint
  let gethProxy = await fetch(`https://api.gnosisscan.io/api?module=proxy&action=eth_sendRawTransaction&hex=${rawTransaction}&apikey=YourApiKeyToken`);    
  let response = await gethProxy.json();    
     
  // print the API response
  console.log(response);
  
}

main();

Wrapping Up

We'll need to integrate a helpful JavaScript library known as that will help with interacting with the Ethereum blockchain.

One of the useful classes that 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 or grab one from an existing wallet like

If you are following this article, feel free to get some xDAI from a to fund your address.

the maximum to consume in this transaction, set to 21000 for basic xDAI transfers

the tip paid to miners, introduced in

the maximum price paid per unit of gas, introduced in

the sent from the address

the to send the transaction, for example 100 for Gnosis

With the signed raw transaction, we can now pass it to the "" endpoint to be broadcasted to the Gnosis network.

A successfully broadcasted transaction will return a transaction hash, which you can use the "" endpoint or look it up on Gnosisscan!

The full sample code for this tutorial is on , feel free to fork and extend the functionality of it !

📖
Ethers.js
Ethers.js
generate
MetaMask.
🤖
Github
units of gas
EIP-1559
EIP-1559
number of transactions
chain ID
💸
Node.js
Gnosisscan API Key
faucet
eth_sendRawTransaction
eth_getTransactionbyHash