6

How To Use Events In Solidity

 3 years ago
source link: https://hackernoon.com/how-to-use-events-in-solidity-pe1735t5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How To Use Events In Solidity

5
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png

@kamilpolakKamil Polak

I am a huge enthusiast of cryptocurrency and blockchain technology.

In this short tutorial, I would like to show you the basics of events in Solidity. Specifically, I will explain what are the events in solidity and how to use them in our smart contract.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

What are the events?

Following the Solidity documentation:

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Events are inheritable members of contracts. When you call them, they cause the arguments to be stored in the transaction’s log — a special data structure in the blockchain. These logs are associated with the address of the contract, are incorporated into the blockchain, and stay there as long as a block is accessible

Why do we need events?

Events are used to inform external users that something happened on the blockchain. Smart contracts themselves cannot listen to any events.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

All information in the blockchain is public and any actions can be found by looking into the transactions close enough but events are a shortcut to ease the development of outside systems in cooperation with smart contracts. 

0 reactions
heart.png
light.png
money.png
thumbs-down.png

How to use events in Solidity

Solidity defines events with the event keyword. After events are called, their arguments are placed in the blockchain. To use events first, you need to declare them in the following way:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
event moneySent(address _from, address _to, uint _amount);

The definition of the event contains the name of the event and the parameters you want to save when you trigger the event.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

Then you need to emit your event within the function:

0 reactions
heart.png
light.png
money.png
thumbs-down.png
emit moneySent(msg.sender, _to, _amount);

Solidity events are interfaces with Ethereum Virtual Machine logging functionality. You can add an attribute indexed to up to three parameters. When parameters do not have the indexed attribute, they are ABI-encoded into the data portion of the log.

0 reactions
heart.png
light.png
money.png
thumbs-down.png

To better understand the event let’s see the full code.

0 reactions
heart.png
light.png
money.png
thumbs-down.png
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract MyContract{
 
 
 mapping(address => uint) public Balance;
 
 event moneySent(address _from, address _to, uint _amount);
 
 constructor public {
 
 owner = msg.sender;
 moneyBalance[msg.sender] = 50;
 
 }
 
 
 function sendMoney(address _to, uint _amount) public returns(bool) {
 require(msg.sender = owner; "You are not allowe");
 require(Balance[msg.sender] >= _amount; "Not enough money");
 
 
 moneyBalance[msg.sender] -= _amount;
 moneyBalance[_to] += _amount;
 
 emit moneySent(msg.sender, _to, _amount);
 
 
 return true;
 
 
 }

Takeaways

  • There are two types of Solidity event parameters: indexed and not indexed,
  • Events are used for return values from the transaction and as a cheap data storage,
  • Blockchain keeps event parameters in transaction logsEvents can be filtered by name and by contract address

References

5
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
by Kamil Polak @kamilpolak. I am a huge enthusiast of cryptocurrency and blockchain technology. Do not forget to follow me
Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK