Skip to content
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[Unreleased]

### Changed

- Refactor contract ref generation and add automatic re-exporting - [#2710](https://github.com/use-ink/ink/pull/2710)

## Version 6.0.0-beta

### Added
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/call/create_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ where
/// # pub fn message(&self) {}
/// # }
/// # }
/// # use contract::MyContractRef;
/// #
/// let my_contract: MyContractRef = build_create::<MyContractRef>()
/// .code_hash(ink::H256::from([0x42; 32]))
/// .endowment(25.into())
Expand Down Expand Up @@ -431,7 +431,7 @@ where
/// # pub fn message(&self) {}
/// # }
/// # }
/// # use contract::{MyContractRef, ConstructorError};
/// # use contract::ConstructorError;
/// let my_contract: MyContractRef = build_create::<MyContractRef>()
/// .code_hash(ink::H256::from([0x42; 32]))
/// .endowment(25.into())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl GenerateCode for CallBuilder<'_> {
let call_builder_impls = self.generate_call_forwarder_impls();
let call_builder_inherent_impls = self.generate_call_builder_inherent_impls();
quote! {
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
const _: () = {
#call_builder_struct
#auxiliary_trait_impls
Expand Down Expand Up @@ -124,6 +125,7 @@ impl CallBuilder<'_> {
_marker: core::marker::PhantomData<Abi>,
}

#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
const _: () = {
impl ::ink::codegen::ContractCallBuilder for #storage_ident {
type Type<Abi> = #cb_ident<Abi>;
Expand Down
20 changes: 16 additions & 4 deletions crates/ink/codegen/src/generator/as_dependency/contract_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ impl GenerateCode for ContractRef<'_> {
let call_builder_trait_impl = self.generate_call_builder_trait_impl();
let auxiliary_trait_impls = self.generate_auxiliary_trait_impls();
quote! {
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
#contract_ref
#contract_ref_trait_impls
#contract_ref_inherent_impls
#call_builder_trait_impl
#auxiliary_trait_impls

#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
const _: () = {
#contract_ref_trait_impls
#contract_ref_inherent_impls
#call_builder_trait_impl
#auxiliary_trait_impls
};
}
}
}
Expand Down Expand Up @@ -104,6 +109,7 @@ impl ContractRef<'_> {
let ref_ident_abi_alias = format_ident!("{ref_ident_default_abi}{suffix}");
quote! {
#[allow(dead_code)]
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
pub type #ref_ident_abi_alias = #ref_ident::<#abi_ty>;
}
});
Expand Down Expand Up @@ -152,10 +158,12 @@ impl ContractRef<'_> {

// Default type alias (i.e. `ContractRef` for a contract named `Contract`).
#[allow(dead_code)]
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
pub type #ref_ident_default_abi = #ref_ident::<#abi>;
// ABI specific type aliases (i.e. `ContractRefInk` and `ContractRefSol`) as appropriate.
#ref_ident_abi_aliases

#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
const _: () = {
impl ::ink::env::ContractReference for #storage_ident {
type Type = #ref_ident;
Expand Down Expand Up @@ -309,6 +317,10 @@ impl ContractRef<'_> {
&mut self.inner
}
}

impl<TypeAbi> ::ink::codegen::ContractCallBuilder for #ref_ident<TypeAbi> {
type Type<TraitAbi> = <#storage_ident as ::ink::codegen::ContractCallBuilder>::Type<TypeAbi>;
}
};
)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/ink/codegen/src/generator/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,14 @@ impl GenerateCode for Contract<'_> {
#[cfg(any(ink_abi = "sol", ink_abi = "all"))]
let solidity_metadata = self.generate_code_using::<generator::SolidityMetadata>();

let contract_ref_base_ident =
quote::format_ident!("{}Ref", module.storage().ident());

quote! {
// Note: rustdoc can't resolve `self::` prefixes for this re-export.
#[cfg(any(test, feature = "std", feature = "ink-as-dependency"))]
pub use #ident::#contract_ref_base_ident;

#( #attrs )*
#vis mod #ident {
#env
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ impl CallForwarder<'_> {
&mut self.builder
}
}

impl<E, TypeAbi> ::ink::codegen::ContractCallBuilder for #call_forwarder_ident<E, TypeAbi>
where
E: ::ink::env::Environment,
{
type Type<TraitAbi> = #call_builder_ident<E, TypeAbi>;
}
)
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ink/macro/src/contract_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn analyze_or_err(
#trait_def_impl

// Type alias for contract ref.
type #contract_ref_name =
pub type #contract_ref_name =
<<::ink::reflect::TraitDefinitionRegistry<#env> as #trait_name>
::__ink_TraitInfo as ::ink::codegen::TraitCallForwarder>::Forwarder<#abi_ty>;
))
Expand Down
5 changes: 1 addition & 4 deletions crates/primitives/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ pub trait ContractEnv {
/// }
/// }
///
/// use contract::{
/// Contract,
/// ContractRef,
/// };
/// use contract::Contract;
/// # use ink::codegen::utils::IsSameType;
/// # use ink::env::ContractReference;
///
Expand Down
7 changes: 2 additions & 5 deletions integration-tests/internal/lang-err/call-builder/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ mod call_builder {
ChainBackend,
ContractsBackend,
};
use integration_flipper::{
Flipper,
FlipperRef,
};
use integration_flipper::FlipperRef;

type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>;

Expand All @@ -200,7 +197,7 @@ mod call_builder {
.submit()
.await
.expect("instantiate `flipper` failed");
let flipper_call = flipper.call_builder::<Flipper>();
let flipper_call = flipper.call_builder::<FlipperRef>();

let flipper_get = flipper_call.get();
let get_call_result = client.call(&origin, &flipper_get).dry_run().await?;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::constructors_return_value::{
ConstructorError,
ConstructorsReturnValue,
ConstructorsReturnValueRef,
};
pub use self::constructors_return_value::ConstructorError;

#[ink::contract]
pub mod constructors_return_value {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::integration_flipper::{
Flipper,
FlipperRef,
};

#[ink::contract]
pub mod integration_flipper {
#[ink(storage)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::contract1::Contract1Ref;

#[ink::contract]
mod contract1 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::contract2::Contract2Ref;

#[ink::contract]
mod contract2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::virtual_contract::VirtualContractRef;

#[ink::contract]
pub mod virtual_contract {
use ink::env::call::{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::virtual_contract_ver1::VirtualContractVer1Ref;

#[ink::contract]
mod virtual_contract_ver1 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::virtual_contract_ver2::VirtualContractVer2Ref;

#[ink::contract]
mod virtual_contract_ver2 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::other_contract::{
OtherContract,
OtherContractRef,
};

#[ink::contract]
mod other_contract {

Expand Down
5 changes: 0 additions & 5 deletions integration-tests/public/incrementer/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::incrementer::{
Incrementer,
IncrementerRef,
};

#[ink::contract]
mod incrementer {
#[ink(storage)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::accumulator::{
Accumulator,
AccumulatorRef,
};

#[ink::contract]
pub mod accumulator {
/// Holds a simple `i32` value that can be incremented and decremented.
Expand Down
5 changes: 0 additions & 5 deletions integration-tests/public/multi-contract-caller/adder/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::adder::{
Adder,
AdderRef,
};

#[ink::contract]
mod adder {
use accumulator::AccumulatorRef;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::subber::{
Subber,
SubberRef,
};

#[ink::contract]
mod subber {
use accumulator::AccumulatorRef;
Expand Down
1 change: 0 additions & 1 deletion integration-tests/public/multisig/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@

pub use self::multisig::{
ConfirmationStatus,
Multisig,
Transaction,
};

Expand Down
7 changes: 2 additions & 5 deletions integration-tests/public/runtime-call-contract/e2e_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use super::{
Flipper,
FlipperRef,
};
use super::FlipperRef;
use ink_e2e::{
ChainBackend,
ContractsBackend,
Expand All @@ -26,7 +23,7 @@ async fn instantiate_and_get<Client: E2EBackend>(mut client: Client) -> E2EResul
.await
.expect("instantiate failed");

let mut call_builder = contract.call_builder::<Flipper>();
let mut call_builder = contract.call_builder::<FlipperRef>();
let flip_dry_run = client
.call(&ink_e2e::bob(), &call_builder.flip())
.dry_run()
Expand Down
5 changes: 0 additions & 5 deletions integration-tests/public/runtime-call-contract/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use flipper::{
Flipper,
FlipperRef,
};

#[ink::contract]
mod flipper {
#[ink(storage)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std, no_main)]

pub use self::other_contract::{
OtherContract,
OtherContractRef,
};

#[ink::contract]
mod other_contract {

Expand Down
Loading