-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Sway Book and ownership example to use ownership library and s…
…rc-5 standard (#4751) ## Description The Sway Book's ownership example and documentation is outdated and does not include the ownership library. It now links to the SRC-5 Ownership standard, ownership library, and includes additional examples. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: bitzoic <[email protected]> Co-authored-by: SwayStar123 <[email protected]>
- Loading branch information
1 parent
55fbbba
commit 9195d63
Showing
4 changed files
with
97 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
contract; | ||
|
||
use std::constants::ZERO_B256; | ||
use ownership::{*, data_structures::State}; | ||
|
||
abi OwnershipExample { | ||
#[storage(write)] | ||
fn revoke_ownership(); | ||
#[storage(write)] | ||
fn set_owner(identity: Identity); | ||
#[storage(read)] | ||
fn owner() -> Option<Identity>; | ||
fn owner() -> State; | ||
#[storage(read)] | ||
fn only_owner(); | ||
} | ||
|
||
// ANCHOR: set_owner_example_storage | ||
storage { | ||
owner: Option<Identity> = None, | ||
owner: Ownership = Ownership::initialized(Identity::Address(Address::from(ZERO_B256))), | ||
} | ||
// ANCHOR_END: set_owner_example_storage | ||
|
||
impl OwnershipExample for Contract { | ||
// ANCHOR: revoke_owner_example | ||
#[storage(write)] | ||
fn revoke_ownership() { | ||
storage.owner.write(None); | ||
storage.owner.renounce_ownership(); | ||
} | ||
// ANCHOR_END: revoke_owner_example | ||
// ANCHOR: set_owner_example | ||
// ANCHOR: set_owner_example_function | ||
#[storage(write)] | ||
fn set_owner(identity: Identity) { | ||
storage.owner.write(Some(identity)); | ||
storage.owner.set_ownership(identity); | ||
} | ||
// ANCHOR_END: set_owner_example_function | ||
// ANCHOR: get_owner_example | ||
#[storage(read)] | ||
fn owner() -> State { | ||
storage.owner.owner() | ||
} | ||
// ANCHOR_END: set_owner_example | ||
// ANCHOR_END: get_owner_example | ||
// ANCHOR: only_owner_example | ||
#[storage(read)] | ||
fn owner() -> Option<Identity> { | ||
storage.owner.read() | ||
fn only_owner() { | ||
storage.owner.only_owner(); | ||
// Do stuff here | ||
} | ||
// ANCHOR_END: only_owner_example | ||
} |