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

workshop subkey PR #62

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ pub fn init<S: Storage, A: Api, Q: Querier>(
contract: CONTRACT_NAME.to_string(),
version: CONTRACT_VERSION.to_string(),
};
set_contract_version(&mut deps.storage, &version)?;
whitelist_init(deps, env, msg)
let result = whitelist_init(deps, env, msg);
if result.is_ok() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, only set on success.

I guess

let result = whitelist_init(deps, env, msg)?;
set_contract_version(&mut deps.storage, &version)?;
Ok(result);

would be the same, but this works as well

set_contract_version(&mut deps.storage, &version)?;
};
result
}

pub fn handle<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -314,6 +317,7 @@ mod tests {
use cosmwasm_std::testing::{mock_dependencies, mock_env, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins, StakingMsg};
use cw1_whitelist::msg::AdminListResponse;
use cw2::get_contract_version;

// this will set up the init for other tests
fn setup_test_case<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -344,6 +348,42 @@ mod tests {
}
}


#[test]
fn get_contract_version_works() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the testcase. This could be a bit shorter (simple init), but this works as well.

let mut deps = mock_dependencies(20, &coins(1111, "token1"));

let owner = HumanAddr::from("admin0001");
let admins = vec![owner.clone(), HumanAddr::from("admin0002")];

let spender1 = HumanAddr::from("spender0001");
let spender2 = HumanAddr::from("spender0002");
let spender3 = HumanAddr::from("spender0003");
let initial_spenders = vec![spender1.clone(), spender2.clone()];

// Same allowances for all spenders, for simplicity
let denom1 = "token1";
let amount1 = 1111;

let allow1 = coin(amount1, denom1);
let initial_allowances = vec![allow1.clone()];

let expires_never = Expiration::Never {};
let initial_expirations = vec![expires_never.clone(), expires_never.clone()];

let env = mock_env(owner, &[]);
setup_test_case(
&mut deps,
&env,
&admins,
&initial_spenders,
&initial_allowances,
&initial_expirations,
);

assert_eq!(get_contract_version(&deps.storage).unwrap().contract, CONTRACT_NAME)
}

#[test]
fn query_allowance_works() {
let mut deps = mock_dependencies(20, &coins(1111, "token1"));
Expand Down