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

feat(solc): extend Artifact trait #673

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

### Unreleased

- Add more utility functions to the `Artifact` trait [#673](https://github.com/gakonst/ethers-rs/pull/673)
- Return cached artifacts from project `compile` when the cache only contains
some files
- Add support for library linking and make `Bytecode`'s `object` filed an `enum BytecodeObject`
Expand Down
18 changes: 16 additions & 2 deletions ethers-solc/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ pub struct Contract {
}

/// Minimal representation of a contract's abi with bytecode
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct CompactContract {
/// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See https://docs.soliditylang.org/en/develop/abi-spec.html
Expand All @@ -577,7 +577,7 @@ pub struct CompactContract {
}

impl CompactContract {
/// Returns the contents of this type as a single
/// Returns the contents of this type as a single tuple of abi, bytecode and deployed bytecode
pub fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>) {
(
self.abi,
Expand All @@ -598,6 +598,20 @@ impl CompactContract {
}
}

impl From<serde_json::Value> for CompactContract {
fn from(mut val: serde_json::Value) -> Self {
if let Some(map) = val.as_object_mut() {
let abi = map.remove("abi").and_then(|val| serde_json::from_value(val).ok());
let bin = map.remove("bin").and_then(|val| serde_json::from_value(val).ok());
let bin_runtime =
map.remove("bin-runtime").and_then(|val| serde_json::from_value(val).ok());
Self { abi, bin, bin_runtime }
} else {
CompactContract::default()
}
}
}

impl From<Contract> for CompactContract {
fn from(c: Contract) -> Self {
let (bin, bin_runtime) = if let Some(evm) = c.evm {
Expand Down
29 changes: 21 additions & 8 deletions ethers-solc/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,25 +218,38 @@ impl SolcConfigBuilder {
pub type Artifacts<T> = BTreeMap<String, BTreeMap<String, T>>;

pub trait Artifact {
/// Returns the artifact's `Abi` and bytecode
fn into_inner(self) -> (Option<Abi>, Option<Bytes>);

/// Turns the artifact into a container type for abi, bytecode and deployed bytecode
fn into_compact_contract(self) -> CompactContract;

/// Returns the contents of this type as a single tuple of abi, bytecode and deployed bytecode
fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>)
where
Self: Sized,
{
self.into_compact_contract().into_parts()
}
}

impl Artifact for CompactContract {
fn into_inner(self) -> (Option<Abi>, Option<Bytes>) {
(self.abi, self.bin.and_then(|bin| bin.into_bytes()))
}

fn into_compact_contract(self) -> CompactContract {
self
}
}

impl Artifact for serde_json::Value {
fn into_inner(self) -> (Option<Abi>, Option<Bytes>) {
let abi = self.get("abi").map(|abi| {
serde_json::from_value::<Abi>(abi.clone()).expect("could not get artifact abi")
});
let bytecode = self.get("bin").map(|bin| {
serde_json::from_value::<Bytes>(bin.clone()).expect("could not get artifact bytecode")
});

(abi, bytecode)
self.into_compact_contract().into_inner()
}

fn into_compact_contract(self) -> CompactContract {
self.into()
}
}

Expand Down