#alert #trade #hash #manage #create #user #success

bin+lib trade_alerts

Ligthweight framework to create and manage trade alerts

3 releases

0.1.2 Apr 27, 2024
0.1.1 Apr 27, 2024
0.1.0 Apr 27, 2024

#322 in Web programming

Download history 242/week @ 2024-04-22 70/week @ 2024-04-29

312 downloads per month

MIT license

57KB
630 lines

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

Dependencies

~8–22MB
~293K SLoC