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

Commit

Permalink
feat(solc): extend Artifact trait (#673)
Browse files Browse the repository at this point in the history
* feat(solc): extend Artifact trait

* chore: update changelog

* chore: rustfmt
  • Loading branch information
mattsse authored Dec 10, 2021
1 parent ba53fd1 commit 38e1846
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
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

0 comments on commit 38e1846

Please sign in to comment.