fix: remove 0x bytecode object prefix for CompilerOutput (#1424)
* fix: remove 0x bytecode object prefix for CompilerOutput * chore: rustfmt * chore(clippy): make clippy happy
This commit is contained in:
parent
7c6462be02
commit
f6eaa7e551
|
@ -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)]
|
||||
|
@ -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")]
|
||||
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue