Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding per-message conditions and executing custom warp account messages via jobs #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 19 additions & 8 deletions contracts/warp-account/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::state::CONFIG;
use crate::ContractError;
use account::{
Config, ExecuteMsg, IbcTransferMsg, InstantiateMsg, MigrateMsg, QueryMsg, TimeoutBlock,
WithdrawAssetsMsg,
WarpMsg, WithdrawAssetsMsg,
};
use controller::account::{AssetInfo, Cw721ExecuteMsg};
use cosmwasm_std::CosmosMsg::Stargate;
Expand Down Expand Up @@ -52,7 +52,8 @@ pub fn execute(
.add_messages(data.msgs)
.add_attribute("action", "generic")),
ExecuteMsg::WithdrawAssets(data) => withdraw_assets(deps, env, info, data),
ExecuteMsg::IbcTransfer(data) => ibc_transfer(deps, env, info, data),
ExecuteMsg::IbcTransfer(data) => ibc_transfer(env, data),
ExecuteMsg::WarpMsgs(data) => execute_warp_msgs(env, data),
}
}

Expand All @@ -71,12 +72,7 @@ pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response,
Ok(Response::new())
}

pub fn ibc_transfer(
_deps: DepsMut,
env: Env,
_info: MessageInfo,
msg: IbcTransferMsg,
) -> Result<Response, ContractError> {
pub fn ibc_transfer(env: Env, msg: IbcTransferMsg) -> Result<Response, ContractError> {
let mut transfer_msg = msg.transfer_msg.clone();

if msg.timeout_block_delta.is_some() && msg.transfer_msg.timeout_block.is_some() {
Expand Down Expand Up @@ -104,6 +100,21 @@ pub fn ibc_transfer(
}))
}

pub fn execute_warp_msgs(env: Env, msgs: Vec<WarpMsg>) -> Result<Response, ContractError> {
let msgs = msgs
.into_iter()
.map(|msg| -> Result<CosmosMsg, ContractError> {
match msg {
account::WarpMsg::Generic(msg) => Ok(msg),
account::WarpMsg::IbcTransfer(msg) => ibc_transfer(env.clone(), msg)
.map(|val| val.messages.first().unwrap().msg.clone()),
}
})
.collect::<Result<Vec<CosmosMsg>, ContractError>>()?;

Ok(Response::new().add_messages(msgs))
}

pub fn withdraw_assets(
deps: DepsMut,
env: Env,
Expand Down
3 changes: 0 additions & 3 deletions contracts/warp-controller/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ pub fn execute(
ExecuteMsg::UpdateJob(data) => execute::job::update_job(deps, env, info, data),
ExecuteMsg::ExecuteJob(data) => execute::job::execute_job(deps, env, info, data),
ExecuteMsg::EvictJob(data) => execute::job::evict_job(deps, env, info, data),

ExecuteMsg::CreateAccount(data) => execute::account::create_account(deps, env, info, data),

ExecuteMsg::UpdateConfig(data) => execute::controller::update_config(deps, env, info, data),

ExecuteMsg::MigrateAccounts(data) => {
execute::controller::migrate_accounts(deps, env, info, data)
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/warp-controller/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub enum ContractError {

#[error("Eviction period not elapsed.")]
EvictionPeriodNotElapsed {},

#[error("No msgs to trigger")]
NoMsgToTrigger {},
}

impl From<serde_json_wasm::de::Error> for ContractError {
Expand Down
4 changes: 2 additions & 2 deletions contracts/warp-controller/src/execute/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ pub fn migrate_pending_jobs(
description: v1_job.description,
labels: v1_job.labels,
status: v1_job.status,
condition: serde_json_wasm::to_string(&v1_job.condition)?,
condition: Some(serde_json_wasm::to_string(&v1_job.condition)?),
terminate_condition: None,
msgs: new_msgs.to_string(),
vars: serde_json_wasm::to_string(&new_vars)?,
Expand Down Expand Up @@ -382,7 +382,7 @@ pub fn migrate_finished_jobs(
description: v1_job.description,
labels: v1_job.labels,
status: v1_job.status,
condition: serde_json_wasm::to_string(&v1_job.condition)?,
condition: Some(serde_json_wasm::to_string(&v1_job.condition)?),
terminate_condition: None,
msgs: new_msgs,
vars: serde_json_wasm::to_string(&new_vars)?,
Expand Down
46 changes: 29 additions & 17 deletions contracts/warp-controller/src/execute/job.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::state::{ACCOUNTS, CONFIG, FINISHED_JOBS, PENDING_JOBS, STATE};
use crate::ContractError;
use crate::ContractError::EvictionPeriodNotElapsed;
use account::GenericMsg;
use account::{GenericMsg, WarpMsg};
use controller::job::{
CreateJobMsg, DeleteJobMsg, EvictJobMsg, ExecuteJobMsg, Job, JobStatus, UpdateJobMsg,
};
Expand Down Expand Up @@ -333,13 +333,17 @@ pub fn execute_job(
}),
)?;

let resolution: StdResult<bool> = deps.querier.query_wasm_smart(
config.resolver_address.clone(),
&resolver::QueryMsg::QueryResolveCondition(resolver::QueryResolveConditionMsg {
condition: job.condition,
vars: vars.clone(),
}),
);
// Match condition and try to resolve in case it has been provided
let resolution: StdResult<bool> = match job.condition {
Some(condition) => deps.querier.query_wasm_smart(
config.resolver_address.clone(),
&resolver::QueryMsg::QueryResolveCondition(resolver::QueryResolveConditionMsg {
condition,
vars: vars.clone(),
}),
),
None => Ok(true),
};

let mut attrs = vec![];
let mut submsgs = vec![];
Expand Down Expand Up @@ -387,15 +391,23 @@ pub fn execute_job(
id: job.id.u64(),
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: account.account.to_string(),
msg: to_binary(&account::ExecuteMsg::Generic(GenericMsg {
msgs: deps.querier.query_wasm_smart(
config.resolver_address,
&resolver::QueryMsg::QueryHydrateMsgs(QueryHydrateMsgsMsg {
msgs: job.msgs,
vars,
}),
)?,
}))?,
msg: to_binary(&account::ExecuteMsg::WarpMsgs(
deps.querier
.query_wasm_smart::<Vec<WarpMsg>>(
config.resolver_address,
&resolver::QueryMsg::QueryHydrateMsgs(QueryHydrateMsgsMsg {
msgs: job.msgs,
vars,
}),
)
.map(|msgs| {
if msgs.is_empty() {
Err(ContractError::NoMsgToTrigger {})
} else {
Ok(msgs)
}
})??,
))?,
funds: vec![],
}),
gas_limit: None,
Expand Down
4 changes: 3 additions & 1 deletion contracts/warp-resolver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ cw20 = "0.16"
cw721 = "0.16.0"
resolver = { path = "../../packages/resolver", default-features = false, version = "*" }
controller = { path = "../../packages/controller", default-features = false, version = "*" }
account = { path = "../../packages/account", default-features = false, version = "*" }
schemars = "0.8"
serde-cw-value = "0.7.0"
thiserror = "1"
serde-json-wasm = "0.4.1"
serde-json-wasm = "1.0.0"
json-codec-wasm = "0.1.0"

[dev-dependencies]
Expand Down
25 changes: 16 additions & 9 deletions contracts/warp-resolver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use crate::util::variable::{
vars_valid,
};
use crate::ContractError;
use account::WarpMsg;
use cosmwasm_std::{
entry_point, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError,
StdResult,
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};

use resolver::condition::Condition;
Expand Down Expand Up @@ -192,8 +192,11 @@ fn query_validate_job_creation(
_env: Env,
data: QueryValidateJobCreationMsg,
) -> StdResult<String> {
let _condition: Condition = serde_json_wasm::from_str(&data.condition)
.map_err(|e| StdError::generic_err(format!("Condition input invalid: {}", e)))?;
// Try deserialize condition only if provided
if let Some(condition) = &data.condition {
serde_json_wasm::from_str::<Condition>(condition)
.map_err(|e| StdError::generic_err(format!("Condition input invalid: {}", e)))?;
}
let terminate_condition_str = data.terminate_condition.clone().unwrap_or("".to_string());
if !terminate_condition_str.is_empty() {
let _terminate_condition: Condition = serde_json_wasm::from_str(&terminate_condition_str)
Expand All @@ -216,7 +219,11 @@ fn query_validate_job_creation(
));
}

if !(string_vars_in_vector(&vars, &data.condition)
// Check condition contains vars only if condition is provided
if !(data
.condition
.map(|condition| string_vars_in_vector(&vars, &condition))
.unwrap_or(true)
&& string_vars_in_vector(&vars, &terminate_condition_str)
&& string_vars_in_vector(&vars, &data.msgs))
{
Expand Down Expand Up @@ -268,14 +275,14 @@ fn query_apply_var_fn(deps: Deps, env: Env, data: QueryApplyVarFnMsg) -> StdResu
}

fn query_hydrate_msgs(
_deps: Deps,
_env: Env,
deps: Deps,
env: Env,
data: QueryHydrateMsgsMsg,
) -> StdResult<Vec<CosmosMsg>> {
) -> StdResult<Vec<WarpMsg>> {
let vars: Vec<Variable> =
serde_json_wasm::from_str(&data.vars).map_err(|e| StdError::generic_err(e.to_string()))?;

hydrate_msgs(data.msgs, vars).map_err(|e| StdError::generic_err(e.to_string()))
hydrate_msgs(deps, env, data.msgs, vars).map_err(|e| StdError::generic_err(e.to_string()))
}

pub fn query_simulate_query(
Expand Down
Loading