events serve as a high-level abstraction within Solidity code to notify the external world, while logs are the low-level data structures that store the details of these events in the Ethereum transaction logs. External applications can then use these logs to stay informed about the state changes on the blockchain. explain with real life example simple new oneCertainly! Let's consider a simple crowdfunding smart contract where backers can contribute funds to a project, and an event is used to notify the external world when a contribution is made.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Crowdfunding {
address public projectOwner;
uint public fundingGoal;
uint public totalFunds;
// Event to notify when a contribution is made
event ContributionMade(address indexed contributor, uint amount);
// Constructor to set the project owner and funding goal
constructor(address _projectOwner, uint _fundingGoal) {
projectOwner = _projectOwner;
fundingGoal = _fundingGoal;
}
// Function to make a contribution to the project
function contribute() external payable {
require(msg.value > 0, "Contribution must be greater than 0");
totalFunds += msg.value;
// Trigger the ContributionMade event
emit ContributionMade(msg.sender, msg.value);
// Check if the funding goal is reached
if (totalFunds >= fundingGoal) {
// Implement logic to complete the project
// For simplicity, we'll just transfer the funds to the project owner
payable(projectOwner).transfer(totalFunds);
totalFunds = 0; // Reset totalFunds after transferring
}
}
}
Explanation:
Event Declaration (ContributionMade):
event ContributionMade(address indexed contributor, uint amount);
contributor (Ethereum address) and amount (unsigned integer).Event Emission:
emit ContributionMade(msg.sender, msg.value);
ContributionMade event, providing details such as the contributor's address (msg.sender) and the contributed amount (msg.value).Log in Ethereum Transaction:
External Applications Listening:
ContributionMade event.Real-Life Analogy:
In this way, events serve as a higher-level abstraction to notify external systems, and logs provide the underlying data structure that stores these notifications in the blockchain. External applications use these logs to track and respond to state changes on the Ethereum blockchain.