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

feat(solc): add bytecode hash variants #1104

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Changes from 1 commit
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
53 changes: 51 additions & 2 deletions ethers-solc/src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,57 @@ pub struct SettingsMetadata {
/// The metadata hash can be removed from the bytecode via option "none".
/// The other options are "ipfs" and "bzzr1".
/// If the option is omitted, "ipfs" is used by default.
#[serde(default, rename = "bytecodeHash", skip_serializing_if = "Option::is_none")]
pub bytecode_hash: Option<String>,
#[serde(
default,
rename = "bytecodeHash",
skip_serializing_if = "Option::is_none",
with = "serde_helpers::display_from_str_opt"
)]
pub bytecode_hash: Option<BytecodeHash>,
}

impl From<BytecodeHash> for SettingsMetadata {
fn from(hash: BytecodeHash) -> Self {
Self { use_literal_content: None, bytecode_hash: Some(hash) }
}
}

#[derive(Clone, Debug, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BytecodeHash {
Ipfs,
None,
Bzzr1,
}

impl Default for BytecodeHash {
fn default() -> Self {
// ipfs general default https://docs.soliditylang.org/en/latest/using-the-compiler.html#compiler-api
BytecodeHash::Ipfs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it None and document that it's by default None?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sg

}
}

impl FromStr for BytecodeHash {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(BytecodeHash::None),
"ipfs" => Ok(BytecodeHash::Ipfs),
"bzzr1" => Ok(BytecodeHash::Bzzr1),
s => Err(format!("Unknown bytecode hash: {}", s)),
}
}
}

impl fmt::Display for BytecodeHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match self {
BytecodeHash::Ipfs => "ipfs",
BytecodeHash::None => "none",
BytecodeHash::Bzzr1 => "bzzr1",
};
f.write_str(s)
}
}

/// Bindings for [`solc` contract metadata](https://docs.soliditylang.org/en/latest/metadata.html)
Expand Down