Skip to content

Commit

Permalink
force update orer
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 13, 2024
1 parent 4ba55f7 commit cc4861b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
11 changes: 10 additions & 1 deletion contracts/cosmwasm/order/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::StdError;
use cosmwasm_std::{Coin, StdError};

pub fn amount_does_not_decrease_want() -> StdError {
StdError::generic_err("Amount does not decrease want")
Expand All @@ -15,3 +15,12 @@ pub fn filled_order_cannot_be_cross_chain_routed() -> StdError {
pub fn partial_cross_chain_not_implemented() -> StdError {
StdError::generic_err("Partial cross chain not implemented")
}

pub(crate) fn expected_some_funds_in_route() -> StdError {
StdError::generic_err("expected_some_funds_in_route")
}

pub fn banks_funds_must_be_at_least_routed(has: &Coin, expected: &Coin) -> StdError {
StdError::generic_err(format!("banks_funds_must_be_at_least_routed {:?} => {:?} ", has, expected))

}
40 changes: 31 additions & 9 deletions contracts/cosmwasm/order/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl OrderContract<'_> {
"mantis::timeout::cleaning orders {:?} and solutions {:?}",
orders, solutions
));

let orders: Result<Vec<(u128, OrderItem)>, _> = self
.orders
.range(ctx.deps.storage, None, None, Order::Ascending)
Expand Down Expand Up @@ -191,7 +192,7 @@ impl OrderContract<'_> {
);

ctx.deps.api.debug(
&format!("mantis::route:: so here we add route execution tracking to storage and map route to CVM program {:?}", msg)
&format!("mantis::order::route:: so here we add route execution tracking to storage and map route to CVM program {:?}", msg)
);

let cvm: cvm_runtime::outpost::ExecuteMsg =
Expand Down Expand Up @@ -220,7 +221,7 @@ impl OrderContract<'_> {
.map(|x| x.tracking.amount_taken.amount)
.sum();

let funds = vec![
let funds: Vec<Coin> = vec![
Coin {
denom: msg.pair.a,
amount: a_funds,
Expand All @@ -229,7 +230,26 @@ impl OrderContract<'_> {
denom: msg.pair.b,
amount: b_funds,
},
];
]
.into_iter()
.filter(|x| x.amount.u128() > 0)
.collect();

ensure!(
!funds.is_empty(),
crate::errors::expected_some_funds_in_route()
);

for fund in funds.iter().rev() {
let has = ctx
.deps
.querier
.query_balance(ctx.env.contract.address.to_string(), fund.denom.to_string())?;
ensure!(
has.amount >= fund.amount,
crate::errors::banks_funds_must_be_at_least_routed(&has, fund)
)
}

let events = cvm_filled
.iter()
Expand Down Expand Up @@ -265,7 +285,7 @@ impl OrderContract<'_> {
#[msg(exec)]
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 mut all_orders = join_solution_with_orders(&self.orders, &msg, &ctx)?;
let ab = all_orders.first().expect("at least one").pair();

// add solution to total solutions
Expand Down Expand Up @@ -493,11 +513,13 @@ impl OrderContract<'_> {
api: &dyn Api,
) -> StdResult<Vec<CowFillResult>> {
let mut results = vec![];
for (transfer, order) in cows.into_iter() {
let mut order: OrderItem = self.orders.load(storage, order.u128())?;
order.fill(transfer.amount, optimal_price)?;
for (transfer_coin, order_amount) in cows.into_iter() {
let mut order: OrderItem = self.orders.load(storage, order_amount.u128())?;
order.fill(transfer_coin.amount, optimal_price)?;

let (event, remaining) = if order.given.amount.is_zero() {
// hey, need some other data structure for this
TOOD
let (_idx, solver_order) = solver_orders
.iter()
.find_position(|x| x.order.order_id == order.order_id)
Expand All @@ -522,7 +544,7 @@ impl OrderContract<'_> {
(
mantis_order_filled_partially(
&order,
&transfer,
&transfer_coin,
&solver_address,
solution_block_added,
),
Expand All @@ -531,7 +553,7 @@ impl OrderContract<'_> {
};
let transfer = BankMsg::Send {
to_address: order.owner.to_string(),
amount: vec![transfer],
amount: vec![transfer_coin],
};
results.push(CowFillResult {
remaining: if remaining { Some(order) } else { None },
Expand Down
1 change: 0 additions & 1 deletion mantis/node/tests/cvms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,6 @@ async fn cvm_devnet_case() {
&[],
).unwrap();


//panic!("solution: {:?}", solution);
}

Expand Down

0 comments on commit cc4861b

Please sign in to comment.