Skip to main content
Setting a gas limit yourself caps what a transaction can cost you. If execution needs more gas than the limit, the transaction runs out of gas and reverts instead of spending more than you expected. Libraries like ethers.js and web3.js both let you set it. This guide uses ethers.js.

Prerequisites

  • You understand transactions, gas, and gas pricing on EVM networks.
  • You have Node.js and ethers.js installed.
  • You have a funded account and the peaq RPC endpoint.
  • You keep secrets in environment variables, for example in a .env file.

Instructions

1. Set up the environment

  • Install dependencies:
  • Set ESM module: Add the following to your package.json to allow for ESM modules.
  • Configure environment variables: Create a .env file with the following content:
List your .env file in .gitignore so it is never checked into version control.

2. Set up the provider and wallet

Create a sendTransaction.js file, load the environment variables, initialize the provider with the peaq RPC endpoint, and load the wallet from the private key.

3. Decide on a gas limit

Choose a suitable gas limit. Start with a safe upper bound, and adjust later as needed.

4. Craft the transaction

Include the gasLimit in the transaction object.

5. Send the transaction

Send the transaction and wait for it to be mined. If it runs out of gas, the try/catch block catches the error.

6. Adjust gas limits over time

  • If your transactions run out of gas, raise the limit.
  • If you consistently use much less gas than the limit, lower it.
  • For scripts that send transactions regularly, a predefined gas limit protects you against sudden gas spikes.
  • To set the limit from the transaction itself instead of a fixed number, see Estimating Gas Fees.