0
0
Blockchain / Solidityprogramming~5 mins

Monitoring deployed contracts in Blockchain / Solidity

Choose your learning style9 modes available
Introduction

Monitoring deployed contracts helps you know what is happening with your smart contracts after they are live. It keeps you informed about their activity and health.

You want to track transactions interacting with your smart contract.
You need to check if your contract is working as expected after deployment.
You want to get alerts if something unusual happens with your contract.
You want to gather data for analytics about contract usage.
You want to debug or troubleshoot issues in your deployed contract.
Syntax
Blockchain / Solidity
Use blockchain explorer APIs or event listeners in your code to watch contract events and transactions.

Example using Web3.js event subscription:
contract.events.EventName({fromBlock: 'latest'})
.on('data', event => console.log(event))
.on('error', console.error);

Replace EventName with the actual event your contract emits.

Listening from latest block means you only get new events after starting the listener.

Examples
This listens to all Transfer events from the start of the blockchain and logs the details.
Blockchain / Solidity
contract.events.Transfer({fromBlock: 0})
.on('data', event => console.log(event.returnValues));
This fetches past Approval events from block 1,000,000 to the latest block.
Blockchain / Solidity
contract.getPastEvents('Approval', {fromBlock: 1000000, toBlock: 'latest'})
.then(events => console.log(events));
This sets up a live subscription to new MyEvent events and logs them as they happen.
Blockchain / Solidity
const subscription = contract.events.MyEvent({fromBlock: 'latest'})
.on('data', event => {
  console.log('New event:', event);
});
Sample Program

This code connects to the Ethereum mainnet using Infura, sets up a contract instance, and listens for new Transfer events. When a transfer happens, it prints who sent tokens, who received them, and how many.

Blockchain / Solidity
import Web3 from 'web3';

const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
const contractAddress = '0xYourContractAddress';
const abi = [ /* contract ABI here */ ];

const contract = new web3.eth.Contract(abi, contractAddress);

contract.events.Transfer({fromBlock: 'latest'})
.on('data', event => {
  console.log(`Transfer from ${event.returnValues.from} to ${event.returnValues.to} of ${event.returnValues.value} tokens.`);
})
.on('error', console.error);
OutputSuccess
Important Notes

Make sure your contract ABI matches the deployed contract to decode events correctly.

Use reliable node providers like Infura or Alchemy to avoid missing events.

Event subscriptions may disconnect; handle reconnections in production code.

Summary

Monitoring deployed contracts lets you track contract activity in real time or from past blocks.

You can use event listeners or fetch past events using blockchain libraries like Web3.js.

This helps you stay informed, debug, and analyze your smart contracts after deployment.