Читать книгу Blockchain Data Analytics For Dummies - Michael G. Solomon - Страница 76

Logging events on the blockchain

Оглавление

One of the more interesting aspects of blockchain data extends the information you can get from transactions. As mentioned, a transaction is the transfer of some value as a result of a smart contract function. Because the only way to create a transaction is to invoke a smart contract function, you can be sure that a transaction is the result of a function.

The previous statement may sound redundant, but it's extremely important. Smart contract functions can be simple or complex. As smart contracts become more complex, just knowing the function a transaction invoked, along with its input parameter values, isn’t always enough information to describe what’s going on. You need a way to record what happens inside transactions.

Ethereum, and most popular blockchains second generation and beyond, support sophisticated smart contract languages. Ethereum’s EVM (Ethereum virtual machine) is a Turing complete machine, so with enough resources, an Ethereum smart contract can calculate anything. Of course, in the real world, transactions eventually run out of gas, but the point is that your smart contract functions can be as complex as you want.

Go back to Etherscan and dig a little deeper into block 8976776’s transactions. Examine the same transaction in Figure 3-4 (block 8976776 -> Transaction list -> Fourth transaction in the list’s details). Click or tap the Event Logs tab at the top of the page. The Event Logs page shows a list of events that occurred during a smart contract function. Figure 3-9 shows the last two events for the current transaction.


FIGURE 3-9: Ethereum events in Etherscan.

Note that these events have names — LogTransfer() and LogOrderCancelled() — and parameters. Smart contract programmers use events to create messages that Ethereum logs and saves. Events make it easy to notify client applications that certain actions have taken place in a smart contract and also to store important information related to a transaction.

Smart contract programmers use events to record internal details of how smart contracts operate. The programmer defines events and the parameters passed when the events are called. Then, during runtime, the smart contract invokes the event when something notable happens in the code. For example, when using the popular language Solidity for writing smart contract code, the emit command invokes an event. Any time a programmer wants to send a message to the client or record an action, the emit statement invokes an event to do just that.

Most smart contract programmers use event names that describe the action. So we would expect that the LogOrderCancelled() event is present because an order was cancelled in the transaction. Smart contract programmers can create events anywhere in their code. The most common purpose of an event is to record the occurrence of an action, such as cancelling an order. The event parameters, orderHash and by, provide identifying information for the order that was cancelled and who cancelled it. Events take some effort to analyze but can yield interesting analysis data.

Blockchain Data Analytics For Dummies

Подняться наверх