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 18 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
68 changes: 64 additions & 4 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ use crate::{
},
call::{
utils::ReturnType,
Call,
CallParams,
CreateParams,
DelegateCall,
},
engine::{
EnvInstance,
Expand All @@ -34,6 +36,7 @@ use crate::{
HashOutput,
},
topics::Topics,
types::Gas,
Environment,
Result,
};
Expand Down Expand Up @@ -72,7 +75,7 @@ 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<T>(gas: Gas) -> T::Balance
where
T: Environment,
{
Expand All @@ -86,7 +89,7 @@ where
/// # Errors
///
/// If the returned value cannot be properly decoded.
pub fn gas_left<T>() -> u64
pub fn gas_left<T>() -> Gas
where
T: Environment,
{
Expand Down Expand Up @@ -232,7 +235,10 @@ pub fn clear_contract_storage(key: &Key) {
/// - If arguments passed to the called contract message are invalid.
/// - If the called contract execution has trapped.
/// - If the called contract ran out of gas upon execution.
pub fn invoke_contract<T, Args>(params: &CallParams<T, Args, ()>) -> Result<()>
#[allow(clippy::type_complexity)]
pub fn invoke_contract<T, Args>(
params: &CallParams<T, Call<T, T::AccountId, Gas, T::Balance>, Args, ()>,
) -> Result<()>
where
T: Environment,
Args: scale::Encode,
Expand All @@ -242,6 +248,32 @@ where
})
}

/// Invokes a contract message via delegate call.
///
/// # Note
///
/// - Prefer using this over [`eval_contract_delegate`] if possible. [`invoke_contract_delegate`]
HCastano marked this conversation as resolved.
Show resolved Hide resolved
/// will generally have a better performance since it won't try to fetch any results.
/// - This is a low level way to invoke 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<T, Args>(
params: &CallParams<T, DelegateCall<T, T::Hash>, Args, ()>,
) -> Result<()>
where
T: Environment,
Args: scale::Encode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::invoke_contract_delegate::<T, Args>(instance, params)
})
}

/// Evaluates a contract message and returns its result.
///
/// # Note
Expand All @@ -257,7 +289,10 @@ where
/// - 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 eval_contract<T, Args, R>(params: &CallParams<T, Args, ReturnType<R>>) -> Result<R>
#[allow(clippy::type_complexity)]
pub fn eval_contract<T, Args, R>(
params: &CallParams<T, Call<T, T::AccountId, Gas, T::Balance>, Args, ReturnType<R>>,
) -> Result<R>
where
T: Environment,
Args: scale::Encode,
Expand All @@ -268,6 +303,31 @@ where
})
}

/// Evaluates 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 eval_contract_delegate<T, Args, R>(
params: &CallParams<T, DelegateCall<T, T::Hash>, Args, ReturnType<R>>,
) -> Result<R>
where
T: Environment,
Args: scale::Encode,
R: scale::Decode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::eval_contract_delegate::<T, Args, R>(instance, params)
})
}

/// Instantiates another contract.
///
/// # Note
Expand Down
41 changes: 39 additions & 2 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use crate::{
call::{
utils::ReturnType,
Call,
CallParams,
CreateParams,
DelegateCall,
},
hash::{
CryptoHash,
Expand Down Expand Up @@ -364,9 +366,24 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`invoke_contract`][`crate::invoke_contract`]
#[allow(clippy::type_complexity)]
fn invoke_contract<T, Args>(
&mut self,
call_data: &CallParams<T, Args, ()>,
call_data: &CallParams<T, Call<T, T::AccountId, u64, T::Balance>, Args, ()>,
) -> Result<()>
where
T: Environment,
Args: scale::Encode;

/// Invokes a contract message via delegate call.
///
///
/// # Note
///
/// For more details visit: [`invoke_contract_delegate`][`crate::invoke_contract_delegate`]
fn invoke_contract_delegate<T, Args>(
&mut self,
call_data: &CallParams<T, DelegateCall<T, T::Hash>, Args, ()>,
) -> Result<()>
where
T: Environment,
Expand All @@ -377,9 +394,29 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`eval_contract`][`crate::eval_contract`]
#[allow(clippy::type_complexity)]
fn eval_contract<T, Args, R>(
&mut self,
call_data: &CallParams<T, Args, ReturnType<R>>,
call_data: &CallParams<
T,
Call<T, T::AccountId, u64, T::Balance>,
Args,
ReturnType<R>,
>,
) -> Result<R>
where
T: Environment,
Args: scale::Encode,
R: scale::Decode;

/// Evaluates a contract message via delegate call and returns its result.
///
/// # Note
///
/// For more details visit: [`eval_contract_delegate`][`crate::eval_contract_delegate`]
fn eval_contract_delegate<T, Args, R>(
&mut self,
call_data: &CallParams<T, DelegateCall<T, T::Hash>, Args, ReturnType<R>>,
) -> Result<R>
where
T: Environment,
Expand Down
Loading