From 54e75b3a1397da43d4fc37d090b99bdaabeb50e5 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Tue, 23 Apr 2024 17:57:44 +0900 Subject: [PATCH 01/11] test nft collection creation --- pop-api/examples/nfts/Cargo.toml | 2 ++ pop-api/examples/nfts/lib.rs | 28 ++++++++++++++- runtime/devnet/src/extensions.rs | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/pop-api/examples/nfts/Cargo.toml b/pop-api/examples/nfts/Cargo.toml index 49c49f64..1037bc90 100755 --- a/pop-api/examples/nfts/Cargo.toml +++ b/pop-api/examples/nfts/Cargo.toml @@ -9,6 +9,7 @@ ink = { version = "5.0.0-rc.3", default-features = false } pop-api = { path = "../../../pop-api", 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 } +enumflags2 = { version = "0.7.7" } [lib] path = "lib.rs" @@ -20,6 +21,7 @@ std = [ "pop-api/std", "scale/std", "scale-info/std", + "enumflags2/std", ] ink-as-dependency = [] e2e-tests = [] diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index 02fbf986..8b38d4fe 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -1,6 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] use pop_api::nfts; +use pop_api::nfts::*; +use enumflags2::BitFlags; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] @@ -19,7 +21,7 @@ impl From for ContractError { #[ink::contract(env = pop_api::Environment)] mod pop_api_extension_demo { - use super::ContractError; + use super::*; #[ink(storage)] #[derive(Default)] @@ -32,6 +34,30 @@ mod pop_api_extension_demo { Default::default() } + #[ink(message)] + pub fn create_nft_collection( &self ) -> Result<(), ContractError>{ + ink::env::debug_println!("Contract::create_nft_collection: collection creation started."); + let admin = Self::env().caller(); + let item_settings = ItemSettings(BitFlags::from(ItemSetting::Transferable)); + + let mint_settings = MintSettings { + mint_type: MintType::Issuer, + price: Some(0), + start_block: Some(0), + end_block: Some(0), + default_item_settings: item_settings, + }; + + let config = CollectionConfig { + settings: CollectionSettings(BitFlags::from(CollectionSetting::TransferableItems)), + max_supply: None, + mint_settings, + }; + pop_api::nfts::create(admin, config)?; + ink::env::debug_println!("Contract::create_nft_collection: collection created successfully."); + Ok(()) + } + #[ink(message)] pub fn mint_through_runtime( &mut self, diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 59c636b2..70a0062c 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -474,6 +474,68 @@ mod tests { }); } + // Create a test for tesing create_nft_collection + #[test] + #[ignore] + fn dispatch_nfts_create_nft_collection() { + new_test_ext().execute_with(|| { + let _ = env_logger::try_init(); + + let (wasm_binary, _) = load_wasm_module::( + "../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm", + ) + .unwrap(); + + let init_value = 100; + + let result = Contracts::bare_instantiate( + ALICE, + init_value, + GAS_LIMIT, + None, + Code::Upload(wasm_binary), + function_selector("new"), + vec![], + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + ) + .result + .unwrap(); + + assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); + + let addr = result.account_id; + + let function = function_selector("create_nft_collection"); + + let params = + [function].concat(); + + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), + None, + params, + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // // check for revert + assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); + }); + } + #[test] #[ignore] fn dispatch_nfts_mint_from_contract_works() { From 92cd4d99b8870a6f69700377b2f46eeeaa7cb5dc Mon Sep 17 00:00:00 2001 From: R0GUE Date: Tue, 23 Apr 2024 13:16:29 +0200 Subject: [PATCH 02/11] fix: test --- pop-api/examples/nfts/lib.rs | 30 ++++++++++++++---------------- pop-api/src/v0/nfts.rs | 2 +- runtime/devnet/src/extensions.rs | 5 ++--- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index 8b38d4fe..a1c2a45e 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -1,42 +1,40 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -use pop_api::nfts; use pop_api::nfts::*; -use enumflags2::BitFlags; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum ContractError { InvalidCollection, ItemAlreadyExists, - NftsError(nfts::Error), + NftsError(Error), NotOwner, } -impl From for ContractError { - fn from(value: nfts::Error) -> Self { +impl From for ContractError { + fn from(value: Error) -> Self { ContractError::NftsError(value) } } #[ink::contract(env = pop_api::Environment)] -mod pop_api_extension_demo { +mod pop_api_nfts { use super::*; #[ink(storage)] #[derive(Default)] - pub struct PopApiExtensionDemo; + pub struct Nfts; - impl PopApiExtensionDemo { + impl Nfts { #[ink(constructor, payable)] pub fn new() -> Self { - ink::env::debug_println!("Contract::new"); + ink::env::debug_println!("Nfts::new"); Default::default() } #[ink(message)] pub fn create_nft_collection( &self ) -> Result<(), ContractError>{ - ink::env::debug_println!("Contract::create_nft_collection: collection creation started."); + ink::env::debug_println!("Nfts::create_nft_collection: collection creation started."); let admin = Self::env().caller(); let item_settings = ItemSettings(BitFlags::from(ItemSetting::Transferable)); @@ -54,19 +52,19 @@ mod pop_api_extension_demo { mint_settings, }; pop_api::nfts::create(admin, config)?; - ink::env::debug_println!("Contract::create_nft_collection: collection created successfully."); + ink::env::debug_println!("Nfts::create_nft_collection: collection created successfully."); Ok(()) } #[ink(message)] - pub fn mint_through_runtime( + pub fn mint_nft( &mut self, collection_id: u32, item_id: u32, receiver: AccountId, ) -> Result<(), ContractError> { ink::env::debug_println!( - "Contract::mint_through_runtime: collection_id: {:?} item_id {:?} receiver: {:?}", + "Nfts::mint_through_runtime: collection_id: {:?} item_id {:?} receiver: {:?}", collection_id, item_id, receiver @@ -79,13 +77,13 @@ mod pop_api_extension_demo { // mint api pop_api::nfts::mint(collection_id, item_id, receiver)?; - ink::env::debug_println!("Contract::mint_through_runtime: item minted successfully"); + ink::env::debug_println!("Nfts::mint_through_runtime: item minted successfully"); // check owner match pop_api::nfts::owner(collection_id, item_id)? { Some(owner) if owner == receiver => { ink::env::debug_println!( - "Contract::mint_through_runtime success: minted item belongs to receiver" + "Nfts::mint_through_runtime success: minted item belongs to receiver" ); }, _ => { @@ -93,7 +91,7 @@ mod pop_api_extension_demo { }, } - ink::env::debug_println!("Contract::mint_through_runtime end"); + ink::env::debug_println!("Nfts::mint_through_runtime end"); Ok(()) } } diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 55569f2e..9d608310 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -681,7 +681,7 @@ mod types { primitives::{CollectionId, ItemId}, Balance, BlockNumber, }; - use enumflags2::{bitflags, BitFlags}; + pub use enumflags2::{bitflags, BitFlags}; use scale::{Decode, EncodeLike, MaxEncodedLen}; use scale_info::{build::Fields, meta_type, prelude::vec, Path, Type, TypeInfo, TypeParameter}; diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 70a0062c..45769593 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -486,7 +486,7 @@ mod tests { ) .unwrap(); - let init_value = 100; + let init_value = 100 * UNIT; let result = Contracts::bare_instantiate( ALICE, @@ -508,8 +508,7 @@ mod tests { let function = function_selector("create_nft_collection"); - let params = - [function].concat(); + let params = [function].concat(); let result = Contracts::bare_call( ALICE, From 947faaf88e0e161ab3f8811604cb98e09815801f Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Wed, 24 Apr 2024 15:02:06 +0900 Subject: [PATCH 03/11] enumflags2 is already within pop-api dependencies --- pop-api/examples/nfts/Cargo.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pop-api/examples/nfts/Cargo.toml b/pop-api/examples/nfts/Cargo.toml index 1037bc90..49c49f64 100755 --- a/pop-api/examples/nfts/Cargo.toml +++ b/pop-api/examples/nfts/Cargo.toml @@ -9,7 +9,6 @@ ink = { version = "5.0.0-rc.3", default-features = false } pop-api = { path = "../../../pop-api", 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 } -enumflags2 = { version = "0.7.7" } [lib] path = "lib.rs" @@ -21,7 +20,6 @@ std = [ "pop-api/std", "scale/std", "scale-info/std", - "enumflags2/std", ] ink-as-dependency = [] e2e-tests = [] From bc2066c2b11c449ac50f5035f3a8ed492adae802 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Thu, 25 Apr 2024 15:47:36 +0900 Subject: [PATCH 04/11] rename test --- pop-api/examples/nfts/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index a1c2a45e..36cdda66 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -102,7 +102,7 @@ mod pop_api_nfts { #[ink::test] fn default_works() { - PopApiExtensionDemo::new(); + Nfts::new(); } } } From c339caaec6ac5639393d712a1698bfd35a2ae704 Mon Sep 17 00:00:00 2001 From: Alejandro Martinez Andres <11448715+al3mart@users.noreply.github.com> Date: Fri, 26 Apr 2024 04:36:14 +0200 Subject: [PATCH 05/11] typo(pop-api): minor typo in comment --- pop-api/src/v0/nfts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 9d608310..3db08cd1 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -807,7 +807,7 @@ mod types { pub default_item_settings: ItemSettings, } - /// Mint type. Can the NFT be create by anyone, or only the creator of the collection, + /// Mint type. Can the NFT be created by anyone, or only the creator of the collection, /// or only by wallets that already hold an NFT from a certain collection? /// The ownership of a privately minted NFT is still publicly visible. #[derive(Encode)] From 662d28e0ed39f9620ed556c9eea79c21fc0e2c54 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Fri, 26 Apr 2024 12:51:22 +0900 Subject: [PATCH 06/11] check that the collection has been created --- runtime/devnet/src/extensions.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index 45769593..e471a48e 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -530,6 +530,9 @@ mod tests { log::debug!("result: {:?}", result); } + // check that the nft collection was created + assert_eq!(Nfts::collection_owner(0), Some(addr.into())); + // // check for revert assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); }); From ad90df792b002042eb8084cde69f1ff2a82ab4aa Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Fri, 26 Apr 2024 17:11:05 +0900 Subject: [PATCH 07/11] testing --- pop-api/src/v0/nfts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 3db08cd1..b449fb3a 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -669,7 +669,7 @@ impl From for Error { fn from(error: PopApiError) -> Self { match error { PopApiError::Nfts(e) => e, - _ => panic!("expected nfts error"), + _ => panic!(e) } } } From 66c3d979ccd72a635d3ed5a45a7a44becacc7d97 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Fri, 26 Apr 2024 17:13:02 +0900 Subject: [PATCH 08/11] testing --- pop-api/src/v0/nfts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index b449fb3a..61f6455b 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -669,7 +669,7 @@ impl From for Error { fn from(error: PopApiError) -> Self { match error { PopApiError::Nfts(e) => e, - _ => panic!(e) + _ => panic!("{:?}", e), } } } From 02b215222542ceca6887174a0fbb230299a5ff61 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Fri, 26 Apr 2024 17:16:37 +0900 Subject: [PATCH 09/11] revert testing --- pop-api/src/v0/nfts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 61f6455b..3db08cd1 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -669,7 +669,7 @@ impl From for Error { fn from(error: PopApiError) -> Self { match error { PopApiError::Nfts(e) => e, - _ => panic!("{:?}", e), + _ => panic!("expected nfts error"), } } } From 5568120d4425945d0a1779072f4f0a86ecc8bcf1 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Mon, 29 Apr 2024 16:36:28 +0900 Subject: [PATCH 10/11] read collection --- pop-api/examples/nfts/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pop-api/examples/nfts/lib.rs b/pop-api/examples/nfts/lib.rs index 36cdda66..e9d97a99 100755 --- a/pop-api/examples/nfts/lib.rs +++ b/pop-api/examples/nfts/lib.rs @@ -94,6 +94,14 @@ mod pop_api_nfts { ink::env::debug_println!("Nfts::mint_through_runtime end"); Ok(()) } + + #[ink(message)] + pub fn read_collection(&self, collection_id: u32) -> Result<(), ContractError> { + ink::env::debug_println!("Nfts::read_collection: collection_id: {:?}", collection_id); + let collection = pop_api::nfts::collection(collection_id)?; + ink::env::debug_println!("Nfts::read_collection: collection: {:?}", collection); + Ok(()) + } } #[cfg(test)] From 63a945297929ed2ead1456cc4a78c19cc0436163 Mon Sep 17 00:00:00 2001 From: Bruno Galvao Date: Tue, 30 Apr 2024 13:47:26 +0900 Subject: [PATCH 11/11] add test for reading collection --- runtime/devnet/src/extensions.rs | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/runtime/devnet/src/extensions.rs b/runtime/devnet/src/extensions.rs index e471a48e..89c070ee 100644 --- a/runtime/devnet/src/extensions.rs +++ b/runtime/devnet/src/extensions.rs @@ -531,10 +531,35 @@ mod tests { } // check that the nft collection was created - assert_eq!(Nfts::collection_owner(0), Some(addr.into())); + assert_eq!(Nfts::collection_owner(0), Some(addr.clone().into())); - // // check for revert - assert!(!result.result.unwrap().did_revert(), "Contract reverted!"); + // test reading the collection + let function = function_selector("read_collection"); + + let params = [function, 0.encode()].concat(); + + let result = Contracts::bare_call( + ALICE, + addr.clone(), + 0, + Weight::from_parts(100_000_000_000, 3 * 1024 * 1024), + None, + params, + DEBUG_OUTPUT, + pallet_contracts::CollectEvents::Skip, + pallet_contracts::Determinism::Enforced, + ); + + if DEBUG_OUTPUT == pallet_contracts::DebugInfo::UnsafeDebug { + log::debug!( + "Contract debug buffer - {:?}", + String::from_utf8(result.debug_message.clone()) + ); + log::debug!("result: {:?}", result); + } + + // assert that the collection was read successfully + assert_eq!(result.result.clone().unwrap().data, vec![1, 1]); }); }