Skip to content

Commit

Permalink
refactor mutable deps to deps.storage instaed
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Dec 11, 2023
1 parent fca3f6c commit 0a63f88
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
33 changes: 17 additions & 16 deletions contracts/warp-controller/src/execute/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use super::fee::{compute_burn_fee, compute_creation_fee, compute_maintenance_fee
const MAX_TEXT_LENGTH: usize = 280;

pub fn create_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
data: CreateJobMsg,
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn create_job(
.checked_sub(data.reward + total_fees)?;

let mut job = JobQueue::add(
&mut deps,
deps.storage,
Job {
id: state.current_job_id,
prev_id: None,
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn create_job(
let available_account_addr = &available_account.addr;
// Update job.account from placeholder value to job account
job.account = available_account_addr.clone();
JobQueue::sync(&mut deps, env.clone(), job.clone())?;
JobQueue::sync(deps.storage, env.clone(), job.clone())?;

if !native_funds_minus_operational_amount.is_empty() {
// Fund account in native coins
Expand Down Expand Up @@ -300,7 +300,7 @@ pub fn create_job(
let available_account_addr = &available_account.account_addr;
// Update funding_account from placeholder value to funding account
job.funding_account = Some(available_account_addr.clone());
JobQueue::sync(&mut deps, env, job.clone())?;
JobQueue::sync(deps.storage, env, job.clone())?;

// Fund account in native coins
msgs.push(build_transfer_native_funds_msg(
Expand Down Expand Up @@ -354,14 +354,14 @@ pub fn create_job(
}

pub fn delete_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
data: DeleteJobMsg,
config: Config,
fee_denom_paid_amount: Uint128,
) -> Result<Response, ContractError> {
let job = JobQueue::get(&deps, data.id.into())?;
let job = JobQueue::get(deps.storage, data.id.into())?;
let account_addr = job.account.clone();

if job.status != JobStatus::Pending {
Expand All @@ -372,7 +372,7 @@ pub fn delete_job(
return Err(ContractError::Unauthorized {});
}

let _new_job = JobQueue::finalize(&mut deps, env, job.id.into(), JobStatus::Cancelled)?;
let _new_job = JobQueue::finalize(deps.storage, env, job.id.into(), JobStatus::Cancelled)?;

let fee = job.reward * Uint128::from(config.cancellation_fee_percentage) / Uint128::new(100);
if fee > fee_denom_paid_amount {
Expand Down Expand Up @@ -435,12 +435,12 @@ pub fn delete_job(
}

pub fn update_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
data: UpdateJobMsg,
) -> Result<Response, ContractError> {
let job = JobQueue::get(&deps, data.id.into())?;
let job = JobQueue::get(deps.storage, data.id.into())?;

if info.sender != job.owner {
return Err(ContractError::Unauthorized {});
Expand All @@ -454,7 +454,7 @@ pub fn update_job(
return Err(ContractError::NameTooShort {});
}

let job = JobQueue::update(&mut deps, env, data)?;
let job = JobQueue::update(deps.storage, env, data)?;

Ok(Response::new()
.add_attribute("action", "update_job")
Expand All @@ -471,13 +471,13 @@ pub fn update_job(
}

pub fn execute_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
data: ExecuteJobMsg,
config: Config,
) -> Result<Response, ContractError> {
let job = JobQueue::get(&deps, data.id.into())?;
let job = JobQueue::get(deps.storage, data.id.into())?;
let account_addr = job.account.clone();

if job.status != JobStatus::Pending {
Expand Down Expand Up @@ -537,7 +537,7 @@ pub fn execute_job(
Err(e) => {
attrs.push(Attribute::new("job_condition_status", "invalid"));
attrs.push(Attribute::new("error", e.to_string()));
JobQueue::finalize(&mut deps, env, job.id.into(), JobStatus::Failed)?;
JobQueue::finalize(deps.storage, env, job.id.into(), JobStatus::Failed)?;
break;
}
}
Expand Down Expand Up @@ -577,13 +577,13 @@ pub fn execute_job(
}

pub fn evict_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
info: MessageInfo,
data: EvictJobMsg,
config: Config,
) -> Result<Response, ContractError> {
let job = JobQueue::get(&deps, data.id.into())?;
let job = JobQueue::get(deps.storage, data.id.into())?;
let account_addr = job.account.clone();

if job.status != JobStatus::Pending {
Expand All @@ -599,7 +599,8 @@ pub fn evict_job(
let mut msgs = vec![];

// Job will be evicted
let job_status = JobQueue::finalize(&mut deps, env, job.id.into(), JobStatus::Evicted)?.status;
let job_status =
JobQueue::finalize(deps.storage, env, job.id.into(), JobStatus::Evicted)?.status;

// Controller sends eviction reward to evictor
msgs.push(build_transfer_native_funds_msg(
Expand Down
12 changes: 6 additions & 6 deletions contracts/warp-controller/src/reply/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

pub fn create_account_and_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
msg: Reply,
config: Config,
Expand Down Expand Up @@ -90,9 +90,9 @@ pub fn create_account_and_job(
.value,
)?;

let mut job = JobQueue::get(&deps, job_id)?;
let mut job = JobQueue::get(deps.storage, job_id)?;
job.account = account_addr.clone();
JobQueue::sync(&mut deps, env, job.clone())?;
JobQueue::sync(deps.storage, env, job.clone())?;

let mut msgs: Vec<CosmosMsg> = vec![];

Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn create_account_and_job(
}

pub fn create_funding_account_and_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
msg: Reply,
config: Config,
Expand Down Expand Up @@ -204,9 +204,9 @@ pub fn create_funding_account_and_job(
.value,
)?;

let mut job = JobQueue::get(&deps, job_id)?;
let mut job = JobQueue::get(deps.storage, job_id)?;
job.funding_account = Some(funding_account_addr.clone());
JobQueue::sync(&mut deps, env, job.clone())?;
JobQueue::sync(deps.storage, env, job.clone())?;

let msgs: Vec<CosmosMsg> = vec![build_take_funding_account_msg(
config.account_tracker_address.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions contracts/warp-controller/src/reply/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use controller::{
};

pub fn execute_job(
mut deps: DepsMut,
deps: DepsMut,
env: Env,
msg: Reply,
config: Config,
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn execute_job(

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

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

let res_attrs = match msg.result {
SubMsgResult::Err(e) => vec![Attribute::new(
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn execute_job(
operational_amount.checked_sub(finished_job.reward + total_fees)?;

let new_job = JobQueue::add(
&mut deps,
deps.storage,
Job {
id: new_job_id,
prev_id: Some(finished_job.id),
Expand Down
38 changes: 21 additions & 17 deletions contracts/warp-controller/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cosmwasm_std::{DepsMut, Env, Uint64};
use cosmwasm_std::{Env, Storage, Uint64};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, MultiIndex, UniqueIndex};

use controller::{
Expand Down Expand Up @@ -59,16 +59,16 @@ pub const STATE: Item<State> = Item::new("state");
pub struct JobQueue;

impl JobQueue {
pub fn add(deps: &mut DepsMut, job: Job) -> Result<Job, ContractError> {
let state = STATE.load(deps.storage)?;
pub fn add(storage: &mut dyn Storage, job: Job) -> Result<Job, ContractError> {
let state: State = STATE.load(storage)?;

let job = PENDING_JOBS().update(deps.storage, state.current_job_id.u64(), |s| match s {
let job = PENDING_JOBS().update(storage, state.current_job_id.u64(), |s| match s {
None => Ok(job),
Some(_) => Err(ContractError::JobAlreadyExists {}),
})?;

STATE.save(
deps.storage,
storage,
&State {
current_job_id: state.current_job_id.checked_add(Uint64::new(1))?,
q: state.q.checked_add(Uint64::new(1))?,
Expand All @@ -78,14 +78,14 @@ impl JobQueue {
Ok(job)
}

pub fn get(deps: &DepsMut, job_id: u64) -> Result<Job, ContractError> {
let job = PENDING_JOBS().load(deps.storage, job_id)?;
pub fn get(storage: &dyn Storage, job_id: u64) -> Result<Job, ContractError> {
let job = PENDING_JOBS().load(storage, job_id)?;

Ok(job)
}

pub fn sync(deps: &mut DepsMut, env: Env, job: Job) -> Result<Job, ContractError> {
PENDING_JOBS().update(deps.storage, job.id.u64(), |j| match j {
pub fn sync(storage: &mut dyn Storage, env: Env, job: Job) -> Result<Job, ContractError> {
PENDING_JOBS().update(storage, job.id.u64(), |j| match j {
None => Err(ContractError::JobDoesNotExist {}),
Some(job) => Ok(Job {
id: job.id,
Expand All @@ -111,8 +111,12 @@ impl JobQueue {
})
}

pub fn update(deps: &mut DepsMut, _env: Env, data: UpdateJobMsg) -> Result<Job, ContractError> {
PENDING_JOBS().update(deps.storage, data.id.u64(), |h| match h {
pub fn update(
storage: &mut dyn Storage,
_env: Env,
data: UpdateJobMsg,
) -> Result<Job, ContractError> {
PENDING_JOBS().update(storage, data.id.u64(), |h| match h {
None => Err(ContractError::JobDoesNotExist {}),
Some(job) => Ok(Job {
id: job.id,
Expand All @@ -139,7 +143,7 @@ impl JobQueue {
}

pub fn finalize(
deps: &mut DepsMut,
storage: &mut dyn Storage,
env: Env,
job_id: u64,
status: JobStatus,
Expand All @@ -148,7 +152,7 @@ impl JobQueue {
return Err(ContractError::Unauthorized {});
}

let job = PENDING_JOBS().load(deps.storage, job_id)?;
let job = PENDING_JOBS().load(storage, job_id)?;

let new_job = Job {
id: job.id,
Expand All @@ -172,16 +176,16 @@ impl JobQueue {
operational_amount: job.operational_amount,
};

FINISHED_JOBS().update(deps.storage, job_id, |j| match j {
FINISHED_JOBS().update(storage, job_id, |j| match j {
None => Ok(new_job.clone()),
Some(_) => Err(ContractError::JobAlreadyFinished {}),
})?;

PENDING_JOBS().remove(deps.storage, job_id)?;
PENDING_JOBS().remove(storage, job_id)?;

let state = STATE.load(deps.storage)?;
let state = STATE.load(storage)?;
STATE.save(
deps.storage,
storage,
&State {
current_job_id: state.current_job_id,
q: state.q.checked_sub(Uint64::new(1))?,
Expand Down
2 changes: 1 addition & 1 deletion refs.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"codeId": "12318"
},
"warp-controller": {
"codeId": "12333",
"codeId": "12334",
"address": "terra1sylnrt9rkv7lraqvxssdzwmhm804qjtlp8ssjc502538ykyhdn5sns9edd"
},
"warp-resolver": {
Expand Down

0 comments on commit 0a63f88

Please sign in to comment.