Listening to events (frontend) in Blockchain / Solidity - Time & Space Complexity
When a frontend listens to blockchain events, it waits for messages from the blockchain. Analyzing time complexity helps us understand how the work grows as more events happen.
We want to know how the number of events affects the time the frontend spends processing them.
Analyze the time complexity of the following code snippet.
const contract = new ethers.Contract(address, abi, provider);
contract.on('Transfer', (from, to, amount) => {
console.log(`Transfer from ${from} to ${to} of ${amount}`);
updateUI(from, to, amount);
});
This code listens for 'Transfer' events from a blockchain contract and updates the user interface each time an event occurs.
- Primary operation: The event handler runs once for each 'Transfer' event emitted by the blockchain.
- How many times: It runs as many times as there are events, which depends on blockchain activity and user interaction.
Each new event triggers the handler once, so the total work grows directly with the number of events.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 events | 10 handler calls |
| 100 events | 100 handler calls |
| 1000 events | 1000 handler calls |
Pattern observation: The work grows in a straight line as events increase.
Time Complexity: O(n)
This means the time to process events grows directly in proportion to the number of events received.
[X] Wrong: "The event listener runs only once, so time does not grow with more events."
[OK] Correct: The listener runs every time an event happens, so more events mean more calls and more work.
Understanding how event listeners scale helps you build responsive apps that handle real-time data smoothly. This skill shows you can think about performance in interactive blockchain apps.
"What if the event handler also loops through a list of transactions each time it runs? How would the time complexity change?"
Practice
Solution
Step 1: Understand event listening in frontend
Listening to events means the app waits and reacts when blockchain data changes.Step 2: Identify the purpose of event listening
The main goal is to update the app instantly when something happens on the blockchain.Final Answer:
To react instantly when something changes on the blockchain -> Option CQuick Check:
Listening to events = instant reaction [OK]
- Confusing event listening with sending transactions
- Thinking event listening creates contracts
- Mixing event listening with mining blocks
Transfer using ethers.js?Solution
Step 1: Recall ethers.js event listening methods
Usecontract.onto listen continuously to events.Step 2: Match method to event name
For event 'Transfer', the syntax iscontract.on('Transfer', callback).Final Answer:
contract.on('Transfer', callback) -> Option DQuick Check:
Continuous listening = contract.on [OK]
- Using contract.once for continuous listening
- Using non-existent methods like listen or watch
- Confusing method names
contract.once('Approval', (owner, spender, value) => {
console.log(`Owner: ${owner}`);
console.log(`Spender: ${spender}`);
console.log(`Value: ${value}`);
});
// Event emitted: Approval('0xabc', '0xdef', 100)What will be printed to the console?
Solution
Step 1: Understand contract.once behavior
contract.once listens for the next single event and runs the callback once.Step 2: Match event parameters to callback
The event emits Approval('0xabc', '0xdef', 100), so owner='0xabc', spender='0xdef', value=100.Final Answer:
Owner: 0xabc\nSpender: 0xdef\nValue: 100 -> Option AQuick Check:
contract.once prints event data once [OK]
- Swapping owner and spender values
- Thinking contract.once never prints
- Missing the value parameter in output
Deposit event:contract.on('Deposit', (user, amount) => {
console.log(`User ${user} deposited ${amount}`);
});But your app never logs anything even though deposits happen. What is the most likely mistake?
Solution
Step 1: Check event name and ABI
If the event name is wrong or missing in ABI, the listener won't trigger.Step 2: Verify other common issues
Provider connection and import errors usually cause runtime errors, not silent no logs.Final Answer:
The event name 'Deposit' is misspelled or does not exist in ABI -> Option AQuick Check:
Correct event name in ABI = logs appear [OK]
- Assuming contract.once listens continuously
- Ignoring ABI event definitions
- Not connecting provider causes errors, not silent failure
Transfer event but only react to transfers where the from address is your wallet address 0x123. Which approach correctly filters these events using ethers.js?Solution
Step 1: Understand event filtering in ethers.js frontend
Direct filtering by event parameters incontract.onis not supported; filtering must be done inside the callback.Step 2: Choose correct method to filter by 'from'
Usecontract.on('Transfer', callback)and inside callback check iffrom === '0x123'.Final Answer:
contract.on('Transfer', (from, to, amount) => { if (from === '0x123') { /* react */ } }) -> Option BQuick Check:
Filter inside callback for specific addresses [OK]
- Trying to filter events by passing object to contract.on
- Using contract.once for continuous filtering
- Using non-existent filter chaining methods
Ready to prove it?
Pass the test and this pattern moves unknown
