Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

fix: remove 0x bytecode object prefix for CompilerOutput #1424

Merged
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
22 changes: 21 additions & 1 deletion ethers-solc/src/artifacts/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
utils,
};
use ethers_core::{abi::Address, types::Bytes};
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Serialize, Serializer};
use std::collections::BTreeMap;

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
Expand All @@ -16,6 +16,7 @@ pub struct Bytecode {
#[serde(default, skip_serializing_if = "::std::collections::BTreeMap::is_empty")]
pub function_debug_data: BTreeMap<String, FunctionDebugData>,
/// The bytecode as a hex string.
#[serde(serialize_with = "serialize_bytecode_without_prefix")]
pub object: BytecodeObject,
/// Opcodes list (string)
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -367,6 +368,25 @@ impl AsRef<[u8]> for BytecodeObject {
}
}

/// This will serialize the bytecode data without a `0x` prefix, which the `ethers::types::Bytes`
/// adds by default.
///
/// This ensures that we serialize bytecode data in the same way as solc does, See also <https://github.com/gakonst/ethers-rs/issues/1422>
pub fn serialize_bytecode_without_prefix<S>(
bytecode: &BytecodeObject,
s: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match bytecode {
BytecodeObject::Bytecode(code) => s.serialize_str(&hex::encode(code)),
BytecodeObject::Unlinked(code) => {
s.serialize_str(code.strip_prefix("0x").unwrap_or(code.as_str()))
}
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct DeployedBytecode {
#[serde(flatten)]
Expand Down