Skip to content

Commit

Permalink
complete first draft for ArbitrageService
Browse files Browse the repository at this point in the history
  • Loading branch information
kyzooghost committed Sep 29, 2024
1 parent e094bf5 commit d59d959
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
43 changes: 39 additions & 4 deletions arbitrage_engine/src/arbitrage_service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::decorated_edge::DecoratedEdge;
use super::path::DecoratedPath;
use super::engine::{get_all_negative_cycles_0, get_negative_cycle_quick};
use super::path::{DecoratedPath, Path};

use blake3::Hash;
use petgraph::{
graph::{EdgeReference, Graph},
Expand All @@ -11,7 +13,12 @@ use std::collections::HashMap;
trait IArbitrageService {
fn new() -> Self;
fn upsert_path(&mut self, n0: &str, n1: &str, edge: DecoratedEdge) -> bool;
fn scan_arbitrages(&self) -> Option<DecoratedPath>;
/// Returns all arbitrages found
fn scan_arbitrages(&self) -> Vec<DecoratedPath>;
/// Stops at first arbitrage found
fn scan_arbitrages_quick(&self) -> Vec<DecoratedPath>;
fn _decorate_paths(&self, path: Vec<Path<String>>) -> Vec<DecoratedPath>;
// TODO - Provide streaming API for scan_arbitrages, so that we can use results as they become available without having to wait for entire algorithm to run
}

/// Point of contact interacting with the arbitrage functionality
Expand Down Expand Up @@ -139,7 +146,35 @@ impl IArbitrageService for ArbitrageService {
}
}

fn scan_arbitrages(&self) -> Option<DecoratedPath> {
None
fn scan_arbitrages_quick(&self) -> Vec<DecoratedPath> {
let (_, path_option) = get_negative_cycle_quick(&self.graph);
match path_option {
None => Vec::new(),
Some(path) => Self::_decorate_paths(self, vec![path]),
}
}

fn scan_arbitrages(&self) -> Vec<DecoratedPath> {
let path = get_all_negative_cycles_0(&self.graph);
return Self::_decorate_paths(self, path);
}

fn _decorate_paths(&self, path_collection: Vec<Path<String>>) -> Vec<DecoratedPath> {
let edge_index_to_decorated_edge =
|index: EdgeIndex| self.decorated_edges.get(&index).unwrap();

let node_index_to_node = |index: NodeIndex| self.nodes.get(&index).unwrap();

path_collection
.into_iter()
.map(|path| DecoratedPath {
edges: path
.edges()
.into_iter()
.map(edge_index_to_decorated_edge)
.collect(),
nodes: path.nodes().into_iter().map(node_index_to_node).collect(),
})
.collect::<Vec<DecoratedPath>>()
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::graph::{
use super::engine::{
find_cycles, get_all_negative_cycles_0, get_all_negative_cycles_1,
get_all_negative_cycles_for_source, get_negative_cycle_for_source_quick,
get_negative_cycle_quick, has_cycle,
Expand Down
4 changes: 2 additions & 2 deletions arbitrage_engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod arbitrage_service;
pub mod decorated_edge;
pub mod graph;
mod graph_test;
pub mod engine;
mod engine_test;
pub mod path;
pub mod utils;
4 changes: 2 additions & 2 deletions arbitrage_engine/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::{cmp::Ordering, marker::PhantomData};
use crate::decorated_edge::DecoratedEdge;

pub struct DecoratedPath<'a> {
edges: Vec<&'a DecoratedEdge>,
nodes: Vec<&'a str>,
pub edges: Vec<&'a DecoratedEdge>,
pub nodes: Vec<&'a String>,
}

/// Represents a collection of connected graph nodes, in otherwords the arbitrage path
Expand Down

0 comments on commit d59d959

Please sign in to comment.