feat(solc): extend Artifact trait (#673)

* feat(solc): extend Artifact trait

* chore: update changelog

* chore: rustfmt
This commit is contained in:
Matthias Seitz 2021-12-10 20:38:44 +01:00 committed by GitHub
parent ba53fd1466
commit 38e18463dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 9 deletions

View File

@ -24,6 +24,7 @@
### Unreleased ### 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 - Return cached artifacts from project `compile` when the cache only contains
some files some files
- Add support for library linking and make `Bytecode`'s `object` filed an `enum BytecodeObject` - Add support for library linking and make `Bytecode`'s `object` filed an `enum BytecodeObject`

View File

@ -565,7 +565,7 @@ pub struct Contract {
} }
/// Minimal representation of a contract's abi with bytecode /// 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 { pub struct CompactContract {
/// The Ethereum Contract ABI. If empty, it is represented as an empty /// The Ethereum Contract ABI. If empty, it is represented as an empty
/// array. See https://docs.soliditylang.org/en/develop/abi-spec.html /// array. See https://docs.soliditylang.org/en/develop/abi-spec.html
@ -577,7 +577,7 @@ pub struct CompactContract {
} }
impl 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>) { pub fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>) {
( (
self.abi, self.abi,
@ -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 { impl From<Contract> for CompactContract {
fn from(c: Contract) -> Self { fn from(c: Contract) -> Self {
let (bin, bin_runtime) = if let Some(evm) = c.evm { let (bin, bin_runtime) = if let Some(evm) = c.evm {

View File

@ -218,25 +218,38 @@ impl SolcConfigBuilder {
pub type Artifacts<T> = BTreeMap<String, BTreeMap<String, T>>; pub type Artifacts<T> = BTreeMap<String, BTreeMap<String, T>>;
pub trait Artifact { pub trait Artifact {
/// Returns the artifact's `Abi` and bytecode
fn into_inner(self) -> (Option<Abi>, Option<Bytes>); 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 { impl Artifact for CompactContract {
fn into_inner(self) -> (Option<Abi>, Option<Bytes>) { fn into_inner(self) -> (Option<Abi>, Option<Bytes>) {
(self.abi, self.bin.and_then(|bin| bin.into_bytes())) (self.abi, self.bin.and_then(|bin| bin.into_bytes()))
} }
fn into_compact_contract(self) -> CompactContract {
self
}
} }
impl Artifact for serde_json::Value { impl Artifact for serde_json::Value {
fn into_inner(self) -> (Option<Abi>, Option<Bytes>) { fn into_inner(self) -> (Option<Abi>, Option<Bytes>) {
let abi = self.get("abi").map(|abi| { self.into_compact_contract().into_inner()
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) fn into_compact_contract(self) -> CompactContract {
self.into()
} }
} }