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

Implemented the PSP22 via pallet-assets chain extension. #168

Merged
merged 12 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ jobs:
override: true
components: rustfmt, clippy

- name: Build & Run cargo tests
- name: Build & Run all default unit tests
run: |
RUSTFLAGS="-D warnings" cargo +nightly test --all-features --workspace -- --test-threads=10
RUSTFLAGS="-D warnings" cargo +nightly test --workspace --features test-all -- --test-threads=10
- name: Build & Run pallet unit tests
run: |
RUSTFLAGS="-D warnings" cargo +nightly test --workspace --features psp22_pallet -- --test-threads=10
examples-builds:
concurrency:
group: examples-builds-${{ github.ref }}
Expand Down
18 changes: 17 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ std = [
"openbrush_contracts/std",
"openbrush_lang/std",
]
access_control = ["openbrush_contracts/access_control"]
psp22 = ["openbrush_contracts/psp22"]
psp22_pallet = ["openbrush_contracts/psp22_pallet"]
psp34 = ["openbrush_contracts/psp34"]
psp37 = ["openbrush_contracts/psp37"]
access_control = ["openbrush_contracts/access_control"]
ownable = ["openbrush_contracts/ownable"]
payment_splitter = ["openbrush_contracts/payment_splitter"]
reentrancy_guard = ["openbrush_contracts/reentrancy_guard"]
Expand All @@ -73,6 +74,21 @@ timelock_controller = ["openbrush_contracts/timelock_controller"]
proxy = ["openbrush_contracts/proxy"]
diamond = ["openbrush_contracts/diamond"]

test-all = [
"psp22",
# "psp22_pallet", we ignore it during testing. It requries it own run of tests
"psp34",
"psp37",
"access_control",
"ownable",
"payment_splitter",
"reentrancy_guard",
"pausable",
"timelock_controller",
"proxy",
"diamond",
]

[profile.release]
panic = "abort"
lto = true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ but it must be fixed with this [issue](https://github.com/paritytech/ink/issues/
### Other Issues open:

* [#[ink::trait_definition] doesn't support generics and default implementation](https://github.com/Supercolony-net/openbrush-contracts/issues/4)
* [Library provides implementation on Rust level instead of ink! level](https://github.com/Supercolony-net/openbrush-contracts/issues/5)
* [Library provides implementation in Rust level instead of ink! level](https://github.com/Supercolony-net/openbrush-contracts/issues/5)
* [List of issues, solving each of them can simplify usage of library](https://github.com/Supercolony-net/openbrush-contracts/issues/8)
* After [Storage rework](https://github.com/paritytech/ink/pull/1217) we need to refactor upgradeable contracts.

Expand Down Expand Up @@ -204,7 +204,7 @@ $ yarn build:release

### Tests

You can run unit tests by `RUSTFLAGS="-D warnings" cargo test --all-features --workspace -- --test-threads=10` command from the root of the directory.
You can run unit tests by `RUSTFLAGS="-D warnings" cargo +nightly test --workspace --features test-all -- --test-threads=10` command from the root of the directory.

To run integration test you need to start the node with contract-pallet.
- [Setup and start the node with contract-pallet](https://github.com/paritytech/substrate-contracts-node)
Expand Down
18 changes: 18 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ scale-info = { version = "2", default-features = false, features = ["derive"], o

openbrush = { version = "~2.2.0", package = "openbrush_lang", path = "../lang", default-features = false }

pallet-assets-chain-extension = { git = "https://github.com/Supercolony-net/pallet-assets-chain-extension", default-features = false, features = ["ink"] }

[lib]
name = "openbrush_contracts"
path = "src/lib.rs"
Expand All @@ -46,8 +48,10 @@ std = [
"scale-info/std",

"openbrush/std",
"pallet-assets-chain-extension/ink-std",
]
psp22 = []
psp22_pallet = []
psp34 = []
psp37 = []
access_control = []
Expand All @@ -63,4 +67,18 @@ proxy = [
]
diamond = [
"ownable",
]
test-all = [
"psp22",
# "psp22_pallet", we ignore it during testing. It requries it own run of tests
"psp34",
"psp37",
"access_control",
"ownable",
"payment_splitter",
"reentrancy_guard",
"pausable",
"timelock_controller",
"proxy",
"diamond",
]
2 changes: 2 additions & 0 deletions contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub use security::pausable;
pub use security::reentrancy_guard;
#[cfg(feature = "psp22")]
pub use token::psp22;
#[cfg(feature = "psp22_pallet")]
pub use token::psp22_pallet;
#[cfg(feature = "psp34")]
pub use token::psp34;
#[cfg(feature = "psp37")]
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#[cfg(feature = "psp22")]
pub mod psp22;
#[cfg(feature = "psp22_pallet")]
pub mod psp22_pallet;
#[cfg(feature = "psp34")]
pub mod psp34;
#[cfg(feature = "psp37")]
Expand Down
6 changes: 2 additions & 4 deletions contracts/src/token/psp22/extensions/flashmint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ use ink_env::{
CallFlags,
Error as EnvError,
};
use ink_prelude::{
string::String,
vec::Vec,
};
use ink_prelude::vec::Vec;
use openbrush::traits::{
AccountId,
Balance,
Storage,
String,
};

impl<T: Storage<psp22::Data>> FlashLender for T {
Expand Down
6 changes: 4 additions & 2 deletions contracts/src/token/psp22/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ pub use psp22::{
Transfer as _,
};

use ink_prelude::string::String;
use openbrush::traits::Storage;
use openbrush::traits::{
Storage,
String,
};

pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data);

Expand Down
6 changes: 2 additions & 4 deletions contracts/src/token/psp22/psp22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ use ink_env::{
CallFlags,
Error as EnvError,
};
use ink_prelude::{
string::String,
vec::Vec,
};
use ink_prelude::vec::Vec;
use openbrush::{
storage::{
Mapping,
Expand All @@ -47,6 +44,7 @@ use openbrush::{
AccountIdExt,
Balance,
Storage,
String,
},
};

Expand Down
41 changes: 41 additions & 0 deletions contracts/src/token/psp22_pallet/extensions/burnable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2012-2022 Supercolony
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the"Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// 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 use crate::{
psp22_pallet,
traits::psp22::{
extensions::burnable::*,
*,
},
};
pub use psp22_pallet::Internal as _;

use openbrush::traits::{
AccountId,
Balance,
Storage,
};

impl<T: Storage<psp22_pallet::Data>> PSP22Burnable for T {
default fn burn(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._burn_from(account, amount)
}
}
65 changes: 65 additions & 0 deletions contracts/src/token/psp22_pallet/extensions/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2012-2022 Supercolony
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the"Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// 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 use crate::{
psp22_pallet,
psp22_pallet::extensions::metadata,
traits::psp22::{
extensions::metadata::*,
*,
},
};
pub use psp22_pallet::Internal as _;

use openbrush::traits::{
Storage,
String,
};
use pallet_assets_chain_extension::traits::PalletAssets;

impl<T: Storage<psp22_pallet::Data>> PSP22Metadata for T {
default fn token_name(&self) -> Option<String> {
let self_ = self.data();
let name = self_.pallet_assets.metadata_name(self_.asset_id);

if name.is_empty() {
None
} else {
Some(String::from(name))
}
}

default fn token_symbol(&self) -> Option<String> {
let self_ = self.data();
let symbol = self_.pallet_assets.metadata_symbol(self_.asset_id);

if symbol.is_empty() {
None
} else {
Some(String::from(symbol))
}
}

default fn token_decimals(&self) -> u8 {
let self_ = self.data();
self_.pallet_assets.metadata_decimals(self_.asset_id)
}
}
41 changes: 41 additions & 0 deletions contracts/src/token/psp22_pallet/extensions/mintable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2012-2022 Supercolony
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the"Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// 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 use crate::{
psp22_pallet,
traits::psp22::{
extensions::mintable::*,
*,
},
};
pub use psp22_pallet::Internal as _;

use openbrush::traits::{
AccountId,
Balance,
Storage,
};

impl<T: Storage<psp22_pallet::Data>> PSP22Mintable for T {
default fn mint(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._mint(account, amount)
}
}
30 changes: 30 additions & 0 deletions contracts/src/token/psp22_pallet/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2012-2022 Supercolony
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the"Software"),
// to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// 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 psp22_pallet;

pub use psp22_pallet::*;

pub mod extensions {
pub mod burnable;
pub mod metadata;
pub mod mintable;
}
Loading