Skip to content

Commit

Permalink
Unify fallible and non fallible instantiate methods (#1591)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Hernando Castano <[email protected]>
  • Loading branch information
3 people authored Jan 24, 2023
1 parent 0ef7755 commit 59bbd36
Show file tree
Hide file tree
Showing 36 changed files with 813 additions and 644 deletions.
2 changes: 1 addition & 1 deletion crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ink = { version = "4.0.0-beta", path = "../ink" }
ink_env = { version = "4.0.0-beta", path = "../env" }
ink_primitives = { version = "4.0.0-beta", path = "../primitives" }

contract-metadata = { version = "2.0.0-beta.1" }
contract-metadata = { version = "2.0.0-rc" }
impl-serde = { version = "0.3.1", default-features = false }
jsonrpsee = { version = "0.16.0", features = ["ws-client"] }
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
Expand Down
12 changes: 8 additions & 4 deletions crates/e2e/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use scale::Encode;

/// The type returned from `ContractRef` constructors, partially initialized with the execution
/// input arguments.
pub type CreateBuilderPartial<E, Args, R> = CreateBuilder<
pub type CreateBuilderPartial<E, ContractRef, Args, R> = CreateBuilder<
E,
ContractRef,
Unset<<E as Environment>::Hash>,
Unset<u64>,
Unset<<E as Environment>::Balance>,
Expand All @@ -43,9 +44,12 @@ pub type CreateBuilderPartial<E, Args, R> = CreateBuilder<
>;

/// Get the encoded constructor arguments from the partially initialized `CreateBuilder`
pub fn constructor_exec_input<E: Environment, Args: Encode, R>(
builder: CreateBuilderPartial<E, Args, R>,
) -> Vec<u8> {
pub fn constructor_exec_input<E, ContractRef, Args: Encode, R>(
builder: CreateBuilderPartial<E, ContractRef, Args, R>,
) -> Vec<u8>
where
E: Environment,
{
// set all the other properties to default values, we only require the `exec_input`.
builder
.endowment(0u32.into())
Expand Down
12 changes: 6 additions & 6 deletions crates/e2e/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ where
/// Calling this function multiple times is idempotent, the contract is
/// newly instantiated each time using a unique salt. No existing contract
/// instance is reused!
pub async fn instantiate<Args, R>(
pub async fn instantiate<ContractRef, Args, R>(
&mut self,
contract_name: &str,
signer: &Signer<C>,
constructor: CreateBuilderPartial<E, Args, R>,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<InstantiationResult<C, E>, Error<C, E>>
Expand All @@ -374,11 +374,11 @@ where
}

/// Dry run contract instantiation using the given constructor.
pub async fn instantiate_dry_run<Args, R>(
pub async fn instantiate_dry_run<ContractRef, Args, R>(
&mut self,
contract_name: &str,
signer: &Signer<C>,
constructor: CreateBuilderPartial<E, Args, R>,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> ContractInstantiateResult<C::AccountId, E::Balance>
Expand Down Expand Up @@ -406,11 +406,11 @@ where
}

/// Executes an `instantiate_with_code` call and captures the resulting events.
async fn exec_instantiate<Args, R>(
async fn exec_instantiate<ContractRef, Args, R>(
&mut self,
signer: &Signer<C>,
code: Vec<u8>,
constructor: CreateBuilderPartial<E, Args, R>,
constructor: CreateBuilderPartial<E, ContractRef, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<InstantiationResult<C, E>, Error<C, E>>
Expand Down
3 changes: 3 additions & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ secp256k1 = { version = "0.26.0", features = ["recovery", "global-context"], opt
# Never use this crate outside the off-chain environment!
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
ink = { path = "../ink" }

[features]
default = ["std"]
std = [
Expand Down
44 changes: 8 additions & 36 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ use crate::{
call::{
Call,
CallParams,
ConstructorReturnType,
CreateParams,
DelegateCall,
FromAccountId,
},
engine::{
EnvInstance,
Expand Down Expand Up @@ -323,50 +325,20 @@ where
/// - If the instantiation process runs out of gas.
/// - If given insufficient endowment.
/// - If the returned account ID failed to decode properly.
pub fn instantiate_contract<E, Args, Salt, R>(
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_contract::<E, Args, Salt, R>(instance, params)
})
}

/// Attempts to instantiate another contract, returning the instantiation result back to the
/// caller.
///
/// # Note
///
/// This is a low level way to instantiate another smart contract.
///
/// Prefer to use methods on a `ContractRef` or the [`CreateBuilder`](`crate::call::CreateBuilder`)
/// through [`build_create`](`crate::call::build_create`) instead.
///
/// # Errors
///
/// - If the code hash is invalid.
/// - If the arguments passed to the instantiation process are invalid.
/// - If the instantiation process traps.
/// - If the instantiation process runs out of gas.
/// - If given insufficient endowment.
/// - If the returned account ID failed to decode properly.
pub fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
params: &CreateParams<E, Args, Salt, R>,
pub fn instantiate_contract<E, ContractRef, Args, Salt, R>(
params: &CreateParams<E, ContractRef, Args, Salt, R>,
) -> Result<
ink_primitives::ConstructorResult<core::result::Result<E::AccountId, ContractError>>,
ink_primitives::ConstructorResult<<R as ConstructorReturnType<ContractRef>>::Output>,
>
where
E: Environment,
ContractRef: FromAccountId<E>,
Args: scale::Encode,
Salt: AsRef<[u8]>,
ContractError: scale::Decode,
R: ConstructorReturnType<ContractRef>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_fallible_contract::<E, Args, Salt, R, ContractError>(
TypedEnvBackend::instantiate_contract::<E, ContractRef, Args, Salt, R>(
instance, params,
)
})
Expand Down
26 changes: 7 additions & 19 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ use crate::{
call::{
Call,
CallParams,
ConstructorReturnType,
CreateParams,
DelegateCall,
FromAccountId,
},
hash::{
CryptoHash,
Expand Down Expand Up @@ -439,34 +441,20 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`instantiate_contract`][`crate::instantiate_contract`]
fn instantiate_contract<E, Args, Salt, R>(
fn instantiate_contract<E, ContractRef, Args, Salt, R>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
) -> Result<ink_primitives::ConstructorResult<E::AccountId>>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>;

/// Attempts to instantiate another contract, returning the instantiation result back to the
/// caller.
///
/// # Note
///
/// For more details visit: [`instantiate_fallible_contract`][`crate::instantiate_fallible_contract`]
fn instantiate_fallible_contract<E, Args, Salt, R, ContractError>(
&mut self,
params: &CreateParams<E, Args, Salt, R>,
params: &CreateParams<E, ContractRef, Args, Salt, R>,
) -> Result<
ink_primitives::ConstructorResult<
core::result::Result<E::AccountId, ContractError>,
<R as ConstructorReturnType<ContractRef>>::Output,
>,
>
where
E: Environment,
ContractRef: FromAccountId<E>,
Args: scale::Encode,
Salt: AsRef<[u8]>,
ContractError: scale::Decode;
R: ConstructorReturnType<ContractRef>;

/// Terminates a smart contract.
///
Expand Down
Loading

0 comments on commit 59bbd36

Please sign in to comment.