Skip to content

Commit

Permalink
rational are bad
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Nov 22, 2023
1 parent 96c73ea commit 8acc22b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
37 changes: 18 additions & 19 deletions contracts/cosmwasm/order/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ use sylvia::{
types::{ExecCtx, InstantiateCtx, QueryCtx},
};

/// so this is just to make code easy to read, we will optimize later
use num_rational::BigRational;

use cvm::network::NetworkId;

pub struct OrderContract<'a> {
Expand Down Expand Up @@ -311,11 +308,8 @@ impl OrderContract<'_> {
.map(|x| x.given().amount.u128())
.sum();

let alternative_transfers = solves_cows_via_bank(
&alternative_all_orders.clone(),
a_total_in,
b_total_in,
);
let alternative_transfers =
solves_cows_via_bank(&alternative_all_orders.clone(), a_total_in, b_total_in);

ctx.deps.api.debug(&format!(
"mantis::solutions::alternative {:?}",
Expand Down Expand Up @@ -472,11 +466,9 @@ fn order_created(order_id: u128, order: &OrderItem) -> Event {
/// and return proper action to handle settling funds locally according solution
fn solves_cows_via_bank(
all_orders: &Vec<SolvedOrder>,
a_total_in: u128,
b_total_in: u128,
mut a_total_in: u128,
mut b_total_in: u128,
) -> Result<Vec<CowFilledOrder>, StdError> {
let mut a_total_in = BigRational::from_integer(a_total_in.into());
let mut b_total_in = BigRational::from_integer(b_total_in.into());
let mut transfers = vec![];
for order in all_orders.iter() {
let cowed = order.solution.cow_amount;
Expand All @@ -488,17 +480,24 @@ fn solves_cows_via_bank(
// so if not enough was deposited as was taken from original orders, it will fail - so
// solver cannot rob the bank
if order.pair().0 == filled_wanted.denom {
a_total_in -= BigRational::from_integer(cowed.u128().into());
a_total_in = a_total_in.checked_sub(cowed.u128()).ok_or_else(|| {
StdError::generic_err(format!(
"a underflow: {} - {}",
a_total_in,
cowed.u128().to_string()
))
})?;
} else {
b_total_in -= BigRational::from_integer(cowed.u128().into());
b_total_in = b_total_in.checked_sub(cowed.u128()).ok_or_else(|| {
StdError::generic_err(format!(
"b underflow: {} - {}",
b_total_in,
cowed.u128().to_string()
))
})?;
};

transfers.push((filled_wanted, order.order.order_id));
}
if a_total_in < BigRational::default() || b_total_in < BigRational::default() {
return Err(StdError::generic_err(format!(
"SolutionForCowsViaBankIsNotBalanced"
)));
}
Ok(transfers)
}
2 changes: 1 addition & 1 deletion mainnet.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/sh
RUST_BACKTRACE=1 RUST_TRACE=trace cargo run --bin mantis -- --rpc-centauri "https://composable-rpc.polkachu.com:443" --grpc-centauri "https://composable-grpc.polkachu.com:22290" --osmosis "todo" --neutron "todo" --cvm-contract "centauri1wpf2szs4uazej8pe7g8vlck34u24cvxx7ys0esfq6tuw8yxygzuqpjsn0d" --wallet "$WALLET" --order-contract "centauri18ag4r8s6qug4gxwllzw04nj3ht6unk9g93e9sj5lscsukfkf4j6qxe7wsx" --simulate "200000ppica,3ibc/EF48E6B1A1A19F47ECAEA62F5670C37C0580E86A9E88498B7E393EB6F49F33C0"
RUST_BACKTRACE=1 RUST_TRACE=trace cargo run --bin mantis -- --rpc-centauri "https://composable-rpc.polkachu.com:443" --grpc-centauri "https://composable-grpc.polkachu.com:22290" --osmosis "todo" --neutron "todo" --cvm-contract "centauri1wpf2szs4uazej8pe7g8vlck34u24cvxx7ys0esfq6tuw8yxygzuqpjsn0d" --wallet "$WALLET" --order-contract "centauri1p2fj89ncs8gulm2trwuaz7yz9r5nujd9azrx5mxn5pah8dy8cl0qgg90ec" --simulate "200000ppica,3ibc/EF48E6B1A1A19F47ECAEA62F5670C37C0580E86A9E88498B7E393EB6F49F33C0"

0 comments on commit 8acc22b

Please sign in to comment.