1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::{collections::HashSet, marker::PhantomData};
use abstract_os::version_control::Core;
use abstract_sdk::{memory::Memory, IbcCallbackHandlerFn, ReceiveHandlerFn, BASE_STATE};
use cosmwasm_std::{Addr, Empty, StdResult, Storage};
use cw2::{ContractVersion, CONTRACT};
use cw_storage_plus::{Item, Map};
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::ApiError;
pub const TRADER_NAMESPACE: &str = "traders";
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct ApiState {
pub version_control: Addr,
pub memory: Memory,
}
pub struct ApiContract<
'a,
Request: Serialize + DeserializeOwned,
Error: From<cosmwasm_std::StdError> + From<ApiError>,
Receive: Serialize + DeserializeOwned = Empty,
> {
pub traders: Map<'a, Addr, HashSet<Addr>>,
pub base_state: Item<'a, ApiState>,
pub version: Item<'a, ContractVersion>,
pub target_os: Option<Core>,
pub dependencies: &'static [&'static str],
pub(crate) ibc_callbacks: &'a [(&'static str, IbcCallbackHandlerFn<Self, Error>)],
pub(crate) receive_handler: Option<ReceiveHandlerFn<Self, Receive, Error>>,
_phantom_data_request: PhantomData<Request>,
_phantom_data_error: PhantomData<Error>,
_phantom_data_receive: PhantomData<Receive>,
}
impl<
'a,
T: Serialize + DeserializeOwned,
R: Serialize + DeserializeOwned,
E: From<cosmwasm_std::StdError> + From<ApiError>,
> ApiContract<'a, T, E, R>
{
pub const fn new() -> Self {
Self {
version: CONTRACT,
base_state: Item::new(BASE_STATE),
traders: Map::new(TRADER_NAMESPACE),
target_os: None,
ibc_callbacks: &[],
dependencies: &[],
receive_handler: None,
_phantom_data_request: PhantomData,
_phantom_data_receive: PhantomData,
_phantom_data_error: PhantomData,
}
}
pub const fn with_ibc_callbacks(
mut self,
callbacks: &'a [(&'static str, IbcCallbackHandlerFn<Self, E>)],
) -> Self {
self.ibc_callbacks = callbacks;
self
}
pub const fn with_receive(mut self, receive_handler: ReceiveHandlerFn<Self, R, E>) -> Self {
self.receive_handler = Some(receive_handler);
self
}
pub const fn with_dependencies(mut self, dependencies: &'static [&'static str]) -> Self {
self.dependencies = dependencies;
self
}
pub fn state(&self, store: &dyn Storage) -> StdResult<ApiState> {
self.base_state.load(store)
}
pub fn version(&self, store: &dyn Storage) -> StdResult<ContractVersion> {
self.version.load(store)
}
pub fn target(&self) -> Result<&Addr, ApiError> {
Ok(&self
.target_os
.as_ref()
.ok_or(ApiError::NoTargetOS {})?
.proxy)
}
}