ProductSold eventsellProduct function that emits the event with product name and pricesellProductProductSold event is emitted with the expected product name and priceProductSold eventsellProduct function that emits the event with product name and pricesellProductProductSold event is emitted with the expected product name and priceStore. Inside it, declare an event called ProductSold that takes a string productName and a uint price. Also add a public function called sellProduct that takes string memory productName and uint price as parameters but leave the function body empty for now.Remember to use event keyword to declare events in Solidity.
sellProduct function, emit the ProductSold event with the productName and price parameters.Use the emit keyword followed by the event name and parameters.
Store contract. Call the sellProduct function with "Laptop" and 1500. Use expect with to.emit to check that the ProductSold event is emitted with "Laptop" and 1500.Use Hardhat's expect and to.emit syntax to test events.
"Event test passed!" if the event is emitted correctly.Use console.log("Event test passed!") to print the message.
What is the main purpose of event testing in blockchain smart contracts?
Which of the following is the correct syntax to listen for an event named Transfer in a Solidity test using JavaScript?
contractInstance.Transfer({fromBlock: 0, toBlock: 'latest'}).???watch is used to listen for events on contract instances.watch fits the syntax shown; on, emit, and listen are incorrect here.Given this Solidity event and test snippet, what will be the output of console.log(events.length) after calling transfer() once?
event Transfer(address indexed from, address indexed to, uint amount);
// In test:
const events = await contractInstance.getPastEvents('Transfer', {fromBlock: 0, toBlock: 'latest'});
await contractInstance.transfer(receiver, 100);
console.log(events.length);getPastEvents call happens before transfer(), so it fetches events before the transfer.events.length will be 0 because no Transfer event happened yet.Identify the error in this test code snippet that listens for an event Approval:
contractInstance.Approval().watch((error, event) => {
if (!error) {
console.log('Approval event:', event);
}
});contractInstance.Approval({fromBlock: 0}).watch(...); the code uses empty Approval().You want to test that a Deposit event is emitted only when the deposit amount is greater than zero. Which test approach correctly verifies this?
1. Call deposit(0) and check no event emitted 2. Call deposit(100) and check event emitted with amount 100 3. Call deposit(-50) and check event emitted 4. Call deposit(100) and check event emitted with amount 0
Pass the test and this pattern moves unknown