Skip to content

Commit c3311a6

Browse files
committed
chore: owner and a few other methods
1 parent 2fd131c commit c3311a6

File tree

8 files changed

+39
-51
lines changed

8 files changed

+39
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl Contract {
113113
}
114114

115115
pub fn owner_only(&self) {
116-
Self::require_owner();
116+
self.require_owner();
117117

118118
// ...
119119
}

macros/src/upgrade.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn expand(meta: UpgradeMeta) -> Result<TokenStream, darling::Error> {
108108
HookBody::Empty => Some(quote! {}), // empty implementation
109109
HookBody::Custom => None, // user-provided implementation
110110
HookBody::Owner => Some(quote! {
111-
<Self as #me::owner::Owner>::require_owner();
111+
#me::owner::Owner::require_owner(self);
112112
}),
113113
HookBody::Role(role) => Some(quote! {
114114
#me::rbac::Rbac::require_role(self, &#role);

src/escrow.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ pub trait EscrowInternal {
5757
}
5858

5959
/// Inner function to retrieve the slot keyed by it's `Self::Id`
60-
fn locked_slot(&self, id: &Self::Id) -> Slot<Self::State> {
60+
fn slot_locked(id: &Self::Id) -> Slot<Self::State> {
6161
Self::root().field(StorageKey::Locked(id))
6262
}
6363

6464
/// Read the state from the slot
6565
fn get_locked(&self, id: &Self::Id) -> Option<Self::State> {
66-
self.locked_slot(id).read()
66+
Self::slot_locked(id).read()
6767
}
6868

6969
/// Set the state at `id` to `locked`
7070
fn set_locked(&mut self, id: &Self::Id, locked: &Self::State) {
71-
self.locked_slot(id).write(locked);
71+
Self::slot_locked(id).write(locked);
7272
}
7373

7474
/// Clear the state at `id`
7575
fn set_unlocked(&mut self, id: &Self::Id) {
76-
self.locked_slot(id).remove();
76+
Self::slot_locked(id).remove();
7777
}
7878
}
7979

src/owner.rs

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,7 @@ pub trait Owner {
112112
/// Updates proposed owner without any checks or emitting events.
113113
fn update_proposed_unchecked(&mut self, new: Option<AccountId>);
114114

115-
/// Same as require_owner but as a method.
116-
fn assert_owner(&self);
117-
118-
/// Initializes the contract owner. Can only be called once.
119-
///
120-
/// Emits an `OwnerEvent::Transfer` event.
115+
/// Rejects if the predecessor is not the current owner.
121116
///
122117
/// # Examples
123118
///
@@ -131,19 +126,18 @@ pub trait Owner {
131126
///
132127
/// #[near]
133128
/// impl Contract {
134-
/// #[init]
135-
/// pub fn new(owner_id: AccountId) -> Self {
136-
/// let mut contract = Self {};
137-
///
138-
/// Owner::init(&mut contract, &owner_id);
129+
/// pub fn owner_only_function(&self) {
130+
/// self.require_owner();
139131
///
140-
/// contract
132+
/// // ...
141133
/// }
142134
/// }
143135
/// ```
144-
fn init(&mut self, owner_id: &AccountId);
136+
fn require_owner(&self);
145137

146-
/// Requires the predecessor to be the owner.
138+
/// Initializes the contract owner. Can only be called once.
139+
///
140+
/// Emits an `OwnerEvent::Transfer` event.
147141
///
148142
/// # Examples
149143
///
@@ -157,14 +151,17 @@ pub trait Owner {
157151
///
158152
/// #[near]
159153
/// impl Contract {
160-
/// pub fn owner_only(&self) {
161-
/// Self::require_owner();
154+
/// #[init]
155+
/// pub fn new(owner_id: AccountId) -> Self {
156+
/// let mut contract = Self {};
162157
///
163-
/// // ...
158+
/// Owner::init(&mut contract, &owner_id);
159+
///
160+
/// contract
164161
/// }
165162
/// }
166163
/// ```
167-
fn require_owner();
164+
fn init(&mut self, owner_id: &AccountId);
168165

169166
/// Removes the contract's owner. Can only be called by the current owner.
170167
///
@@ -226,7 +223,7 @@ impl<T: OwnerInternal> Owner for T {
226223
proposed_owner.set(new.as_ref());
227224
}
228225

229-
fn assert_owner(&self) {
226+
fn require_owner(&self) {
230227
require!(
231228
&env::predecessor_account_id()
232229
== Self::slot_owner()
@@ -253,26 +250,15 @@ impl<T: OwnerInternal> Owner for T {
253250
.emit();
254251
}
255252

256-
fn require_owner() {
257-
require!(
258-
&env::predecessor_account_id()
259-
== Self::slot_owner()
260-
.read()
261-
.as_ref()
262-
.unwrap_or_else(|| env::panic_str(NO_OWNER_FAIL_MESSAGE)),
263-
ONLY_OWNER_FAIL_MESSAGE,
264-
);
265-
}
266-
267253
fn renounce_owner(&mut self) {
268-
Self::require_owner();
254+
self.require_owner();
269255

270256
self.update_proposed(None);
271257
self.update_owner(None);
272258
}
273259

274260
fn propose_owner(&mut self, account_id: Option<AccountId>) {
275-
Self::require_owner();
261+
self.require_owner();
276262

277263
self.update_proposed(account_id);
278264
}
@@ -312,7 +298,7 @@ pub mod hooks {
312298
C: Owner,
313299
{
314300
fn hook<R>(contract: &mut C, _args: &A, f: impl FnOnce(&mut C) -> R) -> R {
315-
contract.assert_owner();
301+
contract.require_owner();
316302
f(contract)
317303
}
318304
}
@@ -377,7 +363,7 @@ mod tests {
377363
}
378364

379365
pub fn owner_only(&self) {
380-
Self::require_owner();
366+
self.require_owner();
381367
}
382368
}
383369

src/standard/nep148.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub trait Nep148ControllerInternal {
106106
}
107107

108108
/// Returns the storage slot for NEP-148 metadata.
109-
fn metadata() -> Slot<FungibleTokenMetadata> {
109+
fn slot_metadata() -> Slot<FungibleTokenMetadata> {
110110
Self::root().field(StorageKey::Metadata)
111111
}
112112
}
@@ -126,13 +126,13 @@ pub trait Nep148Controller {
126126

127127
impl<T: Nep148ControllerInternal> Nep148Controller for T {
128128
fn get_metadata(&self) -> FungibleTokenMetadata {
129-
Self::metadata()
129+
Self::slot_metadata()
130130
.read()
131131
.unwrap_or_else(|| env::panic_str(ERR_METADATA_UNSET))
132132
}
133133

134134
fn set_metadata(&mut self, metadata: &FungibleTokenMetadata) {
135-
Self::metadata().set(Some(metadata));
135+
Self::slot_metadata().set(Some(metadata));
136136
}
137137
}
138138

tests/macros/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ mod integration {
7676
}
7777

7878
pub fn add_value_setter(&mut self, account_id: AccountId) {
79-
Self::require_owner();
79+
self.require_owner();
8080

8181
self.add_role(&account_id, &Role::CanSetValue);
8282

@@ -140,7 +140,7 @@ struct MigrateIntegration {
140140

141141
impl MigrateHook for MigrateIntegration {
142142
fn on_migrate(old: Integration) -> Self {
143-
Self::require_owner();
143+
old.require_owner();
144144
Self::require_unpaused();
145145

146146
Self {
@@ -153,7 +153,7 @@ impl MigrateHook for MigrateIntegration {
153153
#[near]
154154
impl MigrateIntegration {
155155
pub fn add_value_setter(&mut self, account_id: AccountId) {
156-
Self::require_owner();
156+
self.require_owner();
157157

158158
self.add_role(&account_id, &Role::CanSetValue);
159159

tests/macros/owner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ mod implicit_key {
3131
}
3232

3333
pub fn set_permissioned_item(&mut self, value: u32) {
34-
Self::require_owner();
34+
self.require_owner();
3535

3636
self.permissioned_item = value;
3737
}
@@ -76,7 +76,7 @@ impl OwnedStructExplicitKey {
7676
}
7777

7878
pub fn set_permissioned_item(&mut self, value: u32) {
79-
Self::require_owner();
79+
self.require_owner();
8080

8181
self.permissioned_item = value;
8282
}

workspaces-tests/src/bin/upgrade_old_raw.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspaces_tests::predicate!();
22

3-
use near_sdk::{env, near, PanicOnDefault};
3+
use near_sdk::{env, near, require, PanicOnDefault};
44
use near_sdk_contract_tools::{owner::*, upgrade::PostUpgrade, Owner};
55

66
#[derive(Owner, PanicOnDefault)]
@@ -30,9 +30,11 @@ impl ContractOld {
3030

3131
#[no_mangle]
3232
pub fn upgrade() {
33-
near_sdk::env::setup_panic_hook();
33+
env::setup_panic_hook();
3434

35-
ContractOld::require_owner();
35+
let predecessor = env::predecessor_account_id();
36+
let owner = ContractOld::slot_owner().read();
37+
require!(Some(predecessor) == owner, "Owner only");
3638

3739
unsafe {
3840
near_sdk_contract_tools::upgrade::raw::upgrade(PostUpgrade::default());

0 commit comments

Comments
 (0)