Skip to content

Commit

Permalink
scan for next valid action
Browse files Browse the repository at this point in the history
  • Loading branch information
codehans committed Mar 11, 2024
1 parent 72b023b commit e090b04
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/contract.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -62,23 +62,37 @@ pub fn execute(
pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
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<Option<(Action, CosmosMsg)>> {
// 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<Response, ContractError> {
let config = Config::load(deps.storage)?;
Expand Down

0 comments on commit e090b04

Please sign in to comment.