-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from Supercolony-net/feature/update-access-an…
…d-diamond-docs Feature/update access control and diamond docs
- Loading branch information
Showing
8 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
docs/docs/smart-contracts/access-control/Extensions/enumerable.md
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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
--- | ||
sidebar_position: 1 | ||
title: AccessControl Enumerable | ||
--- | ||
|
||
This example shows how you can reuse the implementation of [AccessControl](https://github.com/Supercolony-net/openbrush-contracts/blob/main/contracts/src/access/access_control/access_control.rs) with [AccessControlEnumerable](https://github.com/Supercolony-net/openbrush-contracts/blob/main/contracts/src/access/access_control/extensions/enumerable.rs) extension, which enables an easier overview of access control roles. | ||
|
||
First, you should implement basic version of [AccessControl](/smart-contracts/access-control). | ||
|
||
## Step 1: Add imports and enable unstable feature | ||
|
||
Import **everything** from `openbrush::contracts::access_control::extensions::enumerable`. | ||
|
||
```rust | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![feature(min_specialization)] | ||
|
||
#[openbrush::contract] | ||
pub mod my_access_control { | ||
use openbrush::contracts::access_control::extensions::enumerable::*; | ||
... | ||
``` | ||
|
||
## Step 2: Define storage | ||
|
||
Pass `enumerable::Members` into `access_control::Data` to be able to use `AcessControlEnumerable` extension in your `AccessControl` implementation. | ||
|
||
```rust | ||
#[derive(Default, SpreadAllocate, Storage)] | ||
#[ink(storage)] | ||
pub struct Contract { | ||
#[storage_field] | ||
access: access_control::Data<enumerable::Members>, | ||
} | ||
``` | ||
|
||
## Step 3: Inherit logic | ||
|
||
Inherit implementation of the `AccessControlEnumerable` trait. You can customize (override) methods in this `impl` block. | ||
|
||
```rust | ||
|
||
impl AccessControl for Contract {} | ||
|
||
impl AccessControlEnumerable for Contract {} | ||
``` | ||
|
||
## Final code | ||
|
||
```rust | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#![feature(min_specialization)] | ||
|
||
#[openbrush::contract] | ||
pub mod my_access_control { | ||
use ink_storage::traits::SpreadAllocate; | ||
use openbrush::{ | ||
contracts::access_control::extensions::enumerable::*, | ||
traits::Storage, | ||
}; | ||
|
||
#[derive(Default, SpreadAllocate, Storage)] | ||
#[ink(storage)] | ||
pub struct Contract { | ||
#[storage_field] | ||
access: access_control::Data<enumerable::Members>, | ||
} | ||
|
||
impl AccessControl for Contract {} | ||
impl AccessControlEnumerable for Contract {} | ||
|
||
impl Contract { | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
ink_lang::codegen::initialize_contract(|_instance: &mut Self| {}) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
And that's it! Your `AccessControl` is now extended by the `AccessControlEnumerable` extension and ready to use its functions! | ||
You can check an example of the usage of [AccessControl Enumerable](https://github.com/Supercolony-net/openbrush-contracts/blob/main/contracts/src/access/access_control/extensions/enumerable.rs). | ||
|
||
You can also check the documentation for the basic implementation of [AccessControl](/smart-contracts/access-control). |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label": "Access Control", | ||
"collapsed": false, | ||
"position": 2 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
sidebar_position: 1 | ||
title: Diamond Loupe | ||
--- | ||
|
||
This example shows how you can reuse the implementation of [Diamond Standard](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/upgradeability/diamond) with [Diamond Loupe](https://github.com/Supercolony-net/openbrush-contracts/blob/main/contracts/src/upgradeability/diamond/extensions/diamond_loupe.rs) extension, which allows you to iterate over diamond contract's facets and available functions. | ||
|
||
## How to use this extension | ||
|
||
First, you should implement basic version of [Diamond standard](/smart-contracts/diamond). | ||
|
||
For your smart contract to use this extension, you only need to implement the `DiamoundLoupe` trait in your | ||
`Diamond` smart contract. Add import for `openbrush::contracts::diamond::extensions::diamond_loupe::*`, | ||
inherit the implementation for `DiamondLoupe` trait, where you can also customize (override) | ||
the original functions from `DiamondLoupe`. | ||
|
||
```rust | ||
use openbrush::contracts::diamond::extensions::diamond_loupe::*; | ||
|
||
impl DiamondLoupe for Contract {} | ||
``` | ||
|
||
And that's it! Your `Diamond` is now extended by the `DiamondLoupe` extension and ready to use its functions! | ||
You can check an example of the usage of [Diamond Loupe](https://github.com/Supercolony-net/openbrush-contracts/tree/main/examples/diamond). |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"label": "PSP22", | ||
"collapsed": false, | ||
"position": 4 | ||
} |
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