Skip to main content
A transaction is required every time you write data to peaq: transferring funds, calling a smart contract, or deploying one. Your code builds the transaction and signs it. This guide does that with ethers.js.

Prerequisites

  • Node.js installed, and npm install ethers dotenv in your project.
  • A funded account and its private key in an environment variable. Never put a private key or mnemonic in your source code.
  • The RPC URL of the network you are using. See Connecting to peaq.
  • The transaction details: recipient address, value, and call data if you are calling a contract.

Instructions

1. Import ethers.js

In your JavaScript file, import ethers.js:

2. Set up a provider

Connect to the network:

3. Configure a signer

Create a wallet instance to sign transactions:

4. Define the transaction

Set up the transaction details:
Leave maxFeePerGas and maxPriorityFeePerGas out of the object and ethers reads the current fees from the node when it sends the transaction. To call a smart contract, add data.

5. Sign and submit the transaction

6. Verify the transaction

Look up txResponse.hash on Subscan, or check the status in your code with provider.getTransactionReceipt.

Contracts

For a contract call, build the contract instance from its ABI and address and connect it to the wallet:
To estimate the gas a contract call needs, use contract.methodName.estimateGas(...args).

Putting it all Together

A complete script that ties the steps together, including environment variable handling.

Running the script

  1. Add "type": "module" to your package.json so Node treats the file as an ES module.
  2. Create a .env file in the root of your project:
  1. Replace 0xRecipientAddressHere with the recipient’s address and adjust value.
  2. Run it: