Skip to content

Commit

Permalink
[ink_e2e] improve call API, remove build_message + callback (#1782)
Browse files Browse the repository at this point in the history
* Cherry picking changes from #1669

* Don't display verbose Debug message for module error.

* Initial conversion of erc20 to use new call

* Remove constraint from InstantiationResult

* Use 3.0.0 release

* Revert "Use 3.0.0 release"

This reverts commit e733996.

* Move instantiate generic parameter

* Update call-runtime example

* Update contract-terminate tests

* Update contract-transfer tests

* Custom allocator

* Custom environment

* e2e-call-runtime

* flipper

* lang-err-integration-tests, compiles but fails

* Don't debug log the entire dispatch error

* constructor-return-value

* contract-ref

* integration-flipper

* mapping-integration-tests

* multi-contract-caller

* set-code-hash

* wildcard-selector

* trait-dyn-cross-contract-calls

* Clippy

* Export CallBuilderFinal type alias

* CHANGELOG.md
  • Loading branch information
ascjones authored May 19, 2023
1 parent 49bd8d3 commit 425d453
Show file tree
Hide file tree
Showing 24 changed files with 374 additions and 567 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Schema generation - [#1765](https://github.com/paritytech/ink/pull/1765)

### Changed
- E2E: improve call API, remove `build_message` + callback - [#1782](https://github.com/paritytech/ink/pull/1782)

## Version 4.2.0

### Added
Expand Down
132 changes: 0 additions & 132 deletions crates/e2e/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use ink::codegen::TraitCallBuilder;
use ink_env::{
call::{
utils::{
ReturnType,
Set,
Unset,
},
Call,
CallBuilder,
CreateBuilder,
ExecutionInput,
FromAccountId,
},
Environment,
};
Expand Down Expand Up @@ -59,131 +55,3 @@ where
.exec_input()
.encode()
}

/// Captures the encoded input for an `ink!` message call, together with the account id of
/// the contract being called.
#[derive(Debug, Clone)]
pub struct Message<E: Environment, RetType> {
account_id: E::AccountId,
exec_input: Vec<u8>,
_return_type: std::marker::PhantomData<RetType>,
}

impl<E, RetType> Message<E, RetType>
where
E: Environment,
{
/// Create a new message from the given account id and encoded message data.
pub fn new(account_id: E::AccountId, exec_input: Vec<u8>) -> Self {
Self {
account_id,
exec_input,
_return_type: Default::default(),
}
}

/// The account id of the contract being called to invoke the message.
pub fn account_id(&self) -> &E::AccountId {
&self.account_id
}

/// The encoded message data, comprised of the selector and the message arguments.
pub fn exec_input(&self) -> &[u8] {
&self.exec_input
}
}

/// Convenience method for building messages for the default environment.
///
/// # Note
///
/// This is hardcoded to [`ink_env::DefaultEnvironment`] so the user does not have to
/// specify this generic parameter, which currently is hardcoded in the E2E testing suite.
pub fn build_message<Ref>(
account_id: <ink_env::DefaultEnvironment as Environment>::AccountId,
) -> MessageBuilder<ink_env::DefaultEnvironment, Ref>
where
Ref: TraitCallBuilder + FromAccountId<ink_env::DefaultEnvironment>,
{
MessageBuilder::from_account_id(account_id)
}

/// Build messages using a contract ref.
pub struct MessageBuilder<E: Environment, Ref> {
account_id: E::AccountId,
contract_ref: Ref,
}

impl<E, Ref> MessageBuilder<E, Ref>
where
E: Environment,
Ref: TraitCallBuilder + FromAccountId<E>,
{
/// Create a new [`MessageBuilder`] to invoke a message on the given contract.
pub fn from_account_id(account_id: E::AccountId) -> Self {
let contract_ref = <Ref as FromAccountId<E>>::from_account_id(account_id.clone());
Self {
account_id,
contract_ref,
}
}

/// Build an encoded call for a message from a [`CallBuilder`] instance returned from
/// a contract ref method.
///
/// This utilizes the generated message inherent methods on the contract ref
/// implementation, which returns a [`CallBuilder`] initialized with the selector
/// and message arguments.
///
/// # Example
/// ```
/// # #[ink::contract]
/// # pub mod my_contract {
/// #
/// # #[ink(storage)]
/// # pub struct MyContract { }
/// #
/// # impl MyContract {
/// # #[ink(constructor)]
/// # pub fn new() -> Self {
/// # Self {}
/// # }
/// #
/// # #[ink(message)]
/// # pub fn message(&self) {}
/// # }
/// # }
/// #
/// # fn message_builder_doc_test() {
/// # use my_contract::MyContractRef;
/// # let contract_acc_id = ink_primitives::AccountId::from([0x00; 32]);
/// ink_e2e::MessageBuilder::<ink::env::DefaultEnvironment, MyContractRef>::from_account_id(
/// contract_acc_id,
/// )
/// .call(|contract| contract.message());
/// # }
/// ```
pub fn call<F, Args, RetType>(mut self, mut message: F) -> Message<E, RetType>
where
F: FnMut(
&mut <Ref as TraitCallBuilder>::Builder,
) -> CallBuilder<
E,
Set<Call<E>>,
Set<ExecutionInput<Args>>,
Set<ReturnType<RetType>>,
>,
Args: scale::Encode,
RetType: scale::Decode,
{
let call_builder = <Ref as TraitCallBuilder>::call_mut(&mut self.contract_ref);
let builder = message(call_builder);
let exec_input = builder.params().exec_input().encode();
Message {
account_id: self.account_id.clone(),
exec_input,
_return_type: Default::default(),
}
}
}
Loading

0 comments on commit 425d453

Please # to comment.