From 6c83e6c1823e6c03cbc7b26158f8cc693edf4d4f Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Wed, 5 Feb 2025 13:56:48 -0500 Subject: [PATCH] chore: bump bytecode crate to 0.3.0 --- bytecode/Cargo.lock | 2 +- bytecode/Cargo.toml | 2 +- bytecode/build.rs | 130 ++++++++++++++++++++++---------------------- 3 files changed, 68 insertions(+), 66 deletions(-) diff --git a/bytecode/Cargo.lock b/bytecode/Cargo.lock index 64ad728..8587edb 100644 --- a/bytecode/Cargo.lock +++ b/bytecode/Cargo.lock @@ -83,7 +83,7 @@ dependencies = [ [[package]] name = "tnt-core-bytecode" -version = "0.2.0" +version = "0.3.0" dependencies = [ "serde_json", ] diff --git a/bytecode/Cargo.toml b/bytecode/Cargo.toml index a29c309..bdf06d1 100644 --- a/bytecode/Cargo.toml +++ b/bytecode/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tnt-core-bytecode" -version = "0.2.0" +version = "0.3.0" edition = "2021" description = "Bytecode exports for TNT Core Solidity contracts" license = "MIT" diff --git a/bytecode/build.rs b/bytecode/build.rs index f02d3da..94fa634 100644 --- a/bytecode/build.rs +++ b/bytecode/build.rs @@ -23,27 +23,30 @@ fn to_screaming_snake_case(name: &str) -> String { fn main() { // Only run the build script if the build-script feature is enabled - if env::var("CARGO_FEATURE_BUILD_SCRIPT").is_ok() { - // Your existing build script logic here - println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-changed=../src"); - - // Run forge build - let status = Command::new("forge") - .arg("build") - .current_dir("..") - .status() - .expect("Failed to build contracts"); - - if !status.success() { - panic!("Failed to build contracts"); - } + if env::var("CARGO_FEATURE_BUILD_SCRIPT").is_err() { + return; + } + + // Your existing build script logic here + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=../src"); - // List of contracts to generate bytecode for - let contracts = vec!["MasterBlueprintServiceManager"]; + // Run forge build + let status = Command::new("forge") + .arg("build") + .current_dir("..") + .status() + .expect("Failed to build contracts"); + + if !status.success() { + panic!("Failed to build contracts"); + } - let mut rust_code = String::from( - r#"//! TNT Core contract bytecode exports + // List of contracts to generate bytecode for + let contracts = vec!["MasterBlueprintServiceManager"]; + + let mut rust_code = String::from( + r#"//! TNT Core contract bytecode exports //! //! This crate exports the bytecode of TNT Core contracts as constant byte vectors //! that can be easily imported and used in other Rust projects. @@ -52,55 +55,54 @@ fn main() { #[rustfmt::skip] pub mod bytecode { "#, - ); - - for contract in contracts { - let json_path = Path::new("..") - .join("out") - .join(format!("{}.sol", contract)) - .join(format!("{}.json", contract)); - - let json_str = fs::read_to_string(&json_path) - .unwrap_or_else(|_| panic!("Failed to read {}", json_path.display())); - - let json: serde_json::Value = - serde_json::from_str(&json_str).expect("Failed to parse JSON"); - - let bytecode = json["bytecode"] - .as_object() - .and_then(|obj| obj.get("object")) - .and_then(|obj| obj.as_str()) - .unwrap_or_else(|| json["bytecode"].as_str().expect("Failed to get bytecode")); - - let bytecode = bytecode.strip_prefix("0x").unwrap_or(bytecode); - let bytes: Vec = bytecode - .as_bytes() - .chunks(2) - .map(|chunk| { - let hex = std::str::from_utf8(chunk).unwrap(); - format!("0x{}", hex) - }) - .collect(); - - let const_name = to_screaming_snake_case(contract); - - rust_code.push_str(&format!( - r#" /// Bytecode for the {} contract + ); + + for contract in contracts { + let json_path = Path::new("..") + .join("out") + .join(format!("{}.sol", contract)) + .join(format!("{}.json", contract)); + + let json_str = fs::read_to_string(&json_path) + .unwrap_or_else(|_| panic!("Failed to read {}", json_path.display())); + + let json: serde_json::Value = + serde_json::from_str(&json_str).expect("Failed to parse JSON"); + + let bytecode = json["bytecode"] + .as_object() + .and_then(|obj| obj.get("object")) + .and_then(|obj| obj.as_str()) + .unwrap_or_else(|| json["bytecode"].as_str().expect("Failed to get bytecode")); + + let bytecode = bytecode.strip_prefix("0x").unwrap_or(bytecode); + let bytes: Vec = bytecode + .as_bytes() + .chunks(2) + .map(|chunk| { + let hex = std::str::from_utf8(chunk).unwrap(); + format!("0x{}", hex) + }) + .collect(); + + let const_name = to_screaming_snake_case(contract); + + rust_code.push_str(&format!( + r#" /// Bytecode for the {} contract pub const {}: &[u8] = &[{}]; "#, - contract, - const_name, - bytes.join(", ") - )); - } + contract, + const_name, + bytes.join(", ") + )); + } - rust_code.push_str( - r#"} + rust_code.push_str( + r#"} "#, - ); + ); - // Write directly to lib.rs - fs::write(Path::new("src").join("lib.rs"), rust_code).expect("Failed to write to lib.rs"); - } + // Write directly to lib.rs + fs::write(Path::new("src").join("lib.rs"), rust_code).expect("Failed to write to lib.rs"); }