From 772514c10bde521545e6adfa18870deb2550ec7b Mon Sep 17 00:00:00 2001 From: Daanvdplas Date: Wed, 31 Jul 2024 17:33:08 +0200 Subject: [PATCH] refactor: IT create token in constructor --- pallets/api/src/fungibles/mod.rs | 2 +- .../contracts/create_token_in_constructor/lib.rs | 5 +++-- pop-api/integration-tests/src/fungibles/mod.rs | 11 +++++++++-- pop-api/integration-tests/src/fungibles/utils.rs | 9 +++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pallets/api/src/fungibles/mod.rs b/pallets/api/src/fungibles/mod.rs index 73835b17..8c2ae26b 100644 --- a/pallets/api/src/fungibles/mod.rs +++ b/pallets/api/src/fungibles/mod.rs @@ -303,7 +303,7 @@ pub mod pallet { /// /// # Parameter /// - `value` - An instance of `Read`, which specifies the type of state query and - /// the associated parameters. + /// the associated parameters. pub fn read_state(value: Read) -> Vec { use Read::*; diff --git a/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs b/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs index 3a800382..e9e5d127 100755 --- a/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs +++ b/pop-api/integration-tests/contracts/create_token_in_constructor/lib.rs @@ -22,8 +22,9 @@ mod create_token_in_constructor { #[ink(constructor, payable)] pub fn new(id: AssetId, min_balance: Balance) -> Result { let contract = Self { id }; - let account = contract.env().account_id(); - api::create(id, account, min_balance)?; + // AccountId of the contract which will be set to the owner of the fungible token. + let owner = contract.env().account_id(); + api::create(id, owner, min_balance)?; Ok(contract) } diff --git a/pop-api/integration-tests/src/fungibles/mod.rs b/pop-api/integration-tests/src/fungibles/mod.rs index ca3922b3..81a276a9 100644 --- a/pop-api/integration-tests/src/fungibles/mod.rs +++ b/pop-api/integration-tests/src/fungibles/mod.rs @@ -358,8 +358,15 @@ fn instantiate_and_create_fungible_works() { let _ = env_logger::try_init(); let contract = "contracts/create_token_in_constructor/target/ink/create_token_in_constructor.wasm"; - let addr = instantiate_and_create_fungible(contract, ASSET_ID, 1); - assert_eq!(asset_exists(addr, ASSET_ID), Ok(Assets::asset_exists(ASSET_ID))); + // Asset already exists. + create_asset(ALICE, 0, 1); + assert_eq!( + instantiate_and_create_fungible(contract, 0, 1), + Err(Module { index: 52, error: 5 }) + ); + // Successfully create an asset when instantiating the contract. + assert_ok!(instantiate_and_create_fungible(contract, ASSET_ID, 1)); + assert!(Assets::asset_exists(ASSET_ID)); }); } diff --git a/pop-api/integration-tests/src/fungibles/utils.rs b/pop-api/integration-tests/src/fungibles/utils.rs index 555bfd50..c4571aec 100644 --- a/pop-api/integration-tests/src/fungibles/utils.rs +++ b/pop-api/integration-tests/src/fungibles/utils.rs @@ -310,7 +310,7 @@ pub(super) fn instantiate_and_create_fungible( contract: &str, asset_id: AssetId, min_balance: Balance, -) -> AccountId32 { +) -> Result<(), Error> { let function = function_selector("new"); let input = [function, asset_id.encode(), min_balance.encode()].concat(); let (wasm_binary, _) = @@ -327,7 +327,8 @@ pub(super) fn instantiate_and_create_fungible( CollectEvents::Skip, ) .result - .unwrap(); - assert!(!result.result.did_revert(), "deploying contract reverted {:?}", result); - result.account_id + .expect("should work") + .result; + decoded::>(result.clone()) + .unwrap_or_else(|_| panic!("Contract reverted: {:?}", result)) }