Skip to content

Commit

Permalink
refactor: IT create token in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Daanvdplas committed Jul 31, 2024
1 parent 584b9c6 commit 772514c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pallets/api/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ pub mod pallet {
///
/// # Parameter
/// - `value` - An instance of `Read<T>`, which specifies the type of state query and
/// the associated parameters.
/// the associated parameters.
pub fn read_state(value: Read<T>) -> Vec<u8> {
use Read::*;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ mod create_token_in_constructor {
#[ink(constructor, payable)]
pub fn new(id: AssetId, min_balance: Balance) -> Result<Self> {
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)
}

Expand Down
11 changes: 9 additions & 2 deletions pop-api/integration-tests/src/fungibles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
}

Expand Down
9 changes: 5 additions & 4 deletions pop-api/integration-tests/src/fungibles/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, _) =
Expand All @@ -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<(), Error>>(result.clone())
.unwrap_or_else(|_| panic!("Contract reverted: {:?}", result))
}

0 comments on commit 772514c

Please sign in to comment.