Skip to content

Commit a20ffc1

Browse files
author
German
authored
Persist Environment in metadata (#1741)
* environment metadata spec * persisting env in metadata * Changelog entry * docs * typo * derive TypeInfo for chain extension * address clippy complaints * remove unnecessary borrow * add comments * typo
1 parent a087941 commit a20ffc1

File tree

10 files changed

+485
-5
lines changed

10 files changed

+485
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
- Persist `Environment` in metadata - [#1741](https://github.com/paritytech/ink/pull/1741)
11+
912
### Changed
1013
- Upgraded `syn` to version `2` - [#1731](https://github.com/paritytech/ink/pull/1731)
1114

crates/env/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ categories = ["no-std", "embedded"]
1616
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
1717

1818
[dependencies]
19-
ink_metadata = { version = "4.1.0", path = "../metadata", default-features = false, features = ["derive"], optional = true }
2019
ink_allocator = { version = "4.1.0", path = "../allocator", default-features = false }
2120
ink_storage_traits = { version = "4.1.0", path = "../storage/traits", default-features = false }
2221
ink_prelude = { version = "4.1.0", path = "../prelude", default-features = false }
@@ -56,7 +55,6 @@ ink = { path = "../ink" }
5655
[features]
5756
default = ["std"]
5857
std = [
59-
"ink_metadata/std",
6058
"ink_allocator/std",
6159
"ink_prelude/std",
6260
"ink_primitives/std",

crates/env/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ pub trait Environment {
177177
}
178178

179179
/// Placeholder for chains that have no defined chain extension.
180+
#[cfg_attr(feature = "std", derive(TypeInfo))]
180181
pub enum NoChainExtension {}
181182

182183
/// The fundamental types of the default configuration.

crates/ink/codegen/src/generator/chain_extension.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ impl GenerateCode for ChainExtension<'_> {
133133
let instance_ident = format_ident!("__ink_{}Instance", ident);
134134
quote_spanned!(span =>
135135
#(#attrs)*
136+
#[cfg_attr(feature = "std", derive(
137+
::scale_info::TypeInfo,
138+
))]
136139
pub enum #ident {}
137140

138141
const _: () = {

crates/ink/codegen/src/generator/env.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ impl GenerateCode for Env<'_> {
3939
type Hash = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::Hash;
4040
type Timestamp = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::Timestamp;
4141
type BlockNumber = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::BlockNumber;
42+
type ChainExtension = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::ChainExtension;
43+
const MAX_EVENT_TOPICS: usize = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::MAX_EVENT_TOPICS;
4244
}
4345
}
4446
}

crates/ink/codegen/src/generator/metadata.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ use quote::{
2828
quote,
2929
quote_spanned,
3030
};
31-
use syn::spanned::Spanned as _;
31+
use syn::{
32+
parse_quote,
33+
spanned::Spanned as _,
34+
};
3235

3336
/// Generates code to generate the metadata of the contract.
3437
#[derive(From)]
@@ -96,6 +99,7 @@ impl Metadata<'_> {
9699
::ink::LangError
97100
};
98101
let error = Self::generate_type_spec(&error_ty);
102+
let environment = self.generate_environment();
99103
quote! {
100104
::ink::metadata::ContractSpec::new()
101105
.constructors([
@@ -113,6 +117,9 @@ impl Metadata<'_> {
113117
.lang_error(
114118
#error
115119
)
120+
.environment(
121+
#environment
122+
)
116123
.done()
117124
}
118125
}
@@ -407,6 +414,35 @@ impl Metadata<'_> {
407414
)
408415
})
409416
}
417+
418+
fn generate_environment(&self) -> TokenStream2 {
419+
let span = self.contract.module().span();
420+
421+
let account_id: syn::Type = parse_quote!(AccountId);
422+
let balance: syn::Type = parse_quote!(Balance);
423+
let hash: syn::Type = parse_quote!(Hash);
424+
let timestamp: syn::Type = parse_quote!(Timestamp);
425+
let block_number: syn::Type = parse_quote!(BlockNumber);
426+
let chain_extension: syn::Type = parse_quote!(ChainExtension);
427+
428+
let account_id = Self::generate_type_spec(&account_id);
429+
let balance = Self::generate_type_spec(&balance);
430+
let hash = Self::generate_type_spec(&hash);
431+
let timestamp = Self::generate_type_spec(&timestamp);
432+
let block_number = Self::generate_type_spec(&block_number);
433+
let chain_extension = Self::generate_type_spec(&chain_extension);
434+
quote_spanned!(span=>
435+
::ink::metadata::EnvironmentSpec::new()
436+
.account_id(#account_id)
437+
.balance(#balance)
438+
.hash(#hash)
439+
.timestamp(#timestamp)
440+
.block_number(#block_number)
441+
.chain_extension(#chain_extension)
442+
.max_event_topics(MAX_EVENT_TOPICS)
443+
.done()
444+
)
445+
}
410446
}
411447

412448
#[cfg(test)]

crates/ink/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub use ink_env as env;
3636
pub use ink_metadata as metadata;
3737
pub use ink_prelude as prelude;
3838
pub use ink_primitives as primitives;
39+
#[cfg(feature = "std")]
40+
pub use metadata::TypeInfo;
3941

4042
pub mod storage {
4143
pub mod traits {

crates/metadata/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub use self::specs::{
3636
ContractSpec,
3737
ContractSpecBuilder,
3838
DisplayName,
39+
EnvironmentSpec,
40+
EnvironmentSpecBuilder,
3941
EventParamSpec,
4042
EventParamSpecBuilder,
4143
EventSpec,
@@ -51,6 +53,7 @@ pub use self::specs::{
5153

5254
use impl_serde::serialize as serde_hex;
5355

56+
pub use scale_info::TypeInfo;
5457
#[cfg(feature = "derive")]
5558
use scale_info::{
5659
form::PortableForm,

0 commit comments

Comments
 (0)