Skip to content

Commit

Permalink
Merge branch 'main' into bruno/refactor-pop-api-example-contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
brunopgalvao authored May 2, 2024
2 parents c8b0e73 + bc41540 commit e48f308
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
34 changes: 33 additions & 1 deletion pop-api/examples/nfts/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,31 @@ mod nfts {
}

#[ink(message)]
pub fn mint(
pub fn create_nft_collection( &self ) -> Result<(), ContractError>{
ink::env::debug_println!("Nfts::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!("Nfts::create_nft_collection: collection created successfully.");
Ok(())
}

#[ink(message)]
pub fn mint_nft(
&mut self,
collection_id: u32,
item_id: u32,
Expand Down Expand Up @@ -70,6 +94,14 @@ mod nfts {
ink::env::debug_println!("Nfts::mint 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)]
Expand Down
4 changes: 2 additions & 2 deletions pop-api/src/v0/nfts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)]
Expand Down
89 changes: 89 additions & 0 deletions runtime/devnet/src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,95 @@ 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::<Runtime>(
"../../pop-api/examples/nfts/target/ink/pop_api_nft_example.wasm",
)
.unwrap();

let init_value = 100 * UNIT;

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 that the nft collection was created
assert_eq!(Nfts::collection_owner(0), Some(addr.clone().into()));

// 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]);
});
}

#[test]
#[ignore]
fn dispatch_nfts_mint_from_contract_works() {
Expand Down

0 comments on commit e48f308

Please sign in to comment.