-
Notifications
You must be signed in to change notification settings - Fork 180
/
extension.rs
241 lines (231 loc) · 10.3 KB
/
extension.rs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use std::marker::PhantomData;
use cosmwasm_std::Empty;
use crate::{
state::Cw721Config,
traits::{Cw721CustomMsg, Cw721State},
DefaultOptionalCollectionExtension, DefaultOptionalCollectionExtensionMsg,
DefaultOptionalNftExtension, DefaultOptionalNftExtensionMsg, EmptyOptionalCollectionExtension,
EmptyOptionalCollectionExtensionMsg, EmptyOptionalNftExtension, EmptyOptionalNftExtensionMsg,
};
/// Opionated version of generic `Cw721Extensions` with default onchain nft and collection extensions using:
/// - `DefaultOptionalNftExtension` for NftInfo extension (onchain metadata).
/// - `DefaultOptionalNftExtensionMsg` for NftInfo extension msg for onchain metadata.
/// - `DefaultOptionalCollectionExtension` for CollectionInfo extension (onchain attributes).
/// - `DefaultOptionalCollectionExtensionMsg` for CollectionInfo extension msg for onchain collection attributes.
/// - `Empty` for custom extension msg for custom contract logic.
/// - `Empty` for custom query msg for custom contract logic.
/// - `Empty` for custom response msg for custom contract logic.
pub struct Cw721OnchainExtensions<'a> {
pub config: Cw721Config<'a, DefaultOptionalNftExtension>,
pub(crate) _collection_extension: PhantomData<DefaultOptionalCollectionExtension>,
pub(crate) _nft_extension_msg: PhantomData<DefaultOptionalNftExtensionMsg>,
pub(crate) _collection_extension_msg: PhantomData<DefaultOptionalCollectionExtensionMsg>,
pub(crate) _extension_msg: PhantomData<Empty>,
pub(crate) _extension_query_msg: PhantomData<Empty>,
pub(crate) _custom_response_msg: PhantomData<Empty>,
}
impl Default for Cw721OnchainExtensions<'static> {
fn default() -> Self {
Self {
config: Cw721Config::<DefaultOptionalNftExtension>::default(),
_collection_extension: PhantomData,
_nft_extension_msg: PhantomData,
_collection_extension_msg: PhantomData,
_extension_msg: PhantomData,
_extension_query_msg: PhantomData,
_custom_response_msg: PhantomData,
}
}
}
/// Opionated version of generic `Cw721Extensions` with `EmptyOptionalNftExtension` and `DefaultOptionalCollectionExtension` using:
/// - `Empty` for NftInfo extension (onchain metadata).
/// - `Empty` for NftInfo extension msg for onchain metadata.
/// - `DefaultOptionalCollectionExtension` for CollectionInfo extension (onchain attributes).
/// - `DefaultOptionalCollectionExtensionMsg` for CollectionInfo extension msg for onchain collection attributes.
/// - `Empty` for custom extension msg for custom contract logic.
/// - `Empty` for custom query msg for custom contract logic.
/// - `Empty` for custom response msg for custom contract logic.
pub struct Cw721BaseExtensions<'a> {
pub config: Cw721Config<'a, EmptyOptionalNftExtension>,
pub(crate) _collection_extension: PhantomData<DefaultOptionalCollectionExtension>,
pub(crate) _nft_extension_msg: PhantomData<EmptyOptionalNftExtensionMsg>,
pub(crate) _collection_extension_msg: PhantomData<DefaultOptionalCollectionExtensionMsg>,
pub(crate) _extension_msg: PhantomData<Empty>,
pub(crate) _extension_query_msg: PhantomData<Empty>,
pub(crate) _custom_response_msg: PhantomData<Empty>,
}
impl Default for Cw721BaseExtensions<'static> {
fn default() -> Self {
Self {
config: Cw721Config::<EmptyOptionalNftExtension>::default(),
_collection_extension: PhantomData,
_nft_extension_msg: PhantomData,
_collection_extension_msg: PhantomData,
_extension_msg: PhantomData,
_extension_query_msg: PhantomData,
_custom_response_msg: PhantomData,
}
}
}
/// Opionated version of generic `Cw721Extensions` with empty onchain nft and collection extensions using:
/// - `Empty` for NftInfo extension (onchain metadata).
/// - `Empty` for NftInfo extension msg for onchain metadata.
/// - `Empty` for CollectionInfo extension (onchain attributes).
/// - `Empty` for CollectionInfo extension msg for onchain collection attributes.
/// - `Empty` for custom extension msg for custom contract logic.
/// - `Empty` for custom query msg for custom contract logic.
/// - `Empty` for custom response msg for custom contract logic.
pub struct Cw721EmptyExtensions<'a> {
pub config: Cw721Config<'a, EmptyOptionalNftExtension>,
pub(crate) _collection_extension: PhantomData<EmptyOptionalCollectionExtension>,
pub(crate) _nft_extension_msg: PhantomData<EmptyOptionalNftExtensionMsg>,
pub(crate) _collection_extension_msg: PhantomData<EmptyOptionalCollectionExtensionMsg>,
pub(crate) _extension_msg: PhantomData<Empty>,
pub(crate) _extension_query_msg: PhantomData<Empty>,
pub(crate) _custom_response_msg: PhantomData<Empty>,
}
impl Default for Cw721EmptyExtensions<'static> {
fn default() -> Self {
Self {
config: Cw721Config::<EmptyOptionalNftExtension>::default(),
_collection_extension: PhantomData,
_nft_extension_msg: PhantomData,
_collection_extension_msg: PhantomData,
_extension_msg: PhantomData,
_extension_query_msg: PhantomData,
_custom_response_msg: PhantomData,
}
}
}
/// Generic `Cw721Extensions` which may be extended by other contracts with custom onchain nft and collection extensions.
///
/// Contract with generic onchain nft and collection extensions allows handling with:
/// - no extensions: by defining all extensions as `Empty` or `Option<Empty>`.
/// - opionated `DefaultOptionalNftExtension` and `DefaultOptionalCollectionExtension`.
/// - `DefaultOptionalNftExtension`: either with nft metadata (`Some<NftExtension>`) or none `None`.
/// - `DefaultOptionalCollectionExtension`: either with collection metadata (`Some<CollectionExtension>`) or none `None`.
/// - custom extensions: by defining custom extensions.
///
/// Generics:
/// - `TNftExtension` for NftInfo extension (onchain metadata).
/// - `TNftExtensionMsg` for NftInfo extension msg for onchain metadata.
/// - `TCollectionExtension` for CollectionInfo extension (onchain attributes).
/// - `TCollectionExtensionMsg` for CollectionInfo extension msg for onchain collection attributes.
/// - `TExtensionMsg` for custom extension msg for custom contract logic.
/// - `TExtensionQueryMsg` for custom query msg for custom contract logic.
/// - `TCustomResponseMsg` for custom response msg for custom contract logic.
///
/// Example:
/// ```rust
/// // instantiate:
/// let contract = Cw721Extensions::<
/// DefaultOptionalNftExtension, // use `Option<Empty>` or `Empty` for no nft metadata
/// DefaultOptionalNftExtensionMsg, // use `Option<Empty>` or `Empty` for no nft metadata
/// DefaultOptionalCollectionExtension, // use `Option<Empty>` or `Empty` for no collection metadata
/// DefaultOptionalCollectionExtensionMsg, // use `Option<Empty>` or `Empty` for no collection metadata
/// Empty, // no custom extension msg
/// Empty, // no custom query msg
/// Empty, // no custom response msg
/// >::default();
/// let info = mock_info(CREATOR, &[]);
/// let init_msg = Cw721InstantiateMsg {
/// name: "SpaceShips".to_string(),
/// symbol: "SPACE".to_string(),
/// collection_info_extension: None,
/// minter: None,
/// creator: None,
/// withdraw_address: None,
/// };
/// //...
/// // mint:
/// let token_id = "Enterprise";
/// let token_uri = Some("https://starships.example.com/Starship/Enterprise.json".into());
/// let extension = Some(NftExtensionMsg {
/// description: Some("description1".into()),
/// name: Some("name1".to_string()),
/// attributes: Some(vec![Trait {
/// display_type: None,
/// trait_type: "type1".to_string(),
/// value: "value1".to_string(),
/// }]),
/// ..NftExtensionMsg::default()
/// });
/// let exec_msg = Cw721ExecuteMsg::<
/// DefaultOptionalNftExtensionMsg,
/// DefaultOptionalCollectionExtensionMsg,
/// Empty,
/// >::Mint {
/// token_id: token_id.to_string(),
/// owner: "john".to_string(),
/// token_uri: token_uri.clone(),
/// extension: extension.clone(), // use `extension: None` for no metadata
/// };
/// //...
/// ```
pub struct Cw721Extensions<
'a,
// NftInfo extension (onchain metadata).
TNftExtension,
// NftInfo extension msg for onchain metadata.
TNftExtensionMsg,
// CollectionInfo extension (onchain attributes).
TCollectionExtension,
// CollectionInfo extension msg for onchain collection attributes.
TCollectionExtensionMsg,
// Custom extension msg for custom contract logic. Default implementation is a no-op.
TExtensionMsg,
// Custom query msg for custom contract logic. Default implementation returns an empty binary.
TExtensionQueryMsg,
// Defines for `CosmosMsg::Custom<T>` in response. Barely used, so `Empty` can be used.
TCustomResponseMsg,
> where
TNftExtension: Cw721State,
TNftExtensionMsg: Cw721CustomMsg,
TCollectionExtension: Cw721State,
TCollectionExtensionMsg: Cw721CustomMsg,
{
pub config: Cw721Config<'a, TNftExtension>,
pub(crate) _collection_extension: PhantomData<TCollectionExtension>,
pub(crate) _nft_extension_msg: PhantomData<TNftExtensionMsg>,
pub(crate) _collection_extension_msg: PhantomData<TCollectionExtensionMsg>,
pub(crate) _extension_msg: PhantomData<TExtensionMsg>,
pub(crate) _extension_query_msg: PhantomData<TExtensionQueryMsg>,
pub(crate) _custom_response_msg: PhantomData<TCustomResponseMsg>,
}
impl<
TNftExtension,
TNftExtensionMsg,
TCollectionExtension,
TCollectionExtensionMsg,
TExtensionMsg,
TExtensionQueryMsg,
TCustomResponseMsg,
> Default
for Cw721Extensions<
'static,
TNftExtension,
TNftExtensionMsg,
TCollectionExtension,
TCollectionExtensionMsg,
TExtensionMsg,
TExtensionQueryMsg,
TCustomResponseMsg,
>
where
TNftExtension: Cw721State,
TNftExtensionMsg: Cw721CustomMsg,
TCollectionExtension: Cw721State,
TCollectionExtensionMsg: Cw721CustomMsg,
{
fn default() -> Self {
Self {
config: Cw721Config::default(),
_collection_extension: PhantomData,
_nft_extension_msg: PhantomData,
_collection_extension_msg: PhantomData,
_extension_msg: PhantomData,
_extension_query_msg: PhantomData,
_custom_response_msg: PhantomData,
}
}
}