Send a transaction with Polygon
As with Ethereum, transactions are state-changing actions on the Polygon PoS blockchain. Examples of transactions are:
  • Regular transactions from one account to another.
  • Contract deployment transactions, or calling a function in a smart contract.
This tutorial uses the Ethereum Web3 JavaScript library to send a transaction between two accounts on the Polygon-Mumbai testnet.
You can also use Infura Transactions (ITX) to send transactions on Polygon Mainnet. ITX is not supported on the Polygon Mumbai testnet.

Prerequisites

You can use MetaMask or a similar Ethereum wallet to create an account for testing purposes on the Polygon blockchain.

Steps

1. Add Polygon network to MetaMask

Refer to the Polygon instruction to add the Polygon networks to MetaMask. This tutorial uses the Polygon-Mumbai network.

2. Fund your account

Use the Polygon faucet to load testnet MATIC on your account for the Mumbai network.

3. Create a project directory

Create a new directory for your project. This can be done from the command line:
1
mkdir sendTransaction
Copied!
Change into the new directory:
1
cd sendTransaction
Copied!

4. Install required packages

Install the web3 and dotenv packages in the project directory.
The dotenv package allows you to use a .env file to securely store private environment variables on your local machine.
Install the web3 package:
1
npm install web3
Copied!
Install the dotenv package:
1
npm install dotenv --save
Copied!

5. Create the .env file

Create a .env file in your project directory to store the project and account details.
1
POLYGON_NETWORK = "polygon-mumbai"
2
INFURA_PROJECT_ID = "<Project-ID>"
3
SIGNER_PRIVATE_KEY = "<Private-Key>"
Copied!
Ensure you replace the following values in the .env file:
Never disclose your private key. Anyone with your private keys can steal any assets held in your account.

6. Create send.js file

In this example we'll create a JavaScript file (send.js) in the project directory which configures and sends the transaction.
1
const Web3 = require("web3");
2
3
async function main() {
4
// Configuring the connection to the Polygon node
5
const network = process.env.POLYGON_NETWORK;
6
const web3 = new Web3(
7
new Web3.providers.HttpProvider(
8
`https://${network}.infura.io/v3/${process.env.INFURA_PROJECT_ID}`
9
)
10
);
11
// Creating a signing account from a private key
12
const signer = web3.eth.accounts.privateKeyToAccount(
13
process.env.SIGNER_PRIVATE_KEY
14
);
15
web3.eth.accounts.wallet.add(signer);
16
// Creating the transaction object
17
const tx = {
18
from: signer.address,
19
to: "0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4",
20
value: web3.utils.toWei("0.001"),
21
};
22
// Assigning the right amount of gas
23
tx.gas = await web3.eth.estimateGas(tx);
24
25
// Sending the transaction to the network
26
const receipt = await web3.eth
27
.sendTransaction(tx)
28
.once("transactionHash", (txhash) => {
29
console.log(`Mining transaction ...`);
30
console.log(`Transaction hash: ${txhash}`);
31
});
32
// The transaction is now on chain!
33
console.log(`Mined in block ${receipt.blockNumber}`);
34
}
35
36
require("dotenv").config();
37
main();
Copied!

7. Execute the transaction

To execute the transaction, run:
1
node sendTx.js
Copied!
The command line will display a link to view the transaction details.

8. View the transaction details

Copy transaction hash and view the transaction in the Polygon Mumbai block explorer.