Skip to content

Commit

Permalink
Merge pull request #138 from Supercolony-net/feature/using-custom-map…
Browse files Browse the repository at this point in the history
…ping

Use OpenBrush's `Mapping` everywhere with optimal types
  • Loading branch information
xgreenx authored Jun 22, 2022
2 parents dcc13f3 + 4ce19d5 commit 9a780b1
Show file tree
Hide file tree
Showing 41 changed files with 416 additions and 302 deletions.
19 changes: 11 additions & 8 deletions contracts/src/access/access_control/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ use openbrush::{
declare_storage_trait,
modifier_definition,
modifiers,
storage::Mapping,
storage::{
Mapping,
ValueGuard,
},
traits::AccountId,
};

Expand All @@ -38,7 +41,7 @@ pub struct AccessControlData<B = Members>
where
B: AccessControlMemberManager,
{
pub admin_roles: Mapping<RoleType, RoleType>,
pub admin_roles: Mapping<RoleType, RoleType, ValueGuard<RoleType>>,
pub members: B,
pub _reserved: Option<()>,
}
Expand Down Expand Up @@ -72,10 +75,10 @@ where
}

default fn get_role_admin(&self, role: RoleType) -> RoleType {
get_role_admin(self, &role)
get_role_admin(self, role)
}

#[modifiers(only_role(get_role_admin(self, &role)))]
#[modifiers(only_role(get_role_admin(self, role)))]
default fn grant_role(&mut self, role: RoleType, account: AccountId) -> Result<(), AccessControlError> {
if self.get().members.has_role(role, &account) {
return Err(AccessControlError::RoleRedundant)
Expand All @@ -85,7 +88,7 @@ where
Ok(())
}

#[modifiers(only_role(get_role_admin(self, &role)))]
#[modifiers(only_role(get_role_admin(self, role)))]
default fn revoke_role(&mut self, role: RoleType, account: AccountId) -> Result<(), AccessControlError> {
check_role(self, role, account)?;
self._do_revoke_role(role, account);
Expand Down Expand Up @@ -169,12 +172,12 @@ where
}

default fn _set_role_admin(&mut self, role: RoleType, new_admin: RoleType) {
let mut entry = self.get_mut().admin_roles.get(&role);
let mut entry = self.get_mut().admin_roles.get(role);
if entry.is_none() {
entry = Some(Self::_default_admin());
}
let old_admin = entry.unwrap();
self.get_mut().admin_roles.insert(&role, &new_admin);
self.get_mut().admin_roles.insert(role, &new_admin);
self._emit_role_admin_changed(role, old_admin, new_admin);
}
}
Expand All @@ -190,7 +193,7 @@ where
Ok(())
}

pub fn get_role_admin<T, B>(instance: &T, role: &RoleType) -> RoleType
pub fn get_role_admin<T, B>(instance: &T, role: RoleType) -> RoleType
where
B: AccessControlMemberManager,
T: AccessControlStorage<Data = AccessControlData<B>>,
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/access/access_control/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

pub mod extensions {
pub mod enumerable;
}

mod access_control;
mod members;

Expand Down
2 changes: 0 additions & 2 deletions contracts/src/access/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@

#[cfg(feature = "access_control")]
pub mod access_control;
#[cfg(feature = "access_control")]
pub mod access_control_enumerable;
#[cfg(feature = "ownable")]
pub mod ownable;
2 changes: 1 addition & 1 deletion contracts/src/finance/payment_splitter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
pub use crate::traits::payment_splitter::*;
pub use derive::PaymentSplitterStorage;
use ink_prelude::vec::Vec;
use ink_storage::Mapping;
use openbrush::{
declare_storage_trait,
storage::Mapping,
traits::{
AccountId,
AccountIdExt,
Expand Down
17 changes: 13 additions & 4 deletions contracts/src/token/psp22/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ use ink_prelude::{
string::String,
vec::Vec,
};
use ink_storage::Mapping;
use openbrush::{
declare_storage_trait,
storage::{
Mapping,
TypeGuard,
},
traits::{
AccountId,
AccountIdExt,
Expand All @@ -56,10 +59,16 @@ pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("openbrush::PSP22Data");
pub struct PSP22Data {
pub supply: Balance,
pub balances: Mapping<AccountId, Balance>,
pub allowances: Mapping<(AccountId, AccountId), Balance>,
pub allowances: Mapping<(AccountId, AccountId), Balance, AllowancesKey>,
pub _reserved: Option<()>,
}

pub struct AllowancesKey;

impl<'a> TypeGuard<'a> for AllowancesKey {
type Type = &'a (&'a AccountId, &'a AccountId);
}

declare_storage_trait!(PSP22Storage);

impl<T: PSP22Storage<Data = PSP22Data> + Flush> PSP22 for T {
Expand All @@ -72,7 +81,7 @@ impl<T: PSP22Storage<Data = PSP22Data> + Flush> PSP22 for T {
}

default fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance {
self.get().allowances.get((&owner, &spender)).unwrap_or(0)
self.get().allowances.get(&(&owner, &spender)).unwrap_or(0)
}

default fn transfer(&mut self, to: AccountId, value: Balance, data: Vec<u8>) -> Result<(), PSP22Error> {
Expand Down Expand Up @@ -256,7 +265,7 @@ impl<T: PSP22Storage<Data = PSP22Data> + Flush> PSP22Internal for T {
return Err(PSP22Error::ZeroRecipientAddress)
}

self.get_mut().allowances.insert((&owner, &spender), &amount);
self.get_mut().allowances.insert(&(&owner, &spender), &amount);
self._emit_approval_event(owner, spender, amount);
Ok(())
}
Expand Down
21 changes: 16 additions & 5 deletions contracts/src/token/psp34/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@ pub use derive::{
PSP34Storage,
};
use ink_prelude::vec::Vec;
use ink_storage::Mapping;
use openbrush::declare_storage_trait;
use openbrush::{
declare_storage_trait,
storage::{
Mapping,
TypeGuard,
},
};

pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("openbrush::PSP32MetadataData");

#[derive(Default, Debug)]
#[openbrush::storage(STORAGE_KEY)]
pub struct PSP34MetadataData {
pub attributes: Mapping<(Id, Vec<u8>), Vec<u8>>,
pub attributes: Mapping<(Id, Vec<u8>), Vec<u8>, AttributesKey>,
pub _reserved: Option<()>,
}

pub struct AttributesKey;

impl<'a> TypeGuard<'a> for AttributesKey {
type Type = &'a (&'a Id, &'a Vec<u8>);
}

declare_storage_trait!(PSP34MetadataStorage);

impl<T: PSP34MetadataStorage<Data = PSP34MetadataData>> PSP34Metadata for T {
default fn get_attribute(&self, id: Id, key: Vec<u8>) -> Option<Vec<u8>> {
self.get().attributes.get((&id, &key))
self.get().attributes.get(&(&id, &key))
}
}

Expand All @@ -54,7 +65,7 @@ pub trait PSP34MetadataInternal {

impl<T: PSP34MetadataStorage<Data = PSP34MetadataData> + PSP34Internal> PSP34MetadataInternal for T {
default fn _set_attribute(&mut self, id: Id, key: Vec<u8>, value: Vec<u8>) {
self.get_mut().attributes.insert((&id, &key), &value);
self.get_mut().attributes.insert(&(&id, &key), &value);
self._emit_attribute_set_event(id, key, value);
}
}
23 changes: 17 additions & 6 deletions contracts/src/token/psp35/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,34 @@ pub use crate::{
};
pub use derive::PSP35MetadataStorage;
use ink_prelude::vec::Vec;
use ink_storage::Mapping;
use openbrush::declare_storage_trait;
use openbrush::{
declare_storage_trait,
storage::{
Mapping,
TypeGuard,
},
};

pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("openbrush::PSP35MetadataData");

#[derive(Default, Debug)]
#[openbrush::storage(STORAGE_KEY)]
pub struct PSP35MetadataData {
pub attributes: Mapping<(Id, Vec<u8>), Vec<u8>>,
pub attributes: Mapping<(Id, Vec<u8>), Vec<u8>, AttributesKey>,
pub _reserved: Option<()>,
}

pub struct AttributesKey;

impl<'a> TypeGuard<'a> for AttributesKey {
type Type = &'a (&'a Id, &'a Vec<u8>);
}

declare_storage_trait!(PSP35MetadataStorage);

impl<T: PSP35MetadataStorage<Data = PSP35MetadataData>> PSP35Metadata for T {
default fn get_attribute(&self, id: Id, key: Vec<u8>) -> Option<Vec<u8>> {
self.get().attributes.get(&(id, key))
self.get().attributes.get(&(&id, &key))
}
}

Expand All @@ -55,13 +66,13 @@ pub trait PSP35MetadataInternal {

impl<T: PSP35MetadataStorage<Data = PSP35MetadataData>> PSP35MetadataInternal for T {
default fn _set_attribute(&mut self, id: &Id, key: &Vec<u8>, data: &Vec<u8>) -> Result<(), PSP35Error> {
self.get_mut().attributes.insert((id, key), data);
self.get_mut().attributes.insert(&(&id, &key), data);
self._emit_attribute_set_event(id, key, data);
Ok(())
}

default fn _get_attribute(&self, id: &Id, key: &Vec<u8>) -> Option<Vec<u8>> {
self.get().attributes.get((id, key))
self.get().attributes.get(&(&id, &key))
}

default fn _emit_attribute_set_event(&self, _id: &Id, _key: &Vec<u8>, _data: &Vec<u8>) {}
Expand Down
6 changes: 0 additions & 6 deletions contracts/src/token/psp35/psp35.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ where
pub _reserved: Option<()>,
}

pub struct BalancesKey;

impl<'a> TypeGuard<'a> for BalancesKey {
type Type = &'a (&'a Id, &'a AccountId);
}

pub struct ApprovalsKey;

impl<'a> TypeGuard<'a> for ApprovalsKey {
Expand Down
4 changes: 2 additions & 2 deletions contracts/src/upgradability/diamond/diamond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use ink_env::{
Clear,
};
use ink_prelude::vec::Vec;
use ink_storage::Mapping;
use openbrush::{
modifiers,
traits::{
Expand All @@ -42,6 +41,7 @@ use openbrush::{
};

pub use derive::DiamondStorage;
use openbrush::storage::Mapping;

pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("openbrush::DiamondData");

Expand Down Expand Up @@ -149,7 +149,7 @@ impl<T: DiamondStorage<Data = DiamondData> + Flush + DiamondCut> DiamondInternal
default fn _fallback(&self) -> ! {
let selector = ink_env::decode_input::<Selector>().unwrap_or_else(|_| panic!("Calldata error"));

let delegate_code = DiamondStorage::get(self).selector_to_hash.get(selector);
let delegate_code = DiamondStorage::get(self).selector_to_hash.get(&selector);

if delegate_code.is_none() {
panic!("Function is not registered");
Expand Down
29 changes: 16 additions & 13 deletions contracts/src/upgradability/diamond/extensions/diamond_loupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ pub use crate::{
traits::diamond::extensions::diamond_loupe::*,
};
use ink_prelude::vec::Vec;
use ink_storage::Mapping;
use openbrush::{
declare_storage_trait,
traits::Hash,
};

pub use derive::DiamondLoupeStorage;
use openbrush::storage::{
Mapping,
ValueGuard,
};

pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("openbrush::DiamondLoupeData");

#[derive(Default, Debug)]
#[openbrush::storage(STORAGE_KEY)]
pub struct DiamondLoupeData {
// number of registered code hashes
pub code_hashes: u16,
pub code_hashes: u32,
// mapping of facet to its position in all facets list
pub hash_to_id: Mapping<Hash, u16>,
pub hash_to_id: Mapping<Hash, u32>,
// mapping of facet id to its facet
pub id_to_hash: Mapping<u16, Hash>,
pub id_to_hash: Mapping<u32, Hash, ValueGuard<u32>>,
pub _reserved: Option<()>,
}

Expand All @@ -53,21 +56,21 @@ impl<T: DiamondLoupeStorage<Data = DiamondLoupeData>> DiamondCut for T {
default fn _on_add_facet(&mut self, code_hash: Hash) {
let hash_id = self.get().code_hashes;
self.get_mut().hash_to_id.insert(&code_hash, &hash_id);
self.get_mut().id_to_hash.insert(&hash_id, &code_hash);
self.get_mut().id_to_hash.insert(hash_id, &code_hash);
self.get_mut().code_hashes += 1;
}

default fn _on_remove_facet(&mut self, code_hash: Hash) {
let new_hash_id = self.get().code_hashes - 1;
let removed_hash_id = self.get().hash_to_id.get(&code_hash).unwrap();
let last_hash = self.get().id_to_hash.get(&new_hash_id).unwrap();
let last_hash = self.get().id_to_hash.get(new_hash_id).unwrap();

if last_hash != code_hash {
self.get_mut().id_to_hash.insert(&removed_hash_id, &last_hash);
self.get_mut().id_to_hash.insert(removed_hash_id, &last_hash);
self.get_mut().hash_to_id.insert(&last_hash, &removed_hash_id);
self.get_mut().id_to_hash.remove(&new_hash_id);
self.get_mut().id_to_hash.remove(new_hash_id);
} else {
self.get_mut().id_to_hash.remove(&removed_hash_id);
self.get_mut().id_to_hash.remove(removed_hash_id);
}

self.get_mut().hash_to_id.remove(&code_hash);
Expand All @@ -79,7 +82,7 @@ impl<T: DiamondLoupeStorage<Data = DiamondLoupeData> + DiamondStorage<Data = Dia
default fn facets(&self) -> Vec<FacetCut> {
let mut out_vec = Vec::new();
for i in 0..DiamondLoupeStorage::get(self).code_hashes {
let hash = DiamondLoupeStorage::get(self).id_to_hash.get(&i).unwrap();
let hash = DiamondLoupeStorage::get(self).id_to_hash.get(i).unwrap();
let selectors = DiamondStorage::get(self).hash_to_selectors.get(&hash).unwrap();
out_vec.push(FacetCut { hash, selectors })
}
Expand All @@ -89,19 +92,19 @@ impl<T: DiamondLoupeStorage<Data = DiamondLoupeData> + DiamondStorage<Data = Dia
default fn facet_function_selectors(&self, facet: Hash) -> Vec<Selector> {
DiamondStorage::get(self)
.hash_to_selectors
.get(facet)
.get(&facet)
.unwrap_or(Vec::<Selector>::new())
}

default fn facet_code_hashes(&self) -> Vec<Hash> {
let mut out_vec = Vec::new();
for i in 0..DiamondLoupeStorage::get(self).code_hashes {
out_vec.push(DiamondLoupeStorage::get(self).id_to_hash.get(&i).unwrap())
out_vec.push(DiamondLoupeStorage::get(self).id_to_hash.get(i).unwrap())
}
out_vec
}

default fn facet_code_hash(&self, selector: Selector) -> Option<Hash> {
DiamondStorage::get(self).selector_to_hash.get(selector)
DiamondStorage::get(self).selector_to_hash.get(&selector)
}
}
10 changes: 7 additions & 3 deletions docs/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ Welcome to OpenBrush documentation! This documentation aims to guide you through

## What is OpenBrush
OpenBrush is a library for smart contract development on ink!
It provides standard contracts ([based on PSP](https://github.com/w3f/PSPs)), as well as useful contracts and macros to help you build ink! smart contracts.
It provides standard contracts ([based on PSP](https://github.com/w3f/PSPs)),
as well as useful contracts and macros to help you build ink! smart contracts.

## Why OpenBrush
OpenBrush attempts to analogize OpenZeppelin perfectly with Rust’s paradigm, enabling users to import contracts implemented by another user without problems and reuse the code.
There was a need to have a library that can provide base implementations of ERCs and to import/reuse them by customizing their own logic.
OpenBrush attempts to analogize OpenZeppelin perfectly with Rust’s paradigm,
enabling users to import contracts implemented by another user without problems
and reuse the code.
There was a need to have a library that can provide base implementations of
ERCs and to import/reuse them by customizing their own logic.
Loading

0 comments on commit 9a780b1

Please sign in to comment.