Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the work with storage #137

Merged
merged 16 commits into from
Jul 12, 2022
Merged

Refactored the work with storage #137

merged 16 commits into from
Jul 12, 2022

Conversation

xgreenx
Copy link
Contributor

@xgreenx xgreenx commented Jun 21, 2022

One Storage trait for all contracts

Instead of defining the storage trait for each data structure, OpenBrush provides the openbrush::traits::Storage trait for all contracts. Removed derive macro at all. Simplified implementation of Storage trait.

It simplified the usage of the library:

/// Define the storage for PSP22 data, Metadata data and Ownable data
#[ink(storage)]
#[derive(Default, SpreadAllocate, Storage)]
pub struct SharesContract {
    #[storage_field]
    psp22: psp22::Data,
    #[storage_field]
    ownable: ownable::Data,
    #[storage_field]
    metadata: metadata::Data,
}

Instead of the old version with many storage traits:

/// Define the storage for PSP22 data, Metadata data and Ownable data
#[ink(storage)]
#[derive(Default, SpreadAllocate, PSP22Storage, OwnableStorage, PSP22MetadataStorage)]
pub struct SharesContract {
    #[PSP22StorageField]
    psp22: PSP22Data,
    #[OwnableStorageField]
    ownable: OwnableData,
    #[PSP22MetadataStorageField]
    metadata: PSP22MetadataData,
}

Helpers StorageAsRef and StorageAsMut traits

Added helpers StorageAsRef and StorageAsMut traits. If you implement the Storage trait those traits are implemented automatically for you. It simplified selection between several data storages in generic implementation. New versions:

fn foo<T: Storage<psp22::Data> + Storage<psp34::Data>>(instance: &mut T) {
    let psp22 = instance.data::<psp22::Data>();
    let psp34 = instance.data::<psp34::Data>();
}

Old version:

fn foo<T: PSP22Storage + PSP34Storage>(instance: &mut T) {
    let psp22 = PSP22Storage::get(instance);
    let psp34 = PSP34Storage::get_mut(instance);
}

OccupiedStorage and OccupyStorage traits

Added the OccupiedStorage trait to allow generics in the data structures. Also, it is an additional guard not to occupy the same storage key. Each upgradeable data in OpenBrush implements OccupyStorage with provided storage key into openbrush::upgradeable_storage.

OccupiedStorage is implemented by derive Storage macro. So if you want to use it then the data type should implement OccupyStorage.

The simplest way to not worry about it is to use openbrush::upgradeable_storage during the definition of your structure and use Storage derive in the main contract.

Minor improvements

Removed module duplicating in the naming of data, traits, etc.
Each main data structure is now named Data instead of PSP22Data or PSP34Data.
Each internal trait is now named Internal instead of PSP22Internal or PSP34Internal.

If you want to have several Data in scope or several Internal traits, you can use namespace to specify it:

struct Contract {
    foo: psp22::Data,
    bar: psp34::Data,
}

impl psp22::Internal for Contract {}
impl psp34::Internal for Contract {}

image


Updated storage key to be u32(soon, a new version of ink! will use it as the main key instead of [u8; 32]). Provided a new macro openbrush::storage_unique_key that generates a storage key based on the module path to the data structure.


Improved exporting of all stuff from contracts. Now when you import openbrush::contracts::psp22::*, it also exports psp22 itself. So you can use psp22::Data or Data. Usage of psp22::Data is better because it is more readable.


Updated documentation. Removed common parts of each example documentation into the Overview section. Renamed storage of examples into Contract.

Instead of defining the storage trait for each data structure we have one `Storage` trait for all contracts.
Simplified selection between several data storages.
Removed derive macro at all. Simplified implementation of `Storage` trait.
Removed module duplicating in the name.
Updated storage key to be `u32`(soon new version of ink! will use it as main key instead of `[u8; 32]`).
Added `OccupiedStorage` trait to allow generics in the data structures. Also it is additional guard to not occupy the same storage key.
The storage key of each data structure in OpenBursh is a hash of the module path to the struct instead of hash of the string.
@xgreenx xgreenx requested review from coreggon11 and Artemka374 June 21, 2022 17:51
@xgreenx xgreenx self-assigned this Jun 21, 2022
xgreenx added 2 commits June 22, 2022 11:28
# Conflicts:
#	contracts/derive/lib.rs
#	contracts/src/token/psp35/extensions/batch.rs
#	contracts/src/token/psp35/psp35.rs
xgreenx added 10 commits June 23, 2022 17:02
# Conflicts:
#	contracts/Cargo.toml
#	contracts/derive/Cargo.toml
#	contracts/derive/lib.rs
#	contracts/src/access/access_control/mod.rs
#	contracts/src/finance/payment_splitter/mod.rs
#	contracts/src/governance/timelock_controller/mod.rs
#	contracts/src/token/psp22/psp22.rs
#	contracts/src/token/psp34/extensions/metadata.rs
#	contracts/src/token/psp35/extensions/metadata.rs
#	contracts/src/upgradability/diamond/diamond.rs
#	contracts/src/upgradability/diamond/extensions/diamond_loupe.rs
#	docs/docs/smart-contracts/example/impls.md
#	example_project_structure/impls/lending/data.rs
# Conflicts:
#	contracts/src/token/psp35/extensions/enumerable.rs
#	contracts/src/token/psp35/psp35.rs
Added description of new traits.

The doc will be updated in separate commit
Added general description of importing default implementation from OpenBrush.
Refactored all examples to use general description to reduce boilerplate documentation.
Added examples of final code into documentation.
Removed `psp22::extensions::capped` because it is refactored in separate PR.
Removed reentrancy guard description because it is outdated. It will be updated after trait refactoring.
Updated usage of storage in all examples.
@xgreenx xgreenx marked this pull request as ready for review July 8, 2022 09:58
@RustNinja RustNinja self-requested a review July 12, 2022 07:21
@xgreenx xgreenx merged commit fa89cd1 into main Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants