From f74d71a9c389d046aa628694a2ad34e0e123a191 Mon Sep 17 00:00:00 2001 From: dzmitry-lahoda Date: Fri, 5 Apr 2024 19:47:30 +0100 Subject: [PATCH] native calculations --- crates/mantis-cw/src/ordered_coin_pair.rs | 20 ++++++++++++++++++++ mantis/node/src/mantis/solve.rs | 18 +++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/crates/mantis-cw/src/ordered_coin_pair.rs b/crates/mantis-cw/src/ordered_coin_pair.rs index c93a435d..bf7a1a0b 100644 --- a/crates/mantis-cw/src/ordered_coin_pair.rs +++ b/crates/mantis-cw/src/ordered_coin_pair.rs @@ -1,3 +1,5 @@ +use cosmwasm_std::{Coin, Uint128}; + use crate::{Denom, OrderedTuple2}; /// CosmWasm Coin pair ordered by denom @@ -14,6 +16,24 @@ impl OrderCoinPair { b: cosmwasm_std::Coin { denom : ab.b, ..Default::default()}, } } + + pub fn add_a(&mut self, amount: Uint128) { + self.a.amount += amount; + } + + pub fn add_b(&mut self, amount: Uint128) { + self.b.amount += amount; + } + + pub fn add(&mut self, coin: Coin) { + if coin.denom == self.a.denom { + self.a.amount += coin.amount; + } else if coin.denom == self.b.denom { + self.b.amount += coin.amount; + } else { + panic!("invalid coin denom"); + } + } } impl From<(Denom, Denom)> for OrderCoinPair { diff --git a/mantis/node/src/mantis/solve.rs b/mantis/node/src/mantis/solve.rs index 24581bfe..180c421d 100644 --- a/mantis/node/src/mantis/solve.rs +++ b/mantis/node/src/mantis/solve.rs @@ -42,18 +42,30 @@ impl IntentBankInput { /// given CoW solution and total amount of assets, aggregate remaining to bank for two sides pub fn find_intent_amount(cows: &[OrderSolution], orders: &[OrderItem], optimal_ratio: Ratio, cvm_glt: &GetConfigResponse, pair: DenomPair ) -> (IntentBankInput, IntentBankInput) { - let pair = OrderCoinPair::from(pair); + // native calculations + let mut pair = OrderCoinPair::from(pair); + let mut a_to_b = Vec::new(); + let mut b_to_a = Vec::new(); + for cow in cows { match cow.cross_chain_part { Some(OrderAmount::All) => { let order = orders.iter().find(|x| x.order_id == cow.order_id).expect("order").clone(); - let remaining = order.remaining(cow.cow_out_amount, optimal_ratio); - + order.fill(cow.cow_out_amount, optimal_ratio).expect("off chain"); + pair.add(order.given); + + if order.given.denom == pair.a.denom { + a_to_b.push((order.owner, order.given.amount)); + } else { + b_to_a.push((order.owner, order.given.amount)); + } }, None => {}, _ => panic!("unsupported cross chain part") } } + + // making MANTIS route request in CVM form } }