trade_alerts

Crates.iotrade_alerts
lib.rstrade_alerts
version0.1.4
sourcesrc
created_at2024-04-27 17:10:11.207809
updated_at2024-05-29 02:07:19.927259
descriptionLigthweight framework to create and manage trade alerts
homepage
repository
max_upload_size
id1222764
size101,948
Hadi (hadi-xlx)

documentation

README

Trade ALerts library

trade_alerts is a library that allows you to create, store and manage trade alerts for a variety of exchanges and users.

Coming soon

A list of supported exchanges and ore data providers :]

Getting Started

To use the Trade Alerts Library, add it to your Cargo.toml:

[dependencies]
trade_alerts = "0.1.0"

Features

Examples

Here are examples of how to use the library:

Prerequisites

To use the Supabase Client, you need to set the initialize the client.

    // Initialize Supabase client
    let supabase = match Supabase::new_env().await {
        Ok(client) => client,
        Err(e) => {
            eprintln!("{}", e);
            return;
        },
    };

Configuration for tables

We need to setup all the table names so we can route everything accordingly

    // Define a TableConfig
    let config: TableConfig = TableConfig::new(
        "alerts".to_string(),
        "hash".to_string(),
        "price_level".to_string(),
        "user_id".to_string(),
        "symbol".to_string(),
    );

Add an alert

We first need to create an alert and then add it to the database.

    // Create a new alert
    let alert: Alert = Alert::new(
        Hash { hash: "unique_hash_string".to_string() },
        1.2345, // price level
        "aud/chf".to_string(), // symbol
        "user1234".to_string() // user ID
    );

    // Adding an alert
    match supabase.add_alert(
       alert.clone(),
       config.clone()
       ).await {
        Ok(_) => println!("Alert added successfully"),
        Err(e) => eprintln!("{}", e),
    };

Fetch hashes by user ID

    // Fetching hashes by user ID
    match supabase.fetch_hashes_by_user_id(
       &alert.user_id,
       config.clone()
       ).await {
        Ok(hashes) => println!("Fetched hashes: {:?}", hashes),
        Err(e) => eprintln!("{}", e),
    };

Fetch alert details

    // Fetching details by hash
    match supabase.fetch_details_by_hash(
       &alert.hash.hash,
       &config
       ).await {
        Ok(details) => println!("Fetched details: {:?}", details),
        Err(e) => eprintln!("{}", e),
    }; 

Alert Management

We assume that the Table config is already set up in this example and your supabase client is initialized.


#[tokio::main]
async fn main() {

    // Initialize XylexApi
    let xylex_api = match XylexApi::new_env().await {
        Ok(api) => api,
        Err(e) => {
            eprintln!("{}", e);
            return;
        },
    };

    let symbols: HashSet<&str> = [
        "aud/chf", "eur/usd"
    ].iter().cloned().collect();
    
    match xylex_api.fetch_prices_for_symbols(
        symbols
    ).await {
        Ok(prices) => println!("Prices: {:?}", prices),
        Err(e) => eprintln!("{}", e),
    };

    // Check and delete triggered alerts
    match xylex_api.check_and_fetch_triggered_alert_hashes(
        &supabase,
        &config
    ).await {
        Ok(triggered_hashes) => {
            if triggered_hashes.is_empty() {
                println!("No triggered alerts.");
                return;
            }
            match xylex_api.delete_triggered_alerts_by_hashes(
                &supabase,
                &config,
                triggered_hashes
            ).await {
                Ok(_) => println!("Successfully deleted triggered alerts"),
                Err(e) => eprintln!("{}", e),
            }
        },
        Err(e) => eprintln!("{}", e),
    };
}

Hash Generation

use trade_alerts::HashComponents; 

let components: HashComponents = HashComponents::new(
     100.0, 
    "user123".to_string(), 
    "AAPL".to_string()
);

let hash = components.generate_hash().await;

println!("Generated Hash: {}", hash);

Handling Success and Errors

Commit count: 0

cargo fmt