Solidity

Solidity Events

Solidity Events Main Tips

  • In Solidity, events allow convenient use of EVM logging facilities, that can be used for “calling” JavaScript callbacks inside the user interface of the dapp, allowing you to listen to the events.
  • Events, as members of contracts, are inheritable.

Solidity Events

Events let you conveniently access the EVM logging facilities, that may be used for “calling” JavaScript callbacks inside the dapp’s user interface, allowing you to listen for the events.

Additionally, events are inheritable members of the contracts. When called, they would make the arguments get stored inside the transaction’s log, which is a special data structure found in the blockchain. Those logs are associated with the contract’s address and are to be incorporated into the blockchain itself to stay there as long so long a block is accessible (permanently as of Homestead and Frontier, however, this may be changed with Serenity). Event and log data cannot be accessed from inside contracts (this includes the contract that created the event as well).

SPV proofs for logs are viable, so in case an outsider entity were to supply a contract with a proof like that, it may check that the log really does exist in the blockchain. Either way, you should be aware of the fact that block headers must be supplied since the contract is only capable of seeing the last 256 block hashes.

No more than three parameters are capable of receiving the indexed attribute that is going to causing the search for each of their arguments: The Indexed arguments’ specific values can be filtered for.

In the case of arrays (this includes bytes and string) get used as indexed arguments, the Keccak-256 hash of it will be stored in the form of topic instead.

The signature’s hash of the event is one of the topics, unless you used the anonymous specifier to declare the event. This would mean filtering for anonymous, specific events by name is not possible.

Each non-indexed argument is going to be stored inside the log’s data part.

Example

pragma solidity ^0.4.0;

contract Receipt {
    event DepositLog(
        uint _value
        bytes32 indexed _id,
        address indexed _from,
    );

    function depositFunc(bytes32 _id) payable {
        // All calls directed at that function, including deeply nested
        // are going to be detected by the JavaScript API via 
        // filtering for DepositLog to be called.
        DepositLog(msg.sender, _id, msg.value);
    }
}

 

Try on Remix Try live on Hosting

In the JavaScript API  the use would be like this:

Example

var abi = /* abi however the compiler generates it */;
var receipt = Receipt.at("0x1234...ab67" /* address */);
var Receipt = web3.eth.contract(abi);

var sampleEvent = receipt.Deposit();

// Watching to notice changes
sampleEvent.watch(function(error, result){
    // The result contains various info
    // which includes the arguments given 
    // to the deposit call.
    if (!error)
        console.log(result);
});

// Alternatively, to start watching right away, pass a callback
var sampleEvent = receipt.Deposit(function(error, result) {
    if (!error)
        console.log(result);
});

 

Try on Remix Try live on Hosting

Note: Arguments that are indexed are not going to be stored themselvse, meaning that you may only look for the values, however, retrieving the values themselves is not possible.


Solidity Events Low-Level Interface

Access the low-level interface to the logging mechanism is also possible using the functions such as:

  • log0
  • log1
  • log2
  • log3
  • log4
  • logi

logi would take i + 1 parameter of bytes32 type, the first argument being used for the log’s data part as well as the others as topics. The event call above is possible to perform similarly as:

Example

pragma solidity ^0.4.10;

contract Cont {
    function func() {
        bytes32 _id = 0x420042;
        log3(
            bytes32(msg.sender),
            bytes32(msg.value),
            bytes32(/*hexadecimal equivalent to kecak256("Deposit(address,hash256,uint256)")*/),
            _id
        );
    }
}

 

Try on Remix Try live on Hosting


Solidity Events Learn More

Use links provided in this list to learn more about how Solidity events work:

Read previous post:
Solidity Fallback Functions

Solidity Fallback Functions Main Tips In Solidity, a contract may have precisely one unnamed function, which cannot have arguments, nor...

Close