From 4bb6dfbeff3828b930c92dfcb311f0eb51933221 Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Wed, 15 Nov 2023 02:01:39 -0800 Subject: [PATCH] Use entry API for HashMap --- bisenzone-cw-mvp/src/contract.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/bisenzone-cw-mvp/src/contract.rs b/bisenzone-cw-mvp/src/contract.rs index 9109826..77ad58f 100644 --- a/bisenzone-cw-mvp/src/contract.rs +++ b/bisenzone-cw-mvp/src/contract.rs @@ -66,10 +66,10 @@ pub mod execute { *state .utilization - .get_mut(&creditor) - .unwrap() - .get_mut(&info.sender) - .unwrap() += amount; + .entry(creditor) + .or_default() + .entry(info.sender) + .or_default() += amount; Ok(state) })?; @@ -131,13 +131,18 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { pub mod query { use super::*; + use std::collections::HashMap; use crate::msg::GetObligationsResponse; pub fn get_obligations(deps: Deps, creditor: Addr) -> StdResult { let state = STATE.load(deps.storage)?; Ok(GetObligationsResponse { - obligations: state.utilization[&creditor].clone(), + obligations: state + .utilization + .get(&creditor) + .unwrap_or(&HashMap::new()) + .clone(), }) } }