Bird
Raised Fist0
Blockchain / Solidityprogramming~20 mins

Monitoring deployed contracts in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Master of Monitoring Deployed Contracts
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this command monitoring a deployed contract event?
You run the command to listen for Transfer events on an ERC-20 token contract:
web3.eth.subscribe('logs', {address: '0xTokenAddress', topics: [web3.utils.sha3('Transfer(address,address,uint256)')]})

What will this command output when a transfer happens?
Blockchain / Solidity
web3.eth.subscribe('logs', {address: '0xTokenAddress', topics: [web3.utils.sha3('Transfer(address,address,uint256)')]})
AA boolean true indicating subscription success
BA list of all past Transfer events from the contract
CA syntax error because 'topics' is not a valid parameter
DAn event log object containing transaction hash, block number, and data about the transfer
Attempts:
2 left
💡 Hint
Think about what 'subscribe' does in web3.js for event logs.
Configuration
intermediate
2:00remaining
Which configuration correctly sets up a Prometheus exporter for Ethereum node metrics?
You want to monitor your Ethereum node using Prometheus. Which Prometheus scrape config snippet correctly scrapes metrics from a node exposing metrics at http://localhost:9545/metrics?
A
scrape_configs:
  - job_name: 'eth_node'
    metrics_path: '/metrics'
    static_configs:
      - targets: ['localhost:9545']
B
scrape_configs:
  - job_name: 'eth_node'
    static_configs:
      - targets: ['http://localhost:9545/metrics']
C
scrape_configs:
  - job_name: 'eth_node'
    static_configs:
      - targets: ['localhost']
D
scrape_configs:
  - job_name: 'eth_node'
    static_configs:
      - targets: ['localhost:9545']
Attempts:
2 left
💡 Hint
Prometheus targets should be host:port without protocol, and metrics_path is set separately.
Troubleshoot
advanced
2:00remaining
Why does your contract event listener miss some events?
You wrote a script to listen to contract events using web3.js subscription, but it misses some events when the node restarts. What is the most likely cause?
AThe subscription only listens to new events after connection; missed events during downtime are lost
BThe contract ABI is outdated and cannot decode events correctly
CThe node is not synced fully and cannot provide event data
DThe web3.js version does not support event subscriptions
Attempts:
2 left
💡 Hint
Think about what happens to event subscriptions when the connection drops.
🔀 Workflow
advanced
2:00remaining
What is the correct workflow to monitor contract state changes reliably?
You want to monitor a deployed smart contract's state changes and ensure no events are missed even if your monitoring service restarts. Which workflow is best?
AUse event subscriptions only and restart them on service start
BOnly query the current contract state on demand without event listening
CPeriodically query past events from the last known block and combine with live subscriptions
DUse a centralized logging service that stores all blockchain transactions
Attempts:
2 left
💡 Hint
Consider how to handle downtime and missed events.
Best Practice
expert
3:00remaining
Which practice best improves monitoring scalability for many deployed contracts?
You manage monitoring for hundreds of deployed contracts on Ethereum. What is the best practice to scale event monitoring efficiently?
ARun one full node per contract to isolate event streams
BUse a centralized event indexing service like The Graph to query events
CSubscribe to all events on the main node and filter in your application
DPoll each contract's state every second to detect changes
Attempts:
2 left
💡 Hint
Think about reducing load and complexity on your own infrastructure.

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

  1. Step 1: Understand contract deployment

    Once a smart contract is deployed, it runs on the blockchain and can emit events or change state.
  2. Step 2: Purpose of monitoring

    Monitoring helps track these events and state changes to stay informed and debug issues.
  3. Final Answer:

    To track contract activity and events after deployment -> Option A
  4. 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

  1. Step 1: Recall Web3.js event listening syntax

    Web3.js uses contract.events.EventName(options, callback) to listen for events.
  2. Step 2: Match syntax to options

    contract.events.Transfer({}, callback); matches this syntax exactly for the Transfer event.
  3. Final Answer:

    contract.events.Transfer({}, callback); -> Option D
  4. Quick Check:

    Web3.js event listener = contract.events.EventName [OK]
Hint: Web3.js event listeners use contract.events.EventName() [OK]
Common Mistakes:
  • Using .on() which is for ethers.js, not Web3.js
  • Using .listen() which is invalid
  • Using .getEvent() which does not exist
3. Given this code snippet using Web3.js to fetch past events:
const events = await contract.getPastEvents('Approval', { fromBlock: 100, toBlock: 'latest' });
console.log(events.length);

What does events.length represent?
medium
A. The number of transactions in block 100
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

  1. Step 1: Understand getPastEvents usage

    The method fetches all events named 'Approval' emitted by the contract between specified blocks.
  2. Step 2: Meaning of events.length

    The length of the returned array is the count of those events found in that block range.
  3. Final Answer:

    The number of Approval events emitted between block 100 and the latest block -> Option B
  4. 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

  1. Step 1: Check Web3.js event listener syntax

    The correct syntax requires an options object before the callback, even if empty.
  2. Step 2: Identify missing options object

    The code lacks the empty object {} before the callback, so the event listener does not register properly.
  3. Final Answer:

    Missing empty options object before callback -> Option C
  4. 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

  1. Step 1: Understand fetching past events

    Use getPastEvents with fromBlock to fetch historical events from a specific block.
  2. Step 2: Understand real-time event listening

    Use contract.events.Deposit() without block filters to listen for new events as they happen.
  3. 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.
  4. Final Answer:

    Use contract.getPastEvents('Deposit', { fromBlock: 5000 }) for past events and contract.events.Deposit() for real-time listening -> Option A
  5. 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
  • Assuming getPastEvents listens for new events