Crates.io | trade_alerts |
lib.rs | trade_alerts |
version | 0.1.4 |
source | src |
created_at | 2024-04-27 17:10:11.207809 |
updated_at | 2024-05-29 02:07:19.927259 |
description | Ligthweight framework to create and manage trade alerts |
homepage | |
repository | |
max_upload_size | |
id | 1222764 |
size | 101,948 |
trade_alerts
is a library that allows you to create, store and manage trade alerts for a variety of exchanges and users.
A list of supported exchanges and ore data providers :]
To use the Trade Alerts Library, add it to your Cargo.toml
:
[dependencies]
trade_alerts = "0.1.0"
Here are examples of how to use the library:
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;
},
};
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(),
);
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),
};
// 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),
};
// 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),
};
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),
};
}
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);
Success::SupabaseSuccess
: Success outcomes for Supabase operations.
Success::XylexApiSuccess
: Success outcomes for Xylex API operations.
Error::SupabaseError
: Errors related to Supabase operations.
Error::TableConfigError
: Errors related to table configuration.
Error::XylexApiError
: Errors related to Xylex API operations.