Skip to content

Commit

Permalink
rename account to job_account
Browse files Browse the repository at this point in the history
  • Loading branch information
simke9445 committed Dec 12, 2023
1 parent 31afa7f commit d194ecd
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 116 deletions.
20 changes: 10 additions & 10 deletions contracts/warp-account-tracker/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ pub fn execute(
}

match msg {
ExecuteMsg::TakeAccount(data) => {
ExecuteMsg::TakeJobAccount(data) => {
nonpayable(&info).unwrap();
execute::account::take_account(deps, data)
execute::account::take_job_account(deps, data)
}
ExecuteMsg::FreeAccount(data) => {
ExecuteMsg::FreeJobAccount(data) => {
nonpayable(&info).unwrap();
execute::account::free_account(deps, data)
execute::account::free_job_account(deps, data)
}
ExecuteMsg::TakeFundingAccount(data) => {
nonpayable(&info).unwrap();
Expand All @@ -71,14 +71,14 @@ pub fn execute(
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::QueryConfig(_) => to_binary(&query::account::query_config(deps)?),
QueryMsg::QueryTakenAccounts(data) => {
to_binary(&query::account::query_taken_accounts(deps, data)?)
QueryMsg::QueryTakenJobAccounts(data) => {
to_binary(&query::account::query_taken_job_accounts(deps, data)?)
}
QueryMsg::QueryFreeAccounts(data) => {
to_binary(&query::account::query_free_accounts(deps, data)?)
QueryMsg::QueryFreeJobAccounts(data) => {
to_binary(&query::account::query_free_job_accounts(deps, data)?)
}
QueryMsg::QueryFirstFreeAccount(data) => {
to_binary(&query::account::query_first_free_account(deps, data)?)
QueryMsg::QueryFirstFreeJobAccount(data) => {
to_binary(&query::account::query_first_free_job_account(deps, data)?)
}
QueryMsg::QueryFundingAccounts(data) => {
to_binary(&query::account::query_funding_accounts(deps, data)?)
Expand Down
30 changes: 15 additions & 15 deletions contracts/warp-account-tracker/src/execute/account.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::state::{
FREE_ACCOUNTS, FUNDING_ACCOUNTS_BY_USER, TAKEN_ACCOUNTS, TAKEN_FUNDING_ACCOUNT_BY_JOB,
FREE_JOB_ACCOUNTS, FUNDING_ACCOUNTS_BY_USER, TAKEN_FUNDING_ACCOUNT_BY_JOB, TAKEN_JOB_ACCOUNTS,
};
use crate::ContractError;
use account_tracker::{
AddFundingAccountMsg, FreeAccountMsg, FreeFundingAccountMsg, FundingAccount, TakeAccountMsg,
TakeFundingAccountMsg,
AddFundingAccountMsg, FreeFundingAccountMsg, FreeJobAccountMsg, FundingAccount,
TakeFundingAccountMsg, TakeJobAccountMsg,
};
use cosmwasm_std::{DepsMut, Response};

pub fn take_account(deps: DepsMut, data: TakeAccountMsg) -> Result<Response, ContractError> {
pub fn take_job_account(deps: DepsMut, data: TakeJobAccountMsg) -> Result<Response, ContractError> {
let account_owner_ref = &deps.api.addr_validate(data.account_owner_addr.as_str())?;
let account_addr_ref = &deps.api.addr_validate(data.account_addr.as_str())?;
FREE_ACCOUNTS.remove(deps.storage, (account_owner_ref, account_addr_ref));
TAKEN_ACCOUNTS.update(
FREE_JOB_ACCOUNTS.remove(deps.storage, (account_owner_ref, account_addr_ref));
TAKEN_JOB_ACCOUNTS.update(
deps.storage,
(account_owner_ref, account_addr_ref),
|s| match s {
Expand All @@ -21,16 +21,16 @@ pub fn take_account(deps: DepsMut, data: TakeAccountMsg) -> Result<Response, Con
},
)?;
Ok(Response::new()
.add_attribute("action", "take_account")
.add_attribute("action", "take_job_account")
.add_attribute("account_addr", data.account_addr)
.add_attribute("job_id", data.job_id))
}

pub fn free_account(deps: DepsMut, data: FreeAccountMsg) -> Result<Response, ContractError> {
pub fn free_job_account(deps: DepsMut, data: FreeJobAccountMsg) -> Result<Response, ContractError> {
let account_owner_ref = &deps.api.addr_validate(data.account_owner_addr.as_str())?;
let account_addr_ref = &deps.api.addr_validate(data.account_addr.as_str())?;
TAKEN_ACCOUNTS.remove(deps.storage, (account_owner_ref, account_addr_ref));
FREE_ACCOUNTS.update(
TAKEN_JOB_ACCOUNTS.remove(deps.storage, (account_owner_ref, account_addr_ref));
FREE_JOB_ACCOUNTS.update(
deps.storage,
(account_owner_ref, account_addr_ref),
|s| match s {
Expand All @@ -40,7 +40,7 @@ pub fn free_account(deps: DepsMut, data: FreeAccountMsg) -> Result<Response, Con
},
)?;
Ok(Response::new()
.add_attribute("action", "free_account")
.add_attribute("action", "free_job_account")
.add_attribute("account_addr", data.account_addr))
}

Expand All @@ -52,8 +52,8 @@ pub fn take_funding_account(
let account_addr_ref = &deps.api.addr_validate(data.account_addr.as_str())?;

// prevent taking job accounts as funding accounts
if TAKEN_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, account_addr_ref))
|| FREE_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, account_addr_ref))
if TAKEN_JOB_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, account_addr_ref))
|| FREE_JOB_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, account_addr_ref))
{
return Err(ContractError::AccountAlreadyTakenError {});
}
Expand Down Expand Up @@ -152,8 +152,8 @@ pub fn add_funding_account(
let account_addr_ref = deps.api.addr_validate(&data.account_addr)?;

// prevent adding job accounts as funding accounts
if TAKEN_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, &account_addr_ref))
|| FREE_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, &account_addr_ref))
if TAKEN_JOB_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, &account_addr_ref))
|| FREE_JOB_ACCOUNTS.has(deps.storage, (&account_owner_addr_ref, &account_addr_ref))
{
return Err(ContractError::AccountAlreadyTakenError {});
}
Expand Down
38 changes: 19 additions & 19 deletions contracts/warp-account-tracker/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
mod tests {
use account_tracker::{
Account, AccountResponse, AccountsResponse, Config, ConfigResponse, ExecuteMsg,
FreeAccountMsg, InstantiateMsg, QueryConfigMsg, QueryFirstFreeAccountMsg,
QueryFreeAccountsMsg, QueryMsg, QueryTakenAccountsMsg, TakeAccountMsg,
FreeJobAccountMsg, InstantiateMsg, QueryConfigMsg, QueryFirstFreeJobAccountMsg,
QueryFreeJobAccountsMsg, QueryMsg, QueryTakenJobAccountsMsg, TakeJobAccountMsg,
};
use anyhow::Result as AnyResult;
use cosmwasm_std::{Addr, Coin, Empty, Uint128, Uint64};
Expand Down Expand Up @@ -95,7 +95,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFirstFreeAccount(QueryFirstFreeAccountMsg {
&QueryMsg::QueryFirstFreeJobAccount(QueryFirstFreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
})
),
Expand All @@ -104,7 +104,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFreeAccounts(QueryFreeAccountsMsg {
&QueryMsg::QueryFreeJobAccounts(QueryFreeJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand All @@ -118,7 +118,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryTakenAccounts(QueryTakenAccountsMsg {
&QueryMsg::QueryTakenJobAccounts(QueryTakenJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand All @@ -134,7 +134,7 @@ mod tests {
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeAccount(FreeAccountMsg {
&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,
Expand All @@ -147,7 +147,7 @@ mod tests {
app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeAccount(FreeAccountMsg {
&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,
Expand All @@ -161,7 +161,7 @@ mod tests {
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeAccount(FreeAccountMsg {
&ExecuteMsg::FreeJobAccount(FreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
last_job_id: DUMMY_JOB_2_ID,
Expand All @@ -173,7 +173,7 @@ mod tests {
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeAccount(FreeAccountMsg {
&ExecuteMsg::FreeJobAccount(FreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_3_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
Expand All @@ -185,7 +185,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFirstFreeAccount(QueryFirstFreeAccountMsg {
&QueryMsg::QueryFirstFreeJobAccount(QueryFirstFreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
})
),
Expand All @@ -201,7 +201,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFreeAccounts(QueryFreeAccountsMsg {
&QueryMsg::QueryFreeJobAccounts(QueryFreeJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand Down Expand Up @@ -230,7 +230,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryTakenAccounts(QueryTakenAccountsMsg {
&QueryMsg::QueryTakenJobAccounts(QueryTakenJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand All @@ -246,7 +246,7 @@ mod tests {
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::TakeAccount(TakeAccountMsg {
&ExecuteMsg::TakeJobAccount(TakeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
job_id: DUMMY_JOB_1_ID,
Expand All @@ -259,7 +259,7 @@ mod tests {
app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::TakeAccount(TakeAccountMsg {
&ExecuteMsg::TakeJobAccount(TakeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
job_id: DUMMY_JOB_2_ID,
Expand All @@ -273,7 +273,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFreeAccounts(QueryFreeAccountsMsg {
&QueryMsg::QueryFreeJobAccounts(QueryFreeJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand All @@ -298,7 +298,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryTakenAccounts(QueryTakenAccountsMsg {
&QueryMsg::QueryTakenJobAccounts(QueryTakenJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand All @@ -317,7 +317,7 @@ mod tests {
let _ = app.execute_contract(
Addr::unchecked(USER_1),
warp_account_tracker_contract_addr.clone(),
&ExecuteMsg::FreeAccount(FreeAccountMsg {
&ExecuteMsg::FreeJobAccount(FreeJobAccountMsg {
account_owner_addr: USER_1.to_string(),
account_addr: DUMMY_WARP_ACCOUNT_2_ADDR.to_string(),
last_job_id: DUMMY_JOB_1_ID,
Expand All @@ -329,7 +329,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr.clone(),
&QueryMsg::QueryFreeAccounts(QueryFreeAccountsMsg {
&QueryMsg::QueryFreeJobAccounts(QueryFreeJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand Down Expand Up @@ -358,7 +358,7 @@ mod tests {
assert_eq!(
app.wrap().query_wasm_smart(
warp_account_tracker_contract_addr,
&QueryMsg::QueryTakenAccounts(QueryTakenAccountsMsg {
&QueryMsg::QueryTakenJobAccounts(QueryTakenJobAccountsMsg {
account_owner_addr: USER_1.to_string(),
start_after: None,
limit: None
Expand Down
34 changes: 19 additions & 15 deletions contracts/warp-account-tracker/src/query/account.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use cosmwasm_std::{Deps, Order, StdResult};
use cw_storage_plus::{Bound, PrefixBound};

use crate::state::{CONFIG, FREE_ACCOUNTS, FUNDING_ACCOUNTS_BY_USER, TAKEN_ACCOUNTS};
use crate::state::{CONFIG, FREE_JOB_ACCOUNTS, FUNDING_ACCOUNTS_BY_USER, TAKEN_JOB_ACCOUNTS};

use account_tracker::{
Account, AccountResponse, AccountsResponse, ConfigResponse, FundingAccountResponse,
FundingAccountsResponse, QueryFirstFreeAccountMsg, QueryFirstFreeFundingAccountMsg,
QueryFreeAccountsMsg, QueryFundingAccountMsg, QueryFundingAccountsMsg, QueryTakenAccountsMsg,
FundingAccountsResponse, QueryFirstFreeFundingAccountMsg, QueryFirstFreeJobAccountMsg,
QueryFreeJobAccountsMsg, QueryFundingAccountMsg, QueryFundingAccountsMsg,
QueryTakenJobAccountsMsg,
};

const QUERY_LIMIT: u32 = 50;
Expand All @@ -16,40 +17,40 @@ pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
Ok(ConfigResponse { config })
}

pub fn query_first_free_account(
pub fn query_first_free_job_account(
deps: Deps,
data: QueryFirstFreeAccountMsg,
data: QueryFirstFreeJobAccountMsg,
) -> StdResult<AccountResponse> {
let account_owner_ref = &deps.api.addr_validate(data.account_owner_addr.as_str())?;
let maybe_free_account = FREE_ACCOUNTS
let maybe_free_job_account = FREE_JOB_ACCOUNTS
.prefix_range(
deps.storage,
Some(PrefixBound::inclusive(account_owner_ref)),
Some(PrefixBound::inclusive(account_owner_ref)),
Order::Ascending,
)
.next();
let free_account = match maybe_free_account {
let free_job_account = match maybe_free_job_account {
Some(Ok((account, last_job_id))) => Some(Account {
addr: account.1,
taken_by_job_id: Some(last_job_id),
}),
_ => None,
};
Ok(AccountResponse {
account: free_account,
account: free_job_account,
})
}

pub fn query_taken_accounts(
pub fn query_taken_job_accounts(
deps: Deps,
data: QueryTakenAccountsMsg,
data: QueryTakenJobAccountsMsg,
) -> StdResult<AccountsResponse> {
let account_owner_ref = &deps.api.addr_validate(data.account_owner_addr.as_str())?;
let iter = match data.start_after {
Some(start_after) => {
let start_after_account_addr = &deps.api.addr_validate(start_after.as_str())?;
TAKEN_ACCOUNTS.range(
TAKEN_JOB_ACCOUNTS.range(
deps.storage,
Some(Bound::exclusive((
account_owner_ref,
Expand All @@ -59,7 +60,7 @@ pub fn query_taken_accounts(
Order::Descending,
)
}
None => TAKEN_ACCOUNTS.prefix_range(
None => TAKEN_JOB_ACCOUNTS.prefix_range(
deps.storage,
Some(PrefixBound::inclusive(account_owner_ref)),
Some(PrefixBound::inclusive(account_owner_ref)),
Expand All @@ -81,12 +82,15 @@ pub fn query_taken_accounts(
})
}

pub fn query_free_accounts(deps: Deps, data: QueryFreeAccountsMsg) -> StdResult<AccountsResponse> {
pub fn query_free_job_accounts(
deps: Deps,
data: QueryFreeJobAccountsMsg,
) -> StdResult<AccountsResponse> {
let account_owner_ref = &deps.api.addr_validate(data.account_owner_addr.as_str())?;
let iter = match data.start_after {
Some(start_after) => {
let start_after_account_addr = &deps.api.addr_validate(start_after.as_str())?;
FREE_ACCOUNTS.range(
FREE_JOB_ACCOUNTS.range(
deps.storage,
Some(Bound::exclusive((
account_owner_ref,
Expand All @@ -96,7 +100,7 @@ pub fn query_free_accounts(deps: Deps, data: QueryFreeAccountsMsg) -> StdResult<
Order::Descending,
)
}
None => FREE_ACCOUNTS.prefix_range(
None => FREE_JOB_ACCOUNTS.prefix_range(
deps.storage,
Some(PrefixBound::inclusive(account_owner_ref)),
Some(PrefixBound::inclusive(account_owner_ref)),
Expand Down
4 changes: 2 additions & 2 deletions contracts/warp-account-tracker/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use cw_storage_plus::{Item, Map};
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");
pub const TAKEN_JOB_ACCOUNTS: Map<(&Addr, &Addr), Uint64> = Map::new("taken_job_accounts");

// 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), Uint64> = Map::new("free_accounts");
pub const FREE_JOB_ACCOUNTS: Map<(&Addr, &Addr), Uint64> = Map::new("free_job_accounts");

// owner address -> funding_account[]
// - user can have multiple funding accounts
Expand Down
Loading

0 comments on commit d194ecd

Please sign in to comment.