ReasonJun

Solidity : event / emit 본문

Blockchain/Solidity

Solidity : event / emit

ReasonJun 2023. 10. 16. 14:13
728x90

Events and emit are two important concepts in Solidity. Events are a way to log and notify external entities (such as user interfaces or other smart contracts) about specific occurrences within a smart contract. Emit is the keyword used to trigger or emit events within the smart contract code.

 

Events are similar to logs or records that capture important information, allowing external observers to react to them and obtain relevant data.

 

Benefits of using events

  • Events can be used to track the state of a smart contract.
  • Events can be used to notify external entities about changes to the state of a smart contract.
  • Events can be used to trigger actions in other smart contracts.
  • Events can be used to generate audit trails of smart contract activity.

How to use events

To use events, you first need to declare them in your smart contract code. This is done using the event keyword. The event declaration must include the name of the event and the types of data that the event will emit.

Once you have declared an event, you can emit it using the emit keyword. The emit keyword must be followed by the name of the event and the values of the data that the event is emitting.

 

Example (1)

The following example shows how to declare and emit an event:

event Transfer(address indexed from, address indexed to, uint256 amount);

function transfer(address to, uint256 amount) public {
    emit Transfer(msg.sender, to, amount);
}

The Transfer event has three parameters: from, to, and amount. The from parameter is the address of the sender of the transfer. The to parameter is the address of the receiver of the transfer. The amount parameter is the amount of tokens that are being transferred.

 

The transfer() function emits the Transfer event whenever it is called. The Transfer event can then be used by external entities to track token transfers and to take appropriate action.

 

Example (2)

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Loop {
    uint[] public num;

    function doWhileLoop() public {
        uint x = 0;
        do{
            num.push(x*3);
            x++;
        } while (x != 5);
    }

    event CountryIndexName(uint256 indexed _index, string _name);
    string[] private countryList = ["South", "North", "USA", "China", "Japan"];

    function forLoopEvents() public {
        for(uint256 i = 0; i < countryList.length; i++) {
            emit CountryIndexName(i, countryList[i]);
        }
    }

}

Example (3)

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Search {
     event CountryIndexName(uint256 indexed _index, string _name);
    string[] private countryList = ["South", "North", "USA", "China", "Japan"];

    // Strings cannot be compared within Solidity.
    // Convert the string to a binary value, hash it (keccak256), and compare.
    // The parameter of keccak256 is byte types.
    function linearSearch(string memory _search) public view returns(uint256, string memory) {
        for(uint256 i = 0; i< countryList.length; i++) {
            
            if(keccak256(bytes(countryList[i])) == keccak256(bytes(_search))){
                return (i, countryList[i]);
            }
        }

        return(0, "Nothing");
    }
}

Example (4)

// SPDX-License-Identifier: MIT
pragma solidity >= 0.7.0 < 0.9.0;

contract Ex1 {

event Message(address indexed _from, address indexed _to, string message);

    function sendMessage(address _to, string calldata message) external {
        emit Message(msg.sender, _to, message);
    }

}

Conclusion

Events are a powerful tool that can be used to improve the functionality and usability of smart contracts. By using events, you can log and notify external entities about important occurrences within your smart contract, which can lead to more transparent, auditable, and reliable code.

728x90

'Blockchain > Solidity' 카테고리의 다른 글

Solidity : mapping  (0) 2023.10.16
Solidity : keccak256  (0) 2023.10.16
Solidity : function / Parameter / wei / ether / gas  (0) 2023.10.16
Solidity : Modifier (pure, view)  (0) 2023.10.16
Solidity : Visibility  (0) 2023.10.15
Comments