Skip to content

Commit

Permalink
make free job|funding account idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Mar 17, 2024
1 parent 66c5b9a commit 3ea7fa1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
16 changes: 10 additions & 6 deletions contracts/warp-account-tracker/src/execute/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ pub fn free_job_account(deps: DepsMut, data: FreeJobAccountMsg) -> Result<Respon
FREE_JOB_ACCOUNTS.update(
deps.storage,
(account_owner_ref, account_addr_ref),
|s| match s {
None => Ok(data.last_job_id),
Some(_) => Err(ContractError::AccountAlreadyFreeError {}),
|s| -> Result<Uint64, ContractError> {
match s {
None => Ok(data.last_job_id),
Some(last_job_id) => Ok(last_job_id), // idempotent use case, if already freed, do nothing
}
},
)?;

Expand Down Expand Up @@ -180,9 +182,11 @@ pub fn free_funding_account(
FREE_FUNDING_ACCOUNTS.update(
deps.storage,
(account_owner_addr_ref, account_addr_ref),
|s| match s {
None => Ok(vec![data.job_id]),
Some(_) => Err(ContractError::AccountAlreadyFreeError {}),
|s| -> Result<Vec<Uint64>, ContractError> {
match s {
None => Ok(vec![data.job_id]),
Some(job_ids) => Ok(job_ids), // idempotent use case, if already freed, do nothing
}
},
)?;
} else {
Expand Down
23 changes: 10 additions & 13 deletions contracts/warp-account-tracker/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,16 @@ mod tests {
&[],
);

// Cannot free account twice
assert_err(
app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeJobAccount(FreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_1_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
),
ContractError::AccountAlreadyFreeError {},
// free account idempotent
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeJobAccount(FreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_1_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
}),
&[],
);

// Mark second account as free
Expand Down

0 comments on commit 3ea7fa1

Please sign in to comment.