# Events

# Event

jigu.core.event.Event

# Attributes

str
type

The event type.

JiguBox[str, str[]]
type

The event's attributes, keys mapping to list of string values.

# Working with Events

Events are data structures that are emitted by the node while processing blockchain state transitions, in places such as message handlers, BeginBlock and EndBlock. They are primarily used as a lightweight logging utility for clients to track important state changes, such as the result of swaps, monetary lever updates, delegations, just to name a few.

Unfortunately, due to their lack of a role in determining consensus, there is no guarantee that event types will have a predictable structure, as anybody can modify their node to change which events get emitted without altering finality and consensus. In spite of this, Jigu does have some facilities that make working with events easier.

# How events are represented

Take for instance, a transfer event:

After fetching data from the blockchain, an event exists as a list of JSON objects with a key and value attributes, which are both strings. However, events may have been emitted where the attributes contain multiple values for the same key (such as sender above). Jigu resolves this issue by keeping all attributes as a list of values, preserves the order of values as they appear in the original list.

transfer = Event(...) # transfer is loaded with the data
transfer.type # -> "transfer"
transfer.sender # -> ["terra...wxyz", "terra...abcd"]
transfer.amount[0] # -> "1023uusd"

Jigu will convert any events it encounters to the generic event class, where you can access the attributes directly from the object by attribute access. If you know the structure of the event, you can easily write a class to parse the event logs for you, to make an easier-to-use event.

class TransferEvent:
    """Turns the .amount from a list of 1 string to a Coin object."""

    def __init__(self, event):
        self.type = "transfer"
        self.senders = event.sender # all senders
        self.amount = Coin.from_str(event.amount[0])

new_transfer = TransferEvent(transfer)
new_transfer.senders[0] # -> "terra...wxyz"
new_transfer.amount # -> Coin('uusd', 1023)
Updated on: 3/4/2020, 10:24:49 AM