Skip to content

Commit c86692f

Browse files
ascjonesHCastano
andauthored
Make cross-contract callee non-optional (#1636)
* Make cross-contract callee non-optional * clippy * Fmt * Fix * clippy * clippy * clippy * Add a similar method for `code_hash` * Fix doc tests * RustFmt * Rename top level methods to `call` and `delegate` * Fix some renames --------- Co-authored-by: Hernando Castano <hernando@hcastano.com>
1 parent b7feae2 commit c86692f

9 files changed

Lines changed: 62 additions & 97 deletions

File tree

crates/env/src/call/call_builder.rs

Lines changed: 44 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use crate::{
2828
Error,
2929
};
3030
use core::marker::PhantomData;
31-
use ink_primitives::Clear;
3231
use num_traits::Zero;
3332

3433
/// The final parameters to the cross-contract call.
@@ -71,10 +70,8 @@ where
7170
E: Environment,
7271
{
7372
/// Returns the account ID of the called contract instance.
74-
///
75-
/// Returns `None` if no account ID has been set for the call.
7673
#[inline]
77-
pub fn callee(&self) -> &Option<E::AccountId> {
74+
pub fn callee(&self) -> &E::AccountId {
7875
&self.call_type.callee
7976
}
8077

@@ -205,11 +202,9 @@ where
205202
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
206203
/// # type Balance = <DefaultEnvironment as Environment>::Balance;
207204
/// build_call::<DefaultEnvironment>()
208-
/// .call_type(
209-
/// Call::new()
210-
/// .callee(AccountId::from([0x42; 32]))
211-
/// .gas_limit(5000)
212-
/// .transferred_value(10))
205+
/// .call(AccountId::from([0x42; 32]))
206+
/// .gas_limit(5000)
207+
/// .transferred_value(10)
213208
/// .exec_input(
214209
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
215210
/// .push_arg(42u8)
@@ -241,9 +236,8 @@ where
241236
/// # };
242237
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
243238
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
244-
/// .call_type(Call::new()
245-
/// .callee(AccountId::from([0x42; 32]))
246-
/// .gas_limit(5000))
239+
/// .call_type(Call::new(AccountId::from([0x42; 32])))
240+
/// .gas_limit(5000)
247241
/// .transferred_value(10)
248242
/// .exec_input(
249243
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
@@ -270,8 +264,7 @@ where
270264
/// # use ink_primitives::Clear;
271265
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
272266
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
273-
/// .call_type(DelegateCall::new()
274-
/// .code_hash(<DefaultEnvironment as Environment>::Hash::CLEAR_HASH))
267+
/// .delegate(<DefaultEnvironment as Environment>::Hash::CLEAR_HASH)
275268
/// .exec_input(
276269
/// ExecutionInput::new(Selector::new([0xDE, 0xAD, 0xBE, 0xEF]))
277270
/// .push_arg(42u8)
@@ -306,12 +299,9 @@ where
306299
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
307300
/// # type Balance = <DefaultEnvironment as Environment>::Balance;
308301
/// let call_result = build_call::<DefaultEnvironment>()
309-
/// .call_type(
310-
/// Call::new()
311-
/// .callee(AccountId::from([0x42; 32]))
312-
/// .gas_limit(5000)
313-
/// .transferred_value(10),
314-
/// )
302+
/// .call(AccountId::from([0x42; 32]))
303+
/// .gas_limit(5000)
304+
/// .transferred_value(10)
315305
/// .try_invoke()
316306
/// .expect("Got an error from the Contract's pallet.");
317307
///
@@ -343,41 +333,26 @@ where
343333
/// The default call type for cross-contract calls. Performs a cross-contract call to `callee`
344334
/// with gas limit `gas_limit`, transferring `transferred_value` of currency.
345335
pub struct Call<E: Environment> {
346-
callee: Option<E::AccountId>,
336+
callee: E::AccountId,
347337
gas_limit: Gas,
348338
transferred_value: E::Balance,
349339
}
350340

351-
impl<E: Environment> Default for Call<E> {
352-
fn default() -> Self {
353-
Call {
354-
callee: Default::default(),
341+
impl<E: Environment> Call<E> {
342+
/// Returns a clean builder for [`Call`].
343+
pub fn new(callee: E::AccountId) -> Self {
344+
Self {
345+
callee,
355346
gas_limit: Default::default(),
356347
transferred_value: E::Balance::zero(),
357348
}
358349
}
359350
}
360351

361-
impl<E: Environment> Call<E> {
362-
/// Returns a clean builder for [`Call`].
363-
pub fn new() -> Self {
364-
Default::default()
365-
}
366-
}
367-
368352
impl<E> Call<E>
369353
where
370354
E: Environment,
371355
{
372-
/// Sets the `callee` for the current cross-contract call.
373-
pub fn callee(self, callee: E::AccountId) -> Self {
374-
Call {
375-
callee: Some(callee),
376-
gas_limit: self.gas_limit,
377-
transferred_value: self.transferred_value,
378-
}
379-
}
380-
381356
/// Sets the `gas_limit` for the current cross-contract call.
382357
pub fn gas_limit(self, gas_limit: Gas) -> Self {
383358
Call {
@@ -404,16 +379,8 @@ pub struct DelegateCall<E: Environment> {
404379

405380
impl<E: Environment> DelegateCall<E> {
406381
/// Returns a clean builder for [`DelegateCall`]
407-
pub const fn new() -> Self {
408-
DelegateCall {
409-
code_hash: E::Hash::CLEAR_HASH,
410-
}
411-
}
412-
}
413-
414-
impl<E: Environment> Default for DelegateCall<E> {
415-
fn default() -> Self {
416-
Self::new()
382+
pub const fn new(code_hash: E::Hash) -> Self {
383+
DelegateCall { code_hash }
417384
}
418385
}
419386

@@ -521,26 +488,43 @@ where
521488
}
522489
}
523490

524-
impl<E, Args, RetType> CallBuilder<E, Set<Call<E>>, Args, RetType>
491+
impl<E, CallType, Args, RetType> CallBuilder<E, Unset<CallType>, Args, RetType>
525492
where
526493
E: Environment,
527494
{
528-
/// Sets the `callee` for the current cross-contract call.
529-
pub fn callee(self, callee: E::AccountId) -> Self {
530-
let call_type = self.call_type.value();
495+
/// Prepares the `CallBuilder` for a cross-contract [`Call`].
496+
pub fn call(
497+
self,
498+
callee: E::AccountId,
499+
) -> CallBuilder<E, Set<Call<E>>, Args, RetType> {
531500
CallBuilder {
532-
call_type: Set(Call {
533-
callee: Some(callee),
534-
gas_limit: call_type.gas_limit,
535-
transferred_value: call_type.transferred_value,
536-
}),
501+
call_type: Set(Call::new(callee)),
502+
call_flags: self.call_flags,
503+
exec_input: self.exec_input,
504+
return_type: self.return_type,
505+
_phantom: Default::default(),
506+
}
507+
}
508+
509+
/// Prepares the `CallBuilder` for a cross-contract [`DelegateCall`].
510+
pub fn delegate(
511+
self,
512+
code_hash: E::Hash,
513+
) -> CallBuilder<E, Set<DelegateCall<E>>, Args, RetType> {
514+
CallBuilder {
515+
call_type: Set(DelegateCall::new(code_hash)),
537516
call_flags: self.call_flags,
538517
exec_input: self.exec_input,
539518
return_type: self.return_type,
540519
_phantom: Default::default(),
541520
}
542521
}
522+
}
543523

524+
impl<E, Args, RetType> CallBuilder<E, Set<Call<E>>, Args, RetType>
525+
where
526+
E: Environment,
527+
{
544528
/// Sets the `gas_limit` for the current cross-contract call.
545529
pub fn gas_limit(self, gas_limit: Gas) -> Self {
546530
let call_type = self.call_type.value();

crates/env/src/engine/on_chain/impls.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,7 @@ impl TypedEnvBackend for EnvInstance {
412412
{
413413
let mut scope = self.scoped_buffer();
414414
let gas_limit = params.gas_limit();
415-
let callee = params
416-
.callee()
417-
.as_ref()
418-
.expect("An account ID must be set in order to call a contract.");
419-
let enc_callee = scope.take_encoded(callee);
415+
let enc_callee = scope.take_encoded(params.callee());
420416
let enc_transferred_value = scope.take_encoded(params.transferred_value());
421417
let call_flags = params.call_flags();
422418
let enc_input = if !call_flags.forward_input() && !call_flags.clone_input() {

crates/ink/codegen/src/generator/as_dependency/call_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ impl CallBuilder<'_> {
391391
#( , #input_bindings : #input_types )*
392392
) -> #output_type {
393393
::ink::env::call::build_call::<Environment>()
394-
.call_type(::ink::env::call::Call::new().callee(::ink::ToAccountId::to_account_id(self)))
394+
.call(::ink::ToAccountId::to_account_id(self))
395395
.exec_input(
396396
::ink::env::call::ExecutionInput::new(
397397
::ink::env::call::Selector::new([ #( #selector_bytes ),* ])

crates/ink/codegen/src/generator/trait_def/call_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ impl CallBuilder<'_> {
319319
#( , #input_bindings : #input_types )*
320320
) -> Self::#output_ident {
321321
::ink::env::call::build_call::<Self::Env>()
322-
.call_type(::ink::env::call::Call::new().callee(::ink::ToAccountId::to_account_id(self)))
322+
.call(::ink::ToAccountId::to_account_id(self))
323323
.exec_input(
324324
::ink::env::call::ExecutionInput::new(
325325
::ink::env::call::Selector::new([ #( #selector_bytes ),* ])

crates/ink/src/env_access.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,7 @@ where
524524
/// pub fn invoke_contract(&self) -> i32 {
525525
/// let call_params = build_call::<DefaultEnvironment>()
526526
/// .call_type(
527-
/// Call::new()
528-
/// .callee(AccountId::from([0x42; 32]))
527+
/// Call::new(AccountId::from([0x42; 32]))
529528
/// .gas_limit(5000)
530529
/// .transferred_value(10))
531530
/// .exec_input(
@@ -588,8 +587,7 @@ where
588587
/// pub fn invoke_contract_delegate(&self) -> i32 {
589588
/// let call_params = build_call::<DefaultEnvironment>()
590589
/// .call_type(
591-
/// DelegateCall::new()
592-
/// .code_hash(<DefaultEnvironment as ink::env::Environment>::Hash::CLEAR_HASH))
590+
/// DelegateCall::new(<DefaultEnvironment as ink::env::Environment>::Hash::CLEAR_HASH))
593591
/// .exec_input(
594592
/// ExecutionInput::new(Selector::new([0xCA, 0xFE, 0xBA, 0xBE]))
595593
/// .push_arg(42u8)

examples/erc1155/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,15 +358,15 @@ mod erc1155 {
358358
{
359359
use ink::env::call::{
360360
build_call,
361-
Call,
362361
ExecutionInput,
363362
Selector,
364363
};
365364

366365
// If our recipient is a smart contract we need to see if they accept or
367366
// reject this transfer. If they reject it we need to revert the call.
368367
let result = build_call::<Environment>()
369-
.call_type(Call::new().callee(to).gas_limit(5000))
368+
.call(to)
369+
.gas_limit(5000)
370370
.exec_input(
371371
ExecutionInput::new(Selector::new(ON_ERC_1155_RECEIVED_SELECTOR))
372372
.push_arg(caller)

examples/lang-err-integration-tests/call-builder/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ mod call_builder {
2121
use ink::env::{
2222
call::{
2323
build_call,
24-
Call,
2524
ExecutionInput,
2625
Selector,
2726
},
@@ -52,7 +51,7 @@ mod call_builder {
5251
selector: [u8; 4],
5352
) -> Option<ink::LangError> {
5453
let result = build_call::<DefaultEnvironment>()
55-
.call_type(Call::new().callee(address))
54+
.call(address)
5655
.exec_input(ExecutionInput::new(Selector::new(selector)))
5756
.returns::<()>()
5857
.try_invoke()
@@ -79,7 +78,7 @@ mod call_builder {
7978
use ink::env::call::build_call;
8079

8180
build_call::<DefaultEnvironment>()
82-
.call_type(Call::new().callee(address))
81+
.call(address)
8382
.exec_input(ExecutionInput::new(Selector::new(selector)))
8483
.returns::<()>()
8584
.invoke()

examples/multisig/lib.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ mod multisig {
6767
env::{
6868
call::{
6969
build_call,
70-
Call,
7170
ExecutionInput,
7271
},
7372
CallFlags,
@@ -310,7 +309,6 @@ mod multisig {
310309
/// use ink::env::{
311310
/// call::{
312311
/// utils::ArgumentList,
313-
/// Call,
314312
/// CallParams,
315313
/// ExecutionInput,
316314
/// Selector,
@@ -536,12 +534,9 @@ mod multisig {
536534
let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID);
537535
assert!(self.env().transferred_value() == t.transferred_value);
538536
let result = build_call::<<Self as ::ink::env::ContractEnv>::Env>()
539-
.call_type(
540-
Call::new()
541-
.callee(t.callee)
542-
.gas_limit(t.gas_limit)
543-
.transferred_value(t.transferred_value),
544-
)
537+
.call(t.callee)
538+
.gas_limit(t.gas_limit)
539+
.transferred_value(t.transferred_value)
545540
.call_flags(CallFlags::default().set_allow_reentry(t.allow_reentry))
546541
.exec_input(
547542
ExecutionInput::new(t.selector.into()).push_arg(CallInput(&t.input)),
@@ -574,12 +569,9 @@ mod multisig {
574569
self.ensure_confirmed(trans_id);
575570
let t = self.take_transaction(trans_id).expect(WRONG_TRANSACTION_ID);
576571
let result = build_call::<<Self as ::ink::env::ContractEnv>::Env>()
577-
.call_type(
578-
Call::new()
579-
.callee(t.callee)
580-
.gas_limit(t.gas_limit)
581-
.transferred_value(t.transferred_value),
582-
)
572+
.call(t.callee)
573+
.gas_limit(t.gas_limit)
574+
.transferred_value(t.transferred_value)
583575
.call_flags(CallFlags::default().set_allow_reentry(t.allow_reentry))
584576
.exec_input(
585577
ExecutionInput::new(t.selector.into()).push_arg(CallInput(&t.input)),

examples/upgradeable-contracts/forward-calls/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
#[ink::contract]
1919
pub mod proxy {
20-
use ink::env::call::Call;
2120

2221
/// A simple proxy contract.
2322
#[ink(storage)]
@@ -70,12 +69,9 @@ pub mod proxy {
7069
#[ink(message, payable, selector = _)]
7170
pub fn forward(&self) -> u32 {
7271
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
73-
.call_type(
74-
Call::new()
75-
.callee(self.forward_to)
76-
.transferred_value(self.env().transferred_value())
77-
.gas_limit(0),
78-
)
72+
.call(self.forward_to)
73+
.transferred_value(self.env().transferred_value())
74+
.gas_limit(0)
7975
.call_flags(
8076
ink::env::CallFlags::default()
8177
.set_forward_input(true)

0 commit comments

Comments
 (0)