UNLOCKING POTENTIAL

Empowering Casper: A Toolkit for Event-Driven Architectures

Andrzej Broński
Casper Association R & D
4 min readMay 2, 2024

--

In the rapidly evolving world of blockchain, seamlessly connecting on-chain logic with off-chain functionalities has become essential, not just a nice-to-have.

Often, while part of the application logic resides in the smart contract realm, the rest — the server, frontend — hangs out in the traditional web 2.0 space. But it’s not just about observing if a transaction was successful; we’re hungry for more data. Enter the world of event-driven architecture, facilitated by creating a side channel where smart contracts emit events that are picked up by the external world.

Modern, artistic representation of cloud technology, featuring two large, mesh-like cloud shapes interconnected by a network of nodes and lines, set against a cream-colored background with geometric patterns.

This article will dive into how this magic ✨ happens on Casper, with the help of the Casper Event Toolkit dedicated for Rust developers.

Event Standard

Though Casper hasn’t baked contract-level events into its core (yet!), the community didn’t just sit back and wait. They crafted the Casper Event Standard (CES), commonly used in the NFT contracts — so we have events like Mint, Transfer, and Burn.

The essence of this standard is to store all event data in the contract’s named keys:

  • __events_schemaevents schema, based on Casper’s custom serialization.
  • __events — a dictionary, where the key is an incrementing ID starting from 0, and value is particular event data.
  • __events_length — an integer representing events count.
Diagram illustrating the structure of Casper’s global storage for a CES context.
Idea of Casper Event Standard.

In CES emitting events is done by placing serialized data in a blockchain dictionary (more performance-efficient than an array) and bumping up the event count. This data can then be queried by external apps, and that’s where parsers come into play.

Parsers Unveiled: The Gateway to Event Data

To date, we had two main libraries for parsing CES:

Their APIs let you extract events from a specific deployment by providing the smart contract hash (to fetch the event schema) and the deployment hash (to fetch execution results and parse the data according to CES).

But if you’re just after the events from the smart contract, minus the deployment noise, well, these libraries don’t cover that!

This gap, also missing Rust implementation, led to the creation of a new toolkit 👷.

Meet New Toolkit!

Introducing the Casper Event Toolkit:

Toolkit for fetching and parsing contract-level events that follow the Casper Event Standard. Enables the event-driven pattern for Rust applications integrated with Casper blockchain.

NOTE: Casper Event Toolkit is similar to MAKE’s ces-go-parser and ces-js-parser, but enhanced with ability to get events directly from the global storage.

A picture’s worth a thousand words, and a video 🎥? Even more. Check out the demo below!

Screencast of Casper Event Toolkit’s example usage to fetch events data from NFT contract.
Fetching events from NFT contract.

API Essentials

I encourage you to visit project README, as it contains a full step-by-step usage guide.

Here’s a sneak peek of library usage, prepared for quick digestion:

let NFT_CONTRACT_HASH: &str = "fe03021407879ce6fc5e035b70ff6a90941afdbea325a9164c7a497827efa7ff";

let client = CasperClient::default_mainnet();
let metadata = CesMetadataRef::fetch_metadata(&client, NFT_CONTRACT_HASH).await?;
let fetcher = Fetcher { client, ces_metadata: metadata };
let schemas = fetcher.fetch_schema().await?;

let num_events = fetcher.fetch_events_count().await?;

let event_id = 3;
let event = fetcher.fetch_event(event_id, &schemas).await?;

Simple, right? 😌

Plus, the toolkit allows using a local schema instead of fetching it, and fetching events by deployment hash.

Observing Events in Real-Time

Now we’ve got a Rust library handling contract-level events, but is it possible to observe them in real-time instead of polling the blockchain?

Short answer: Not really — just set up a periodic query.

Long Answer

To fully understand topic, there are more components coming into play.

Meme with ‘We need to go deeper’ caption.

There is SSE (Server-Side Events) stream available in Casper nodes, however those events are associated with nodes themself (consensus level), and cannot be used for passing information from the smart contract.

However, if your off-chain app is the one sending deployments (so you know deployment hashes), you can use SSE to be notified about processed deployments, then use the Casper Event Toolkit to scoop up the event data. That together will provide you real-time events 🤝.

Looking Ahead: Alternatives and Future Possibilities

If you prefer a hands-off approach, consider using cspr.cloud, which gives easy access to stream with all contract events.

Streaming API presented at https://cspr.cloud/.
Streaming API for contract-level events provided by https://cspr.cloud/.

That’s not all though. With Casper 2.0 on the horizon, we expect native contract-level events integrated directly into the SSE stream, allowing real-time updates without a hitch 🔥!

Wrap up

In conclusion, while the Casper Event Toolkit allows Rust developers to set up event-driven architectures on the Casper blockchain, it’s just one piece of the broader puzzle.

We encourage you to try it out, experiment, and let us know what works and what doesn’t.

“Every act of creation is first an act of destruction.” — Pablo Picasso

Your feedback is invaluable and helps us make the toolkit better for everyone in the community 💪.

--

--