/
lib.rs
63 lines (53 loc) · 1.96 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use trading_bot_lib::TradeDecision;
use reqwest;
use redis::AsyncCommands;
use std::error::Error;
use futures::StreamExt;
pub async fn listen_for_decisions() -> Result<(), Box<dyn Error>> {
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_async_connection().await?;
let pubsub_con = client.get_async_connection().await?;
let mut pubsub = pubsub_con.into_pubsub();
pubsub.subscribe("arbitrage_decision").await?;
while let Some(msg) = pubsub.on_message().next().await {
let payload: String = msg.get_payload()?;
let decision: TradeDecision = serde_json::from_str(&payload)?;
// Based on the decision, buy or sell bitcoin
let amount = decision.quantity();
let price = decision.price();
if decision.action() == "buy" {
let res = buy_bitcoin(price, amount).await?;
// Publish result back to Redis
let _: () = con.publish("trading_result", res).await?;
} else if decision.action() == "sell" {
let res = sell_bitcoin(price, amount).await?;
// Publish result back to Redis
let _: () = con.publish("trading_result", res).await?;
}
}
Ok(())
}
async fn buy_bitcoin(price: f64, amount: f64) -> reqwest::Result<String> {
let client = reqwest::Client::new();
let res = client.post("http://api.yourserver.com/buy_bitcoin")
.json(&serde_json::json!({
"price": price,
"amount": amount
}))
.send()
.await?;
// Assume the API returns a string
res.text().await
}
async fn sell_bitcoin(price: f64, amount: f64) -> reqwest::Result<String> {
let client = reqwest::Client::new();
let res = client.post("http://api.yourserver.com/sell_bitcoin")
.json(&serde_json::json!({
"price": price,
"amount": amount
}))
.send()
.await?;
// Assume the API returns a string
res.text().await
}