بلاگ
Solana: How to display the event passed by emit! on the block explorer
See events in Solana Block Explorer
In this article, we will explore how to see the events approved by emit!
In Solana Block Explorer.
What is emits!
In the Solana block chain, emit!
It is a function used to send events to the network. It allows you to define and perform personalized functions that interact with other contracts or the block chain itself. In this case, we will use it to see an event at Block Explorer.
Definition of the structure of the event
First, we define the structure of the event:
`Oxide
#[event]
Stuct milk pub {
Pub commitment: U64,
}
This defines a simple "imprisonment with a single" commitment "field, which represents the number of commitments (or interactions) in the contract.
Usingemit!
Now, we useemit!To send the "complaint to the explorer block:
Oxide
Issue! (complaint {participation: 123});
However, as mentioned earlier, this will only work if we define an event structure. To solve this problem, we must add a new type of event and use theEvent(available in SDK Rust SDK) to define our personalized event:
Oxide
Use Solana_SDK :: Event :: {event, eventcord};
#[event]
Stuct milk pub {
Pub commitment: U64,
}
Implicit event for complaints {
FN Signature (& self) -> & [u8] {
// Returns the signing of the event (it is not shown in this example)
Not implemented! ()
}
}
In this updated code, we define a new structure 'complaint' and implement the 'event' section. Thefirm () method is called to return the metadata of the event, but for now it returns an empty portion of bytes.
See events at block explorer
To see events at Block Explorer, you must use the ‘Blockchain’ module:
`Oxide
Use blockchain :: {block, event, blockid};
Fn Main () {
// Obtain the current block ID
Let Block_id = Block :: Blockid :: New ();
// Define an event record for our personalized event
Leave Event_record = Eventorord :: New ();
Event_record.set_field ("engagement", 123);
// Create a new block object
Leave block_data = block_data! {
ID: Block_id.to_sting (),
Events: some (neighbor! [Event_record.clone ()]),
};
// Send blocking data to the block chain
Let mut tx = block :: Transaction :: new (). Set_data (block_data) .signer (). Build ();
Blockchain :: Sent_transation (& tx, "My-Transtation");
}
In this example, we use the 'Blockchain' module to create a new block object and add our personalized events record. Therefore, we send the blockchain block data using theSend_transation ()`.
Example of use cases
This example shows how to see events in Solana Block Explorer. It is possible to apply a logic similar to other contracts or additions, such as:
- Sample of user participation metrics
- See chronology transactions
- Call the registration API
Keep in mind that this is a simplified example and should consider factors such as the persistence of the event, registration and safety when they implement personalized events in the Solana block chain.