-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
executable file
·337 lines (281 loc) · 12 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
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
#![cfg_attr(not(feature = "std"), no_std)]
// use psp22::PSP22;
#[ink::contract]
pub mod staking {
// =====================================
//Library IMPORTED
// =====================================
use openbrush::{
contracts::{
traits::psp22::PSP22Ref,
},
}; // this would be used for psp22 token interaction
use ink::{storage::Mapping};
use ink::env::CallFlags;
use ink::prelude::vec;
// =========================================
// ERROR DECLARATION
// =========================================
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum Error {
NotOwner,
AddressIsAddressZero,
AmountShouldBeGreaterThanZero,
NotEnoughBalanceForReward,
TokenTransferFailed,
StakingStillInProgress
}
#[ink(storage)]
pub struct Staking {
psp22_stake_token: ink::ContractRef<PSP22>,
psp22_reward_token: ink::ContractRef<PSP22>,
owner: AccountId,
duration: Balance,
finish_at: Balance,
updated_at: Balance,
reward_rate: Balance,
reward_per_token_stored: Balance,
total_supply: Balance,
user_reward_per_token_paid: Mapping<AccountId, Balance>,
rewards: Mapping<AccountId, Balance>,
balance_of: Mapping<AccountId, Balance>,
}
impl Staking {
// =========================================
// Constructor
// =========================================
#[ink(constructor)]
pub fn new(
reward_duration: Balance,
psp22_stake_token: ink::ContractRef<PSP22>,
psp22_reward_token: ink::ContractRef<PSP22>,
) -> Self {
Self {
psp22_stake_token,
psp22_reward_token,
owner: Self::env().caller(),
duration: reward_duration,
finish_at: 0,
updated_at: 0,
reward_rate: 0,
reward_per_token_stored: 0,
total_supply: 0,
user_reward_per_token_paid: Mapping::default(),
rewards: Mapping::default(),
balance_of: Mapping::default(),
}
}
// =========================================
// Modifiers
// =========================================
fn only_owner (&self) -> Result<(), Error> {
if self.env().caller() == self.owner {
Ok(())
} else {
Err(Error::NotOwner)
}
}
fn update_reward (&mut self, address_acount: AccountId) {
self.reward_per_token_stored = self.reward_per_token();
self.updated_at = self.last_time_reward_applicable();
if address_acount != self.zero_address() {
self.rewards.insert(address_acount, &(self.earned(address_acount)));
self.user_reward_per_token_paid.insert(address_acount, &(self.reward_per_token_stored));
}
}
// fn address_zero_checker (&self) -> Result<(), Error> {
// if self.env().caller() == self.zero_address() {
// Err(Error::AddressIsAddressZero)
// }else {
// Ok(())
// }
// }
fn zero_address(&self) -> AccountId {
[0u8; 32].into()
}
// =========================================
// Write functions
// =========================================
/// Function is used to the reward duration
#[ink(message)]
pub fn set_rewards_duration(&mut self, reward_duration: Balance) -> Result<(), Error>{
self.only_owner()?;
if self.env().block_timestamp() as u128 >= self.finish_at {
return Err(Error::StakingStillInProgress)
}
self.duration += reward_duration;
Ok(())
}
#[ink(message)]
pub fn reward_per_token(&self) -> Balance {
let result = if self.total_supply == 0 {
self.reward_per_token_stored
}else {
self.reward_per_token_stored +
(self.reward_rate * (self.last_time_reward_applicable() - self.updated_at) * 1e18 as u128) /
self.total_supply
};
result
}
/// This function is called by the user to stake into the contract
#[ink(message)]
pub fn stake(&mut self, stake_amount: Balance) -> Result<(), Error>{
self.update_reward(self.env().caller());
if stake_amount <= 0 {
return Err(Error::AmountShouldBeGreaterThanZero)
}
let caller = self.env().caller();
// Transfer the token into the contract
self.psp22_stake_token.transfer_from(caller, self.env().account_id(), &stake_amount);
let curent_bal = self.balance_of.get(self.env().caller()).unwrap_or(0) + &stake_amount;
self.balance_of.insert(self.env().caller(), &curent_bal);
self.total_supply += &stake_amount;
Ok(())
}
#[ink(message)]
pub fn withdraw(&self, _amount: Balance) -> Result<(), Error> {
self.update_reward(self.env().caller());
if _amount < 0 {
return Err(Error::AmountShouldBeGreaterThanZero)
}
let caller = self.env().caller();
let new_bal = self.balance_of.get(self.env().caller()).unwrap_or(0) - &_amount;
self.balance_of.insert(self.env().caller(), &new_bal);
self.total_supply -= &_amount;
// Transfer the token to the person
self.psp22_stake_token.transfer_from(self.env().account_id(), caller, _amount);
Ok(())
}
#[ink(message)]
pub fn get_reward(&self) -> Result<(), Error> {
self.update_reward(self.env().caller());
let caller = self.env().caller();
let reward = self.rewards.get(self.env().caller()).unwrap_or(0);
if &reward > 0 {
self.get_result.insert(self.env().caller(), 0);
// Transfer the reward to the person
self.psp22_reward_token.transfer_from(self.env().account_id(), caller, reward);
}
Ok(())
}
#[ink(message)]
pub fn update_reward_rate(&self, _amount: Balance) -> Result<(), Error> {
self.only_owner()?;
self.update_reward(self.zero_address());
let caller = self.env().caller();
self.psp22_reward_token.transfer_from(caller, self.env().account_id(), &_amount);
if self.env().block_timestamp() as u128 >= self.finish_at {
self.reward_rate = &_amount / self.duration;
}else {
let remaining_reward = (self.finish_at - self.env().block_timestamp() as u128) * self.reward_rate;
self.reward_rate = (&_amount + self.remaining_reward) / self.duration;
}
if self.reward_rate < 0 {
return Err(Error::)
}
if self.reward_rate * self.duration >= self.psp22_reward_token.balance_of(self.env().account_id()) as u128 {
return Err(Error::NotEnoughBalanceForReward)
}
self.finish_at = self.env().block_timestamp() as u128 + self.duration();
self.updated_at = self.env().block_timestamp() as u128;
Ok(())
}
// =========================================
// View functions
// =========================================
#[ink(message)]
pub fn last_time_reward_applicable (&self) -> Balance {
self.min(self.finish_at, self.env().block_timestamp() as u128)
}
#[ink(message)]
pub fn earned (&self, address_account: AccountId) -> Balance {
(self.balance_of.get(address_account).unwrap_or(0) *
(self.reward_per_token() - self.user_reward_per_token_paid.get(address_account).unwrap_or(0)) / 1e18 as u128) +
self.rewards.get(address_account).unwrap_or(0)
}
fn min (&self, x: Balance, y: Balance) -> Balance {
if x <= y {
x
} else {
y
}
}
}
/// Unit tests in Rust are normally defined within such a `#[cfg(test)]`
/// module and test functions are marked with a `#[test]` attribute.
/// The below code is technically just normal Rust code.
#[cfg(test)]
mod tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
// We test a simple use case of our contract.
// #[ink::test]
// fn it_works() {
// let mut staking = Staking::new(false);
// assert_eq!(staking.get(), false);
// staking.flip();
// assert_eq!(staking.get(), true);
// }
}
/// This is how you'd write end-to-end (E2E) or integration tests for ink! contracts.
///
/// When running these you need to make sure that you:
/// - Compile the tests with the `e2e-tests` feature flag enabled (`--features e2e-tests`)
/// - Are running a Substrate node which contains `pallet-contracts` in the background
#[cfg(all(test, feature = "e2e-tests"))]
mod e2e_tests {
/// Imports all the definitions from the outer scope so we can use them here.
use super::*;
/// A helper function used for calling contract messages.
use ink_e2e::build_message;
/// The End-to-End test `Result` type.
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;
/// We test that we can upload and instantiate the contract using its default constructor.
#[ink_e2e::test]
async fn default_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Given
let constructor = StakingRef::default();
// When
let contract_account_id = client
.instantiate("staking", &ink_e2e::alice(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;
// Then
let get = build_message::<StakingRef>(contract_account_id.clone())
.call(|staking| staking.get());
let get_result = client.call_dry_run(&ink_e2e::alice(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), false));
Ok(())
}
/// We test that we can read and write a value from the on-chain contract contract.
#[ink_e2e::test]
async fn it_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
// Given
let constructor = StakingRef::new(false);
let contract_account_id = client
.instantiate("staking", &ink_e2e::bob(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;
let get = build_message::<StakingRef>(contract_account_id.clone())
.call(|staking| staking.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), false));
// When
let flip = build_message::<StakingRef>(contract_account_id.clone())
.call(|staking| staking.flip());
let _flip_result = client
.call(&ink_e2e::bob(), flip, 0, None)
.await
.expect("flip failed");
// Then
let get = build_message::<StakingRef>(contract_account_id.clone())
.call(|staking| staking.get());
let get_result = client.call_dry_run(&ink_e2e::bob(), &get, 0, None).await;
assert!(matches!(get_result.return_value(), true));
Ok(())
}
}
}