-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
222 lines (197 loc) · 5.47 KB
/
lib.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
#![cfg_attr(not(feature = "std"), no_std, no_main)]
use ink::{
storage::Mapping,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[ink::scale_derive(Encode, Decode, TypeInfo)]
pub enum FaucetError {
InCoolDown,
NotActive,
NotEnoughFunds,
NotOwner,
ValueTooLarge,
}
#[ink::contract]
mod fungibles {
use super::*;
/// Some tokens have been dripped.
#[ink(event)]
pub struct Drip {
value: Balance,
to: AccountId,
}
#[ink(storage)]
pub struct Faucet {
// Whether this faucet is active.
active: bool,
// Number of blocks an account should wait between drip requests.
cooldown: BlockNumber,
// Amount of tokens to drip per request.
drip_amount: Balance,
// Account owner of the contract. Set to the deployer at constructor.
owner: Option<AccountId>,
// Accounting of last request per account.
last_request_of: Mapping<AccountId, BlockNumber>,
}
impl Faucet {
/// Instantiate the faucet with the given cooldown and drip amount.
/// Deployer becomes the contract owner.
///
/// # Parameters
/// * - `cooldown` - Number of blocks an account should wait between drip requests.
/// * - `drip_amount` - Amount of tokens to drip per `drip` call.
#[ink(constructor, payable)]
pub fn new(cooldown: BlockNumber, drip_amount: Balance) -> Self {
Self {
active: false,
cooldown,
drip_amount,
owner: Some(Self::env().caller()),
last_request_of: Mapping::default(),
}
}
/// Check if the faucet is active.
fn ensure_active(&self) -> Result<(), FaucetError> {
if !&self.active {
return Err(FaucetError::NotActive);
}
Ok(())
}
/// Check if the caller is the owner of the contract.
fn ensure_owner(&self) -> Result<(), FaucetError> {
if self.owner != Some(self.env().caller()) {
return Err(FaucetError::NotOwner);
}
Ok(())
}
/// Check if caller can request a drip.
fn can_request(&self) -> Result<(), FaucetError> {
let caller = Self::env().caller();
let last_request_result = self.last_request_of.try_get(caller);
match last_request_result {
Some(Ok(last_drip)) => {
let current_block = self.env().block_number();
if last_drip.saturating_add(self.cooldown) > current_block {
return Err(FaucetError::InCoolDown);
}
}
Some(Err(_)) => {
// if either
// - (a) the encoded key doesn’t fit into the static buffer
// - (b) the value existed but its length exceeds the static buffer size.
return Err(FaucetError::ValueTooLarge);
}
None => {
return Ok(());
}
}
Ok(())
}
/// Check if faucet holds enough balance to drip.
fn can_withdraw(&self) -> Result<(), FaucetError> {
// Don't let balance go lower than 1.
if self.drip_amount.saturating_add(1) >= self.env().balance() {
return Err(FaucetError::NotEnoughFunds);
}
Ok(())
}
/// Faucet's cooldown.
#[ink(message)]
pub fn cooldown(&self) -> BlockNumber {
self.cooldown
}
/// Faucet's drip amount.
#[ink(message)]
pub fn drip_amount(&self) -> Balance {
self.drip_amount
}
/// Whether Faucet is active or not.
#[ink(message)]
pub fn is_active(&self) -> bool {
self.active
}
/// Caller's last drip block number.
#[ink(message)]
pub fn last_request_of(&self) -> Option<BlockNumber> {
self.last_request_of.get(self.env().caller())
}
/// Faucet owner account, if there is one.
#[ink(message)]
pub fn owner(&self) -> Option<AccountId> {
self.owner
}
/// Transfer drip_amount tokens to the caller.
/// if:
/// - faucet is active,
/// - caller is not in cooldown,
/// - faucet holds enough funds.
#[ink(message)]
pub fn drip(&mut self) -> Result<(), FaucetError> {
self.ensure_active()?;
self.can_withdraw()?;
self.can_request()?;
let caller = self.env().caller();
// Do drip.
self.env()
.transfer(caller, self.drip_amount).expect("Some tokens have been transferred");
// Register drip block# for caller.
self.last_request_of
.try_insert(caller, &self.env().block_number())
.map_err(|_| FaucetError::ValueTooLarge)?;
// Notify.
self.env().emit_event(
Drip {
value: self.drip_amount,
to: self.env().caller(),
}
);
Ok(())
}
/// Mutate the value of cooldown.
///
/// # Parameters
/// - `cooldown` - New cooldown time.
#[ink(message)]
pub fn set_cooldown(&mut self, cooldown: BlockNumber) -> Result<(), FaucetError> {
self.ensure_owner()?;
self.cooldown = cooldown;
Ok(())
}
/// Activates or deactivates the faucet.
/// The faucet will only drip tokens while active.
#[ink(message)]
pub fn start_stop(&mut self) -> Result<(), FaucetError> {
self.ensure_owner()?;
self.active = !self.active;
Ok(())
}
/// Removes the owner of the contract.
/// Effectively disable any action that requires ownership authorization.
#[ink(message)]
pub fn remove_ownership(&mut self) -> Result<(), FaucetError> {
self.ensure_owner()?;
self.owner = None;
Ok(())
}
/// Mutate the value of drip_amount.
///
/// # Parameters
/// - `drip_amount` - New drip amount.
#[ink(message)]
pub fn set_drip_amount(&mut self, drip_amount: Balance) -> Result<(), FaucetError> {
self.ensure_owner()?;
self.drip_amount = drip_amount;
Ok(())
}
/// Transfer the ownership of the contract to another account.
///
/// # Parameters
/// - `owner` - New owner account.
#[ink(message)]
pub fn transfer_ownership(&mut self, new_owner: AccountId) -> Result<(), FaucetError> {
self.ensure_owner()?;
self.owner = Some(new_owner);
Ok(())
}
}
}