#blockchain #data-stream

fuel-streams

A library for working with streams of Fuel blockchain data

11 releases

0.0.16 Dec 28, 2024
0.0.15 Dec 27, 2024
0.0.13 Nov 28, 2024
0.0.11 Oct 15, 2024
0.0.6 Aug 29, 2024

#20 in #cryptocurrencies

Download history 155/week @ 2024-09-21 41/week @ 2024-09-28 8/week @ 2024-10-05 119/week @ 2024-10-12 17/week @ 2024-10-19 4/week @ 2024-11-02 107/week @ 2024-11-16 166/week @ 2024-11-23 21/week @ 2024-11-30 8/week @ 2024-12-07 1/week @ 2024-12-14 152/week @ 2024-12-21 222/week @ 2024-12-28

376 downloads per month

Apache-2.0

310KB
6.5K SLoC

Logo

Fuel Streams

A library for working with streams of Fuel blockchain data

CI Coverage Crates.io MSRV crates.io docs

📚 Documentation   🐛 Report Bug   ✨ Request Feature

📝 About The Project

[!WARNING] This project is currently under development and is not yet ready for production use.

Fuel Streams is a Rust library designed for working with streams of Fuel blockchain data. It provides an efficient and user-friendly interface for developers to interact with real-time blockchain data, offering support for Fuel-specific data types and leveraging NATS for scalable streaming.

🚀 Features

  • Real-time streaming of Fuel blockchain data
  • Support for Fuel-specific data types
  • Efficient data handling using NATS
  • Easy-to-use API for subscribing to and processing blockchain events
  • Customizable filters for targeted data retrieval
  • Seamless integration with other Fuel ecosystem tools

🛠️ Installing

First, add these dependencies to your project:

cargo add fuel-streams futures tokio

📊 Usage

Here are some examples to get you started with Fuel Streams:

Basic Connection and Subscription

use fuel_streams::prelude::*;
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a client and establish connection
    let mut client = Client::new(FuelNetwork::Local).await?;
    let mut connection = client.connect().await?;

    println!("Listening for blocks...");

    // Create a subject for all blocks
    let subject = BlocksSubject::new();

    // Subscribe to blocks with last delivery policy
    let mut stream = connection
        .subscribe::<Block>(subject, DeliverPolicy::Last)
        .await?;

    while let Some(block) = stream.next().await {
        println!("Received block: {:?}", block);
    }

    Ok(())
}

Custom Connection Options

use fuel_streams::prelude::*;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create client with custom connection options
    let client = Client::with_opts(ConnectionOpts {
        network: FuelNetwork::Local,
        username: "custom_user".to_string(),
        password: "custom_pass".to_string(),
    }).await?;

    Ok(())
}

Subject Types and Filtering

Each data type has its own subject builder for filtering. Here's an example using transaction filtering:

use fuel_streams::prelude::*;
use futures::StreamExt;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut client = Client::new(FuelNetwork::Testnet).await?;
    let mut connection = client.connect().await?;

    println!("Listening for transactions...");

    // Create a subject for script transactions
    let subject = TransactionsSubject::new()
        .with_kind(Some(TransactionKind::Script));

    // Subscribe to the filtered transaction stream
    let mut stream = connection
        .subscribe::<Transaction>(subject, DeliverPolicy::Last)
        .await?;

    while let Some(transaction) = stream.next().await {
        println!("Received transaction: {:?}", transaction);
    }

    Ok(())
}

Available subject builders include:

  • BlocksSubject::new()
  • TransactionsSubject::new()
  • InputsSubject::new()
  • OutputsSubject::new()
  • LogsSubject::new()
  • UtxosSubject::new()

Each subject builder provides specific filtering methods relevant to its data type. For example, TransactionsSubject allows filtering by transaction kind using the with_kind() method.

DeliverPolicy Options

The DeliverPolicy enum provides control over message delivery in your subscriptions:

  • All: Delivers all messages in the stream
  • Last: Delivers only the last message for the selected subjects
  • New: Delivers only new messages that arrive after subscription
  • ByStartSequence(u64): Delivers messages starting from a specific sequence number
  • ByStartTime(DateTime<Utc>): Delivers messages starting from a specific time

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

For more information on contributing, please see the CONTRIBUTING.md file in the root of the repository.

📜 License

This project is licensed under the Apache-2.0 license. See LICENSE for more information.

Dependencies

~202MB
~4M SLoC