Skip to content

Commit

Permalink
magic
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 11, 2024
1 parent 0f9990b commit c8c12a7
Show file tree
Hide file tree
Showing 12 changed files with 131 additions and 109 deletions.
16 changes: 11 additions & 5 deletions contracts/cosmwasm/order/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,23 @@ mod test {
convert: None,
timeout: 1,
min_fill: None,
virtual_given: None,
},
given: Coin {
denom: "given".to_string(),
amount: 100u128.into(),
},
order_id: 1u128.into(),
};
order.fill(50u128.into(), optimal_price).unwrap();
order.fill(50u128.into(), optimal_price.into()).unwrap();
assert_eq!(order.given.amount, Uint128::from(50u128));
assert_eq!(order.msg.wants.amount, Uint128::from(50u128));
order.fill(15u128.into(), optimal_price).unwrap();
order.fill(15u128.into(), optimal_price.into()).unwrap();
assert_eq!(order.given.amount, Uint128::from(35u128));
assert_eq!(order.msg.wants.amount, Uint128::from(35u128));
order.fill(Uint128::from(50u128), optimal_price).unwrap();
order
.fill(Uint128::from(50u128), optimal_price.into())
.unwrap();
assert_eq!(order.given.amount, Uint128::from(0u128));
assert_eq!(order.msg.wants.amount, Uint128::from(0u128));

Expand All @@ -139,6 +142,7 @@ mod test {
convert: None,
timeout: 1,
min_fill: None,
virtual_given: None,
},
given: Coin {
denom: "given".to_string(),
Expand All @@ -147,8 +151,10 @@ mod test {
order_id: 1u128.into(),
};

assert!(order.fill(500u128.into(), optimal_price).is_err());
order.fill(50000000u128.into(), optimal_price).unwrap();
assert!(order.fill(500u128.into(), optimal_price.into()).is_err());
order
.fill(50000000u128.into(), optimal_price.into())
.unwrap();
assert_eq!(order.given.amount, Uint128::from(98u128));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cvm-runtime/src/outpost/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct GetAssetResponse {
pub asset: AssetItem,
}

pub type CvmGlt = GetConfigResponse;
pub type CvmGlt = GetConfigResponse;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
Expand Down
12 changes: 9 additions & 3 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async fn simulate_orders(simulate_args: &SimulateArgs) {
.await;
}

enum CoinToss{}
enum CoinToss {}

impl Get<bool> for CoinToss {
fn get() -> bool {
Expand Down Expand Up @@ -189,14 +189,20 @@ async fn get_data_and_solve(
None => None,
};

let msgs = solve::<CoinToss>(all_orders, signing_key, tip, cvm_glt, router_api).await;
let msgs = mantis_node::mantis::blackbox::solve::<CoinToss>(
all_orders,
signing_key,
tip,
cvm_glt,
router_api,
)
.await;

for msg in msgs {
send_solution(msg, tip, signing_key, order_contract, rpc, gas).await;
}
}


async fn send_solution(
msg: cw_mantis_order::ExecMsg,
tip: &Tip,
Expand Down
3 changes: 2 additions & 1 deletion mantis/node/src/bin/simulator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use mantis_cw::OrderSide;
use mantis_node::solver::{orderbook::OrderList, solution::Solution, types::SolverOrder};
use mantis_node::solver::cows::{orderbook::OrderList, solution::Solution};
use mantis_node::solver::types::SolverOrder;
use mantis_node::{prelude::*, solver::types::Price};

fn main() {
Expand Down
1 change: 1 addition & 0 deletions mantis/node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(let_chains)]
pub mod mantis;
pub mod prelude;
pub mod solver;
93 changes: 92 additions & 1 deletion mantis/node/src/mantis/blackbox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use blackbox_rs::{types::*, Client};
use bounded_collections::Get;
/// Given total amount it, order owners and desired out, produce CVM program from and by requesting route
use cvm_runtime::{
outpost::GetConfigResponse,
shared::{CvmFundsFilter, CvmInstruction, CvmProgram},
Amount,
};
use cw_mantis_order::{CrossChainPart, OrderItem, SolutionSubMsg};

use crate::solver::router::shortest_path;

use super::solve::IntentBankInput;
use super::{
cosmos::client::Tip,
solve::{find_cows, IntentBankInput, PairSolution},
};

/// given route and CVM stub with amount, build it to the end
fn build_next(
Expand Down Expand Up @@ -136,3 +141,89 @@ pub async fn get_route(
panic!("so need to build instruction so can plug into one program (transaciton)")
}
}

pub async fn solve<Decider: Get<bool>>(
active_orders: Vec<OrderItem>,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
tip: &Tip,
cvm_glt: Option<cw_cvm_outpost::msg::CvmGlt>,
router: &String,
) -> Vec<cw_mantis_order::ExecMsg> {
let cows_per_pair = find_cows(&active_orders);
let mut msgs = vec![];
for pair_solution in cows_per_pair {
let salt = super::cosmos::cvm::calculate_salt(signing_key, tip, pair_solution.ab.clone());
let cvm_program = if let Some(ref cvm_glt) = cvm_glt {
let cvm_program = intent_banks_to_cvm_program(
pair_solution.clone(),
&active_orders,
cvm_glt,
router,
&salt,
)
.await;

Some(cvm_program)
} else {
None
};

// would be reasonable to do do cross chain if it solves some % of whole trade
let route = if let Some(cvm_program) = cvm_program
&& Decider::get()
{
Some(CrossChainPart::new(
cvm_program,
salt.clone(),
pair_solution.optimal_price.into(),
))
} else {
None
};
let msg = SolutionSubMsg {
cows: pair_solution.cows.clone(),
route,
timeout: tip.timeout(12),
optimal_price: pair_solution.optimal_price.into(),
};
let msg = cw_mantis_order::ExecMsg::Solve { msg };
msgs.push(msg);
}
msgs
}

async fn intent_banks_to_cvm_program(
pair_solution: PairSolution,
all_orders: &Vec<OrderItem>,
cvm_glt: &cw_cvm_outpost::msg::GetConfigResponse,
router_api: &String,
salt: &Vec<u8>,
) -> CvmProgram {
let (a, b) = IntentBankInput::find_intent_amount(
pair_solution.cows.as_ref(),
all_orders,
pair_solution.optimal_price,
cvm_glt,
pair_solution.ab.clone(),
);

log::info!(target:"mantis::solver::", "found for cross chain a: {:?}, b: {:?}", a, b);

let mut instructions = vec![];

if a.in_asset_amount.0.gt(&0) {
let mut a_cvm_route = get_route(router_api, a, cvm_glt, salt.as_ref()).await;
instructions.append(&mut a_cvm_route);
}
if b.in_asset_amount.0.gt(&0) {
let mut b_cvm_route = get_route(router_api, b, cvm_glt, salt.as_ref()).await;
instructions.append(&mut b_cvm_route);
}
log::info!(target: "mantis::solver", "built instructions: {:?}", instructions);

let cvm_program = CvmProgram {
tag: salt.to_vec(),
instructions,
};
cvm_program
}
6 changes: 4 additions & 2 deletions mantis/node/src/mantis/cosmos/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ pub struct BlockAgent {

impl Debug for BlockAgent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BlockAgent").field("tip", &self.tip).field("public_key", &self.key.public_key()).finish()
f.debug_struct("BlockAgent")
.field("tip", &self.tip)
.field("public_key", &self.key.public_key())
.finish()
}
}


impl Tip {
pub fn timeout(&self, delta: u32) -> u64 {
self.block.value() + delta as u64
Expand Down
8 changes: 6 additions & 2 deletions mantis/node/src/mantis/cosmos/cvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ use super::client::Tip;

/// given key and latest block and sequence number, produces binary salt for cross chain block
/// isolating execution of one cross chain transaction from other
pub fn calculate_salt(signing_key: &cosmrs::crypto::secp256k1::SigningKey, tip: &Tip, pair: DenomPair) -> Vec<u8> {
use sha2::{Sha256, Digest};
pub fn calculate_salt(
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
tip: &Tip,
pair: DenomPair,
) -> Vec<u8> {
use sha2::{Digest, Sha256};
let mut base = signing_key.public_key().to_bytes().to_vec();
base.extend(tip.block.value().to_be_bytes().to_vec());
base.extend(tip.account.sequence.to_be_bytes().to_vec());
Expand Down
2 changes: 1 addition & 1 deletion mantis/node/src/mantis/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use num_rational::Ratio;

use crate::{
prelude::*,
solver::{orderbook::OrderList, solution::Solution},
solver::cows::{orderbook::OrderList, solution::Solution},
};

/// input batched summarized from users for routing
Expand Down
5 changes: 3 additions & 2 deletions mantis/node/src/solver/cows/optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use mantis_cw::OrderSide;
use rand_distr::num_traits::FromPrimitive;

use crate::prelude::*;
use crate::solver::orderbook::*;
use crate::solver::solution::Solution;
use crate::solver::cows::orderbook::*;
use crate::solver::types::*;

use super::solution::Solution;

#[derive(Clone, Debug)]
pub struct Solver<Id> {
orders: OrderList<Id>,
Expand Down
2 changes: 1 addition & 1 deletion mantis/node/src/solver/cows/solution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::orderbook::*;
use crate::prelude::*;
use crate::solver::orderbook::*;
use crate::solver::types::*;

#[derive(Clone, Debug)]
Expand Down
90 changes: 0 additions & 90 deletions mantis/node/src/solver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,93 +1,3 @@
pub mod cows;
pub mod router;
pub mod types;



async fn solve<Decider: Get<bool>>(
active_orders: Vec<OrderItem>,
signing_key: &cosmrs::crypto::secp256k1::SigningKey,
tip: &Tip,
cvm_glt: Option<cw_cvm_outpost::msg::CvmGlt>,
router: &String,
) -> Vec<cw_mantis_order::ExecMsg> {
let cows_per_pair = mantis_node::mantis::solve::find_cows(&active_orders);
let mut msgs = vec![];
for pair_solution in cows_per_pair {
let salt = crate::cvm::calculate_salt(signing_key, tip, pair_solution.ab.clone());
let cvm_program = if let Some(ref cvm_glt) = cvm_glt {
let cvm_program = intent_banks_to_cvm_program(
pair_solution.clone(),
&active_orders,
cvm_glt,
router,
&salt,
)
.await;

Some(cvm_program)
} else {
None
};

// would be reasonable to do do cross chain if it solves some % of whole trade
let route = if let Some(cvm_program) = cvm_program
&& Decider::get()
{
Some(CrossChainPart::new(
cvm_program,
salt.clone(),
pair_solution.optimal_price.into(),
))
} else {
None
};
let msg = SolutionSubMsg {
cows: pair_solution.cows.clone(),
route,
timeout: tip.timeout(12),
optimal_price: pair_solution.optimal_price.into(),
};
let msg = cw_mantis_order::ExecMsg::Solve { msg };
msgs.push(msg);
}
msgs
}



async fn intent_banks_to_cvm_program(
pair_solution: PairSolution,
all_orders: &Vec<OrderItem>,
cvm_glt: &cw_cvm_outpost::msg::GetConfigResponse,
router_api: &String,
salt: &Vec<u8>,
) -> CvmProgram {
let (a, b) = mantis_node::mantis::solve::IntentBankInput::find_intent_amount(
pair_solution.cows.as_ref(),
all_orders,
pair_solution.optimal_price,
cvm_glt,
pair_solution.ab.clone(),
);

log::info!(target:"mantis::solver::", "found for cross chain a: {:?}, b: {:?}", a, b);

let mut instructions = vec![];

if a.in_asset_amount.0.gt(&0) {
let mut a_cvm_route = blackbox::get_route(router_api, a, cvm_glt, salt.as_ref()).await;
instructions.append(&mut a_cvm_route);
}
if b.in_asset_amount.0.gt(&0) {
let mut b_cvm_route = blackbox::get_route(router_api, b, cvm_glt, salt.as_ref()).await;
instructions.append(&mut b_cvm_route);
}
log::info!(target: "mantis::solver", "built instructions: {:?}", instructions);

let cvm_program = CvmProgram {
tag: salt.to_vec(),
instructions,
};
cvm_program
}

0 comments on commit c8c12a7

Please sign in to comment.