Skip to content

Commit

Permalink
Merge pull request #6 from ComposableFi/dz/5
Browse files Browse the repository at this point in the history
feat: orders/solutions mapping and sending to tx
  • Loading branch information
dzmitry-lahoda authored Nov 21, 2023
2 parents f085b4d + 837ee8c commit 779e1ca
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
47 changes: 40 additions & 7 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use mantis_node::{
},
},
prelude::*,
solver::{orderbook::OrderList, solution::Solution},
};

#[tokio::main]
Expand Down Expand Up @@ -199,17 +200,49 @@ async fn solve(
cvm_contract: &String,
) {
let query = cw_mantis_order::QueryMsg::GetAllOrders {};
let orders: Vec<OrderItem> = smart_query(order_contract, query, read).await;
let orders = orders.into_iter().group_by(|x| {
let all_orders: Vec<OrderItem> = smart_query(order_contract, query, read).await;
let all_orders = all_orders.into_iter().group_by(|x| {
let mut ab = (x.given.denom.clone(), x.msg.wants.denom.clone());
ab.sort_selection();
ab
});
for (((a,b)), orders) in orders.into_iter() {
for ((a, b), orders) in all_orders.into_iter() {
let orders = orders.collect::<Vec<_>>();
use mantis_node::solver::solver::*;
use mantis_node::solver::types::*;
let orders = orders.iter().map(|x| {
let (side, price) = if x.given.denom == a {
(
OrderType::Buy,
Price::new_float(
x.msg.wants.amount.u128() as f64 / x.given.amount.u128() as f64,
),
)
} else {
(
OrderType::Sell,
Price::new_float(
x.given.amount.u128() as f64 / x.msg.wants.amount.u128() as f64,
),
)
};

mantis_node::solver::types::Order::new(
Amount::from_f64_retain(x.given.amount.u128() as f64).expect("decimal"),
price,
side,
x.order_id,
)
});
let orders = OrderList {
value: orders.collect(),
};
orders.print();
let optimal_price = orders.compute_optimal_price(1000);
let mut solution = Solution::new(orders.value.clone());
solution = solution.match_orders(optimal_price);
solution.print();


// solve here !
// post solution
// just print them for now
println!("pair {pair:?} orders: {:?}", orders.collect::<Vec<_>>());
}
}
1 change: 1 addition & 0 deletions mantis/node/src/bin/simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
/// finds maximal volume price
let optimal_price = orders.compute_optimal_price(50);

/// fill orders
let mut solution = Solution::new(orders.value.clone());
solution = solution.match_orders(optimal_price);
solution.print();
Expand Down
2 changes: 1 addition & 1 deletion mantis/node/src/solver/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<Id: Copy + PartialEq + Debug> Solution<Id> {
self.sell_volume.0,
self.orders.sell().amount_filled().0,
);
}
}
}

pub fn print(&self) {
Expand Down

0 comments on commit 779e1ca

Please sign in to comment.