Links

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:
mkdir sendTransaction
Change into the new directory:
cd sendTransaction

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:
npm install web3
Install the dotenv package:
npm install dotenv --save

5. Create the .env file

Create a .env file in your project directory to store the project and account details.
POLYGON_NETWORK = "polygon-mumbai"
INFURA_API_KEY = "<API-Key>"
SIGNER_PRIVATE_KEY = "<Private-Key>"
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.
const Web3 = require("web3");
async function main() {
// Configuring the connection to the Polygon node
const network = process.env.POLYGON_NETWORK;
const web3 = new Web3(
new Web3.providers.HttpProvider(
`https://${network}.infura.io/v3/${process.env.INFURA_API_KEY}`
)
);
// Creating a signing account from a private key
const signer = web3.eth.accounts.privateKeyToAccount(
process.env.SIGNER_PRIVATE_KEY
);
web3.eth.accounts.wallet.add(signer);
// Creating the transaction object
const tx = {
from: signer.address,
to: "0xeAD9C93b79Ae7C1591b1FB5323BD777E86e150d4",
value: web3.utils.toWei("0.001"),
};
// Assigning the right amount of gas
tx.gas = await web3.eth.estimateGas(tx);
// Sending the transaction to the network
const receipt = await web3.eth
.sendTransaction(tx)
.once("transactionHash", (txhash) => {
console.log(`Mining transaction ...`);
console.log(`Transaction hash: ${txhash}`);
});
// The transaction is now on chain!
console.log(`Mined in block ${receipt.blockNumber}`);
}
require("dotenv").config();
main();

7. Execute the transaction

To execute the transaction, run:
node sendTx.js
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.