-
Notifications
You must be signed in to change notification settings - Fork 430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unify fallible and non fallible instantiate methods #1591
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks interesting 👀
crates/env/src/api.rs
Outdated
> | ||
where | ||
E: Environment, | ||
Args: scale::Encode, | ||
Salt: AsRef<[u8]>, | ||
ContractError: scale::Decode, | ||
R: InstantiateResult<R>, | ||
ContractRef: FromAccountId<E>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the E::AccountId
type directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did start with that, but would have had to implement some map
capability that does id
e.g. AccountId -> AccountId
and AccountId -> Result<(), AccountId>
and it was slightly tricky. Maybe possible but thought it was easier just to call FromAccountId
in place to get the thing working.
crates/env/src/engine/mod.rs
Outdated
let contract_err = <<R as InstantiateResult<ContractStorage>>::Error | ||
as scale::Decode>::decode(out_return_value)?; | ||
let err = <R as InstantiateResult<ContractStorage>>::err(contract_err); | ||
Ok(Ok(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I'd use the typedefs here to make it more clear as to which Result
is which
Ok(Ok(err)) | |
EnvResult::Ok(ConstructorResult::Ok(err)) |
crates/env/src/engine/mod.rs
Outdated
1 => { | ||
let contract_err = <<R as InstantiateResult<ContractStorage>>::Error | ||
as scale::Decode>::decode(out_return_value)?; | ||
let err = <R as InstantiateResult<ContractStorage>>::err(contract_err); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let err = <R as InstantiateResult<ContractStorage>>::err(contract_err); | |
let contract_err = <R as InstantiateResult<ContractStorage>>::err(contract_err); |
Codecov Report
@@ Coverage Diff @@
## hc-get-lang-error-from-create-builder #1591 +/- ##
=========================================================================
- Coverage 65.86% 65.45% -0.41%
=========================================================================
Files 205 205
Lines 6330 6285 -45
=========================================================================
- Hits 4169 4114 -55
- Misses 2161 2171 +10
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
ccf61eb
to
a7a2f7a
Compare
/// | ||
/// fn main() {} | ||
/// ``` | ||
pub trait ContractEnv { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These traits have just been copied from ink::reflect
so we can use them here in ink_env
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's still a couple things I want to review, but in general it's looking good 👍
@@ -34,7 +34,7 @@ pub mod constructors_return_value { | |||
} | |||
} | |||
|
|||
/// A constructor which reverts and fills the output buffer with an erronenously encoded | |||
/// A constructor which reverts and fills the output buffer with an erroneously encoded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lol, awkward that this was spelt erroneously 😅
/// | ||
/// fn main() {} | ||
/// ``` | ||
pub trait ContractEnv { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I don't really understand why we don't use the Environment
trait directly. E.g why not have T: Environment
instead of going through this extra wrapper?
Fixes some inconsistent errors between Clippy and `rustc`
4dd4a3f
to
a9c0223
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning up the API, looks much better now!
Context: #1512 (comment)
Since #1512
CreateBuilder
has 4 differentinstantiate
methods:instantiate
instantiate_fallible
try_instantiate
try_instantiate_faillble
The use case for this is when we have a "fallible" constructor which returns a
Result
e.g.In order to invoke this from another contract, the following is required:
Some issues with this approach:
instantiate
to call based on the return type of the constructor. Granted it should not compile with the wrong one, but still will cause some friction.instantiate
method.This PR unifies the
instantaite
andinstantiate_fallible
versions of each method call, such that the user only needs to callinstantiate
orinstantiate_fallible
, and theCreateBuilder
method will return eitherResult<ContranctRef>
or plainContractRef
, depending on the actual return type of the constructor.See this UI test for the variations of constructor return types.