-
Notifications
You must be signed in to change notification settings - Fork 5
/
msg.rs
350 lines (323 loc) · 11.5 KB
/
msg.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::ContractError;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Addr, Timestamp, Uint64};
use cw721::{AllNftInfoResponse, TokensResponse};
use cw721_base::InstantiateMsg as Cw721InstantiateMsg;
use cw721_poap::Metadata;
use url::Url;
#[cw_serde]
#[schemars(rename = "PoapInstantiateMsg", title = "InstantiateMsg")]
pub struct InstantiateMsg {
/// Address of who will have the right to administer the contract.
pub admin: String,
/// Address of who can call the [`ExecuteMsg::MintTo`] other then the admin.
pub minter: String,
/// Id of the CW721 contract to initialize together with this contract.
pub cw721_code_id: Uint64,
/// Initialization message that will be sent to the CW721 contract.
pub cw721_instantiate_msg: Cw721InstantiateMsg,
/// Information about the event.
pub event_info: EventInfo,
}
#[cw_serde]
pub struct EventInfo {
/// User that created the event.
pub creator: String,
/// Time at which the event begins.
pub start_time: Timestamp,
/// Time at which the event ends.
pub end_time: Timestamp,
/// Max amount of poap that a single user can mint.
pub per_address_limit: u32,
/// Identifies a valid IPFS URI corresponding to where the assets and metadata of the POAPs are stored.
pub poap_uri: String,
}
#[cw_serde]
pub enum ExecuteMsg {
/// Allows the contract's admin to enable the [`ExecuteMsg::Mint`].
EnableMint {},
/// Allows the contract's admin to disable the [`ExecuteMsg::Mint`].
DisableMint {},
/// If the mint is enabled, allow the user to mint the poap by themself.
/// It's disabled before the start of the event and after the event's end.
Mint {},
/// Allows the contract's admin or the minter to mint a POAP for a specific recipient.
/// It's disabled before the start of the event and after the event's end.
MintTo { recipient: String },
/// Message that allows the event's creator to change the time frame of the event
/// if it's not started or finished.
UpdateEventInfo {
start_time: Timestamp,
end_time: Timestamp,
},
/// Allows the contract's admin to transfer the admin rights to another user.
UpdateAdmin { new_admin: String },
/// Allows the contract's admin to transfer the minting rights to another user.
UpdateMinter { new_minter: String },
}
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// Returns the configuration info as a [`QueryConfigResponse`].
#[returns(QueryConfigResponse)]
Config {},
/// Returns the event info as a [`QueryEventInfoResponse`].
#[returns(QueryEventInfoResponse)]
EventInfo {},
/// Returns the amount of poaps minted from `user` as [`QueryMintedAmountResponse`].
#[returns(QueryMintedAmountResponse)]
MintedAmount { user: String },
/// Returns the nft info with approvals from cw721 contract as a [`AllNftInfoResponse`]
#[returns(AllNftInfoResponse<Metadata>)]
AllNftInfo {
token_id: String,
include_expired: Option<bool>,
},
/// Returns all the tokens ids owned by the given owner from cw721 contract as a [`TokensResponse`]
#[returns(TokensResponse)]
Tokens {
owner: String,
start_after: Option<String>,
limit: Option<u32>,
},
}
/// Response to [`QueryMsg::Config`].
#[cw_serde]
pub struct QueryConfigResponse {
/// Address of the contract administrator.
pub admin: Addr,
/// Address of the entity that is allowed to use [`ExecuteMsg::MintTo`].
pub minter: Addr,
/// Tells if the users can execute the [`ExecuteMsg::Mint`].
pub mint_enabled: bool,
/// The maximus number of poap that an user can request.
pub per_address_limit: u32,
/// Id of the cw721 contract that this contract has initialized.
pub cw721_contract_code: Uint64,
/// Address of the cw721 contract that this contract is using to
/// mint the poaps.
pub cw721_contract: Addr,
}
/// Response to [`QueryMsg::EventInfo`].
#[cw_serde]
pub struct QueryEventInfoResponse {
/// Address of who created the event.
pub creator: Addr,
/// Time at which the event starts.
pub start_time: Timestamp,
/// Time at which the event ends.
pub end_time: Timestamp,
/// IPFS uri where the event's metadata are stored
pub poap_uri: String,
}
/// Response to [`QueryMsg::MintedAmount`].
#[cw_serde]
pub struct QueryMintedAmountResponse {
/// Address for which the request was made.
pub user: Addr,
/// Amount of poaps minted from the user.
pub amount: u32,
}
impl InstantiateMsg {
/// Checks that the data inside the message are coherent.
/// NOTE: This function don't checks if the address are valid.
pub fn validate(&self) -> Result<(), ContractError> {
// Check that the end time is after the start time
if self.event_info.start_time >= self.event_info.end_time {
return Err(ContractError::StartTimeAfterEndTime {
start: self.event_info.start_time.to_owned(),
end: self.event_info.end_time.to_owned(),
});
}
// Check per address limit
if self.event_info.per_address_limit == 0 {
return Err(ContractError::InvalidPerAddressLimit {});
}
// Check that the poap uri is a valid IPFS url
let poap_uri = Url::parse(&self.event_info.poap_uri)
.map_err(|_err| ContractError::InvalidPoapUri {})?;
if poap_uri.scheme() != "ipfs" {
return Err(ContractError::InvalidPoapUri {});
}
Ok(())
}
}
impl ExecuteMsg {
/// Checks that the data inside the message are coherent.
/// NOTE: This function don't checks if the address are valid.
pub fn validate(&self) -> Result<(), ContractError> {
match &self {
ExecuteMsg::UpdateEventInfo {
start_time,
end_time,
} => {
if start_time >= end_time {
Err(ContractError::StartTimeAfterEndTime {
start: start_time.to_owned(),
end: end_time.to_owned(),
})
} else {
Ok(())
}
}
_ => Ok(()),
}
}
}
#[cfg(test)]
mod tests {
use crate::msg::{EventInfo, ExecuteMsg, InstantiateMsg};
use crate::ContractError;
use cosmwasm_std::Timestamp;
use cw721_base::InstantiateMsg as Cw721InstantiateMsg;
#[test]
fn instantiate_with_start_time_after_end_time_error() {
let start = Timestamp::from_seconds(2);
let end = Timestamp::from_seconds(1);
let msg = InstantiateMsg {
admin: "".to_string(),
minter: "".to_string(),
cw721_code_id: 0u64.into(),
cw721_instantiate_msg: Cw721InstantiateMsg {
name: "".to_string(),
minter: "".to_string(),
symbol: "".to_string(),
},
event_info: EventInfo {
creator: "".to_string(),
start_time: start.clone(),
end_time: end.clone(),
per_address_limit: 1,
poap_uri: "ipfs://domain.com".to_string(),
},
};
assert_eq!(
ContractError::StartTimeAfterEndTime { start, end },
msg.validate().unwrap_err()
);
}
#[test]
fn instantiate_with_start_time_equal_end_time_error() {
let start = Timestamp::from_seconds(1);
let end = Timestamp::from_seconds(1);
let msg = InstantiateMsg {
admin: "".to_string(),
minter: "".to_string(),
cw721_code_id: 0u64.into(),
cw721_instantiate_msg: Cw721InstantiateMsg {
name: "".to_string(),
minter: "".to_string(),
symbol: "".to_string(),
},
event_info: EventInfo {
creator: "".to_string(),
start_time: start.clone(),
end_time: end.clone(),
per_address_limit: 1,
poap_uri: "ipfs://domain.com".to_string(),
},
};
assert_eq!(
ContractError::StartTimeAfterEndTime { start, end },
msg.validate().unwrap_err()
);
}
#[test]
fn instantiate_with_invalid_per_address_limit_error() {
let msg = InstantiateMsg {
admin: "".to_string(),
minter: "".to_string(),
cw721_code_id: 0u64.into(),
cw721_instantiate_msg: Cw721InstantiateMsg {
name: "".to_string(),
minter: "".to_string(),
symbol: "".to_string(),
},
event_info: EventInfo {
creator: "".to_string(),
start_time: Timestamp::from_seconds(1),
end_time: Timestamp::from_seconds(2),
per_address_limit: 0,
poap_uri: "ipfs://domain.com".to_string(),
},
};
assert_eq!(
ContractError::InvalidPerAddressLimit {},
msg.validate().unwrap_err()
);
}
#[test]
fn instantiate_with_invalid_poap_uri_error() {
let msg = InstantiateMsg {
admin: "".to_string(),
minter: "".to_string(),
cw721_code_id: 0u64.into(),
cw721_instantiate_msg: Cw721InstantiateMsg {
name: "".to_string(),
minter: "".to_string(),
symbol: "".to_string(),
},
event_info: EventInfo {
creator: "".to_string(),
start_time: Timestamp::from_seconds(1),
end_time: Timestamp::from_seconds(2),
per_address_limit: 1,
poap_uri: "invalid_base_poap_uri".to_string(),
},
};
assert_eq!(
ContractError::InvalidPoapUri {},
msg.validate().unwrap_err()
);
}
#[test]
fn instantiate_with_non_ipfs_poap_uri_error() {
let msg = InstantiateMsg {
admin: "".to_string(),
minter: "".to_string(),
cw721_code_id: 0u64.into(),
cw721_instantiate_msg: Cw721InstantiateMsg {
name: "".to_string(),
minter: "".to_string(),
symbol: "".to_string(),
},
event_info: EventInfo {
creator: "".to_string(),
start_time: Timestamp::from_seconds(1),
end_time: Timestamp::from_seconds(2),
per_address_limit: 1,
poap_uri: "https://domain.com".to_string(),
},
};
assert_eq!(
ContractError::InvalidPoapUri {},
msg.validate().unwrap_err()
);
}
#[test]
fn update_event_info_start_time_after_end_time_error() {
let start = Timestamp::from_seconds(2);
let end = Timestamp::from_seconds(1);
let msg = ExecuteMsg::UpdateEventInfo {
start_time: start.clone(),
end_time: end.clone(),
};
assert_eq!(
ContractError::StartTimeAfterEndTime { start, end },
msg.validate().unwrap_err()
);
}
#[test]
fn update_event_info_start_time_equal_end_time_error() {
let start = Timestamp::from_seconds(1);
let end = Timestamp::from_seconds(1);
let msg = ExecuteMsg::UpdateEventInfo {
start_time: start.clone(),
end_time: end.clone(),
};
assert_eq!(
ContractError::StartTimeAfterEndTime { start, end },
msg.validate().unwrap_err()
);
}
}