Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesgun committed Jun 18, 2024
1 parent d047f2e commit 8b8edcd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
6 changes: 4 additions & 2 deletions contracts/contract-callback/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ mod tests {
count: 17,
cw721_archid_addr: cw721_addr,
archid_registry_addr: archid_registry_addr,
denom: "aarch".to_string()
denom: "aarch".to_string(),
cost_per_year: 250_000_000_000_000_000_u128,
};
let info = mock_info("creator", &coins(1000, "earth"));

Expand Down Expand Up @@ -170,7 +171,8 @@ mod tests {
count: 17,
cw721_archid_addr: cw721_addr,
archid_registry_addr: archid_registry_addr,
denom: "aarch".to_string()
denom: "aarch".to_string(),
cost_per_year: 250_000_000_000_000_000_u128,
};
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
Expand Down
14 changes: 7 additions & 7 deletions contracts/contract-callback/src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub fn mint_domain(deps: DepsMut, info: MessageInfo, _env: Env, domain_name: Str
let registry_contract = config.archid_registry_addr;
let cw721_contract = config.cw721_archid_addr;

let funds = &info.funds[0];
let res = cw_utils::must_pay(&info, &config.denom)?;
let registration: u64 =
u64::try_from(((res.checked_div(config.cost_per_year.into())).unwrap()).u128()).unwrap();
if registration < 1 {
return Err(ContractError::InvalidPayment { amount: res });
}

// let res = cw_utils::must_pay(&info, &denom)?;
// let registration: u64 =
// u64::try_from(((res.checked_div(cost_per_year.into())).unwrap()).u128()).unwrap();
// if registration < 1 {
// return Err(ContractError::InvalidPayment { amount: res });
// }
let funds = &info.funds[0];

// Create registration msg
let register_msg = archid_registry::msg::ExecuteMsg::Register {
Expand Down
3 changes: 2 additions & 1 deletion contracts/contract-callback/src/handlers/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub fn instantiate_handler(
denom: msg.denom,
start_block: env.block.height,
cron_period: 120_000, // 7 days
cron_fee_amount: 270_000_000_000_000_000 // reservation fee
cron_fee_amount: 270_000_000_000_000_000, // reservation fee
cost_per_year: 250_000_000_000_000_000_u128,
}
)?;

Expand Down
3 changes: 2 additions & 1 deletion contracts/contract-callback/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub struct InstantiateMsg {
pub count: u64,
pub cw721_archid_addr: Addr,
pub archid_registry_addr: Addr,
pub denom: String
pub denom: String,
pub cost_per_year: u128,
}

#[cw_serde]
Expand Down
4 changes: 2 additions & 2 deletions contracts/contract-callback/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pub struct Config {
pub denom: String,
pub start_block: u64,
pub cron_period: u32,
pub cron_fee_amount: u128

pub cron_fee_amount: u128,
pub cost_per_year: u128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down
75 changes: 75 additions & 0 deletions contracts/contract-callback/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Ok;
use contract_callback::{
msg::{GetCountResponse, InstantiateMsg, QueryMsg}, AppContract, AppExecuteMsgFns, AppQueryMsgFns, ContractError
};
Expand Down Expand Up @@ -232,6 +233,79 @@ fn renew_domain() -> anyhow::Result<()> {
)?;
assert_eq!(resolve_info.names, Some(vec!["domain1.arch".to_string()]));

Ok(())
}

#[test]
fn deposit_unauthorized() -> anyhow::Result<()> {
let mock = MockBech32::new("mock");
let user1 = mock.addr_make("user1");

mock.add_balance(
&user1,
coins(5_000_000_000_000_000_000, "aarch".to_string()))?;

let (contract, cw721_archid_addr, archid_registry_addr) = setup(mock.clone())?;

let res = contract
.call_as(&user1)
.deposit(
&[coin(1_000_000_000_000_000_000, "aarch".to_string())]
);

assert_eq!(true, res.is_err());

Ok(())
}

#[test]
fn withdraw_unauthorized() -> anyhow::Result<()> {
let mock = MockBech32::new("mock");
let user1 = mock.addr_make("user1");

mock.add_balance(
&user1,
coins(5_000_000_000_000_000_000, "aarch".to_string()))?;

let (contract, cw721_archid_addr, archid_registry_addr) = setup(mock.clone())?;

let res = contract
.call_as(&user1)
.withdraw();

assert_eq!(true, res.is_err());

Ok(())
}

#[test]
fn deposit_withdraw_funds() -> anyhow::Result<()> {
let mock = MockBech32::new("mock");
let admin: Addr = mock.sender.clone();

mock.add_balance(
&admin,
coins(5_000_000_000_000_000_000, "aarch".to_string()))?;

let (contract, cw721_archid_addr, archid_registry_addr) = setup(mock.clone())?;

let res = contract
.call_as(&admin)
.deposit(
&[coin(2_000_000_000_000_000_000, "aarch".to_string())]
)?;

let balance = mock.balance(contract.addr_str()?, Some("aarch".to_string()))?;

assert_eq!(balance, coins(2_000_000_000_000_000_000, "aarch".to_string()));

let res = contract
.call_as(&admin)
.withdraw()?;

let balance = mock.balance(contract.addr_str()?, Some("aarch".to_string()))?;

assert_eq!(balance, coins(0, "aarch".to_string()));

Ok(())
}
Expand Down Expand Up @@ -320,6 +394,7 @@ fn setup(mock: MockBech32) -> anyhow::Result<(AppContract<MockBech32>, Addr, Add
cw721_archid_addr: cw721_archid_addr.clone(),
archid_registry_addr: archid_registry_addr.clone(),
denom: "aarch".to_string(),
cost_per_year: 250_000_000_000_000_000_u128,
};
let init_resp = contract.instantiate(
&msg,
Expand Down

0 comments on commit 8b8edcd

Please sign in to comment.