Skip to content

Commit

Permalink
oh my type
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Mar 25, 2024
1 parent faca2d4 commit 1b77071
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions mantis/node/src/mantis/blackbox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use blackbox_rs::{prelude::*, types::*, Client};
/// Given total amount it, order owners and desired out, produce CVM program from and by requesting route
use cvm_runtime::{
outpost::GetConfigResponse, proto::cvm, shared::{CvmInstruction, CvmProgram, Displayed, XcAddr}, Amount, AssetId, ExchangeId
outpost::GetConfigResponse,
proto::cvm,
shared::{CvmFundsFilter, CvmInstruction, CvmProgram, Displayed, XcAddr},
Amount, AssetId, ExchangeId,
};


/// input batched summarized from users for routing
/// input batched summarized from users for routing
struct BankInput {
pub in_asset_id: AssetId,
pub in_asset_amount: Displayed<u64>,
Expand All @@ -32,46 +34,58 @@ impl BankInput {
/// given route and CVM stub with amount, build it to the end
fn build_next(current: &mut CvmProgram, next: &mut [NextItem]) {
match next.split_first_mut() {
Some((head, rest)) => {
match head {
NextItem::Exchange(exchange) => {
let exchange = new_exchange(exchange);
current.instructions.push(CvmInstruction::Exchange(exchange));
build_next(current, rest);
},
NextItem::Spawn(spawn) => {
let spawn = new_spawn(spawn);
current.instructions.push(CvmInstruction::Spawn(spawn));
build_next(spawn.program, rest);
},
Some((head, rest)) => match head {
NextItem::Exchange(exchange) => {
let exchange = new_exchange(exchange);
current
.instructions
.push(exchange);
build_next(current, rest);
}
NextItem::Spawn(spawn) => {
let spawn = new_spawn(spawn);
current.instructions.push(CvmInstruction::Spawn(spawn));
build_next(spawn.program, rest);
}
}
None => {
log::info!("no more routes");
},
None => {
log::info!("no more routes");
}
}
}

fn new_exchange(exchange: &Exchange) -> CvmInstruction {
let exchange_id = match exchange.pool_id {
PoolId::Variant1(id) => id.parse().expect("pool id"),
_ => panic!("exchange_id")
_ => panic!("exchange_id"),
};
CvmInstruction::Exchange{
let in_asset_id = match exchange.in_asset_id {
InAssetId::Variant1(id) => id.parse().expect("in_asset_id"),
_ => panic!("in_asset_id"),
};

let in_amount : Amount = match exchange.in_asset_amount {
InAssetAmount::Variant0(x) => x.try_into().expect("in_asset_amount"),
InAssetAmount::Variant1(x) => x.parse().expect("in_asset_amount"),
InAssetAmount::Variant2(x) => x.try_into().expect("in_asset_amount"),
};

let out_asset_id = match exchange.out_asset_id {
OutAssetId::Variant1(id) => id.parse().expect("in_asset_id"),
_ => panic!("in_asset_id"),
};

CvmInstruction::Exchange {
exchange_id,
give: todo!(),
want: todo!(),
give: CvmFundsFilter::one(in_asset_id, in_amount),
want: CvmFundsFilter::one(out_asset_id, 1.into()),
}
}

/// `order_accounts` - account of order where to dispatch amounts (part of whole)
async fn route(
server: &str,
input : BankInput,
glt: GetConfigResponse,
) -> CvmProgram {
async fn route(server: &str, input: BankInput, glt: GetConfigResponse) -> CvmProgram {
let blackbox: Client = Client::new(server);
let mut route = blackbox
let mut route = blackbox
.simulator_router_simulator_router_get(
&InAssetAmount::Variant0(input.in_asset_amount.0.try_into().expect("in_asset_amount")),
&InAssetId::Variant1(input.in_asset_id.to_string()),
Expand All @@ -81,9 +95,11 @@ async fn route(
)
.await
.expect("route found")
.into_inner().pop().expect("at least one route");
.into_inner()
.pop()
.expect("at least one route");

let mut program = CvmProgram::default();
build_next(&mut program, &mut route.next);
return program;
return program;
}

0 comments on commit 1b77071

Please sign in to comment.