Skip to content

Commit

Permalink
Merge pull request #158 from Supercolony-net/feature/update-access-an…
Browse files Browse the repository at this point in the history
…d-diamond-docs

Feature/update access control and diamond docs
  • Loading branch information
Artemka374 authored Jul 29, 2022
2 parents d6e29f0 + b361778 commit ee30756
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 9 deletions.
84 changes: 84 additions & 0 deletions docs/docs/smart-contracts/access-control/Extensions/enumerable.md
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).
5 changes: 5 additions & 0 deletions docs/docs/smart-contracts/access-control/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"label": "Access Control",
"collapsed": false,
"position": 2
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 1
title: Access Control
---

Expand Down Expand Up @@ -38,7 +38,7 @@ impl Contract {

## Step 3: Customize your contract

Customize it by adding access control logic. We will add a `restricted_function` to `Contract` implemenation,
Customize it by adding access control logic. We will add a `restricted_function` to `Contract` implementation,
which will use the `only_role` modifier with `CALLER` parameter, which verifies that the caller has the `CALLER` role.

```rust
Expand Down
24 changes: 24 additions & 0 deletions docs/docs/smart-contracts/diamond/Extensions/loupe.md
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).
5 changes: 5 additions & 0 deletions docs/docs/smart-contracts/diamond/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"label": "PSP22",
"collapsed": false,
"position": 4
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 4
sidebar_position: 1
title: Diamond Standard
---

Expand Down Expand Up @@ -54,7 +54,7 @@ with the code hash of your new facet and the selectors of all the functions from
facet you want to use. The diamond will register them and anytime you call this function
on your diamond contract, it will make the delegate call to the facet the function belongs to.
You can add, remove or replace these functions anytime with the `diamond_cut` function,
some of the limitations are, that you can not add functions with the same selectors,
some of limitations are, that you can not add functions with the same selectors,
when replacing functions, the new function needs to be from a different contract,
then currently in use, and when removing functions, the function needs to be registered in the diamond contract.

Expand Down
8 changes: 4 additions & 4 deletions docs/docs/smart-contracts/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ you can find instructions on how to work with them:
* [PSP35Burnable](PSP35/Extensions/burnable.md): destruction of own tokens.
* [PSP35Batch](PSP35/Extensions/batch.md): batch transferring of tokens.
* [PSP35Enumerable](PSP35/Extensions/enumerable.md): iterating over contract's tokens.
* [Access Control](access-control.md) shows how you can use the implementation of
* [Access Control](docs/smart-contracts/access-control/access-control.md) shows how you can use the implementation of
[access-control](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/access/access_control) and
[psp34](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/token/psp34) together to provide rights to mint and burn NFT tokens.
* [AccessControlEnumerable](PSP34/Extensions/enumerable.md): iterating over contract's tokens.
* [AccessControlEnumerable](docs/smart-contracts/access-control/Extensions/enumerable.md): iterating over contract's roles.
* [Ownable](ownable.md) shows how you can use the implementation of
[ownable](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/access/ownable) and
[psp35](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/token/psp35) together to provide rights to mint and burn tokens.
Expand All @@ -256,10 +256,10 @@ you can find instructions on how to work with them:
* [PaymentSplitter](payment-splitter.md) shows how you can use the implementation of
[payment-splitter](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/finance/payment_splitter)
to split received native tokens between participants of the contract.
* [Diamond](diamond.md) shows how you can use the implementation of
* [Diamond](docs/smart-contracts/diamond/diamond.md) shows how you can use the implementation of
[diamond](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/upgradeability/diamond)
pattern to split your contract into small parts and support upgradeability.
* [DiamondLoupe](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/upgradeability/diamond/extensions): iterating over contract's facets.
* [DiamondLoupe](docs/smart-contracts/diamond/Extensions/loupe.md): iterating over contract's facets.
* [Proxy](proxy.md) shows how you can use the implementation of
[proxy](https://github.com/Supercolony-net/openbrush-contracts/tree/main/contracts/src/upgradeability/proxy)
pattern to support upgradeability of your contract.
2 changes: 1 addition & 1 deletion docs/docs/smart-contracts/upgradeable.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ call to the smart contract to the corresponding facet (logic layer).
`Diamond` contract has a function `diamond_cut` that allows registering each facet.

OpenBrush provides default implementation for `Diamond` standard on ink!.
For more details you can check [Diamond](diamond.md).
For more details you can check [Diamond](docs/smart-contracts/diamond/diamond.md).

All suggestions above ideally describe how to develop an upgradeable contract
with multi-logic layers and many logic units.
Expand Down

0 comments on commit ee30756

Please sign in to comment.