/ src / lib.rs
lib.rs
  1  #[macro_use]
  2  extern crate nestify;
  3  
  4  use anthol_store::item::AttrKeys;
  5  use ic_cdk_macros::*;
  6  use ic_stable_structures::{
  7      memory_manager::{MemoryId, MemoryManager, VirtualMemory},
  8      {DefaultMemoryImpl, StableBTreeMap, StableLog},
  9  };
 10  use shared::{
 11      actor::{
 12          account::{
 13              AccountPageError, AccountPageResponse, SetUserProfileError, SetUserProfileRequest,
 14          },
 15          id::ActorId,
 16          ActorPrincipal,
 17      },
 18      item::{ItemKey, Tag},
 19      market::{MarketId, MarketPrincipal},
 20      route::home::{HomePageError, HomePageRequest, HomePageResponse},
 21      store::{StoreId, StorePrincipal},
 22      unit::Currency,
 23  };
 24  use std::cell::RefCell;
 25  
 26  mod actor;
 27  mod favorites;
 28  mod item;
 29  mod log;
 30  mod market;
 31  mod route;
 32  mod store;
 33  mod tag;
 34  mod test;
 35  
 36  #[cfg(feature = "canbench-rs")]
 37  mod benches;
 38  
 39  use actor::ActorData;
 40  use item::ItemFluctuateData;
 41  use market::MarketData;
 42  use store::StoreData;
 43  use tag::TagData;
 44  
 45  type Memory = VirtualMemory<DefaultMemoryImpl>;
 46  
 47  thread_local! {
 48      static MEMORY_MANAGER: RefCell<MemoryManager<DefaultMemoryImpl>> =
 49          RefCell::new(MemoryManager::init(DefaultMemoryImpl::default()));
 50  
 51      pub(crate) static LOG: RefCell<StableLog<(), Memory, Memory>> = RefCell::new(
 52          StableLog::init(
 53              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(0))),
 54              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(1))),
 55          ).unwrap()
 56      );
 57  
 58      pub(crate) static ACTORS: RefCell<StableBTreeMap<ActorPrincipal, ActorData, Memory>> = RefCell::new(
 59          StableBTreeMap::init(
 60              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(2))),
 61          )
 62      );
 63  
 64      pub(crate) static ACTORS_IN_ID: RefCell<StableBTreeMap<ActorId, ActorPrincipal, Memory>> = RefCell::new(
 65          StableBTreeMap::init(
 66              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(3))),
 67          )
 68      );
 69  
 70      pub(crate) static STORES: RefCell<StableBTreeMap<StorePrincipal, StoreData, Memory>> = RefCell::new(
 71          StableBTreeMap::init(
 72              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(4))),
 73          )
 74      );
 75  
 76      pub(crate) static STORES_IN_ID: RefCell<StableBTreeMap<StoreId, StorePrincipal, Memory>> = RefCell::new(
 77          StableBTreeMap::init(
 78              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(5))),
 79          )
 80      );
 81  
 82      pub(crate) static MARKETS: RefCell<StableBTreeMap<MarketPrincipal, MarketData, Memory>> = RefCell::new(
 83          StableBTreeMap::init(
 84              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(6))),
 85          )
 86      );
 87  
 88      pub(crate) static MARKETS_IN_ID: RefCell<StableBTreeMap<MarketId, MarketPrincipal, Memory>> = RefCell::new(
 89          StableBTreeMap::init(
 90              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(7))),
 91          )
 92      );
 93  
 94      pub(crate) static TAGS: RefCell<StableBTreeMap<Tag, TagData, Memory>> = RefCell::new(
 95          StableBTreeMap::init(
 96              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(8))),
 97          )
 98      );
 99  
100      pub(crate) static ITEM_FLUCTUATE_DATA: RefCell<StableBTreeMap<(StorePrincipal, ItemKey, AttrKeys), ItemFluctuateData, Memory>> = RefCell::new(
101          StableBTreeMap::init(
102              MEMORY_MANAGER.with(|m| m.borrow().get(MemoryId::new(9))),
103          )
104      );
105  }
106  
107  #[update]
108  async fn test_init() {
109      test::init::test_init().await;
110  }
111  
112  #[query(composite = true)]
113  async fn get_item_page_data(
114      arg: shared::item::ItemPageRequest,
115  ) -> Result<shared::item::ItemPageResponse, String> {
116      match store::get_item_page_data(arg).await {
117          Ok(res) => Ok(res),
118          Err(e) => {
119              // TODO: Log error
120              Err(e.1)
121          }
122      }
123  }
124  
125  #[query(composite = true)]
126  async fn get_home_page_data(arg: HomePageRequest) -> Result<HomePageResponse, HomePageError> {
127      route::home::get_home_page_data(arg).await
128  }
129  
130  #[query]
131  fn get_user_basket_page_data(
132      currency: Currency,
133  ) -> Result<
134      shared::route::basket::UserBasketPageResponse,
135      (shared::route::basket::UserBasketPageError, String),
136  > {
137      route::basket::get_user_basket_page_data(currency)
138  }
139  
140  #[query]
141  fn get_account_page_data() -> Result<AccountPageResponse, AccountPageError> {
142      route::account::get_account_page_data()
143  }
144  
145  #[update]
146  async fn set_user_profile(profile: SetUserProfileRequest) -> Result<(), SetUserProfileError> {
147      actor::user::set_user_profile(profile)
148  }
149  
150  #[update]
151  async fn get_trusted_origins() -> Vec<String> {
152      vec![
153          // Origins should be in the format defined by the Window.postMessage method (https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#the_dispatched_event)
154          String::from("https://anthol.net"),
155          String::from("https://www.anthol.net"),
156          String::from("https://qqyox-jyaaa-aaaap-abtwa-cai.icp0.io/"),
157      ]
158  }
159  
160  ic_cdk::export_candid!();