@solana/web3.js
: This Solana JavaScript library provides the functionality to interact with the Solana blockchain from a JavaScript application.dotenv
: A utility package that loads environment variables from a .env
file into process.env
, helping securely manage sensitive information like RPC URLs.package.json
file:
.env
file in the root of your project directory..env
file. For instance:.env
file or any files containing sensitive data to version control. Make sure to include the .env
file in your .gitignore
.index.js
and paste the following code into it.
@solana/web3.js
: The Solana JavaScript library provides the necessary functionality to interact with the Solana blockchain.dotenv/config
: Automatically loads environment variables from a .env
file into process.env
, securing sensitive information such as RPC URLs.ConnectionPool
classConnection
object for each. These connections are stored for subsequent use in listening for account changes.
connectionPool
. Each connection attempts to establish a listener that triggers on account changes, capturing new balance amounts. It logs the balance change along with a timestamp, ensuring only new changes are considered, thanks to the shared state tracking the latest balance.
ConnectionPool
with these endpoints. It specifies a public key to monitor and invokes listenForBalanceChanges
to monitor balance updates across the network. It demonstrates a practical use case for real-time balance tracking on the Solana blockchain.
.env
file, then run the start command in the console: