Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Apr 20, 2024
1 parent 4489b34 commit ea50688
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
12 changes: 12 additions & 0 deletions contracts/cosmwasm/order/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ impl OrderContract<'_> {
Ok(Response::default().add_event(order_created))
}

#[msg(query)]
pub fn has_stale(&self, ctx: QueryCtx) -> StdResult<bool> {
Ok(self
.orders
.range(ctx.deps.storage, None, None, Order::Ascending)
.any(|x| {
let (_id, order) = x.as_ref().unwrap();
order.msg.timeout < ctx.env.block.height
}))
}

/// Hook/crank for cleanup.
/// Caller receives small reward for doing so.
/// This is to prevent spamming of old orders.
Expand All @@ -143,6 +154,7 @@ impl OrderContract<'_> {
let (_id, order) = x.as_ref().unwrap();
order.msg.timeout < ctx.env.block.height
})
.take(30) // in batches
.collect();
let mut msgs = vec![];
for order in orders? {
Expand Down
30 changes: 4 additions & 26 deletions mantis/node/src/bin/mantis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use mantis_node::{
args::*,
autopilot, blackbox,
cosmos::{client::*, cosmwasm::to_exec_signed, *},
indexer::{get_active_orders, get_cvm_glt},
indexer::{get_active_orders, get_cvm_glt, has_stale_orders},
simulate,
solve::PairSolution,
},
Expand Down Expand Up @@ -80,7 +80,9 @@ async fn solve_orders(solver_args: &SolverArgs) {
get_latest_block_and_account_by_key(&args.rpc_centauri, &args.grpc_centauri, &signer)
.await;

if rand::random::<u8>() > 150 {
if rand::random::<u8>() > 200
|| has_stale_orders(&args.order_contract, &mut wasm_read_client, &tip).await
{
autopilot::cleanup(
&mut write_client,
&mut cosmos_query_client,
Expand All @@ -92,30 +94,6 @@ async fn solve_orders(solver_args: &SolverArgs) {
)
.await;
tip.account.sequence += 1;
} else {
let stale_orders = mantis_node::mantis::indexer::get_stale_orders(
&args.order_contract,
&mut wasm_read_client,
&tip,
)
.await;
if stale_orders.len() > 0 {
// need to have function to check count, cause this timeouts
if stale_orders.len() > 0 {
log::warn!(target: "mantis::autopilot", "timedouted orders");
}
autopilot::cleanup(
&mut write_client,
&mut cosmos_query_client,
args.order_contract.clone(),
&signer,
&cosmos_chain_info,
&tip,
gas,
)
.await;
tip.account.sequence += 1;
}
}

let mut tip =
Expand Down
9 changes: 9 additions & 0 deletions mantis/node/src/mantis/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub async fn get_stale_orders(
.collect::<Vec<OrderItem>>()
}

pub async fn has_stale_orders(
order_contract: &String,
cosmos_query_client: &mut CosmWasmReadClient,
tip: &Tip,
) -> bool {
let query = cw_mantis_order::QueryMsg::HasStale {};
smart_query::<_, bool>(order_contract, query, cosmos_query_client).await
}

pub async fn get_cvm_glt(
contract: &String,
cosmos_query_client: &mut CosmWasmReadClient,
Expand Down

0 comments on commit ea50688

Please sign in to comment.