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

Commit

Permalink
fix: remove 0x bytecode object prefix for CompilerOutput (#1424)
Browse files Browse the repository at this point in the history
* fix: remove 0x bytecode object prefix for CompilerOutput

* chore: rustfmt

* chore(clippy): make clippy happy
  • Loading branch information
mattsse authored Jun 28, 2022
1 parent 7c6462b commit f6eaa7e
Showing 1 changed file with 21 additions and 1 deletion.
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

0 comments on commit f6eaa7e

Please sign in to comment.