Skip to content

Commit

Permalink
fix reply
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Nov 6, 2023
1 parent d7a01fc commit 6ac2f51
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 46 deletions.
18 changes: 4 additions & 14 deletions contracts/warp-controller/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ use crate::{

use controller::{Config, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg, State};

// Reply id for job creation
// For user does not have available account
// So we create new job account account and job
pub const REPLY_ID_CREATE_JOB_ACCOUNT_AND_JOB: u64 = 1;
// Reply id for job execution
pub const REPLY_ID_EXECUTE_JOB: u64 = 2;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
Expand Down Expand Up @@ -259,13 +252,10 @@ 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> {
let config = CONFIG.load(deps.storage)?;

match msg.id {
// Job account has been created, now create job
REPLY_ID_CREATE_JOB_ACCOUNT_AND_JOB => {
reply::account::create_job_account_and_job(deps, env, msg, config)
}
// Job has been executed
REPLY_ID_EXECUTE_JOB => reply::job::execute_job(deps, env, msg, config),
_ => Err(ContractError::UnknownReplyId {}),
// use 0 as hack to call create_job_account_and_job
0 => reply::account::create_job_account_and_job(deps, env, msg, config),
_id => reply::job::execute_job(deps, env, msg, config),
}
}
5 changes: 2 additions & 3 deletions contracts/warp-controller/src/execute/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use cosmwasm_std::{
};

use crate::{
contract::{REPLY_ID_CREATE_JOB_ACCOUNT_AND_JOB, REPLY_ID_EXECUTE_JOB},
state::LEGACY_ACCOUNTS,
util::{
fee::deduct_reward_and_fee_from_native_funds,
Expand Down Expand Up @@ -138,7 +137,7 @@ pub fn create_job(
None => {
// Create account then create job in reply
submsgs.push(SubMsg {
id: REPLY_ID_CREATE_JOB_ACCOUNT_AND_JOB,
id: 0,
msg: build_instantiate_warp_account_msg(
job.id,
env.contract.address.to_string(),
Expand Down Expand Up @@ -417,7 +416,7 @@ pub fn execute_job(
match resolution {
Ok(true) => {
submsgs.push(SubMsg {
id: REPLY_ID_EXECUTE_JOB,
id: data.id.u64(),
msg: CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: job.account.to_string(),
msg: to_binary(&job_account::ExecuteMsg::Generic(GenericMsg {
Expand Down
30 changes: 2 additions & 28 deletions contracts/warp-controller/src/reply/job.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
Attribute, BalanceResponse, BankQuery, Coin, DepsMut, Env, QueryRequest, Reply, Response,
StdError, StdResult, SubMsgResult, Uint128, Uint64,
StdResult, SubMsgResult, Uint128, Uint64,
};

use crate::{
Expand Down Expand Up @@ -34,33 +34,7 @@ pub fn execute_job(
SubMsgResult::Err(_) => JobStatus::Failed,
};

let reply = msg
.result
.clone()
.into_result()
.map_err(StdError::generic_err)?;

// TODO: how to get this event
let event = reply
.events
.iter()
.find(|event| {
event
.attributes
.iter()
.any(|attr| attr.key == "action" && attr.value == "execute_job")
})
.ok_or_else(|| StdError::generic_err("cannot find `execute_job` event"))?;

let job_id_str = event
.attributes
.iter()
.cloned()
.find(|attr| attr.key == "job_id")
.ok_or_else(|| StdError::generic_err("cannot find `job_id` attribute"))?
.value;

let job_id = job_id_str.as_str().parse::<u64>()?;
let job_id = msg.id;

let finished_job = JobQueue::finalize(&mut deps, env.clone(), job_id, new_status)?;

Expand Down
2 changes: 1 addition & 1 deletion contracts/warp-job-account-tracker/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ pub const CONFIG: Item<Config> = Item::new("config");
// Key is the (account owner address, account address), value is the ID of the pending job currently using it
pub const TAKEN_ACCOUNTS: Map<(&Addr, &Addr), Uint64> = Map::new("taken_accounts");

// Key is the (account owner address, account address), value is a dummy data that is always true to make it behave like a set
// Key is the (account owner address, account address), value is id of the last job which reserved it
pub const FREE_ACCOUNTS: Map<(&Addr, &Addr), bool> = Map::new("free_accounts");

0 comments on commit 6ac2f51

Please sign in to comment.