Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: bump bytecode crate to 0.3.0 #54

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bytecode/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bytecode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
130 changes: 66 additions & 64 deletions bytecode/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<String> = 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<String> = 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");
}
Loading