Skip to content
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

CallBuilder delegated calls API #1133

Merged
merged 21 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Specifically you need to upgrade to at least the pallet version

## Added
- Added support for wildcard selectors ‒ [#1020](https://github.com/paritytech/ink/pull/1020).
- This enables writing upgradable smart contracts using the proxy/forward pattern.
- This enables writing upgradeable smart contracts using the proxy/forward pattern.
We added a new example to illustrate this ‒ the [proxy](https://github.com/paritytech/ink/tree/master/examples/proxy) example.
- Annotating a wildcard selector in traits is not supported.
- The ink! codegen now heavily relies on static type information based on traits defined in `ink_lang` ‒ [#665](https://github.com/paritytech/ink/pull/665).
Expand Down
134 changes: 81 additions & 53 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ use crate::{
TypedEnvBackend,
},
call::{
Call,
CallParams,
CreateParams,
DelegateCall,
},
engine::{
EnvInstance,
Expand All @@ -33,6 +35,7 @@ use crate::{
HashOutput,
},
topics::Topics,
types::Gas,
Environment,
Result,
};
Expand All @@ -43,12 +46,12 @@ use ink_primitives::Key;
/// # Errors
///
/// If the returned caller cannot be properly decoded.
pub fn caller<T>() -> T::AccountId
pub fn caller<E>() -> E::AccountId
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::caller::<T>(instance)
TypedEnvBackend::caller::<E>(instance)
})
}

Expand All @@ -57,12 +60,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn transferred_value<T>() -> T::Balance
pub fn transferred_value<E>() -> E::Balance
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::transferred_value::<T>(instance)
TypedEnvBackend::transferred_value::<E>(instance)
})
}

Expand All @@ -71,12 +74,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn weight_to_fee<T>(gas: u64) -> T::Balance
pub fn weight_to_fee<E>(gas: Gas) -> E::Balance
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::weight_to_fee::<T>(instance, gas)
TypedEnvBackend::weight_to_fee::<E>(instance, gas)
})
}

Expand All @@ -85,12 +88,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn gas_left<T>() -> u64
pub fn gas_left<E>() -> Gas
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::gas_left::<T>(instance)
TypedEnvBackend::gas_left::<E>(instance)
})
}

Expand All @@ -99,12 +102,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn block_timestamp<T>() -> T::Timestamp
pub fn block_timestamp<E>() -> E::Timestamp
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::block_timestamp::<T>(instance)
TypedEnvBackend::block_timestamp::<E>(instance)
})
}

Expand All @@ -117,12 +120,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn account_id<T>() -> T::AccountId
pub fn account_id<E>() -> E::AccountId
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::account_id::<T>(instance)
TypedEnvBackend::account_id::<E>(instance)
})
}

Expand All @@ -131,12 +134,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn balance<T>() -> T::Balance
pub fn balance<E>() -> E::Balance
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::balance::<T>(instance)
TypedEnvBackend::balance::<E>(instance)
})
}

Expand All @@ -145,12 +148,12 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn block_number<T>() -> T::BlockNumber
pub fn block_number<E>() -> E::BlockNumber
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::block_number::<T>(instance)
TypedEnvBackend::block_number::<E>(instance)
})
}

Expand All @@ -160,23 +163,23 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn minimum_balance<T>() -> T::Balance
pub fn minimum_balance<E>() -> E::Balance
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::minimum_balance::<T>(instance)
TypedEnvBackend::minimum_balance::<E>(instance)
})
}

/// Emits an event with the given event data.
pub fn emit_event<T, Event>(event: Event)
pub fn emit_event<E, Event>(event: Event)
where
T: Environment,
E: Environment,
Event: Topics + scale::Encode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::emit_event::<T, Event>(instance, event)
TypedEnvBackend::emit_event::<E, Event>(instance, event)
})
}

Expand Down Expand Up @@ -230,14 +233,39 @@ pub fn clear_contract_storage(key: &Key) {
/// - If the called contract execution has trapped.
/// - If the called contract ran out of gas upon execution.
/// - If the returned value failed to decode properly.
pub fn invoke_contract<T, Args, R>(params: &CallParams<T, Args, R>) -> Result<R>
pub fn invoke_contract<E, Args, R>(params: &CallParams<E, Call<E>, Args, R>) -> Result<R>
where
T: Environment,
E: Environment,
Args: scale::Encode,
R: scale::Decode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::invoke_contract::<T, Args, R>(instance, params)
TypedEnvBackend::invoke_contract::<E, Args, R>(instance, params)
})
}

/// Invokes a contract message via delegate call and returns its result.
///
/// # Note
///
/// This is a low level way to evaluate another smart contract via delegate call.
/// Prefer to use the ink! guided and type safe approach to using this.
///
/// # Errors
///
/// - If the specified code hash does not exist.
/// - If arguments passed to the called code message are invalid.
/// - If the called code execution has trapped.
pub fn invoke_contract_delegate<E, Args, R>(
params: &CallParams<E, DelegateCall<E>, Args, R>,
) -> Result<R>
where
E: Environment,
Args: scale::Encode,
R: scale::Decode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::invoke_contract_delegate::<E, Args, R>(instance, params)
})
}

Expand All @@ -256,16 +284,16 @@ 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<T, Args, Salt, C>(
params: &CreateParams<T, Args, Salt, C>,
) -> Result<T::AccountId>
pub fn instantiate_contract<E, Args, Salt, C>(
params: &CreateParams<E, Args, Salt, C>,
) -> Result<E::AccountId>
where
T: Environment,
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::instantiate_contract::<T, Args, Salt, C>(instance, params)
TypedEnvBackend::instantiate_contract::<E, Args, Salt, C>(instance, params)
})
}

Expand All @@ -279,12 +307,12 @@ where
/// This function never returns. Either the termination was successful and the
/// execution of the destroyed contract is halted. Or it failed during the termination
/// which is considered fatal and results in a trap and rollback.
pub fn terminate_contract<T>(beneficiary: T::AccountId) -> !
pub fn terminate_contract<E>(beneficiary: E::AccountId) -> !
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::terminate_contract::<T>(instance, beneficiary)
TypedEnvBackend::terminate_contract::<E>(instance, beneficiary)
})
}

Expand All @@ -302,12 +330,12 @@ where
/// - If the transfer had brought the sender's total balance below the
/// minimum balance. You need to use [`terminate_contract`] in case
/// this is your intention.
pub fn transfer<T>(destination: T::AccountId, value: T::Balance) -> Result<()>
pub fn transfer<E>(destination: E::AccountId, value: E::Balance) -> Result<()>
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::transfer::<T>(instance, destination, value)
TypedEnvBackend::transfer::<E>(instance, destination, value)
})
}

Expand Down Expand Up @@ -376,12 +404,12 @@ where
/// made afterwards), then ensure no further commitments may be made and repeatedly
/// call this on later blocks until the block number returned is later than the latest
/// commitment.
pub fn random<T>(subject: &[u8]) -> Result<(T::Hash, T::BlockNumber)>
pub fn random<E>(subject: &[u8]) -> Result<(E::Hash, E::BlockNumber)>
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::random::<T>(instance, subject)
TypedEnvBackend::random::<E>(instance, subject)
})
}

Expand Down Expand Up @@ -477,12 +505,12 @@ pub fn ecdsa_recover(
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn is_contract<T>(account: &T::AccountId) -> bool
pub fn is_contract<E>(account: &E::AccountId) -> bool
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::is_contract::<T>(instance, account)
TypedEnvBackend::is_contract::<E>(instance, account)
})
}

Expand All @@ -498,11 +526,11 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn caller_is_origin<T>() -> bool
pub fn caller_is_origin<E>() -> bool
where
T: Environment,
E: Environment,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::caller_is_origin::<T>(instance)
TypedEnvBackend::caller_is_origin::<E>(instance)
})
}
Loading