-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pipelines, conditions and a draft of TradingEngine
- Loading branch information
Showing
11 changed files
with
451 additions
and
144 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::collections::HashMap; | ||
|
||
use super::pipeline::{Condition, ConditionType}; | ||
pub struct Evaluator; | ||
|
||
impl Evaluator { | ||
pub fn evaluate_conditions(conditions: &[Condition], prices: &HashMap<String, f64>) -> bool { | ||
conditions | ||
.iter() | ||
.all(|c| Self::evaluate_condition(c, prices)) | ||
} | ||
|
||
fn evaluate_condition(condition: &Condition, prices: &HashMap<String, f64>) -> bool { | ||
match &condition.condition_type { | ||
ConditionType::PriceAbove { asset, threshold } => { | ||
prices.get(asset).map(|p| p >= threshold).unwrap_or(false) | ||
} | ||
ConditionType::PriceBelow { asset, threshold } => { | ||
prices.get(asset).map(|p| p <= threshold).unwrap_or(false) | ||
} | ||
ConditionType::And(sub) => sub.iter().all(|c| Self::evaluate_condition(c, prices)), | ||
ConditionType::Or(sub) => sub.iter().any(|c| Self::evaluate_condition(c, prices)), | ||
// PercentageChange would require historical data tracking | ||
_ => false, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use super::order::Order; | ||
use super::privy_config::PrivyConfig; | ||
use super::types::{SignAndSendEvmTransactionParams, SignAndSendEvmTransactionRequest}; | ||
use super::types::{ | ||
SignAndSendTransactionParams, SignAndSendTransactionRequest, SignAndSendTransactionResponse, | ||
}; | ||
use super::util::create_http_client; | ||
use anyhow::{anyhow, Result}; | ||
|
||
pub struct Executor { | ||
http_client: reqwest::Client, | ||
} | ||
|
||
impl Executor { | ||
pub fn from_env() -> Result<Self> { | ||
let privy_config = PrivyConfig::from_env()?; | ||
let http_client = create_http_client(&privy_config); | ||
Ok(Self { http_client }) | ||
} | ||
|
||
pub async fn execute_order(&self, order: Order) -> Result<String> { | ||
if order.is_solana() { | ||
if order.solana_transaction.is_none() { | ||
return Err(anyhow!("Solana transaction required for Solana order")); | ||
} | ||
self.execute_solana_transaction( | ||
order.address, | ||
order.solana_transaction.unwrap(), | ||
order.caip2, | ||
) | ||
.await | ||
} else { | ||
if order.evm_transaction.is_none() { | ||
return Err(anyhow!("EVM transaction required for EVM order")); | ||
} | ||
self.execute_evm_transaction(order.address, order.evm_transaction.unwrap(), order.caip2) | ||
.await | ||
} | ||
} | ||
|
||
async fn execute_evm_transaction( | ||
&self, | ||
address: String, | ||
transaction: serde_json::Value, | ||
caip2: String, | ||
) -> Result<String> { | ||
tracing::info!(?address, "Executing EVM transaction"); | ||
let request = SignAndSendEvmTransactionRequest { | ||
address, | ||
chain_type: "ethereum".to_string(), | ||
method: "eth_sendTransaction".to_string(), | ||
caip2, | ||
params: SignAndSendEvmTransactionParams { transaction }, | ||
}; | ||
|
||
let response = self | ||
.http_client | ||
.post("https://auth.privy.io/api/v1/wallets/rpc") | ||
.json(&request) | ||
.send() | ||
.await?; | ||
|
||
if !response.status().is_success() { | ||
return Err(anyhow!( | ||
"Failed to send transaction: {}", | ||
response.text().await? | ||
)); | ||
} | ||
|
||
let result: SignAndSendTransactionResponse = response.json().await?; | ||
tracing::info!( | ||
?result.method, | ||
?result.data.hash, | ||
?result.data.caip2, | ||
"Transaction sent", | ||
); | ||
Ok(result.data.hash) | ||
} | ||
|
||
async fn execute_solana_transaction( | ||
&self, | ||
address: String, | ||
transaction: String, | ||
caip2: String, | ||
) -> Result<String> { | ||
tracing::info!(?address, "Executing Solana transaction"); | ||
let request = SignAndSendTransactionRequest { | ||
address, | ||
chain_type: "solana".to_string(), | ||
method: "signAndSendTransaction".to_string(), | ||
caip2, | ||
params: SignAndSendTransactionParams { | ||
transaction, | ||
encoding: "base64".to_string(), | ||
}, | ||
}; | ||
|
||
let response = self | ||
.http_client | ||
.post("https://api.privy.io/v1/wallets/rpc") | ||
.json(&request) | ||
.send() | ||
.await?; | ||
|
||
if !response.status().is_success() { | ||
return Err(anyhow!( | ||
"Failed to sign transaction: {}", | ||
response.text().await? | ||
)); | ||
} | ||
|
||
let result: SignAndSendTransactionResponse = response.json().await?; | ||
tracing::info!( | ||
?result.method, | ||
?result.data.hash, | ||
?result.data.caip2, | ||
"Transaction sent", | ||
); | ||
Ok(result.data.hash) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::trading_engine::caip2::Caip2; | ||
use crate::trading_engine::constants::*; | ||
use crate::trading_engine::executor::Executor; | ||
use crate::trading_engine::order::Order; | ||
|
||
#[tokio::test] | ||
async fn test_execute_order_eth() { | ||
let engine = Executor::from_env().unwrap(); | ||
let order = Order { | ||
user_id: "-".to_string(), | ||
address: TEST_ADDRESS_EVM.to_string(), | ||
caip2: Caip2::ARBITRUM.to_string(), | ||
evm_transaction: Some(serde_json::json!({ | ||
"from": TEST_ADDRESS_EVM, | ||
"to": TEST_ADDRESS_EVM, | ||
"value": "0x111", | ||
})), | ||
solana_transaction: None, | ||
}; | ||
let result = engine.execute_order(order).await.unwrap(); | ||
assert_eq!(result.len(), 66); | ||
} | ||
} |
Oops, something went wrong.