Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Monitoring Deployed Contracts
📖 Scenario: You are working as a blockchain developer. You have deployed several smart contracts on a blockchain network. Now, you want to monitor these deployed contracts to check their status and keep track of their addresses.
🎯 Goal: Build a simple Python script that stores deployed contract addresses, sets a monitoring threshold, filters contracts based on the threshold, and prints the filtered list.
📋 What You'll Learn
Create a dictionary with contract names and their deployed block numbers.
Add a variable to set a block number threshold for monitoring.
Filter contracts deployed after the threshold block number.
Print the filtered contracts.
💡 Why This Matters
🌍 Real World
Blockchain developers often need to monitor deployed smart contracts to track their deployment status and ensure they are active on the network.
💼 Career
This project teaches basic data handling and filtering skills useful for blockchain monitoring tools and DevOps automation scripts.
Progress0 / 4 steps
1
Create a dictionary of deployed contracts
Create a dictionary called deployed_contracts with these exact entries: 'TokenA': 1050, 'TokenB': 1105, 'TokenC': 980, 'TokenD': 1150
Blockchain / Solidity
Hint
Use curly braces {} to create a dictionary with keys as contract names and values as block numbers.
2
Set the block number threshold
Create a variable called block_threshold and set it to 1000
Blockchain / Solidity
Hint
Just assign the number 1000 to the variable block_threshold.
3
Filter contracts deployed after the threshold
Create a new dictionary called recent_contracts that contains only the contracts from deployed_contracts where the block number is greater than block_threshold. Use a dictionary comprehension with contract and block as variables.
Blockchain / Solidity
Hint
Use a dictionary comprehension to filter items where block number is greater than block_threshold.
4
Print the filtered contracts
Write a print statement to display the recent_contracts dictionary.
Blockchain / Solidity
Hint
Use print(recent_contracts) to show the filtered contracts.
Practice
(1/5)
1. What is the main purpose of monitoring deployed smart contracts?
easy
A. To track contract activity and events after deployment
B. To write new smart contracts
C. To compile smart contracts before deployment
D. To delete contracts from the blockchain
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 A
Quick Check:
Monitoring = track activity [OK]
Hint: Monitoring means watching contract events after deployment [OK]
Common Mistakes:
Confusing monitoring with writing or compiling contracts
Thinking monitoring deletes contracts
Assuming monitoring happens before deployment
2. Which of the following is the correct syntax to listen for an event named Transfer using Web3.js?
easy
A. contract.on('Transfer', callback);
B. contract.getEvent('Transfer', callback);
C. contract.listen('Transfer', callback);
D. contract.events.Transfer({}, callback);
Solution
Step 1: Recall Web3.js event listening syntax
Web3.js uses contract.events.EventName(options, callback) to listen for events.
Step 2: Match syntax to options
contract.events.Transfer({}, callback); matches this syntax exactly for the Transfer event.
Final Answer:
contract.events.Transfer({}, callback); -> Option D
B. The number of Approval events emitted between block 100 and the latest block
C. The total number of blocks from 100 to the latest
D. The number of contracts deployed after block 100
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 B
Quick Check:
events.length = count of fetched events [OK]
Hint: getPastEvents returns array; length = number of matching events [OK]
Common Mistakes:
Confusing events with blocks or transactions
Thinking length counts blocks or contracts
Assuming it counts all events, not filtered by name
4. You wrote this code to listen for events but it never triggers:
contract.events.Transfer(callback);
What is the likely error?
medium
A. Callback function is not defined
B. Using wrong event name 'Transfer'
C. Missing empty options object before callback
D. Contract is not deployed
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 C
Quick Check:
Event listener syntax needs options object [OK]
Hint: Always include {} before callback in Web3.js event listeners [OK]
Common Mistakes:
Assuming event name is wrong without checking
Ignoring syntax requirements for event listeners
Not defining callback function properly
5. You want to monitor a deployed contract's 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?
hard
A. Use contract.getPastEvents('Deposit', { fromBlock: 5000 }) for past events and contract.events.Deposit() for real-time listening
B. Use contract.events.Deposit({ fromBlock: 5000 }) for real-time and past events together
C. Use contract.events.Deposit() only, it covers past and real-time events
D. Use contract.getPastEvents('Deposit') only, it covers real-time events too
Solution
Step 1: Understand fetching past events
Use getPastEvents with fromBlock to fetch historical events from a specific block.
Step 2: Understand real-time event listening
Use contract.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, call getPastEvents for history, then set up events.Deposit() for live updates.
Final Answer:
Use contract.getPastEvents('Deposit', { fromBlock: 5000 }) for past events and contract.events.Deposit() for real-time listening -> Option A
Quick Check:
Past events + real-time = getPastEvents + events [OK]
Hint: Fetch past with getPastEvents; listen live with events.EventName() [OK]
Common Mistakes:
Trying to get past and live events with one method
Using events with fromBlock to get past events only