forked from Near-One/near-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
58 lines (51 loc) · 1.59 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
use near_plugins::{only, Ownable};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{near_bindgen, AccountId, PanicOnDefault};
#[near_bindgen]
#[derive(Ownable, PanicOnDefault, BorshDeserialize, BorshSerialize)]
pub struct Counter {
counter: u64,
}
#[near_bindgen]
impl Counter {
/// Optionally set the owner in the constructor.
#[init]
pub fn new(owner: Option<AccountId>) -> Self {
let mut contract = Self { counter: 0 };
if owner.is_some() {
contract.owner_set(owner);
}
contract
}
/// Returns the value of the counter.
pub fn get_counter(&self) -> u64 {
self.counter
}
/// Anyone may call this method successfully.
pub fn increase(&mut self) -> u64 {
self.counter += 1;
self.counter
}
/// _Only_ the owner or the contract itself may call this method successfully. It panics if
/// anyone else calls it.
#[only(self, owner)]
pub fn increase_2(&mut self) -> u64 {
self.counter += 2;
self.counter
}
/// _Only_ the owner may call this method successfully. It panics if anyone else calls it.
#[only(owner)]
pub fn increase_3(&mut self) -> u64 {
self.counter += 3;
self.counter
}
/// _Only_ the contract itself may call this method successfully. It panics if anyone else calls
/// it.
///
/// It is possible to use `#[only(self)]` even if the contract does not derive `Ownable`.
#[only(self)]
pub fn increase_4(&mut self) -> u64 {
self.counter += 4;
self.counter
}
}