From e090b042f243eecbb76f39b96ded5afea28ce08e Mon Sep 17 00:00:00 2001 From: codehans <94654388+codehans@users.noreply.github.com> Date: Mon, 11 Mar 2024 13:48:48 +0000 Subject: [PATCH] scan for next valid action --- src/contract.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/contract.rs b/src/contract.rs index 5770c48..f0804e8 100644 --- a/src/contract.rs +++ b/src/contract.rs @@ -1,8 +1,8 @@ #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - to_json_binary, Binary, Deps, DepsMut, Env, Event, MessageInfo, Reply, Response, StdResult, - SubMsg, + to_json_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Env, Event, MessageInfo, Reply, + Response, StdResult, SubMsg, }; use kujira::{fee_address, Denom}; @@ -62,23 +62,37 @@ pub fn execute( pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result { match msg { SudoMsg::Run {} => { - if let Some(action) = Action::next(deps.storage)? { - let balance = deps - .querier - .query_balance(env.contract.address, action.denom.to_string())?; - if let Some(msg) = action.execute(balance)? { - let event = - Event::new("revenue/run").add_attribute("denom", action.denom.to_string()); - return Ok(Response::default() - .add_event(event) - .add_submessage(SubMsg::reply_always(msg, 0))); - } + if let Some((action, msg)) = get_action_msg(deps, &env.contract.address)? { + let event = + Event::new("revenue/run").add_attribute("denom", action.denom.to_string()); + return Ok(Response::default() + .add_event(event) + .add_submessage(SubMsg::reply_always(msg, 0))); } return Ok(Response::default()); } } } +fn get_action_msg(deps: DepsMut, contract: &Addr) -> StdResult> { + // Fetch the next action in the iterator + if let Some(action) = Action::next(deps.storage)? { + let balance = deps + .querier + .query_balance(contract, action.denom.to_string())?; + + return match action.execute(balance)? { + None => { + // Nothing to do. Don't waste this execution, look for the next action with something to do + // Action::next will have stored the previous key and continue the iterator, until failing at the end + get_action_msg(deps, contract) + } + Some(msg) => Ok(Some((action, msg))), + }; + } + Ok(None) +} + #[cfg_attr(not(feature = "library"), entry_point)] pub fn reply(deps: DepsMut, env: Env, _msg: Reply) -> Result { let config = Config::load(deps.storage)?;