Monitoring deployed contracts in Blockchain / Solidity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When monitoring deployed contracts, we want to know how the time to check contract events grows as more events happen.
We ask: How does monitoring cost change when the number of contract events increases?
Analyze the time complexity of the following code snippet.
// Pseudocode for monitoring contract events
function monitorEvents(contractAddress, fromBlock, toBlock) {
events = blockchain.getEvents(contractAddress, fromBlock, toBlock)
for (event of events) {
process(event)
}
}
This code fetches all events from a contract between two blocks and processes each event one by one.
- Primary operation: Looping through all events returned from the blockchain.
- How many times: Once for each event between fromBlock and toBlock.
As the number of events grows, the time to process them grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 event processings |
| 100 | 100 event processings |
| 1000 | 1000 event processings |
Pattern observation: The work grows directly with the number of events.
Time Complexity: O(n)
This means the time to monitor events grows in a straight line with the number of events.
[X] Wrong: "Monitoring events takes the same time no matter how many events there are."
[OK] Correct: Each event must be processed, so more events mean more work and more time.
Understanding how monitoring scales helps you design systems that stay fast as data grows, a key skill in real projects.
"What if we only monitor events from the last 10 blocks instead of a large range? How would the time complexity change?"
Practice
Solution
Step 1: Understand contract deployment
Once a smart contract is deployed, it runs on the blockchain and can emit events or change state.Step 2: Purpose of monitoring
Monitoring helps track these events and state changes to stay informed and debug issues.Final Answer:
To track contract activity and events after deployment -> Option AQuick Check:
Monitoring = track activity [OK]
- Confusing monitoring with writing or compiling contracts
- Thinking monitoring deletes contracts
- Assuming monitoring happens before deployment
Transfer using Web3.js?Solution
Step 1: Recall Web3.js event listening syntax
Web3.js usescontract.events.EventName(options, callback)to listen for events.Step 2: Match syntax to options
contract.events.Transfer({}, callback); matches this syntax exactly for theTransferevent.Final Answer:
contract.events.Transfer({}, callback); -> Option DQuick Check:
Web3.js event listener = contract.events.EventName [OK]
- Using .on() which is for ethers.js, not Web3.js
- Using .listen() which is invalid
- Using .getEvent() which does not exist
const events = await contract.getPastEvents('Approval', { fromBlock: 100, toBlock: 'latest' });
console.log(events.length);What does
events.length represent?Solution
Step 1: Understand getPastEvents usage
The method fetches all events named 'Approval' emitted by the contract between specified blocks.Step 2: Meaning of events.length
The length of the returned array is the count of those events found in that block range.Final Answer:
The number of Approval events emitted between block 100 and the latest block -> Option BQuick Check:
events.length = count of fetched events [OK]
- Confusing events with blocks or transactions
- Thinking length counts blocks or contracts
- Assuming it counts all events, not filtered by name
contract.events.Transfer(callback);
What is the likely error?
Solution
Step 1: Check Web3.js event listener syntax
The correct syntax requires an options object before the callback, even if empty.Step 2: Identify missing options object
The code lacks the empty object{}before the callback, so the event listener does not register properly.Final Answer:
Missing empty options object before callback -> Option CQuick Check:
Event listener syntax needs options object [OK]
- Assuming event name is wrong without checking
- Ignoring syntax requirements for event listeners
- Not defining callback function properly
Deposit events in real time and also fetch all past Deposit events from block 5000 onwards. Which approach correctly combines both tasks using Web3.js?Solution
Step 1: Understand fetching past events
UsegetPastEventswithfromBlockto fetch historical events from a specific block.Step 2: Understand real-time event listening
Usecontract.events.Deposit()without block filters to listen for new events as they happen.Step 3: Combine both methods
To monitor both past and real-time events, callgetPastEventsfor history, then set upevents.Deposit()for live updates.Final Answer:
Use contract.getPastEvents('Deposit', { fromBlock: 5000 }) for past events and contract.events.Deposit() for real-time listening -> Option AQuick Check:
Past events + real-time = getPastEvents + events [OK]
- Trying to get past and live events with one method
- Using events with fromBlock to get past events only
- Assuming getPastEvents listens for new events
