How to perform events in solidity

How to perform events in solidity


EVENTS

A contract's inheritable component, known as an event, records the parameters supplied when issued in the transaction logs.

Other languages are comparable to Solidity. Let's examine what events are in other languages and compare them to events in Solidity.

The main difference is how?events in Solidity operate, since they are on the blockchain and follow a slightly different logic. Let's examine how Solidity's Events function. Occurrences work in conjunction with the user interface to ensure that the user is informed of any events that need notification, such as a transaction passing or failing, deposits failing, etc. Events provide users with information about?Blockchain events to enhance user-friendliness when using dApps.

BASIC CODE FOR EVENTS IN SOLIDITY

pragma solidity.0.8 .0 <= 0.9 .0;
contract SimpleAuction {
    event HighestBidIncreased(address bidder, uint amount); // Event
    function bid() public payable {
        // ...
        emit HighestBidIncreased(msg.sender, msg.value);
    }
}
        

JavaScriptCopy

Events are present inside a log, which allows us to print information to the logging structure in a way that’s more gas efficient than actually saving it to something like a storage variable.

Events and smart contracts live in a special data structure that isn’t accessible to the smart contract. Events are incredibly powerful, and they boast a range of uses. They’re also good for testing and some other things.

In events, we use a new keyword called indexed, which is quite important. In the events, we have indexed parameters and the non-indexed parameters.

We can have up to three?indexed parameters in an event’s arguments. These are known as topics. These indexed parameters are much easier to search for and much easier to query than non-indexed parameters. Non-indexed parameters are harder to search, because they get abi encoded. So, we have to know?the abi in order to decode it.

We need to actually emit that event in order to store that data into the logging data structure of EVM.

BASIC EXAMPLE OF EMIT IN SOLIDITY

pragma solidity > 0.8 .0 <= 0.9 .0;
contract Allowance {
    event AllowanceChanged(address indexed _forwho, address indexed _towhom, uint oldAmount, uint newAmount);

    function allowance public payable {
        // ...
        emit AllowanceChanged(_who, msg.sender, allowance[_who], allowance[who] - amount); //EMIT
    }
}
        

JavaScriptCopy

This is how it looks when we emit an event. This is done by emit and then event name and the parameters.

WHAT ARE EVENTS FOR?

  • To test particular variables in your smart contracts
  • Index variables to rebuild storage state
  • To modify a front end, pay attention to events
  • To speed up data reading, create subgraphs

WHAT ARE THE KEY TAKEAWAYS?

  • Solidity event parameters come in two varieties: indexed and not indexed.
  • Events are utilised as inexpensive data storage, and for transaction return values.
  • Blockchain tracks transactions with event parameters. Events may be filtered by contract address and by name.

要查看或添加评论,请登录

Dinesh Ravishankar的更多文章

  • Understanding the Importance of Layer 3 Blockchain

    Understanding the Importance of Layer 3 Blockchain

    Cryptocurrency has transformed the way we perceive and interact with digital assets. With the advent of Bitcoin, the…

    1 条评论
  • Unlocking the Secret World of Zero-Knowledge Proofs used in Blockchain,web3 and hyperledger

    Unlocking the Secret World of Zero-Knowledge Proofs used in Blockchain,web3 and hyperledger

    Secretly Share Sensitive Data Without Risk! Zero Knowledge: A Secure Approach to Data Security Zero knowledge is a…

    1 条评论
  • Gas Optimization Strategies: 8 Proven ways to reduce Ethereum transaction costs

    Gas Optimization Strategies: 8 Proven ways to reduce Ethereum transaction costs

    Introduction The Ethereum network, like any vehicle, requires gasoline to function properly. We call it a gas.

    1 条评论
  • Consensus Algorithm

    Consensus Algorithm

    What is Consensus We know that blockchain is a distributed ledger, so let’s say we have 4 mining nodes here. So, all…

  • ERC And Its Types

    ERC And Its Types

    ERC- Ethereum Request for Comment It is a set of rules and regulations that creates a blueprint for Ethereum based…

  • The Web 5 Is Here

    The Web 5 Is Here

    Currently we are working on web2 and we are excited about web3 and Jack Dorsey says, hey we are coming up with web 5…

  • Created a crypto Wallet

    Created a crypto Wallet

    This wallet can do all the functionality which other wallets can do, also additionally it can add allowance and it can…

  • The Fractional NFTs

    The Fractional NFTs

    NFT Market Limitations Everyone would be aware of NFTs and these NFTs are booming in the market. There is a number of…

  • Smart contracts

    Smart contracts

    SMART CONTRACTS IN BLOCKCHAIN What are smart contracts? Smart contracts are line of codes which you run on Ethereum…

  • Hyperledger And Its Frameworks

    Hyperledger And Its Frameworks

    #blockchain #hyperledger #web3 #hyperledgerfabric Hyperledger Fabric What is Hyperledger Before understanding about…

    1 条评论

社区洞察

其他会员也浏览了