diff --git a/Cargo.toml b/Cargo.toml index 5ca9f0cff..b7a4ba6de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ crate-type = [ ] [dev-dependencies] -ink_env = "4.3.0" +ink_env = "5.0.0-alpha" trybuild = "1.0.80" ink_e2e = "5.0.0-alpha" @@ -56,7 +56,6 @@ std = [ "openbrush_lang/std", ] psp22 = ["openbrush_contracts/psp22"] -psp22_pallet = ["openbrush_contracts/psp22_pallet"] psp34 = ["openbrush_contracts/psp34"] psp37 = ["openbrush_contracts/psp37"] access_control = ["openbrush_contracts/access_control"] diff --git a/contracts/Cargo.toml b/contracts/Cargo.toml index 5d59a27dd..d1bcf4ddd 100644 --- a/contracts/Cargo.toml +++ b/contracts/Cargo.toml @@ -22,8 +22,6 @@ hex = { version = "0.4.3", default-features = false, features = ["alloc"] } openbrush = { version = "~4.0.0-beta.1", package = "openbrush_lang", path = "../lang", default-features = false, features = ["crypto", "checkpoints"] } -pallet-assets-chain-extension = { git = "https://github.com/Brushfam/pallet-assets-chain-extension", branch = "polkadot-v0.9.37", default-features = false, features = ["ink-lang"] } - [lib] name = "openbrush_contracts" path = "src/lib.rs" @@ -38,10 +36,8 @@ std = [ "scale/std", "scale-info/std", "openbrush/std", - "pallet-assets-chain-extension/ink-std", ] psp22 = ["nonces", "crypto"] -psp22_pallet = [] psp34 = [] psp37 = [] access_control = [] diff --git a/contracts/src/token/psp22_pallet/extensions/burnable.rs b/contracts/src/token/psp22_pallet/extensions/burnable.rs deleted file mode 100644 index 53e0e3cfe..000000000 --- a/contracts/src/token/psp22_pallet/extensions/burnable.rs +++ /dev/null @@ -1,49 +0,0 @@ -// 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 ink::env::DefaultEnvironment; -use openbrush::traits::{ - AccountId, - Balance, - Storage, -}; -pub use pallet_assets_chain_extension::traits::{ - Error, - Origin, -}; -pub use psp22_pallet::{ - Internal as _, - InternalImpl as _, - PSP22PalletImpl, -}; - -pub trait PSP22PalletBurnableImpl: Storage + psp22_pallet::Internal { - fn burn(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - self._burn_from(account, amount) - } -} diff --git a/contracts/src/token/psp22_pallet/extensions/metadata.rs b/contracts/src/token/psp22_pallet/extensions/metadata.rs deleted file mode 100644 index db5b23647..000000000 --- a/contracts/src/token/psp22_pallet/extensions/metadata.rs +++ /dev/null @@ -1,80 +0,0 @@ -// 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 ink::env::DefaultEnvironment; -use openbrush::traits::Storage; -pub use openbrush::traits::String; -pub use pallet_assets_chain_extension::traits::{ - Error, - Origin, - PalletAssets, -}; -pub use psp22_pallet::{ - Internal as _, - InternalImpl as _, - PSP22PalletImpl, -}; - -pub trait PSP22PalletMetadataImpl: Storage { - fn token_name(&self) -> Option { - let self_ = self.data(); - let name = self_ - .pallet_assets - .get_or_default() - .metadata_name(self_.asset_id.get_or_default()); - - if name.is_empty() { - None - } else { - Some(String::from_utf8(name).expect("Invalid UTF-8 string for token")) - } - } - - fn token_symbol(&self) -> Option { - let self_ = self.data(); - let symbol = self_ - .pallet_assets - .get_or_default() - .metadata_symbol(self_.asset_id.get_or_default()); - - if symbol.is_empty() { - None - } else { - Some(String::from_utf8(symbol).expect("Invalid UTF-8 string for token")) - } - } - - fn token_decimals(&self) -> u8 { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .metadata_decimals(self_.asset_id.get_or_default()) - } -} diff --git a/contracts/src/token/psp22_pallet/extensions/mintable.rs b/contracts/src/token/psp22_pallet/extensions/mintable.rs deleted file mode 100644 index 8bfa3caba..000000000 --- a/contracts/src/token/psp22_pallet/extensions/mintable.rs +++ /dev/null @@ -1,49 +0,0 @@ -// 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 ink::env::DefaultEnvironment; -use openbrush::traits::{ - AccountId, - Balance, - Storage, -}; -pub use pallet_assets_chain_extension::traits::{ - Error, - Origin, -}; -pub use psp22_pallet::{ - Internal as _, - InternalImpl as _, - PSP22PalletImpl, -}; - -pub trait PSP22PalletMintableImpl: Storage + psp22_pallet::Internal { - fn mint(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - self._mint_to(account, amount) - } -} diff --git a/contracts/src/token/psp22_pallet/mod.rs b/contracts/src/token/psp22_pallet/mod.rs deleted file mode 100644 index e103e886a..000000000 --- a/contracts/src/token/psp22_pallet/mod.rs +++ /dev/null @@ -1,30 +0,0 @@ -// 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; -} diff --git a/contracts/src/token/psp22_pallet/psp22_pallet.rs b/contracts/src/token/psp22_pallet/psp22_pallet.rs deleted file mode 100644 index ab4428d48..000000000 --- a/contracts/src/token/psp22_pallet/psp22_pallet.rs +++ /dev/null @@ -1,282 +0,0 @@ -// 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::*, -}; -pub use ink::{ - env::DefaultEnvironment, - prelude::vec::Vec, -}; -use openbrush::traits::{ - AccountId, - Balance, - Storage, - String, -}; -pub use pallet_assets_chain_extension::{ - ink::*, - traits::*, -}; -pub use psp22_pallet::{ - Internal as _, - InternalImpl as _, - PSP22PalletImpl as _, -}; - -#[derive(Default, Debug)] -#[openbrush::storage_item] -pub struct Data { - /// Asset id of the token on the pallet. - #[lazy] - pub asset_id: u32, - /// Default origin of the contract. - #[lazy] - pub origin: Origin, - /// Extension to interact with `pallet-assets` - #[lazy] - pub pallet_assets: AssetsExtension, -} - -pub trait PSP22PalletImpl: Storage + Internal { - fn total_supply(&self) -> Balance { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .total_supply(self_.asset_id.get_or_default()) - } - - fn balance_of(&self, owner: AccountId) -> Balance { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .balance_of(self_.asset_id.get_or_default(), owner) - } - - fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .allowance(self_.asset_id.get_or_default(), owner, spender) - } - - fn transfer(&mut self, to: AccountId, value: Balance, _data: Vec) -> Result<(), PSP22Error> { - if value == 0 { - return Ok(()) - } - - let self_ = self.data(); - self_.pallet_assets.get_or_default().transfer( - self_.origin.get_or_default(), - self_.asset_id.get_or_default(), - to.clone(), - value, - )?; - self._emit_transfer_event(Some(self._sender()), Some(to), value); - Ok(()) - } - - fn transfer_from( - &mut self, - from: AccountId, - to: AccountId, - value: Balance, - _data: Vec, - ) -> Result<(), PSP22Error> { - if value == 0 { - return Ok(()) - } - - let self_ = self.data(); - self_.pallet_assets.get_or_default().transfer_approved( - self_.origin.get_or_default(), - self_.asset_id.get_or_default(), - from.clone(), - to.clone(), - value, - )?; - self._emit_transfer_event(Some(from), Some(to), value); - Ok(()) - } - - fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { - if value == 0 { - return Ok(()) - } - - let caller = self._sender(); - let allowance = self.allowance(caller.clone(), spender.clone()); - let self_ = self.data(); - - if allowance > 0 { - // First we reset the previous approve and after set a new one. - self_.pallet_assets.get_or_default().cancel_approval( - self_.origin.get_or_default(), - self_.asset_id.get_or_default(), - spender.clone(), - )?; - } - - self_.pallet_assets.get_or_default().approve_transfer( - self_.origin.get_or_default(), - self_.asset_id.get_or_default(), - spender, - value, - )?; - self._emit_approval_event(caller, spender, value); - Ok(()) - } - - fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { - if delta_value == 0 { - return Ok(()) - } - - let caller = self._sender(); - let allowance = self.allowance(caller.clone(), spender.clone()); - let self_ = self.data(); - // `approve_transfer` increases by default - self_.pallet_assets.get_or_default().approve_transfer( - self_.origin.get_or_default(), - self_.asset_id.get_or_default(), - spender, - delta_value, - )?; - self._emit_approval_event(caller, spender, allowance + delta_value); - - Ok(()) - } - - fn decrease_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { - if delta_value == 0 { - return Ok(()) - } - - let caller = self._sender(); - - let mut allowance = self.allowance(caller.clone(), spender.clone()); - - if allowance < delta_value { - return Err(PSP22Error::InsufficientAllowance) - } - allowance -= delta_value; - - self.approve(spender, allowance)?; - self._emit_approval_event(caller, spender, allowance); - - Ok(()) - } -} - -pub trait Internal { - /// User must override those methods in their contract. - fn _emit_transfer_event(&self, _from: Option, _to: Option, _amount: Balance); - - fn _emit_approval_event(&self, _owner: AccountId, _spender: AccountId, _amount: Balance); - - fn _mint_to(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error>; - - fn _burn_from(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error>; - - fn _create( - &mut self, - asset_id: u32, - admin: AccountId, - min_balance: Balance, - ) -> Result<(), Error>; - - fn _sender(&self) -> AccountId; -} - -pub trait InternalImpl: Storage + Internal { - fn _emit_transfer_event(&self, _from: Option, _to: Option, _amount: Balance) {} - - fn _emit_approval_event(&self, _owner: AccountId, _spender: AccountId, _amount: Balance) {} - - fn _mint_to(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .mint(self_.asset_id.get_or_default(), account.clone(), amount)?; - Internal::_emit_transfer_event(self, None, Some(account), amount); - Ok(()) - } - - fn _burn_from(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - let self_ = self.data(); - self_ - .pallet_assets - .get_or_default() - .burn(self_.asset_id.get_or_default(), account.clone(), amount)?; - Internal::_emit_transfer_event(self, Some(account), None, amount); - Ok(()) - } - - fn _create( - &mut self, - asset_id: u32, - admin: AccountId, - min_balance: Balance, - ) -> Result<(), Error> { - self.data() - .pallet_assets - .get_or_default() - .create(asset_id, admin, min_balance) - } - - fn _sender(&self) -> AccountId { - match self.data().origin.get_or_default() { - Origin::Caller => Self::env().caller(), - Origin::Address => Self::env().account_id(), - } - } -} - -impl From> for PSP22Error { - fn from(error: Error) -> Self { - match error { - Error::ContractIsNotAdmin => PSP22Error::Custom(String::from("ContractIsNotAdmin")), - Error::BalanceLow => PSP22Error::InsufficientBalance, - Error::NoAccount => PSP22Error::Custom(String::from("NoAccount")), - Error::NoPermission => PSP22Error::Custom(String::from("NoPermission")), - Error::Unknown => PSP22Error::Custom(String::from("Unknown")), - Error::Frozen => PSP22Error::Custom(String::from("Frozen")), - Error::InUse => PSP22Error::Custom(String::from("InUse")), - Error::BadWitness => PSP22Error::Custom(String::from("BadWitness")), - Error::MinBalanceZero => PSP22Error::Custom(String::from("MinBalanceZero")), - Error::NoProvider => PSP22Error::Custom(String::from("NoProvider")), - Error::BadMetadata => PSP22Error::Custom(String::from("BadMetadata")), - Error::Unapproved => PSP22Error::InsufficientAllowance, - Error::WouldDie => PSP22Error::Custom(String::from("WouldDie")), - Error::AlreadyExists => PSP22Error::Custom(String::from("AlreadyExists")), - Error::NoDeposit => PSP22Error::Custom(String::from("NoDeposit")), - Error::WouldBurn => PSP22Error::Custom(String::from("WouldBurn")), - Error::AssetPalletInternal => PSP22Error::Custom(String::from("AssetPalletInternal")), - // All future errors should be `AssetPalletInternal` - _ => panic!("other error are not supported"), - } - } -} diff --git a/examples/psp22_pallet/.gitignore b/examples/psp22_pallet/.gitignore deleted file mode 100644 index 8de8f877e..000000000 --- a/examples/psp22_pallet/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/examples/psp22_pallet/Cargo.toml b/examples/psp22_pallet/Cargo.toml deleted file mode 100644 index ec04e9279..000000000 --- a/examples/psp22_pallet/Cargo.toml +++ /dev/null @@ -1,39 +0,0 @@ -[package] -name = "my_psp22_pallet" -version= "4.0.0-beta.1" -authors = ["Brushfam "] -edition = "2021" - -[dependencies] -ink = { version = "5.0.0-alpha", default-features = false} - -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } - -# These dependencies -openbrush = { path = "../..", default-features = false, features = ["psp22_pallet"] } - -[dev-dependencies] -ink_e2e = "5.0.0-alpha" -test_helpers = { path = "../test_helpers", default-features = false } -rand = "0.8.5" - -[lib] -name = "my_psp22_pallet" -path = "lib.rs" - - -[features] -default = ["std"] -std = [ - "ink/std", - "scale/std", - "scale-info/std", - # These dependencies - "openbrush/std", -] -ink-as-dependency = [] -e2e-tests = [] - -[profile.dev] -codegen-units = 16 diff --git a/examples/psp22_pallet/README.md b/examples/psp22_pallet/README.md deleted file mode 100644 index a85665d87..000000000 --- a/examples/psp22_pallet/README.md +++ /dev/null @@ -1,13 +0,0 @@ -## PSP22 Pallet contract (ERC20 analogue) - -Implementation of [PSP22](https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md) token standard via [Pallet assets](https://github.com/727-Ventures/pallet-assets-chain-extension) chain extension in Polkadot blockchain. - -This implementation standardizes the `pallet-assets` assets into `PSP22` standard. So the functions within this implementation resemble the `PSP22` standard, while those functions are calling the underlying functions of `pallet-asset`. Here is how it works: - -- the constructor will create a new asset on the `pallet-assets` -- we can now manipulate with this asset as with any other asset on `pallet-assets` -- however we can also call the functions of this smart contract, and it will call the underlying functions from `pallet-assets` - -With this we can achieve usage of `pallet-assets` based tokens within ink! smart contracts, which are based on the `PSP22` standard (as the `PSP22Pallet` implements the `PSP22` standard functions), and we can even transfer our smart-contract based token to chains which do not support smart contracts but do support `pallet-assets` (such as Statemint). You can read more about the cross-chain asset transfer and see a guide [here](https://medium.com/@krikolkk/xcm-and-cross-chain-asset-transferring-6922a0ba209). - -[See example](https://727-ventures.github.io/openbrush-contracts/smart-contracts/PSP22-Pallet/) diff --git a/examples/psp22_pallet/lib.rs b/examples/psp22_pallet/lib.rs deleted file mode 100644 index ebe5d9c83..000000000 --- a/examples/psp22_pallet/lib.rs +++ /dev/null @@ -1,143 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std, no_main)] - -#[openbrush::implementation(PSP22Pallet)] -#[openbrush::contract] -pub mod my_psp22_pallet { - use openbrush::traits::Storage; - - #[ink(storage)] - #[derive(Default, Storage)] - pub struct Contract { - #[storage_field] - pallet: psp22_pallet::Data, - } - - impl Contract { - /// During instantiation of the contract, you need to pass native tokens as a deposit - /// for asset creation. - #[ink(constructor)] - #[ink(payable)] - pub fn new(asset_id: u32, min_balance: Balance, total_supply: Balance) -> Self { - let mut instance = Self::default(); - - psp22_pallet::Internal::_create(&mut instance, asset_id, Self::env().account_id(), min_balance) - .expect("Should create an asset"); - instance.pallet.asset_id.set(&asset_id); - instance.pallet.origin.set(&Origin::Caller); - psp22_pallet::Internal::_mint_to(&mut instance, Self::env().caller(), total_supply).expect("Should mint"); - - instance - } - - /// Asset id of the asset in the `pallet-assets` - #[ink(message)] - pub fn asset_id(&self) -> u32 { - self.pallet.asset_id.get_or_default() - } - } - - #[cfg(all(test, feature = "e2e-tests"))] - pub mod tests { - use openbrush::contracts::psp22_pallet::psp22_external::PSP22; - #[rustfmt::skip] - use super::*; - - use test_helpers::{ - address_of, - balance_of, - }; - use ink_e2e::ContractsBackend; - - fn random_num() -> u32 { - use rand::Rng; - rand::thread_rng().gen_range(1..1000) - } - - type E2EResult = Result>; - - #[ink_e2e::test] - async fn assigns_initial_balance(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 100); - let contract = client - .instantiate( - "my_psp22_pallet", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.balance_of(address_of!(Alice)); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - }; - - assert!(matches!(result.return_value(), 100)); - - Ok(()) - } - - #[ink_e2e::test] - async fn transfer_adds_amount_to_destination_account(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 100); - let contract = client - .instantiate( - "my_psp22_pallet", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.transfer(address_of!(Bob), 50, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("transfer failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_alice = balance_of!(client, call, Alice); - - let balance_of_bob = balance_of!(client, call, Bob); - - assert_eq!(balance_of_bob, 50, "Bob should have 50 tokens"); - assert_eq!(balance_of_alice, 50, "Alice should have 50 tokens"); - - Ok(()) - } - - #[ink_e2e::test] - async fn cannot_transfer_above_the_amount(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 100); - let contract = client - .instantiate( - "my_psp22_pallet", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.transfer(address_of!(Bob), 101, vec![]); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - }; - - assert!(matches!(result.return_value(), Err(PSP22Error::InsufficientBalance))); - - Ok(()) - } - } -} diff --git a/examples/psp22_pallet_extensions/burnable/.gitignore b/examples/psp22_pallet_extensions/burnable/.gitignore deleted file mode 100644 index 8de8f877e..000000000 --- a/examples/psp22_pallet_extensions/burnable/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/examples/psp22_pallet_extensions/burnable/Cargo.toml b/examples/psp22_pallet_extensions/burnable/Cargo.toml deleted file mode 100644 index 3b089c981..000000000 --- a/examples/psp22_pallet_extensions/burnable/Cargo.toml +++ /dev/null @@ -1,40 +0,0 @@ -[package] -name = "my_psp22_pallet_burnable" -version= "4.0.0-beta.1" -authors = ["Brushfam "] -edition = "2021" - -[dependencies] -ink = { version = "5.0.0-alpha", default-features = false} - -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } - -# These dependencies -openbrush = { path = "../../..", default-features = false, features = ["psp22_pallet"] } - -[dev-dependencies] -ink_e2e = "5.0.0-alpha" -test_helpers = { path = "../../test_helpers", default-features = false } -rand = "0.8.5" - - -[lib] -name = "my_psp22_pallet_burnable" -path = "lib.rs" - - -[features] -default = ["std"] -std = [ - "ink/std", - "scale/std", - "scale-info/std", - # These dependencies - "openbrush/std", -] -ink-as-dependency = [] -e2e-tests = [] - -[profile.dev] -codegen-units = 16 diff --git a/examples/psp22_pallet_extensions/burnable/README.md b/examples/psp22_pallet_extensions/burnable/README.md deleted file mode 100644 index e2b213d86..000000000 --- a/examples/psp22_pallet_extensions/burnable/README.md +++ /dev/null @@ -1,5 +0,0 @@ -## PSP22 contract (ERC20 analogue) with 'Burnable' extension - -Implementation of 'Burnable' extension for [PSP22](https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md) token standard in Polkadot blockchain, which implies destroying owners tokens or tokens from another account if have an allowance, and decreasing total supply. - -[See example](https://727-Ventures.github.io/openbrush-contracts/smart-contracts/psp22_pallet/extensions/burnable) diff --git a/examples/psp22_pallet_extensions/burnable/lib.rs b/examples/psp22_pallet_extensions/burnable/lib.rs deleted file mode 100644 index 99a489882..000000000 --- a/examples/psp22_pallet_extensions/burnable/lib.rs +++ /dev/null @@ -1,359 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std, no_main)] - -#[openbrush::implementation(PSP22Pallet, PSP22PalletBurnable)] -#[openbrush::contract] -pub mod my_psp22_pallet_burnable { - use openbrush::traits::Storage; - - #[ink(storage)] - #[derive(Default, Storage)] - pub struct Contract { - #[storage_field] - pallet: psp22_pallet::Data, - } - - impl Contract { - /// During instantiation of the contract, you need to pass native tokens as a deposit - /// for asset creation. - #[ink(constructor)] - #[ink(payable)] - pub fn new(asset_id: u32, min_balance: Balance, total_supply: Balance) -> Self { - let mut instance = Self::default(); - - psp22_pallet::Internal::_create(&mut instance, asset_id, Self::env().account_id(), min_balance) - .expect("Should create an asset"); - instance.pallet.asset_id.set(&asset_id); - instance.pallet.origin.set(&Origin::Caller); - psp22_pallet::Internal::_mint_to(&mut instance, Self::env().caller(), total_supply).expect("Should mint"); - - instance - } - - #[ink(message)] - pub fn burn_from_many(&mut self, accounts: Vec<(AccountId, Balance)>) -> Result<(), PSP22Error> { - for account in accounts.iter() { - PSP22Burnable::burn(self, account.0, account.1)?; - } - Ok(()) - } - } - - #[cfg(all(test, feature = "e2e-tests"))] - pub mod tests { - use openbrush::contracts::psp22_pallet::{ - extensions::burnable::psp22burnable_external::PSP22Burnable, - psp22_external::PSP22, - }; - #[rustfmt::skip] - use super::*; - - use test_helpers::{ - address_of, - balance_of, - }; - use ink_e2e::ContractsBackend; - - type E2EResult = Result>; - - fn random_num() -> u32 { - use rand::Rng; - rand::thread_rng().gen_range(1..1000) - } - - #[ink_e2e::test] - async fn assigns_initial_balance(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let balance_of_alice = balance_of!(client, call, Alice); - - assert!(matches!(balance_of_alice, 1000)); - - Ok(()) - } - - #[ink_e2e::test] - async fn can_burn(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.burn(address_of!(Alice), 10); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_alice = balance_of!(client, call, Alice); - - assert!(matches!(balance_of_alice, 990)); - - Ok(()) - } - - #[ink_e2e::test] - async fn can_burn_without_allowance(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - assert!(matches!(balance_of!(client, call, Bob), 0)); - assert!(matches!(balance_of!(client, call, Alice), 1000)); - - let result = { - let _msg = call.burn(address_of!(Alice), 10); - client.call(&ink_e2e::bob(), &_msg, 0, None).await.expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_alice = balance_of!(client, call, Alice); - - assert!(matches!(balance_of_alice, 990)); - - Ok(()) - } - - #[ink_e2e::test] - async fn decreases_total_supply_after_burning(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let total_supply = { - let _msg = call.total_supply(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - }; - - assert!(matches!(total_supply.return_value(), 1000)); - - let result = { - let _msg = call.burn(address_of!(Alice), 10); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let total_supply = { - let _msg = call.total_supply(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - }; - - assert!(matches!(total_supply.return_value(), 990)); - - Ok(()) - } - - #[ink_e2e::test] - async fn can_burn_from(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.transfer(address_of!(Bob), 10, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - - assert!(matches!(balance_of_bob, 10)); - - let result = { - let _msg =call.burn(address_of!(Bob), 10); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - - assert!(matches!(balance_of_bob, 0)); - - Ok(()) - } - - #[ink_e2e::test] - async fn can_burn_from_many(mut client: Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.transfer(address_of!(Bob), 10, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let result = { - let _msg = call.transfer(address_of!(Charlie), 10, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - let balance_of_charlie = balance_of!(client, call, Charlie); - - assert!(matches!(balance_of_bob, 10)); - assert!(matches!(balance_of_charlie, 10)); - - let result = { - let _msg = call.burn_from_many(vec![(address_of!(Bob), 10), (address_of!(Charlie), 10)]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - let balance_of_charlie = balance_of!(client, call, Charlie); - - assert!(matches!(balance_of_bob, 0)); - assert!(matches!(balance_of_charlie, 0)); - - Ok(()) - } - - #[ink_e2e::test] - async fn fails_if_one_of_the_accounts_balance_exceeds_amount_to_burn( - mut client: ink_e2e::Client, - ) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_burnable", - &ink_e2e::alice(), - constructor, - 10000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let result = { - let _msg = call.transfer(address_of!(Bob), 10, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let result = { - let _msg = call.transfer(address_of!(Charlie), 5, vec![]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - let balance_of_charlie = balance_of!(client, call, Charlie); - - assert!(matches!(balance_of_bob, 10)); - assert!(matches!(balance_of_charlie, 5)); - - let result = { - let _msg = call.burn_from_many(vec![(address_of!(Bob), 10), (address_of!(Charlie), 10)]); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("call failed") - }; - // This is not working properly TBD - assert!(matches!(result.return_value(), Ok(()))); - - let balance_of_bob = balance_of!(client, call, Bob); - let balance_of_charlie = balance_of!(client, call, Charlie); - - assert!(matches!(balance_of_bob, 0)); - assert!(matches!(balance_of_charlie, 0)); - - Ok(()) - } - } -} diff --git a/examples/psp22_pallet_extensions/metadata/.gitignore b/examples/psp22_pallet_extensions/metadata/.gitignore deleted file mode 100644 index 8de8f877e..000000000 --- a/examples/psp22_pallet_extensions/metadata/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/examples/psp22_pallet_extensions/metadata/Cargo.toml b/examples/psp22_pallet_extensions/metadata/Cargo.toml deleted file mode 100644 index 868d02d60..000000000 --- a/examples/psp22_pallet_extensions/metadata/Cargo.toml +++ /dev/null @@ -1,39 +0,0 @@ -[package] -name = "my_psp22_pallet_metadata" -version= "4.0.0-beta.1" -authors = ["Brushfam "] -edition = "2021" - -[dependencies] -ink = { version = "5.0.0-alpha", default-features = false} - -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } - -# These dependencies -openbrush = { path = "../../..", default-features = false, features = ["psp22_pallet"] } - -[dev-dependencies] -ink_e2e = "5.0.0-alpha" -test_helpers = { path = "../../test_helpers", default-features = false } -rand = "0.8.5" - -[lib] -name = "my_psp22_pallet_metadata" -path = "lib.rs" - - -[features] -default = ["std"] -std = [ - "ink/std", - "scale/std", - "scale-info/std", - # These dependencies - "openbrush/std", -] -ink-as-dependency = [] -e2e-tests = [] - -[profile.dev] -codegen-units = 16 diff --git a/examples/psp22_pallet_extensions/metadata/README.md b/examples/psp22_pallet_extensions/metadata/README.md deleted file mode 100644 index 3108cc8b8..000000000 --- a/examples/psp22_pallet_extensions/metadata/README.md +++ /dev/null @@ -1,5 +0,0 @@ -## PSP22 contract (ERC20 analogue) with 'Metadata' extension - -Implementation of 'Metadata' extension for [PSP22](https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md) token standard in Polkadot blockchain, which allows to add Metadata to the PSP22 token. - -[See example](https://727-Ventures.github.io/openbrush-contracts/smart-contracts/psp22_pallet/extensions/metadata) diff --git a/examples/psp22_pallet_extensions/metadata/lib.rs b/examples/psp22_pallet_extensions/metadata/lib.rs deleted file mode 100644 index f4ddbfd1f..000000000 --- a/examples/psp22_pallet_extensions/metadata/lib.rs +++ /dev/null @@ -1,104 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std, no_main)] - -#[openbrush::implementation(PSP22Pallet, PSP22PalletMetadata)] -#[openbrush::contract] -pub mod my_psp22_pallet_metadata { - use openbrush::traits::Storage; - - #[ink(storage)] - #[derive(Default, Storage)] - pub struct Contract { - #[storage_field] - pallet: psp22_pallet::Data, - } - - impl Contract { - /// During instantiation of the contract, you need to pass native tokens as a deposit - /// for asset creation. - #[ink(constructor)] - #[ink(payable)] - pub fn new( - asset_id: u32, - min_balance: Balance, - total_supply: Balance, - name: String, - symbol: String, - decimal: u8, - ) -> Self { - let mut instance = Self::default(); - - psp22_pallet::Internal::_create(&mut instance, asset_id, Self::env().account_id(), min_balance) - .expect("Should create an asset"); - instance.pallet.asset_id.set(&asset_id); - instance.pallet.origin.set(&Origin::Caller); - instance - .pallet - .pallet_assets - .get_or_default() - .set_metadata(asset_id, name.into(), symbol.into(), decimal) - .expect("Should set metadata"); - psp22_pallet::Internal::_mint_to(&mut instance, Self::env().caller(), total_supply).expect("Should mint"); - - instance - } - } - - #[cfg(all(test, feature = "e2e-tests"))] - pub mod tests { - use openbrush::contracts::psp22_pallet::extensions::metadata::psp22metadata_external::PSP22Metadata; - - #[rustfmt::skip] - use super::*; - use ink_e2e::ContractsBackend; - - type E2EResult = Result>; - - fn random_num() -> u32 { - use rand::Rng; - rand::thread_rng().gen_range(1..1000) - } - - #[ink_e2e::test] - async fn metadata_works(client: ink_e2e::Client) -> E2EResult<()> { - let _name = String::from("TOKEN"); - let _symbol = String::from("TKN"); - - let constructor = ContractRef::new(random_num(), 1, 1000, _name, _symbol, 18); - let contract = client - .instantiate( - "my_psp22_pallet_metadata", - &ink_e2e::alice(), - constructor, - 1000000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let token_name = { - let _msg = call.token_name(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - } - .return_value(); - - let token_symbol = { - let _msg = call.token_symbol(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - } - .return_value(); - - let token_decimals = { - let _msg = call.token_decimals(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - } - .return_value(); - - assert!(matches!(token_name, Some(_name))); - assert!(matches!(token_symbol, Some(_symbol))); - assert!(matches!(token_decimals, 18)); - - Ok(()) - } - } -} diff --git a/examples/psp22_pallet_extensions/mintable/.gitignore b/examples/psp22_pallet_extensions/mintable/.gitignore deleted file mode 100644 index 8de8f877e..000000000 --- a/examples/psp22_pallet_extensions/mintable/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -# Ignore build artifacts from the local tests sub-crate. -/target/ - -# Ignore backup files creates by cargo fmt. -**/*.rs.bk - -# Remove Cargo.lock when creating an executable, leave it for libraries -# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock -Cargo.lock diff --git a/examples/psp22_pallet_extensions/mintable/Cargo.toml b/examples/psp22_pallet_extensions/mintable/Cargo.toml deleted file mode 100644 index de1997636..000000000 --- a/examples/psp22_pallet_extensions/mintable/Cargo.toml +++ /dev/null @@ -1,39 +0,0 @@ -[package] -name = "my_psp22_pallet_mintable" -version= "4.0.0-beta.1" -authors = ["Brushfam "] -edition = "2021" - -[dependencies] -ink = { version = "5.0.0-alpha", default-features = false} - -scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } -scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true } - -# These dependencies -openbrush = { path = "../../..", default-features = false, features = ["psp22_pallet"] } - -[lib] -name = "my_psp22_pallet_mintable" -path = "lib.rs" - - -[dev-dependencies] -ink_e2e = "5.0.0-alpha" -test_helpers = { path = "../../test_helpers", default-features = false } -rand = "0.8.5" - -[features] -default = ["std"] -std = [ - "ink/std", - "scale/std", - "scale-info/std", - # These dependencies - "openbrush/std", -] -ink-as-dependency = [] -e2e-tests = [] - -[profile.dev] -codegen-units = 16 diff --git a/examples/psp22_pallet_extensions/mintable/README.md b/examples/psp22_pallet_extensions/mintable/README.md deleted file mode 100644 index 9c433753b..000000000 --- a/examples/psp22_pallet_extensions/mintable/README.md +++ /dev/null @@ -1,5 +0,0 @@ -## PSP22 contract (ERC20 analogue) with 'Mintable' extension - -Implementation of 'Mintable' extension for [PSP22](https://github.com/w3f/PSPs/blob/master/PSPs/psp-22.md) token standard in Polkadot blockchain, which allows to create new tokens and assigns them to particular account, increasing the total supply. - -[See example](https://727-Ventures.github.io/openbrush-contracts/smart-contracts/psp22_pallet/extensions/mintable) diff --git a/examples/psp22_pallet_extensions/mintable/lib.rs b/examples/psp22_pallet_extensions/mintable/lib.rs deleted file mode 100644 index e9a692b60..000000000 --- a/examples/psp22_pallet_extensions/mintable/lib.rs +++ /dev/null @@ -1,156 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std, no_main)] - -#[openbrush::implementation(PSP22Pallet, PSP22PalletMintable)] -#[openbrush::contract] -pub mod my_psp22_pallet_mintable { - use openbrush::traits::Storage; - - #[ink(storage)] - #[derive(Default, Storage)] - pub struct Contract { - #[storage_field] - pallet: psp22_pallet::Data, - } - - impl Contract { - /// During instantiation of the contract, you need to pass native tokens as a deposit - /// for asset creation. - #[ink(constructor)] - #[ink(payable)] - pub fn new(asset_id: u32, min_balance: Balance, total_supply: Balance) -> Self { - let mut instance = Self::default(); - let caller = instance.env().caller(); - - psp22_pallet::Internal::_create(&mut instance, asset_id, Self::env().account_id(), min_balance) - .expect("Should create an asset"); - instance.pallet.asset_id.set(&asset_id); - instance.pallet.origin.set(&Origin::Caller); - psp22_pallet::Internal::_mint_to(&mut instance, caller, total_supply).expect("Should mint_to"); - - instance - } - } - - #[cfg(all(test, feature = "e2e-tests"))] - pub mod tests { - use openbrush::contracts::psp22_pallet::{ - extensions::mintable::psp22mintable_external::PSP22Mintable, psp22_external::PSP22, - }; - - #[rustfmt::skip] - use super::*; - - use test_helpers::{address_of, balance_of}; - - use ink_e2e::ContractsBackend; - - fn random_num() -> u32 { - use rand::Rng; - rand::thread_rng().gen_range(1..1000) - } - - type E2EResult = Result>; - - #[ink_e2e::test] - async fn assigns_initial_balance(client: ink_e2e::Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_mintable", - &ink_e2e::alice(), - constructor, - 1000000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - assert!(matches!(balance_of!(client, address, Alice), 1000)); - - Ok(()) - } - - #[ink_e2e::test] - async fn minting_requested_amount(client: ink_e2e::Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 1000); - let contract = client - .instantiate( - "my_psp22_pallet_mintable", - &ink_e2e::alice(), - constructor, - 1000000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - assert!( - matches!(balance_of!(client, address, Bob), 0), - "Bob's balance should be 0" - ); - - let mint_tx = { - let _msg = call.mint(address_of!(Bob), 1000); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("transfer failed") - }; - - assert!(matches!(mint_tx.return_value(), Ok(())), "Minting should be successful"); - - assert!( - matches!(balance_of!(client, address, Bob), 1000), - "Bob's balance should be 1000" - ); - - Ok(()) - } - - #[ink_e2e::test] - async fn increases_total_supply_after_minting(client: ink_e2e::Client) -> E2EResult<()> { - let constructor = ContractRef::new(random_num(), 1, 0); - let contract = client - .instantiate( - "my_psp22_pallet_mintable", - &ink_e2e::alice(), - constructor, - 1000000000000000000, - None, - ) - .await - .expect("instantiate failed"); - let mut call = contract.call::(); - - let total_supply = { - let _msg = call.total_supply(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - } - .return_value(); - - assert!(matches!(total_supply, 0), "Total supply should be 0"); - - let mint_tx = { - let _msg = call.mint(address_of!(Bob), 1000); - client - .call(&ink_e2e::alice(), &_msg, 0, None) - .await - .expect("transfer failed") - }; - - assert!(matches!(mint_tx.return_value(), Ok(())), "Minting should be successful"); - - let total_supply = { - let _msg = call.total_supply(); - client.call_dry_run(&ink_e2e::alice(), &_msg, 0, None).await - } - .return_value(); - - assert!(matches!(total_supply, 1000), "Total supply should be 1000"); - - Ok(()) - } - } -} diff --git a/examples/psp61/Cargo.toml b/examples/psp61/Cargo.toml index 9f63b2c24..b3afc0e73 100644 --- a/examples/psp61/Cargo.toml +++ b/examples/psp61/Cargo.toml @@ -22,7 +22,7 @@ openbrush = { path = "../..", default-features = false, features = [ ] } [dev-dependencies] -ink_e2e = "4.2.1" +ink_e2e = "5.0.0-alpha" test_helpers = { path = "../test_helpers", default-features = false } [lib] diff --git a/examples/utils/nonces/Cargo.toml b/examples/utils/nonces/Cargo.toml index 2f63855bb..9085d04d3 100755 --- a/examples/utils/nonces/Cargo.toml +++ b/examples/utils/nonces/Cargo.toml @@ -14,7 +14,7 @@ scale-info = { version = "2.6", default-features = false, features = ["derive"], openbrush = { path = "../../..", default-features = false, features = ["nonces"] } [dev-dependencies] -ink_e2e = "4.2.1" +ink_e2e = "5.0.0-alpha" test_helpers = { path = "../../test_helpers", default-features = false } [lib] diff --git a/lang/codegen/src/implementation.rs b/lang/codegen/src/implementation.rs index b4187d83b..eed2194e5 100644 --- a/lang/codegen/src/implementation.rs +++ b/lang/codegen/src/implementation.rs @@ -88,10 +88,6 @@ pub fn generate(attrs: TokenStream, ink_module: TokenStream) -> TokenStream { "PSP22Votes" => impl_psp22_votes(&mut impl_args), "Flashmint" => impl_flashmint(&mut impl_args), "PSP22TokenTimelock" => impl_token_timelock(&mut impl_args), - "PSP22Pallet" => impl_psp22_pallet(&mut impl_args), - "PSP22PalletBurnable" => impl_psp22_pallet_burnable(&mut impl_args), - "PSP22PalletMetadata" => impl_psp22_pallet_metadata(&mut impl_args), - "PSP22PalletMintable" => impl_psp22_pallet_mintable(&mut impl_args), "PSP34" => impl_psp34(&mut impl_args), "PSP34Burnable" => impl_psp34_burnable(&mut impl_args), "PSP34Mintable" => impl_psp34_mintable(&mut impl_args), diff --git a/lang/codegen/src/implementations.rs b/lang/codegen/src/implementations.rs index 04ea7c045..b06c73272 100644 --- a/lang/codegen/src/implementations.rs +++ b/lang/codegen/src/implementations.rs @@ -763,218 +763,6 @@ pub(crate) fn impl_psp22_votes(impl_args: &mut ImplArgs) { impl_args.items.push(syn::Item::Impl(psp22_votes_internal)); impl_args.items.push(syn::Item::Impl(psp22_votes)); } -pub(crate) fn impl_psp22_pallet(impl_args: &mut ImplArgs) { - let storage_struct_name = impl_args.contract_name(); - let internal_impl = syn::parse2::(quote!( - impl psp22_pallet::InternalImpl for #storage_struct_name {} - )) - .expect("Should parse"); - - let mut internal = syn::parse2::(quote!( - impl psp22_pallet::Internal for #storage_struct_name { - fn _emit_transfer_event(&self, _from: Option, _to: Option, _amount: Balance) { - psp22_pallet::InternalImpl::_emit_transfer_event(self, _from, _to, _amount) - } - - fn _emit_approval_event(&self, _owner: AccountId, _spender: AccountId, _amount: Balance) { - psp22_pallet::InternalImpl::_emit_approval_event(self, _owner, _spender, _amount) - } - - fn _mint_to(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - psp22_pallet::InternalImpl::_mint_to(self, account, amount) - } - - fn _burn_from(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - psp22_pallet::InternalImpl::_burn_from(self, account, amount) - } - - fn _create( - &mut self, - asset_id: u32, - admin: AccountId, - min_balance: Balance, - ) -> Result<(), Error> { - psp22_pallet::InternalImpl::_create(self, asset_id, admin, min_balance) - } - - fn _sender(&self) -> AccountId { - psp22_pallet::InternalImpl::_sender(self) - } - } - )) - .expect("Should parse"); - - let psp22_pallet_impl = syn::parse2::(quote!( - impl PSP22PalletImpl for #storage_struct_name {} - )) - .expect("Should parse"); - - let mut psp22 = syn::parse2::(quote!( - impl PSP22 for #storage_struct_name { - #[ink(message)] - fn total_supply(&self) -> Balance { - PSP22PalletImpl::total_supply(self) - } - - #[ink(message)] - fn balance_of(&self, owner: AccountId) -> Balance { - PSP22PalletImpl::balance_of(self, owner) - } - - #[ink(message)] - fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance { - PSP22PalletImpl::allowance(self, owner, spender) - } - - #[ink(message)] - fn transfer(&mut self, to: AccountId, value: Balance, data: Vec) -> Result<(), PSP22Error> { - PSP22PalletImpl::transfer(self, to, value, data) - } - - #[ink(message)] - fn transfer_from( - &mut self, - from: AccountId, - to: AccountId, - value: Balance, - data: Vec, - ) -> Result<(), PSP22Error> { - PSP22PalletImpl::transfer_from(self, from, to, value, data) - } - - #[ink(message)] - fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { - PSP22PalletImpl::approve(self, spender, value) - } - - #[ink(message)] - fn increase_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { - PSP22PalletImpl::increase_allowance(self, spender, delta_value) - } - - #[ink(message)] - fn decrease_allowance(&mut self, spender: AccountId, delta_value: Balance) -> Result<(), PSP22Error> { - PSP22PalletImpl::decrease_allowance(self, spender, delta_value) - } - } - )) - .expect("Should parse"); - - let import = syn::parse2::(quote!( - use openbrush::contracts::psp22_pallet::*; - )) - .expect("Should parse"); - impl_args.imports.insert("PSP22Pallet", import); - impl_args.vec_import(); - - override_functions("psp22_pallet::Internal", &mut internal, impl_args.map); - override_functions("PSP22", &mut psp22, impl_args.map); - - impl_args.items.push(syn::Item::Impl(internal_impl)); - impl_args.items.push(syn::Item::Impl(internal)); - impl_args.items.push(syn::Item::Impl(psp22_pallet_impl)); - impl_args.items.push(syn::Item::Impl(psp22)); -} - -pub(crate) fn impl_psp22_pallet_burnable(impl_args: &mut ImplArgs) { - let storage_struct_name = impl_args.contract_name(); - let burnable_impl = syn::parse2::(quote!( - impl PSP22PalletBurnableImpl for #storage_struct_name {} - )) - .expect("Should parse"); - - let mut burnable = syn::parse2::(quote!( - impl PSP22Burnable for #storage_struct_name { - #[ink(message)] - fn burn(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - PSP22PalletBurnableImpl::burn(self, account, amount) - } - } - )) - .expect("Should parse"); - - let import = syn::parse2::(quote!( - use openbrush::contracts::psp22_pallet::extensions::burnable::*; - )) - .expect("Should parse"); - impl_args.imports.insert("PSP22PalletBurnable", import); - impl_args.vec_import(); - - override_functions("PSP22Burnable", &mut burnable, impl_args.map); - - impl_args.items.push(syn::Item::Impl(burnable_impl)); - impl_args.items.push(syn::Item::Impl(burnable)); -} - -pub(crate) fn impl_psp22_pallet_metadata(impl_args: &mut ImplArgs) { - let storage_struct_name = impl_args.contract_name(); - let metadata_impl = syn::parse2::(quote!( - impl PSP22PalletMetadataImpl for #storage_struct_name {} - )) - .expect("Should parse"); - - let mut burnable = syn::parse2::(quote!( - impl PSP22Metadata for #storage_struct_name { - #[ink(message)] - fn token_name(&self) -> Option { - PSP22PalletMetadataImpl::token_name(self) - } - - #[ink(message)] - fn token_symbol(&self) -> Option { - PSP22PalletMetadataImpl::token_symbol(self) - } - - #[ink(message)] - fn token_decimals(&self) -> u8 { - PSP22PalletMetadataImpl::token_decimals(self) - } - } - )) - .expect("Should parse"); - - let import = syn::parse2::(quote!( - use openbrush::contracts::psp22_pallet::extensions::metadata::*; - )) - .expect("Should parse"); - impl_args.imports.insert("PSP22PalletMetadata", import); - impl_args.vec_import(); - - override_functions("PSP22Metadata", &mut burnable, impl_args.map); - - impl_args.items.push(syn::Item::Impl(metadata_impl)); - impl_args.items.push(syn::Item::Impl(burnable)); -} - -pub(crate) fn impl_psp22_pallet_mintable(impl_args: &mut ImplArgs) { - let storage_struct_name = impl_args.contract_name(); - let mintable_impl = syn::parse2::(quote!( - impl PSP22PalletMintableImpl for #storage_struct_name {} - )) - .expect("Should parse"); - - let mut mintable = syn::parse2::(quote!( - impl PSP22Mintable for #storage_struct_name { - #[ink(message)] - fn mint(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - PSP22PalletMintableImpl::mint(self, account, amount) - } - } - )) - .expect("Should parse"); - - let import = syn::parse2::(quote!( - use openbrush::contracts::psp22_pallet::extensions::mintable::*; - )) - .expect("Should parse"); - impl_args.imports.insert("PSP22PalletMintable", import); - impl_args.vec_import(); - - override_functions("PSP22Mintable", &mut mintable, impl_args.map); - - impl_args.items.push(syn::Item::Impl(mintable_impl)); - impl_args.items.push(syn::Item::Impl(mintable)); -} pub(crate) fn impl_psp34(impl_args: &mut ImplArgs) { let storage_struct_name = impl_args.contract_name();