Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 10, 2024
1 parent 41fe876 commit 77114e5
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 33 deletions.
6 changes: 4 additions & 2 deletions contracts/cosmwasm/order/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,14 @@ impl OrderContract<'_> {

/// Provides solution for set of orders.
/// All fully
/// Solver does not pay for solution evaluation upfront, but slashed if his solution violates constrains upon best solution picked.
/// So solver cheap to update his solution and prevent spam.
#[msg(exec)]
pub fn solve(&self, mut ctx: ExecCtx, msg: SolutionSubMsg) -> StdResult<Response> {
pub fn solve(&self, mut ctx: ExecCtx, msg: SolutionSubMsg) -> StdResult<Response> {
// read all orders as solver provided
let mut all_orders = join_solution_with_orders(&self.orders, &msg, &ctx)?;
let at_least_one = all_orders.first().expect("at least one");

// normalize pair
let ab = DenomPair::new(
at_least_one.given().denom.clone(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/cosmwasm/order/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ pub struct OrderItem {
}

impl OrderItem {
pub fn side(&self) -> OrderSide {
if self.msg.wants.denom == self.given.denom {
pub fn side(&self, pair: &DenomPair) -> OrderSide {
if self.given.denom == pair.a {
OrderSide::A
} else {
OrderSide::B
Expand Down
8 changes: 4 additions & 4 deletions mantis/node/src/bin/simulator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use mantis_cw::OrderSide;
use mantis_node::solver::{orderbook::OrderList, solution::Solution, types::Order};
use mantis_node::solver::{orderbook::OrderList, solution::Solution, types::SolverOrder};
use mantis_node::{prelude::*, solver::types::Price};

fn main() {
// decide on basics
let order_a = Order::new_integer(100_000, 3, OrderSide::B, 1);
let order_b = Order::new_integer(3, 100_000, OrderSide::A, 2);
let order_a = SolverOrder::new_integer(100_000, 3, OrderSide::B, 1);
let order_b = SolverOrder::new_integer(3, 100_000, OrderSide::A, 2);
assert!(order_a.is_acceptable_price(order_b.limit_price));
assert!(order_b.is_acceptable_price(order_a.limit_price));

Expand Down Expand Up @@ -35,7 +35,7 @@ fn main() {
);

// randomize price around 2.0 (ratio of 2 price tokens in pair)
let orders = (1..100).map(|x| Order::random_f64(2., 0.1, (50, 150), x));
let orders = (1..100).map(|x| SolverOrder::random_f64(2., 0.1, (50, 150), x));
let orders = OrderList {
value: orders.collect(),
};
Expand Down
8 changes: 4 additions & 4 deletions mantis/node/src/mantis/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,19 @@ pub fn find_cows(all_orders: &[OrderItem]) -> Vec<PairSolution> {
for (ab, orders) in all_orders.into_iter() {
let orders = orders.collect::<Vec<_>>();
let orders = orders.iter().map(|x| {
crate::solver::types::Order::new_integer(
crate::solver::types::SolverOrder::new_integer(
x.given.amount.u128(),
x.msg.wants.amount.u128(),
x.side(),
x.side(&ab),
x.order_id,
)
});
let orders = OrderList {
value: orders.collect(),
};
let optimal_price = orders.compute_optimal_price(1000);
println!("optimal_price: {:?}", optimal_price);
println!("mantis::solver::cows::optimal_price: {:?}", optimal_price);
println!("mantis::solver::cows::orders: {:?}", orders);
let mut solution = Solution::new(orders.value.clone());
solution = solution.match_orders(optimal_price);
let cows = solution
Expand All @@ -141,7 +142,6 @@ pub fn find_cows(all_orders: &[OrderItem]) -> Vec<PairSolution> {
}
})
.collect::<Vec<_>>();
println!("optimal price {:?}", optimal_price);
let optimal_price = decimal_to_fraction(optimal_price.0);
println!("cows: {:?}", cows);
if !cows.is_empty() {
Expand Down
14 changes: 7 additions & 7 deletions mantis/node/src/solver/cows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Solver<Id> {
target_price: Price,
buy_token: BuyToken,
sell_token: SellToken,
order: Order<Id>,
order: SolverOrder<Id>,
}

impl<Id: Copy + PartialEq + Debug> Solver<Id> {
Expand All @@ -29,15 +29,15 @@ impl<Id: Copy + PartialEq + Debug> Solver<Id> {
target_price,
buy_token,
sell_token,
order: Order::new_decimal(dec!(0.0), Price(dec!(0.0)), OrderSide::A, solver_order_id),
order: SolverOrder::new_decimal(dec!(0.0), Price(dec!(0.0)), OrderSide::A, solver_order_id),
}
}

pub fn limit_price(&self) -> Price {
self.target_price
}

fn f_maximize(&self, order: &Order<Id>) -> Amount {
fn f_maximize(&self, order: &SolverOrder<Id>) -> Amount {
let decimal = match order.order_type {
OrderSide::A => {
self.buy_token.0 - order.amount_filled
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<Id: Copy + PartialEq + Debug> Solver<Id> {

let side = if is_buy { OrderSide::A } else { OrderSide::B };

let orders: Vec<Order<_>> = (0..=num_orders)
let orders: Vec<SolverOrder<_>> = (0..=num_orders)
.map(|i| {
self.order_for(
Amount::from_usize(i).expect("works") * original_token_amount
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<Id: Copy + PartialEq + Debug> Solver<Id> {
max_solution.ok_or("No max solution found")
}

fn match_ob_with_order(&self, order: &Order<Id>) -> Result<Solution<Id>, &'static str> {
fn match_ob_with_order(&self, order: &SolverOrder<Id>) -> Result<Solution<Id>, &'static str> {
let mut orderbook = self.orders.clone();
orderbook.value.push(order.clone());
orderbook
Expand All @@ -109,7 +109,7 @@ impl<Id: Copy + PartialEq + Debug> Solver<Id> {
Ok(Solution::new(orderbook.value).match_orders(optimal_price))
}

fn order_for(&self, amount: Decimal, order_type: OrderSide, id: Id) -> Order<Id> {
Order::new_decimal(amount, self.limit_price(), order_type, id)
fn order_for(&self, amount: Decimal, order_type: OrderSide, id: Id) -> SolverOrder<Id> {
SolverOrder::new_decimal(amount, self.limit_price(), order_type, id)
}
}
6 changes: 3 additions & 3 deletions mantis/node/src/solver/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::solver::types::*;

#[derive(Clone, Debug)]
pub struct OrderList<Id> {
pub value: Vec<Order<Id>>,
pub value: Vec<SolverOrder<Id>>,
}

impl<Id: Copy + PartialEq + Debug> OrderList<Id> {
Expand All @@ -15,7 +15,7 @@ impl<Id: Copy + PartialEq + Debug> OrderList<Id> {
}
fn apply_filter<P>(&self, expr: P) -> Self
where
P: FnMut(&Order<Id>) -> bool,
P: FnMut(&SolverOrder<Id>) -> bool,
{
OrderList {
value: self.value.iter().cloned().filter(expr).collect(),
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<Id: Copy + PartialEq + Debug> OrderList<Id> {
self.apply_filter(|order| order.id == id)
}

pub fn all(&self) -> &Vec<Order<Id>> {
pub fn all(&self) -> &Vec<SolverOrder<Id>> {
&self.value
}

Expand Down
4 changes: 2 additions & 2 deletions mantis/node/src/solver/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Solution<Id> {

impl<Id: Copy + PartialEq + Debug> Solution<Id> {
/// ensures orders are sorted
pub fn new(mut orders: Vec<Order<Id>>) -> Self {
pub fn new(mut orders: Vec<SolverOrder<Id>>) -> Self {
orders.sort_by(|a, b| {
a.limit_price
.partial_cmp(&b.limit_price)
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<Id: Copy + PartialEq + Debug> Solution<Id> {
) -> Self {
Self::new(
(0..num_orders)
.map(|_| Order::random_f64(mean, std, volume_range, next()))
.map(|_| SolverOrder::random_f64(mean, std, volume_range, next()))
.collect(),
)
}
Expand Down
20 changes: 11 additions & 9 deletions mantis/node/src/solver/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ pub enum OrderBookStatus {
Matched,
}


/// Order as handled by solver math
#[derive(Debug, Clone)]
pub struct Order<Id> {
pub struct SolverOrder<Id> {
pub amount_in: Amount,
pub filled_price: Amount,
pub order_type: OrderSide,
Expand All @@ -48,7 +50,7 @@ pub struct Order<Id> {
pub limit_price: Price,
}

impl<Id: Copy + PartialEq> Order<Id> {
impl<Id: Copy + PartialEq> SolverOrder<Id> {
pub fn print(&self) {
println!(
"[{}]-{}- Limit Price: {}, In: {}, Filled: {}, Filled price: {}, Out: {}",
Expand All @@ -67,7 +69,7 @@ impl<Id: Copy + PartialEq> Order<Id> {
order_type: OrderSide,
id: Id,
) -> Self {
Order {
SolverOrder {
amount_in,
filled_price: dec!(0.0),
order_type,
Expand All @@ -79,17 +81,17 @@ impl<Id: Copy + PartialEq> Order<Id> {
}
}

pub fn new_integer(amount_in: u128, min_want: u128, order_type: OrderSide, id: Id) -> Self {
pub fn new_integer(amount_in: u128, min_want: u128, order_side: OrderSide, id: Id) -> Self {
let amount_in: Amount = amount_in.try_into().expect("smaller");
let min_want: Amount = min_want.try_into().expect("smaller");
let limit_price = match order_type {
let limit_price = match order_side {
OrderSide::A => amount_in / min_want,
OrderSide::B => min_want / amount_in,
};
Order {
SolverOrder {
amount_in,
filled_price: dec!(0.0),
order_type,
order_type: order_side,
amount_out: dec!(0.0),
amount_filled: dec!(0.0),
status: OrderStatus::Pending,
Expand Down Expand Up @@ -186,7 +188,7 @@ impl<Id: Copy + PartialEq> Order<Id> {
}
}

impl<Id: Copy + PartialEq> Order<Id> {
impl<Id: Copy + PartialEq> SolverOrder<Id> {
pub fn random_f64(mean: f64, std: f64, volume_range: (u64, u64), id: Id) -> Self {
let amount_in = rand::thread_rng().gen_range(volume_range.0..volume_range.1 + 1) as f64;
let normal = rand_distr::Normal::new(mean, std).unwrap();
Expand All @@ -198,7 +200,7 @@ impl<Id: Copy + PartialEq> Order<Id> {
OrderSide::B
};

Order::new_decimal(
SolverOrder::new_decimal(
Decimal::from_f64_retain(amount_in).unwrap(),
Price(Decimal::from_f64_retain(limit_price).unwrap()),
order_type,
Expand Down
2 changes: 2 additions & 0 deletions mantis/node/tests/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fn cows_two_perfect_math_orders () {

// try solve
let orders = query_all_orders(&deps, &env);
assert_eq!(orders.len() , 2);

let cows_per_pair = mantis_node::mantis::solve::find_cows(orders.as_slice());
assert_eq!(cows_per_pair.len() , 1);
do_solve(cows_per_pair, &mut deps, &env, info.clone());
Expand Down

0 comments on commit 77114e5

Please sign in to comment.