-
Notifications
You must be signed in to change notification settings - Fork 5
/
state.rs
126 lines (102 loc) · 3.6 KB
/
state.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
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Timestamp};
use cw_storage_plus::{Item, Map};
#[cw_serde]
pub struct Config {
pub admin: Addr,
pub minter: Addr,
pub mint_enabled: bool,
pub per_address_limit: u32,
pub cw721_code_id: u64,
}
#[cw_serde]
#[schemars(rename = "StateEventInfo")]
pub struct EventInfo {
pub creator: Addr,
pub start_time: Timestamp,
pub end_time: Timestamp,
pub poap_uri: String,
}
#[cw_serde]
pub struct Metadata {
pub claimer: Addr,
}
pub const CONFIG: Item<Config> = Item::new("config");
pub const EVENT_INFO: Item<EventInfo> = Item::new("event_info");
pub const CW721_ADDRESS: Item<Addr> = Item::new("cw721_address");
pub const NEXT_POAP_ID: Item<u64> = Item::new("nex_poap_id");
pub const MINTER_ADDRESS: Map<Addr, u32> = Map::new("minter_address");
impl EventInfo {
/// Checks if the event has already started.
/// * `timestamp` - Reference time used to check if the event has started.
pub fn is_started(&self, timestamp: &Timestamp) -> bool {
&self.start_time <= timestamp
}
/// Checks if the event is ended.
/// * `timestamp` - Reference time used to check if the event is ended.
pub fn is_ended(&self, timestamp: &Timestamp) -> bool {
timestamp >= &self.end_time
}
/// Checks if the event is in progress.
/// * `timestamp` - Reference time used to check if the event is in progress.
pub fn in_progress(&self, timestamp: &Timestamp) -> bool {
self.is_started(timestamp) && !self.is_ended(timestamp)
}
}
#[cfg(test)]
mod tests {
use crate::state::EventInfo;
use cosmwasm_std::{Addr, Timestamp};
fn mock_event_info(start: u64, end: u64) -> EventInfo {
EventInfo {
creator: Addr::unchecked(""),
start_time: Timestamp::from_seconds(start),
end_time: Timestamp::from_seconds(end),
poap_uri: "".to_string(),
}
}
#[test]
fn event_is_started() {
let current_time = Timestamp::from_seconds(300);
let event_info = mock_event_info(200, 400);
assert!(event_info.is_started(¤t_time));
// Test edge case start time = current time
assert!(event_info.is_started(&event_info.start_time));
}
#[test]
fn event_not_started() {
let current_time = Timestamp::from_seconds(300);
let event_info = mock_event_info(350, 400);
assert!(!event_info.is_started(¤t_time));
}
#[test]
fn event_is_ended() {
let current_time = Timestamp::from_seconds(300);
let event_info = mock_event_info(200, 250);
assert!(event_info.is_ended(¤t_time));
// Test edge end time = current time
assert!(event_info.is_ended(&event_info.end_time));
}
#[test]
fn event_not_ended() {
let current_time = Timestamp::from_seconds(300);
let event_info = mock_event_info(200, 400);
assert!(!event_info.is_ended(¤t_time));
}
#[test]
fn event_in_progress() {
let current_time = Timestamp::from_seconds(150);
let event_info = mock_event_info(100, 200);
assert!(event_info.in_progress(¤t_time));
// Test edge case current time = start time
assert!(event_info.in_progress(&event_info.start_time));
}
#[test]
fn event_not_in_progress() {
let current_time = Timestamp::from_seconds(500);
let event_info = mock_event_info(100, 200);
assert!(!event_info.in_progress(¤t_time));
// Test edge case current time = end time
assert!(!event_info.in_progress(&event_info.end_time));
}
}