Skip to content

Commit 59bbd36

Browse files
ascjonesHCastano
andauthored
Unify fallible and non fallible instantiate methods (#1591)
* Remove fallible create_builder.rs methods * WIP experiment * InstantiateResult blanket impl for T and Result * Introduce ContractRef type parameter * Fix up env access * WIP... * Make it compile * Add ContractStorage parameter * Remove commented out instantiate_fallible_contract * Convert to env Error in helper * Return Decode errors in case of invalid Result first byte * Fix impls::instantiate_contract * Remove ContractStorage generic parameter * Fix env access * Use generated constructor ref, introduces update_selector * Fix e2e * Remove commented out code * Typos * Clippy * Rename some instantiate_fallible * Restore `returns` method * Remove ContractReference Result impl * WIP implementing ConstructorReturnType * Reorder ContractRef parameter, move ContractRef and ContractEnv trait to env crate * Fmt and fix * Remove E param from build_create * Fix up build_create * Fix up e2e creat builder * Implement ContstructorReturnType for the storage_ident * Fmt * Fix envaccess test * Fully qualify Result in macro * More fully qualify Result in macro * Fix up build_create examples * Add test for different combos of Self and struct name * Fix ui test * Fmt * Remove unused assoc type * Change error fn to return Option<Result> * Remove commented out code * Fmt * ConstructorReturnType comments * Fix `contract-ref` E2E test compilation * Fix up return types after merge * Fmt * Clippy * Fix create_builder tests * Fix cross-contract compile test * Clean up some comments * Remove outdated doc * Update comment * Another comment fix * Wrap long line * Remove TODO * Bump `contract-metadata` Fixes some inconsistent errors between Clippy and `rustc` * Fix `CreateBuilder` compilation * Fix one of the doc tests * Clean up doc tests a bit Co-authored-by: Hernando Castano <hernando@hcastano.com> Co-authored-by: Hernando Castano <HCastano@users.noreply.github.com>
1 parent 0ef7755 commit 59bbd36

File tree

36 files changed

+813
-644
lines changed

36 files changed

+813
-644
lines changed

crates/e2e/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ink = { version = "4.0.0-beta", path = "../ink" }
2121
ink_env = { version = "4.0.0-beta", path = "../env" }
2222
ink_primitives = { version = "4.0.0-beta", path = "../primitives" }
2323

24-
contract-metadata = { version = "2.0.0-beta.1" }
24+
contract-metadata = { version = "2.0.0-rc" }
2525
impl-serde = { version = "0.3.1", default-features = false }
2626
jsonrpsee = { version = "0.16.0", features = ["ws-client"] }
2727
serde = { version = "1.0.137", default-features = false, features = ["derive"] }

crates/e2e/src/builders.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ use scale::Encode;
3232

3333
/// The type returned from `ContractRef` constructors, partially initialized with the execution
3434
/// input arguments.
35-
pub type CreateBuilderPartial<E, Args, R> = CreateBuilder<
35+
pub type CreateBuilderPartial<E, ContractRef, Args, R> = CreateBuilder<
3636
E,
37+
ContractRef,
3738
Unset<<E as Environment>::Hash>,
3839
Unset<u64>,
3940
Unset<<E as Environment>::Balance>,
@@ -43,9 +44,12 @@ pub type CreateBuilderPartial<E, Args, R> = CreateBuilder<
4344
>;
4445

4546
/// Get the encoded constructor arguments from the partially initialized `CreateBuilder`
46-
pub fn constructor_exec_input<E: Environment, Args: Encode, R>(
47-
builder: CreateBuilderPartial<E, Args, R>,
48-
) -> Vec<u8> {
47+
pub fn constructor_exec_input<E, ContractRef, Args: Encode, R>(
48+
builder: CreateBuilderPartial<E, ContractRef, Args, R>,
49+
) -> Vec<u8>
50+
where
51+
E: Environment,
52+
{
4953
// set all the other properties to default values, we only require the `exec_input`.
5054
builder
5155
.endowment(0u32.into())

crates/e2e/src/client.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,11 @@ where
350350
/// Calling this function multiple times is idempotent, the contract is
351351
/// newly instantiated each time using a unique salt. No existing contract
352352
/// instance is reused!
353-
pub async fn instantiate<Args, R>(
353+
pub async fn instantiate<ContractRef, Args, R>(
354354
&mut self,
355355
contract_name: &str,
356356
signer: &Signer<C>,
357-
constructor: CreateBuilderPartial<E, Args, R>,
357+
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
358358
value: E::Balance,
359359
storage_deposit_limit: Option<E::Balance>,
360360
) -> Result<InstantiationResult<C, E>, Error<C, E>>
@@ -374,11 +374,11 @@ where
374374
}
375375

376376
/// Dry run contract instantiation using the given constructor.
377-
pub async fn instantiate_dry_run<Args, R>(
377+
pub async fn instantiate_dry_run<ContractRef, Args, R>(
378378
&mut self,
379379
contract_name: &str,
380380
signer: &Signer<C>,
381-
constructor: CreateBuilderPartial<E, Args, R>,
381+
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
382382
value: E::Balance,
383383
storage_deposit_limit: Option<E::Balance>,
384384
) -> ContractInstantiateResult<C::AccountId, E::Balance>
@@ -406,11 +406,11 @@ where
406406
}
407407

408408
/// Executes an `instantiate_with_code` call and captures the resulting events.
409-
async fn exec_instantiate<Args, R>(
409+
async fn exec_instantiate<ContractRef, Args, R>(
410410
&mut self,
411411
signer: &Signer<C>,
412412
code: Vec<u8>,
413-
constructor: CreateBuilderPartial<E, Args, R>,
413+
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
414414
value: E::Balance,
415415
storage_deposit_limit: Option<E::Balance>,
416416
) -> Result<InstantiationResult<C, E>, Error<C, E>>

crates/env/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ secp256k1 = { version = "0.26.0", features = ["recovery", "global-context"], opt
4949
# Never use this crate outside the off-chain environment!
5050
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }
5151

52+
[dev-dependencies]
53+
ink = { path = "../ink" }
54+
5255
[features]
5356
default = ["std"]
5457
std = [

crates/env/src/api.rs

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ use crate::{
2323
call::{
2424
Call,
2525
CallParams,
26+
ConstructorReturnType,
2627
CreateParams,
2728
DelegateCall,
29+
FromAccountId,
2830
},
2931
engine::{
3032
EnvInstance,
@@ -323,50 +325,20 @@ where
323325
/// - If the instantiation process runs out of gas.
324326
/// - If given insufficient endowment.
325327
/// - If the returned account ID failed to decode properly.
326-
pub fn instantiate_contract<E, Args, Salt, R>(
327-
params: &CreateParams<E, Args, Salt, R>,
328-
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
329-
where
330-
E: Environment,
331-
Args: scale::Encode,
332-
Salt: AsRef<[u8]>,
333-
{
334-
<EnvInstance as OnInstance>::on_instance(|instance| {
335-
TypedEnvBackend::instantiate_contract::<E, Args, Salt, R>(instance, params)
336-
})
337-
}
338-
339-
/// Attempts to instantiate another contract, returning the instantiation result back to the
340-
/// caller.
341-
///
342-
/// # Note
343-
///
344-
/// This is a low level way to instantiate another smart contract.
345-
///
346-
/// Prefer to use methods on a `ContractRef` or the [`CreateBuilder`](`crate::call::CreateBuilder`)
347-
/// through [`build_create`](`crate::call::build_create`) instead.
348-
///
349-
/// # Errors
350-
///
351-
/// - If the code hash is invalid.
352-
/// - If the arguments passed to the instantiation process are invalid.
353-
/// - If the instantiation process traps.
354-
/// - If the instantiation process runs out of gas.
355-
/// - If given insufficient endowment.
356-
/// - If the returned account ID failed to decode properly.
357-
pub fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
358-
params: &CreateParams<E, Args, Salt, R>,
328+
pub fn instantiate_contract<E, ContractRef, Args, Salt, R>(
329+
params: &CreateParams<E, ContractRef, Args, Salt, R>,
359330
) -> Result<
360-
ink_primitives::ConstructorResult<core::result::Result<E::AccountId, ContractError>>,
331+
ink_primitives::ConstructorResult<<R as ConstructorReturnType<ContractRef>>::Output>,
361332
>
362333
where
363334
E: Environment,
335+
ContractRef: FromAccountId<E>,
364336
Args: scale::Encode,
365337
Salt: AsRef<[u8]>,
366-
ContractError: scale::Decode,
338+
R: ConstructorReturnType<ContractRef>,
367339
{
368340
<EnvInstance as OnInstance>::on_instance(|instance| {
369-
TypedEnvBackend::instantiate_fallible_contract::<E, Args, Salt, R, ContractError>(
341+
TypedEnvBackend::instantiate_contract::<E, ContractRef, Args, Salt, R>(
370342
instance, params,
371343
)
372344
})

crates/env/src/backend.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ use crate::{
1616
call::{
1717
Call,
1818
CallParams,
19+
ConstructorReturnType,
1920
CreateParams,
2021
DelegateCall,
22+
FromAccountId,
2123
},
2224
hash::{
2325
CryptoHash,
@@ -439,34 +441,20 @@ pub trait TypedEnvBackend: EnvBackend {
439441
/// # Note
440442
///
441443
/// For more details visit: [`instantiate_contract`][`crate::instantiate_contract`]
442-
fn instantiate_contract<E, Args, Salt, R>(
444+
fn instantiate_contract<E, ContractRef, Args, Salt, R>(
443445
&mut self,
444-
params: &CreateParams<E, Args, Salt, R>,
445-
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
446-
where
447-
E: Environment,
448-
Args: scale::Encode,
449-
Salt: AsRef<[u8]>;
450-
451-
/// Attempts to instantiate another contract, returning the instantiation result back to the
452-
/// caller.
453-
///
454-
/// # Note
455-
///
456-
/// For more details visit: [`instantiate_fallible_contract`][`crate::instantiate_fallible_contract`]
457-
fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
458-
&mut self,
459-
params: &CreateParams<E, Args, Salt, R>,
446+
params: &CreateParams<E, ContractRef, Args, Salt, R>,
460447
) -> Result<
461448
ink_primitives::ConstructorResult<
462-
core::result::Result<E::AccountId, ContractError>,
449+
<R as ConstructorReturnType<ContractRef>>::Output,
463450
>,
464451
>
465452
where
466453
E: Environment,
454+
ContractRef: FromAccountId<E>,
467455
Args: scale::Encode,
468456
Salt: AsRef<[u8]>,
469-
ContractError: scale::Decode;
457+
R: ConstructorReturnType<ContractRef>;
470458

471459
/// Terminates a smart contract.
472460
///

0 commit comments

Comments
 (0)