-
Notifications
You must be signed in to change notification settings - Fork 12
/
ownable.rs
88 lines (82 loc) · 3.07 KB
/
ownable.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
//! # Ownable:
//!
//! Trait which provides a basic access control mechanism, where
//! there is an account (an owner) that can be granted exclusive access to
//! specific functions.
//!
//! During creation of the contract set the owner using `owner_set`. Protect functions that should
//! only be called by the owner using #[only(owner)].
//!
//! ## Credits:
//!
//! Inspired by Open Zeppelin Ownable module:
//! https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol
use crate::events::{AsEvent, EventMetadata};
use near_sdk::AccountId;
use serde::Serialize;
/// Trait describing the functionality of the _Ownable_ plugin.
pub trait Ownable {
/// Returns the key of storage slot to save the current owner. By default b"__OWNER__" is used.
///
/// Attribute `owner_storage_key` can be used to set a different key:
///
/// ```ignore
/// #[ownable(owner_storage_key="CUSTOM_KEY")]
/// struct Contract { /* ... */}
/// ```
fn owner_storage_key(&self) -> &'static [u8];
/// Returns the current owner of the contract. Result must be a NEAR valid account id or None,
/// in case the account doesn't have an owner.
fn owner_get(&self) -> Option<AccountId>;
/// Replaces the current owner of the contract by a new owner. Use `None` to remove the owner of
/// the contract.
///
/// # Default Implementation:
///
/// Only the current owner can call this method. If no owner is set, only self can call this
/// method. Notice that if the owner is set, self will not be able to call `owner_set` by default.
///
/// # Event
///
/// If ownership is successfully transferred, the following event will be emitted:
///
/// ```json
/// {
/// "standard": "Ownable",
/// "version": "1.0.0",
/// "event": "ownership_transferred",
/// "data": {
/// "previous_owner": "Option<PREV_OWNER_ACCOUNT>",
/// "new_owner": "Option<NEW_OWNER_ACCOUNT>"
/// }
/// }
/// ```
fn owner_set(&mut self, owner: Option<AccountId>);
/// Returns true if the predecessor account id is the owner of the contract.
///
/// # View calls
///
/// This method fails in view calls since getting the predecessor account id is [not allowed] in
/// view calls. A workaround is using [`Self::owner_get`] and checking the returned account id.
///
/// [not allowed]: https://nomicon.io/Proposals/view-change-method
fn owner_is(&self) -> bool;
}
/// Event emitted when ownership is changed.
#[derive(Serialize, Clone)]
pub struct OwnershipTransferred {
/// The previous owner, if any.
pub previous_owner: Option<AccountId>,
/// The new owner, if any.
pub new_owner: Option<AccountId>,
}
impl AsEvent<OwnershipTransferred> for OwnershipTransferred {
fn metadata(&self) -> EventMetadata<OwnershipTransferred> {
EventMetadata {
standard: "Ownable".to_string(),
version: "1.0.0".to_string(),
event: "ownership_transferred".to_string(),
data: Some(self.clone()),
}
}
}