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

Feature/estimate fee #62

Closed
wants to merge 14 commits into from
4 changes: 3 additions & 1 deletion contracts/warp-controller/examples/warp-controller-schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::create_dir_all;
use controller::{
account::{AccountResponse, AccountsResponse},
job::{JobResponse, JobsResponse},
QueryMsg, {Config, ConfigResponse, ExecuteMsg, InstantiateMsg},
QueryMsg, State, StateResponse, {Config, ConfigResponse, ExecuteMsg, InstantiateMsg},
};
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

Expand All @@ -19,6 +19,8 @@ fn main() {
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(Config), &out_dir);
export_schema(&schema_for!(ConfigResponse), &out_dir);
export_schema(&schema_for!(State), &out_dir);
export_schema(&schema_for!(StateResponse), &out_dir);
export_schema(&schema_for!(JobResponse), &out_dir);
export_schema(&schema_for!(JobsResponse), &out_dir);
export_schema(&schema_for!(AccountResponse), &out_dir);
Expand Down
78 changes: 24 additions & 54 deletions contracts/warp-controller/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::map_contract_error;
use crate::state::{ACCOUNTS, CONFIG, FINISHED_JOBS, PENDING_JOBS};
use crate::state::{JobQueue, ACCOUNTS, CONFIG};
use crate::{execute, query, state::STATE, ContractError};
use account::{GenericMsg, WithdrawAssetsMsg};
use controller::account::{Account, Fund, FundTransferMsgs, TransferFromMsg, TransferNftMsg};
Expand Down Expand Up @@ -116,6 +116,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::QueryConfig(data) => {
to_binary(&query::controller::query_config(deps, env, data)?)
}
QueryMsg::QueryState(data) => to_binary(&query::controller::query_state(deps, env, data)?),
}
}

Expand Down Expand Up @@ -189,7 +190,7 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, Co
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
pub fn reply(mut deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
match msg.id {
//account creation
0 => {
Expand Down Expand Up @@ -301,36 +302,14 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
}
//job execution
_ => {
let mut state = STATE.load(deps.storage)?;
let state = STATE.load(deps.storage)?;

let new_status = match msg.result {
SubMsgResult::Ok(_) => JobStatus::Executed,
SubMsgResult::Err(_) => JobStatus::Failed,
};

let job = PENDING_JOBS().load(deps.storage, msg.id)?;
PENDING_JOBS().remove(deps.storage, msg.id)?;

let finished_job = FINISHED_JOBS().update(deps.storage, msg.id, |j| match j {
None => Ok(Job {
id: job.id,
owner: job.owner,
last_update_time: job.last_update_time,
name: job.name,
description: job.description,
labels: job.labels,
status: new_status,
condition: job.condition,
terminate_condition: job.terminate_condition,
msgs: job.msgs,
vars: job.vars,
recurring: job.recurring,
requeue_on_evict: job.requeue_on_evict,
reward: job.reward,
assets_to_withdraw: job.assets_to_withdraw,
}),
Some(_) => Err(ContractError::JobAlreadyFinished {}),
})?;
let finished_job = JobQueue::finalize(&mut deps, env.clone(), msg.id, new_status)?;

let res_attrs = match msg.result {
SubMsgResult::Err(e) => vec![Attribute::new(
Expand Down Expand Up @@ -429,34 +408,27 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
}

if !should_terminate_job {
let new_job = PENDING_JOBS().update(
deps.storage,
state.current_job_id.u64(),
|s| match s {
None => Ok(Job {
id: state.current_job_id,
owner: finished_job.owner.clone(),
last_update_time: Uint64::from(env.block.time.seconds()),
name: finished_job.name.clone(),
description: finished_job.description,
labels: finished_job.labels,
status: JobStatus::Pending,
condition: finished_job.condition.clone(),
terminate_condition: finished_job.terminate_condition.clone(),
vars: new_vars,
requeue_on_evict: finished_job.requeue_on_evict,
recurring: finished_job.recurring,
msgs: finished_job.msgs.clone(),
reward: finished_job.reward,
assets_to_withdraw: finished_job.assets_to_withdraw,
}),
Some(_) => Err(ContractError::JobAlreadyExists {}),
let new_job = JobQueue::add(
&mut deps,
Job {
id: state.current_job_id,
owner: finished_job.owner.clone(),
last_update_time: Uint64::from(env.block.time.seconds()),
name: finished_job.name.clone(),
description: finished_job.description,
labels: finished_job.labels,
status: JobStatus::Pending,
condition: finished_job.condition.clone(),
terminate_condition: finished_job.terminate_condition.clone(),
vars: new_vars,
requeue_on_evict: finished_job.requeue_on_evict,
recurring: finished_job.recurring,
msgs: finished_job.msgs.clone(),
reward: finished_job.reward,
assets_to_withdraw: finished_job.assets_to_withdraw,
},
)?;

state.current_job_id = state.current_job_id.checked_add(Uint64::new(1))?;
state.q = state.q.checked_add(Uint64::new(1))?;

msgs.push(
//send reward to controller
WasmMsg::Execute {
Expand Down Expand Up @@ -531,11 +503,9 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
}
}

STATE.save(deps.storage, &state)?;

Ok(Response::new()
.add_attribute("action", "execute_reply")
.add_attribute("job_id", job.id)
.add_attribute("job_id", finished_job.id)
.add_attributes(res_attrs)
.add_attributes(new_job_attrs)
.add_messages(msgs))
Expand Down
Loading
Loading