diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1861a8383b1cc9095464bfac3fef156118ea0b..5dc469069da3e0809111f3c9d79fb164f36c11a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Update `cargo contract new` template dependencies to ink! `rc4` - [#332](https://github.com/paritytech/cargo-contract/pull/332) - Update `cargo contract new` template dependencies to ink! `rc5` - [#335](https://github.com/paritytech/cargo-contract/pull/335) +- Populate `metadataVersion` field with `ink_metadata` crate version - [#347](https://github.com/paritytech/cargo-contract/pull/347) ## [0.14.0] - 2021-08-12 diff --git a/Cargo.lock b/Cargo.lock index 31e29b49fb41450d6a516bf2b2a97a4fecedb9bf..a5b3ee6a06b81a1414ad04160cb0823b12f67ab9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -638,7 +638,7 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "contract-metadata" -version = "0.3.0" +version = "0.4.0" dependencies = [ "pretty_assertions", "semver", diff --git a/Cargo.toml b/Cargo.toml index f1f8298b9604dbf52f300d05f9af1e943f836418..01eab70f195861b9b048165efbe500e3aec677d9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ colored = "2.0.0" toml = "0.5.8" rustc_version = "0.4.0" blake2 = "0.9.2" -contract-metadata = { version = "0.3.0", path = "./metadata" } +contract-metadata = { version = "0.4.0", path = "./metadata" } semver = { version = "1.0.4", features = ["serde"] } serde = { version = "1.0.130", default-features = false, features = ["derive"] } serde_json = "1.0.68" diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml index 915f6d9aafa5d75d8abe8f99233c329ec3bb17d5..81b306bffed7d42af49f5cbbb840c01287dae09a 100644 --- a/metadata/Cargo.toml +++ b/metadata/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "contract-metadata" -version = "0.3.0" +version = "0.4.0" authors = ["Parity Technologies "] edition = "2018" diff --git a/metadata/lib.rs b/metadata/lib.rs index 90eebbf1328e95ee9d2a2edfaa3715ed6f4b621f..c5d67eb200b42e82ddc4e8023c2c5da4f326865c 100644 --- a/metadata/lib.rs +++ b/metadata/lib.rs @@ -45,21 +45,22 @@ //! let user = User::new(user_json); //! // contract abi raw json generated by contract compilation //! let abi_json: Map = Map::new(); +//! // the version of the `ink_metadata` crate used by the contract +//! let metadata_version = Version::new(3, 0, 0); //! -//! let metadata = ContractMetadata::new(source, contract, Some(user), abi_json); +//! let metadata = ContractMetadata::new(metadata_version, source, contract, Some(user), abi_json); //! //! // serialize to json //! let json = serde_json::to_value(&metadata).unwrap(); //! ``` +pub use semver::Version; + use core::fmt::{Display, Formatter, Result as DisplayResult, Write}; -use semver::Version; use serde::{Serialize, Serializer}; use serde_json::{Map, Value}; use url::Url; -const METADATA_VERSION: &str = "0.1.0"; - /// Smart contract metadata. #[derive(Clone, Debug, Serialize)] pub struct ContractMetadata { @@ -77,14 +78,12 @@ pub struct ContractMetadata { impl ContractMetadata { /// Construct new contract metadata. pub fn new( + metadata_version: semver::Version, source: Source, contract: Contract, user: Option, abi: Map, ) -> Self { - let metadata_version = semver::Version::parse(METADATA_VERSION) - .expect("METADATA_VERSION is a valid semver string"); - Self { metadata_version, source, @@ -556,7 +555,13 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(source, contract, Some(user), abi_json); + let metadata = ContractMetadata::new( + Version::new(0, 1, 0), + source, + contract, + Some(user), + abi_json, + ); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { @@ -621,7 +626,8 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(source, contract, None, abi_json); + let metadata = + ContractMetadata::new(Version::new(0, 1, 0), source, contract, None, abi_json); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { diff --git a/src/cmd/build.rs b/src/cmd/build.rs index eadd570695dac006a5c9b7f92a25eccae2dccaec..eae9205401c8b9a303c231ed942b0f5f22707113 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -436,7 +436,6 @@ fn do_optimization( We use this tool to optimize the size of your contract's Wasm binary.\n\n\ wasm-opt is part of the binaryen package. You can find detailed\n\ installation instructions on https://github.com/WebAssembly/binaryen#tools.\n\n\ - There are ready-to-install packages for many platforms:\n\ * Debian/Ubuntu: apt-get install binaryen\n\ * Homebrew: brew install binaryen\n\ diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 92bb3c3b0647b8d53e0eca9d46d0927d3492e8a1..8caa92856b49ba37abfd188752b84b4cbdf43a4d 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -51,6 +51,7 @@ struct ExtendedMetadataResult { source: Source, contract: Contract, user: Option, + metadata_version: Version, } /// Generates a file with metadata describing the ABI of the smart-contract. @@ -76,6 +77,7 @@ pub(crate) fn execute( source, contract, user, + metadata_version, } = extended_metadata(crate_metadata, final_contract_wasm)?; let generate_metadata = |manifest_path: &ManifestPath| -> Result<()> { @@ -101,7 +103,7 @@ pub(crate) fn execute( )?; let ink_meta: serde_json::Map = serde_json::from_slice(&stdout)?; - let metadata = ContractMetadata::new(source, contract, user, ink_meta); + let metadata = ContractMetadata::new(metadata_version, source, contract, user, ink_meta); { let mut metadata = metadata.clone(); metadata.remove_source_wasm_attribute(); @@ -149,6 +151,7 @@ fn extended_metadata( ) -> Result { let contract_package = &crate_metadata.root_package; let ink_version = &crate_metadata.ink_version; + let metadata_version = crate_metadata.ink_metadata_version.clone(); let rust_version = Version::parse(&rustc_version::version()?.to_string())?; let contract_name = contract_package.name.clone(); let contract_version = Version::parse(&contract_package.version.to_string())?; @@ -209,6 +212,7 @@ fn extended_metadata( source, contract, user, + metadata_version, }) } diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 9b47b7d190f1e4e2461b757c7fe4c03ea5d053ab..e6422e542e0644d728b41a1407092d298d37cf53 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -33,6 +33,7 @@ pub struct CrateMetadata { pub original_wasm: PathBuf, pub dest_wasm: PathBuf, pub ink_version: Version, + pub ink_metadata_version: Version, pub documentation: Option, pub homepage: Option, pub user: Option>, @@ -75,20 +76,26 @@ impl CrateMetadata { dest_wasm.push(lib_name.clone()); dest_wasm.set_extension("wasm"); - let ink_version = metadata - .packages - .iter() - .find_map(|package| { - if package.name == "ink_lang" { - Some( - Version::parse(&package.version.to_string()) - .expect("Invalid ink_lang version string"), - ) - } else { - None - } - }) - .ok_or_else(|| anyhow::anyhow!("No 'ink_lang' dependency found"))?; + let crate_version = |crate_name| { + metadata + .packages + .iter() + .find_map(|package| { + if package.name == crate_name { + Some( + Version::parse(&package.version.to_string()).unwrap_or_else(|e| { + panic!("Invalid {} version string: {}", crate_name, e) + }), + ) + } else { + None + } + }) + .ok_or_else(|| anyhow::anyhow!("No '{}' dependency found", crate_name)) + }; + + let ink_version = crate_version("ink_lang")?; + let ink_metadata_version = crate_version("ink_metadata")?; let ExtraMetadata { documentation, @@ -104,6 +111,7 @@ impl CrateMetadata { original_wasm: original_wasm.into(), dest_wasm: dest_wasm.into(), ink_version, + ink_metadata_version, documentation, homepage, user,