Write Your First Transaction

This guide will walk you through the process of sending your first transaction on the Pharos blockchain using three popular libraries: ethers.js, web3.js, and web3.py. By the end of this guide, you will understand how to send a transaction programmatically using these libraries.

Prerequisites

Before you begin, ensure you have the following:

  • Git: Used for code management and obtain examples.

  • Pharos Devnet/Testnet Access: Access to a Pharos node (local or remote) for interacting with the blockchain.

Setup 1: Pick Your Favorite Contract Develop Framework and IDE

Install your preferred contract framework from the below list:

  • ethers.js

  • web3.js

  • web3.py

Setup 2: Set Up the Project

Clone the example repo and add dependencies.

git clone https://github.com/PharosNetwork/examples
cd examples/transaction/ethersjs
npm install
const { ethers } = require("ethers");

// Connect to Pharos Testnet
const provider = new ethers.providers.JsonRpcProvider("<PHAROS_RPC_URL>");

// Note: Your wallet private key (for testnet only, never expose this in production)
const privateKey = "YOUR_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);

// Transaction details
const tx = {
    to: "RECIPIENT_ADDRESS", // Note: Replace with the recipient's address
    value: ethers.utils.parseEther("0.1"), // Amount to send (0.1 PHAR)
};

// Send the transaction
wallet.sendTransaction(tx)
    .then((transaction) => {
        console.log("Transaction sent:", transaction.hash);
    })
    .catch((error) => {
        console.error("Error sending transaction:", error);
    });

Step 3: Run the Script

Run the script using Node.js:

node index.js

Key Points

  • Private Key: Never expose your private key in production. Use environment variables or secure storage.

  • Recipient Address: Replace RECIPIENT_ADDRESS with the actual recipient's wallet address.

  • Gas Limit: Ensure you include a sufficient gas limit for the transaction.

Troubleshooting

  • Transaction Fails: Ensure you have enough testnet tokens to cover the transaction fee.

  • Incorrect Network: Double-check that you are connected to the Pharos Testnet.

  • Invalid Address: Verify that the recipient's address is correct.

Conclusion

Now that you’ve sent your first transaction using ethers.js, web3.js, and web3.py.

This guide provides a comprehensive introduction to sending transactions on the Pharos blockchain using three popular libraries. If you encounter any issues, refer to the Troubleshooting section or consult the respective library's documentation. Happy building! 🚀

Last updated