Skip to content

Commit

Permalink
Add test for using fallible constructors through ContractRefs
Browse files Browse the repository at this point in the history
  • Loading branch information
HCastano committed Dec 7, 2022
1 parent b720ede commit 75cad9e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/lang-err-integration-tests/contract-ref/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ mod contract_ref {
Self { flipper }
}

#[ink(constructor)]
pub fn try_new(version: u32, flipper_code_hash: Hash) -> Self {
let salt = version.to_le_bytes();
let flipper = FlipperRef::try_new(true)
.endowment(0)
.code_hash(flipper_code_hash)
.salt_bytes(salt)
.instantiate_fallible()
.unwrap_or_else(|error| {
panic!("Received an error from the Contracts pallet while instantiating Flipper {:?}", error)
})
.unwrap_or_else(|error| {
panic!("Received an error from the Flipper constructor while instantiating Flipper {:?}", error)
});

Self { flipper }
}

#[ink(message)]
pub fn flip(&mut self) {
self.flipper.flip();
Expand Down Expand Up @@ -108,5 +126,36 @@ mod contract_ref {

Ok(())
}

#[ink_e2e::test(additional_contracts = "../integration-flipper/Cargo.toml")]
async fn e2e_fallible_ref_can_be_instantiated(
mut client: ink_e2e::Client<C, E>,
) -> E2EResult<()> {
let flipper_hash = client
.upload("integration_flipper", &ink_e2e::bob(), None)
.await
.expect("uploading `flipper` failed")
.code_hash;

let constructor = ContractRefRef::try_new(0, flipper_hash);
let contract_acc_id = client
.instantiate("contract_ref", &ink_e2e::bob(), constructor, 0, None)
.await
.expect("instantiate failed")
.account_id;

let get_check = build_message::<ContractRefRef>(contract_acc_id.clone())
.call(|contract| contract.get_check());
let get_call_result = client
.call(&ink_e2e::bob(), get_check, 0, None)
.await
.expect("Calling `get_check` failed");
let initial_value = get_call_result
.value
.expect("Input is valid, call must not fail.");
assert!(initial_value);

Ok(())
}
}
}
15 changes: 15 additions & 0 deletions examples/lang-err-integration-tests/integration-flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ pub mod integration_flipper {
value: bool,
}

#[derive(scale::Encode, scale::Decode, Debug)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct FlipperError;

impl Flipper {
/// Creates a new integration_flipper smart contract initialized with the given value.
#[ink(constructor)]
Expand All @@ -25,6 +29,17 @@ pub mod integration_flipper {
Self::new(Default::default())
}

/// Attemps to create a new integration_flipper smart contract initialized with the given
/// value.
#[ink(constructor)]
pub fn try_new(succeed: bool) -> Result<Self, FlipperError> {
if succeed {
Ok(Self::new(true))
} else {
Err(FlipperError)
}
}

/// Flips the current value of the Flipper's boolean.
#[ink(message)]
pub fn flip(&mut self) {
Expand Down

0 comments on commit 75cad9e

Please sign in to comment.