Avatar
0
monkey Enlightened
monkey Enlightened
CosmosSDK: Khởi tạo và sử dụng Map
CosmosSDK: Khơi tạo và sử dụng Map thế nào?
  • Answer
cosmos-sdk
Remain: 5
1 Answer
Avatar
monkey Enlightened
monkey Enlightened
use cosmwasm_std::{
  Response, StdError, StdResult, Addr, Storage
};

use cw_storage_plus::Map;const HOSPITALS: Map<&Addr, Hospital> = Map::new("hospitals");pub fn execute_hospital_register(
  store: &mut dyn Storage,
  hospital: &Hospital
) -> Result<Response, ContractError> {

  let save_result = HOSPITALS.save(store, &hospital.address, hospital);

  if save_result.is_err() {
    return Err(ContractError::SaveError {});
  }

  return Ok(Response::default());
}

fn query_hospital(deps: Deps, wallet_address: &Addr) -> StdResult<Binary> {
  let hospital = HOSPITALS.load(deps.storage, wallet_address);
  let resp = match hospital {Ok(data) => HospitalResponse {
    hospital: data.clone(),
  },

  Err(err) => {
    return Err(err);
  }

};
  • 0
  • Reply