Skip to content

Commit

Permalink
We forgot to add an ability to alter the arguments of the call. (#1169)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgreenx authored Mar 8, 2022
1 parent f4252a0 commit 184ec88
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 15 deletions.
85 changes: 79 additions & 6 deletions crates/env/src/call/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ where
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
/// # type Balance = <DefaultEnvironment as Environment>::Balance;
/// build_call::<DefaultEnvironment>()
/// .set_call_type(Call::new().callee(AccountId::from([0x42; 32])).gas_limit(5000).transferred_value(10))
/// .call_type(
/// Call::new()
/// .callee(AccountId::from([0x42; 32]))
/// .gas_limit(5000)
/// .transferred_value(10))
/// .exec_input(
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
/// .push_arg(42u8)
Expand Down Expand Up @@ -193,10 +197,10 @@ where
/// # };
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
/// .set_call_type(Call::new()
/// .call_type(Call::new()
/// .callee(AccountId::from([0x42; 32]))
/// .gas_limit(5000)
/// .transferred_value(10))
/// .gas_limit(5000))
/// .transferred_value(10)
/// .exec_input(
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
/// .push_arg(42u8)
Expand All @@ -223,7 +227,7 @@ where
/// # };
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
/// .set_call_type(DelegateCall::new()
/// .call_type(DelegateCall::new()
/// .code_hash(<DefaultEnvironment as Environment>::Hash::clear()))
/// .exec_input(
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
Expand Down Expand Up @@ -358,7 +362,7 @@ where
/// The type of the call.
#[inline]
#[must_use]
pub fn set_call_type<NewCallType>(
pub fn call_type<NewCallType>(
self,
call_type: NewCallType,
) -> CallBuilder<E, Set<NewCallType>, Args, RetType> {
Expand Down Expand Up @@ -435,6 +439,75 @@ where
}
}

impl<E, Args, RetType> CallBuilder<E, Set<Call<E>>, Args, RetType>
where
E: Environment,
{
/// Sets the `callee` for the current cross-contract call.
pub fn callee(self, callee: E::AccountId) -> Self {
let call_type = self.call_type.value();
CallBuilder {
call_type: Set(Call {
callee,
gas_limit: call_type.gas_limit,
transferred_value: call_type.transferred_value,
}),
call_flags: self.call_flags,
exec_input: self.exec_input,
return_type: self.return_type,
_phantom: Default::default(),
}
}

/// Sets the `gas_limit` for the current cross-contract call.
pub fn gas_limit(self, gas_limit: Gas) -> Self {
let call_type = self.call_type.value();
CallBuilder {
call_type: Set(Call {
callee: call_type.callee,
gas_limit,
transferred_value: call_type.transferred_value,
}),
call_flags: self.call_flags,
exec_input: self.exec_input,
return_type: self.return_type,
_phantom: Default::default(),
}
}

/// Sets the `transferred_value` for the current cross-contract call.
pub fn transferred_value(self, transferred_value: E::Balance) -> Self {
let call_type = self.call_type.value();
CallBuilder {
call_type: Set(Call {
callee: call_type.callee,
gas_limit: call_type.gas_limit,
transferred_value,
}),
call_flags: self.call_flags,
exec_input: self.exec_input,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}

impl<E, Args, RetType> CallBuilder<E, Set<DelegateCall<E>>, Args, RetType>
where
E: Environment,
{
/// Sets the `code_hash` to perform a delegate call with.
pub fn code_hash(self, code_hash: E::Hash) -> Self {
CallBuilder {
call_type: Set(DelegateCall { code_hash }),
call_flags: self.call_flags,
exec_input: self.exec_input,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}

impl<E, Args, RetType>
CallBuilder<E, Set<Call<E>>, Set<ExecutionInput<Args>>, Set<ReturnType<RetType>>>
where
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl CallBuilder<'_> {
#( , #input_bindings : #input_types )*
) -> #output_type {
::ink_env::call::build_call::<Environment>()
.set_call_type(::ink_env::call::Call::new().callee(::ink_lang::ToAccountId::to_account_id(self)))
.call_type(::ink_env::call::Call::new().callee(::ink_lang::ToAccountId::to_account_id(self)))
.exec_input(
::ink_env::call::ExecutionInput::new(
::ink_env::call::Selector::new([ #( #selector_bytes ),* ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ impl CallBuilder<'_> {
#( , #input_bindings : #input_types )*
) -> Self::#output_ident {
::ink_env::call::build_call::<Self::Env>()
.set_call_type(::ink_env::call::Call::new().callee(::ink_lang::ToAccountId::to_account_id(self)))
.call_type(::ink_env::call::Call::new().callee(::ink_lang::ToAccountId::to_account_id(self)))
.exec_input(
::ink_env::call::ExecutionInput::new(
::ink_env::call::Selector::new([ #( #selector_bytes ),* ])
Expand Down
4 changes: 2 additions & 2 deletions crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ where
/// #[ink(message)]
/// pub fn invoke_contract(&self) -> i32 {
/// let call_params = build_call::<DefaultEnvironment>()
/// .set_call_type(
/// .call_type(
/// Call::new()
/// .callee(AccountId::from([0x42; 32]))
/// .gas_limit(5000)
Expand Down Expand Up @@ -594,7 +594,7 @@ where
/// #[ink(message)]
/// pub fn invoke_contract_delegate(&self) -> i32 {
/// let call_params = build_call::<DefaultEnvironment>()
/// .set_call_type(
/// .call_type(
/// DelegateCall::new()
/// .code_hash(<DefaultEnvironment as ink_env::Environment>::Hash::clear()))
/// .exec_input(
Expand Down
2 changes: 1 addition & 1 deletion examples/erc1155/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ mod erc1155 {
// If our recipient is a smart contract we need to see if they accept or
// reject this transfer. If they reject it we need to revert the call.
let params = build_call::<Environment>()
.set_call_type(Call::new().callee(to).gas_limit(5000))
.call_type(Call::new().callee(to).gas_limit(5000))
.exec_input(
ExecutionInput::new(Selector::new(ON_ERC_1155_RECEIVED_SELECTOR))
.push_arg(caller)
Expand Down
4 changes: 2 additions & 2 deletions examples/multisig/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ mod multisig {
let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID);
assert!(self.env().transferred_value() == t.transferred_value);
let result = build_call::<<Self as ::ink_lang::reflect::ContractEnv>::Env>()
.set_call_type(
.call_type(
Call::new()
.callee(t.callee)
.gas_limit(t.gas_limit)
Expand Down Expand Up @@ -548,7 +548,7 @@ mod multisig {
self.ensure_confirmed(trans_id);
let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID);
let result = build_call::<<Self as ::ink_lang::reflect::ContractEnv>::Env>()
.set_call_type(
.call_type(
Call::new()
.callee(t.callee)
.gas_limit(t.gas_limit)
Expand Down
2 changes: 1 addition & 1 deletion examples/proxy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub mod proxy {
#[ink(message, payable, selector = _)]
pub fn forward(&self) -> u32 {
ink_env::call::build_call::<ink_env::DefaultEnvironment>()
.set_call_type(
.call_type(
Call::new()
.callee(self.forward_to)
.transferred_value(self.env().transferred_value())
Expand Down
2 changes: 1 addition & 1 deletion examples/upgradeable-contract/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub mod upgradeable_contract {
#[ink(message, payable, selector = _)]
pub fn forward(&self) -> u32 {
ink_env::call::build_call::<ink_env::DefaultEnvironment>()
.set_call_type(DelegateCall::new().code_hash(self.proxy.forward_to))
.call_type(DelegateCall::new().code_hash(self.proxy.forward_to))
.call_flags(
ink_env::CallFlags::default()
// We don't plan to use the input data after the delegated call, so the
Expand Down

0 comments on commit 184ec88

Please sign in to comment.