From f86bc2483f8f56bbcb85758d822455b7191edfe1 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 27 Jan 2023 19:15:03 +0100 Subject: [PATCH] feat(solc): support emitting bytecode as extra files (#2074) --- .../src/artifact_output/configurable.rs | 36 +++++++++++++++++-- ethers-solc/src/artifacts/output_selection.rs | 6 +++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/ethers-solc/src/artifact_output/configurable.rs b/ethers-solc/src/artifact_output/configurable.rs index 3ed99e32..0c87731d 100644 --- a/ethers-solc/src/artifact_output/configurable.rs +++ b/ethers-solc/src/artifact_output/configurable.rs @@ -13,15 +13,15 @@ use crate::{ bytecode::{CompactBytecode, CompactDeployedBytecode}, contract::{CompactContract, CompactContractBytecode, Contract}, output_selection::{ - BytecodeOutputSelection, ContractOutputSelection, EvmOutputSelection, - EwasmOutputSelection, + BytecodeOutputSelection, ContractOutputSelection, DeployedBytecodeOutputSelection, + EvmOutputSelection, EwasmOutputSelection, }, Ast, CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates, GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout, UserDoc, }, sources::VersionedSourceFile, - ArtifactOutput, SolcConfig, SolcError, SourceFile, + Artifact, ArtifactOutput, SolcConfig, SolcError, SourceFile, }; use serde::{Deserialize, Serialize}; use std::{borrow::Cow, collections::BTreeMap, fs, path::Path}; @@ -509,6 +509,8 @@ pub struct ExtraOutputFiles { pub assembly: bool, pub source_map: bool, pub generated_sources: bool, + pub bytecode: bool, + pub deployed_bytecode: bool, /// PRIVATE: This structure may grow, As such, constructing this structure should /// _always_ be done using a public constructor or update syntax: @@ -537,6 +539,8 @@ impl ExtraOutputFiles { assembly: true, source_map: true, generated_sources: true, + bytecode: true, + deployed_bytecode: true, __non_exhaustive: (), } } @@ -565,6 +569,8 @@ impl ExtraOutputFiles { config.assembly = true; config.generated_sources = true; config.source_map = true; + config.bytecode = true; + config.deployed_bytecode = true; } EvmOutputSelection::Assembly => { config.assembly = true; @@ -572,9 +578,18 @@ impl ExtraOutputFiles { EvmOutputSelection::ByteCode(BytecodeOutputSelection::GeneratedSources) => { config.generated_sources = true; } + EvmOutputSelection::ByteCode(BytecodeOutputSelection::Object) => { + config.bytecode = true; + } EvmOutputSelection::ByteCode(BytecodeOutputSelection::SourceMap) => { config.source_map = true; } + EvmOutputSelection::DeployedByteCode(DeployedBytecodeOutputSelection::All) | + EvmOutputSelection::DeployedByteCode( + DeployedBytecodeOutputSelection::Object, + ) => { + config.deployed_bytecode = true; + } _ => {} }, ContractOutputSelection::Ewasm(_) => { @@ -656,6 +671,21 @@ impl ExtraOutputFiles { } } + if self.bytecode { + if let Some(ref code) = contract.get_bytecode_bytes() { + let code = hex::encode(code.as_ref()); + let file = file.with_extension("bin"); + fs::write(&file, code).map_err(|err| SolcError::io(err, file))? + } + } + if self.deployed_bytecode { + if let Some(ref code) = contract.get_deployed_bytecode_bytes() { + let code = hex::encode(code.as_ref()); + let file = file.with_extension("deployed-bin"); + fs::write(&file, code).map_err(|err| SolcError::io(err, file))? + } + } + Ok(()) } } diff --git a/ethers-solc/src/artifacts/output_selection.rs b/ethers-solc/src/artifacts/output_selection.rs index b6017d91..769b4d4b 100644 --- a/ethers-solc/src/artifacts/output_selection.rs +++ b/ethers-solc/src/artifacts/output_selection.rs @@ -407,7 +407,7 @@ impl FromStr for BytecodeOutputSelection { match s { "evm.bytecode" => Ok(BytecodeOutputSelection::All), "evm.bytecode.functionDebugData" => Ok(BytecodeOutputSelection::FunctionDebugData), - "evm.bytecode.object" => Ok(BytecodeOutputSelection::Object), + "code" | "bin" | "evm.bytecode.object" => Ok(BytecodeOutputSelection::Object), "evm.bytecode.opcodes" => Ok(BytecodeOutputSelection::Opcodes), "evm.bytecode.sourceMap" => Ok(BytecodeOutputSelection::SourceMap), "evm.bytecode.linkReferences" => Ok(BytecodeOutputSelection::LinkReferences), @@ -482,6 +482,10 @@ impl FromStr for DeployedBytecodeOutputSelection { "evm.deployedBytecode.functionDebugData" => { Ok(DeployedBytecodeOutputSelection::FunctionDebugData) } + "deployed-code" | + "deployed-bin" | + "runtime-code" | + "runtime-bin" | "evm.deployedBytecode.object" => Ok(DeployedBytecodeOutputSelection::Object), "evm.deployedBytecode.opcodes" => Ok(DeployedBytecodeOutputSelection::Opcodes), "evm.deployedBytecode.sourceMap" => Ok(DeployedBytecodeOutputSelection::SourceMap),