Transaction and Balance Monitoring Tools in the CapsureLabs platform allow users to track real-time wallet activity, including balance updates and transaction histories. These tools are designed to support both Ethereum-compatible blockchains and can be extended to support additional chains. This documentation provides code examples using Web3.js to monitor balances and transactions efficiently.
1.2 Code for Monitoring Balances and Transactions
1.2.1 Monitoring Wallet Balance
const Web3 = require('web3');
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/YOUR_INFURA_PROJECT_ID');
// Define the user's wallet address
const walletAddress = '0xYourEthereumAddressHere';
// Function to retrieve the current balance
async function checkBalance() {
try {
const balance = await web3.eth.getBalance(walletAddress);
console.log(`Wallet Balance: ${web3.utils.fromWei(balance, 'ether')} ETH`);
} catch (error) {
console.error("Error fetching balance:", error);
}
}
// Check balance at regular intervals (e.g., every minute)
setInterval(checkBalance, 60000);