Skip to content

Commit

Permalink
Use lazy storage for listed regions
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Feb 24, 2024
1 parent 280b4d4 commit 66f035a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions contracts/coretime_market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod coretime_market {
codegen::{EmitEvent, Env},
prelude::vec::Vec,
reflect::ContractEventBase,
storage::Lazy,
EnvAccess,
};
use openbrush::{contracts::traits::psp34::Id, storage::Mapping, traits::Storage};
Expand All @@ -61,7 +62,7 @@ pub mod coretime_market {
/// A mapping that holds information about each region listed on sale.
pub listings: Mapping<RawRegionId, Listing>,
/// A vector containing all the regions listed on sale.
pub listed_regions: Vec<RawRegionId>,
pub listed_regions: Lazy<Vec<RawRegionId>>,
/// The configuration of the market. Set on contract initialization. Can't be changed
/// afterwards.
pub config: Config,
Expand Down Expand Up @@ -134,15 +135,15 @@ pub mod coretime_market {
pub fn listed_regions(&self, maybe_who: Option<AccountId>) -> Vec<RawRegionId> {
if let Some(who) = maybe_who {
self.listed_regions
.clone()
.get_or_default()
.into_iter()
.filter(|region_id| {
let Some(listing) = self.listings.get(region_id) else { return false };
listing.seller == who
})
.collect()
} else {
self.listed_regions.clone()
self.listed_regions.get_or_default()
}
}

Expand Down Expand Up @@ -226,7 +227,10 @@ pub mod coretime_market {
metadata_version: metadata.version,
},
);
self.listed_regions.push(region_id);

let mut listed_regions = self.listed_regions.get_or_default();
listed_regions.push(region_id);
self.listed_regions.set(&listed_regions);

self.emit_event(RegionListed {
region_id,
Expand Down Expand Up @@ -405,12 +409,14 @@ pub mod coretime_market {
fn remove_from_sale(&mut self, region_id: RawRegionId) -> Result<(), MarketError> {
let region_index = self
.listed_regions
.get_or_default()
.iter()
.position(|r| *r == region_id)
.ok_or(MarketError::RegionNotListed)?;

self.listed_regions.remove(region_index);
self.listings.remove(&region_id);
let mut listed_regions = self.listed_regions.get_or_default();
listed_regions.remove(region_index);
self.listed_regions.set(&listed_regions);

Ok(())
}
Expand Down Expand Up @@ -479,7 +485,7 @@ pub mod coretime_market {
MessageBuilder::<ExtendedEnvironment, CoretimeMarketRef>::from_account_id(
market_acc_id.clone(),
)
.call(|market| market.listed_regions());
.call(|market| market.listed_regions(None));
let listed_regions =
client.call_dry_run(&ink_e2e::alice(), &listed_regions, 0, None).await;
assert_eq!(listed_regions.return_value(), vec![]);
Expand Down

0 comments on commit 66f035a

Please sign in to comment.