DEV Community

Cover image for Day 26 - Events and Hashing
Vedant Chainani
Vedant Chainani

Posted on

Day 26 - Events and Hashing

GitHub logo Envoy-VC / 30-Days-of-Solidity

30 Days of Solidity step-by-step guide to learn Smart Contract Development.

This is Day 26 of 30 in Solidity Series
Today I Learned About Events and Hashing in Solidity.

Events

Solidity Events are the same as events in any other programming language. An event is an inheritable member of the contract, which stores the arguments passed in the transaction logs when emitted.

Events are defined within the contracts as global and called within their functions. Events are declared by using the event keyword, followed by an identifier and the parameter list, and ends with a semicolon. The parameter values are used to log the information or for executing the conditional logic. Its information and values are saved as part of the transactions inside the block. There is no need of providing variables, only datatypes are sufficient. An event can be called from any method by using its name and passing the required parameters.
Syntax -

event <eventName>(parameters);
Enter fullscreen mode Exit fullscreen mode

Example -

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract MyContract {

    event transaction(address payable, uint256);

    function sendEther(address payable to) public payable {
        to.transfer(msg.value);
        emit transaction(to,msg.value);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output logs -

[
  {
    from: "0xf8e81D47203A594245E36C48e151709F0C19fBe8",
    topic: "0xa088ceb626d7ab77e5c39d79be36e959ac77a2563e711bdfce14bc1b86293691",
    event: "transaction",
    args: {
      0: "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2",
      1: "10000000000000000000",
    },
  },
];
Enter fullscreen mode Exit fullscreen mode

Hashing

A cryptographic hash function is an algorithm that takes an arbitrary amount of data as input and produces the enciphered text of fixed size. Even a slight change in the input gives a completely different output.

Solidity provides the following cryptographic functions:

Function Properties
keccak256(bytes memory) returns (bytes32) Computes the Keccak-256 hash of the input
sha256(bytes memory) returns (bytes32) Computes the SHA-256 hash of the input
ripemd160(bytes memory) returns (bytes20) Compute RIPEMD-160 hash of the input
sha256(bytes memory) returns (bytes32) Computes the SHA-256 hash of the input
ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address) Recover the address associated with the public key from Elliptic curve signature used for cryptography or return Zero if an error occurs. The parameters correspond to ECDSA Signature values.

Example -

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

contract MyContract {

    uint256 digits = 5;

    function generateRandom(string memory _str) public view returns(uint256) {
        uint random = uint(keccak256(abi.encodePacked(_str))) % (10 ** digits);
        return random;
    }
}
Enter fullscreen mode Exit fullscreen mode

The function generateRandom generates a random number of 5 digits from a string which it first converts to a keccak256 hash and the takes modulus to give a 5 digit number

Output when string is solidity

0:
uint256: 70469
Enter fullscreen mode Exit fullscreen mode

Top comments (0)