0
0
Blockchain / Solidityprogramming~20 mins

Assertion patterns in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Assertion Master - Blockchain
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2:00remaining
Identify the correct assertion for checking event emission
In Solidity testing, which assertion correctly verifies that the Transfer event was emitted with the expected from and to addresses?
Blockchain / Solidity
event Transfer(address indexed from, address indexed to, uint256 value);

// Test snippet
// Assume 'tx' is the transaction receipt after calling a transfer function
Aassert(tx.logs[0].event == 'Transfer' && tx.logs[0].args.from == sender && tx.logs[0].args.to == receiver);
BassertEvent(tx, 'Transfer', {from: sender, to: receiver});
Cassert(tx.TransferEvent.from == sender && tx.TransferEvent.to == receiver);
Dassert(tx.events.Transfer.from == sender && tx.events.Transfer.to == receiver);
Attempts:
2 left
💡 Hint
Look for the correct way to access event logs and their arguments in transaction receipts.
assertion
intermediate
2:00remaining
Choose the assertion that correctly checks a revert reason
Which assertion pattern correctly verifies that a Solidity function call reverts with the reason 'Insufficient balance'?
Blockchain / Solidity
// Assume 'contractInstance' is the deployed contract
// and 'callFunction' is a function expected to revert
Aawait expectRevert(contractInstance.callFunction(), 'Insufficient balance');
Bexpect(contractInstance.callFunction()).to.be.revertedWith('Insufficient balance');
CassertRevert(contractInstance.callFunction(), 'Insufficient balance');
DassertThrows(contractInstance.callFunction(), 'Insufficient balance');
Attempts:
2 left
💡 Hint
Consider the syntax used by the Chai assertion library with Ethereum Waffle or Hardhat.
Predict Output
advanced
2:00remaining
What is the assertion outcome of this balance check?
Given the following Solidity test snippet, what will be the result of the assertion?
Blockchain / Solidity
const initialBalance = await token.balanceOf(user.address);
await token.transfer(receiver.address, 100);
const finalBalance = await token.balanceOf(user.address);
assert(finalBalance.eq(initialBalance.sub(100)));
AFail - The user's balance increased by 100 tokens
BFail - The user's balance did not change
CPass - The user's balance decreased by 100 tokens
DError - 'eq' is not a function on balance values
Attempts:
2 left
💡 Hint
Remember that token balances are BigNumber objects and support eq and sub methods.
🔧 Debug
advanced
2:00remaining
Find the assertion mistake in this Solidity test
Identify the problem with the assertion in this Solidity test snippet:
Blockchain / Solidity
const result = contractInstance.someFunction();
assert(result === true, 'Function should return true');
AThe assertion is correct and will pass if function returns true
BThe assertion message is missing a semicolon
CThe assertion should use 'assert.equal' instead of 'assert'
DThe assertion uses strict equality on a Promise instead of awaited value
Attempts:
2 left
💡 Hint
Check if the function call is awaited before assertion.
framework
expert
3:00remaining
Which assertion pattern best integrates with Hardhat's testing framework for checking emitted events with indexed parameters?
In Hardhat tests using ethers.js and Chai, which assertion pattern correctly verifies that the Approval event was emitted with the expected owner and spender addresses, considering that these parameters are indexed?
Blockchain / Solidity
// Assume 'tx' is the transaction response after calling approve
Aawait expect(tx).to.emit(contract, 'Approval').withArgs(owner, spender, amount);
BassertEvent(tx, 'Approval', {owner: owner, spender: spender, value: amount});
CexpectEvent(tx, 'Approval', {owner: owner, spender: spender, value: amount});
Dawait expect(tx).to.emit(contract, 'Approval').withIndexedArgs(owner, spender, amount);
Attempts:
2 left
💡 Hint
Check the official Hardhat Chai matchers documentation for event assertions.