Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migration to Alloy #3

Merged
merged 12 commits into from
Oct 4, 2023
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ rustdoc-args = ["--cfg", "docsrs"]
all-features = true

[dependencies]
ethers-core = { version = "2", default-features = false }
alloy-primitives = { version = "0.4", default-features = false, features = ["std", "serde", "rand"] }
alloy-dyn-abi = { version = "0.4", default-features = false, features = ["std"] }
alloy-json-abi = { version = "0.4", default-features = false, features = ["std", "serde_json"] }

solang-parser = { version = "=0.3.2", default-features = false }

Expand All @@ -41,7 +43,6 @@ yansi = "0.5.1"
dirs = "5.0"
glob = "0.3.1"


num_cpus = "1.16.0"
dunce = "1.0"
rayon = "1.7"
Expand Down
2 changes: 1 addition & 1 deletion benches/read_all.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! read many sources
#[macro_use]
extern crate criterion;
extern crate rand;

use criterion::Criterion;
use ethers_core::rand;
use foundry_compilers::{artifacts::Source, project_util::TempProject};
use rand::{distributions::Alphanumeric, Rng};
use std::{
Expand Down
21 changes: 11 additions & 10 deletions src/artifact_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use crate::{
sources::VersionedSourceFile,
utils, HardhatArtifact, ProjectPathsConfig, SolFilesCache, SolcError, SolcIoError,
};
use ethers_core::{abi::Abi, types::Bytes};
use alloy_json_abi::JsonAbi;
use alloy_primitives::Bytes;
use semver::Version;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
Expand Down Expand Up @@ -426,7 +427,7 @@ impl<T> Artifacts<T> {
/// A trait representation for a [`crate::Contract`] artifact
pub trait Artifact {
/// Returns the artifact's `Abi` and bytecode
fn into_inner(self) -> (Option<Abi>, Option<Bytes>);
fn into_inner(self) -> (Option<JsonAbi>, Option<Bytes>);

/// Turns the artifact into a container type for abi, compact bytecode and deployed bytecode
fn into_compact_contract(self) -> CompactContract;
Expand All @@ -435,10 +436,10 @@ pub trait Artifact {
fn into_contract_bytecode(self) -> CompactContractBytecode;

/// 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>);
fn into_parts(self) -> (Option<JsonAbi>, Option<Bytes>, Option<Bytes>);

/// Consumes the type and returns the [Abi]
fn into_abi(self) -> Option<Abi>
/// Consumes the type and returns the [JsonAbi]
fn into_abi(self) -> Option<JsonAbi>
where
Self: Sized,
{
Expand All @@ -461,7 +462,7 @@ pub trait Artifact {
}

/// Same as [`Self::into_parts()`] but returns `Err` if an element is `None`
fn try_into_parts(self) -> Result<(Abi, Bytes, Bytes)>
fn try_into_parts(self) -> Result<(JsonAbi, Bytes, Bytes)>
where
Self: Sized,
{
Expand Down Expand Up @@ -524,8 +525,8 @@ pub trait Artifact {
Some(val)
}

/// Returns the reference to the [Abi] if available
fn get_abi(&self) -> Option<Cow<Abi>> {
/// Returns the reference to the [JsonAbi] if available
fn get_abi(&self) -> Option<Cow<JsonAbi>> {
self.get_contract_bytecode().abi
}

Expand Down Expand Up @@ -567,7 +568,7 @@ where
T: Into<CompactContractBytecode> + Into<CompactContract>,
for<'a> &'a T: Into<CompactContractBytecodeCow<'a>>,
{
fn into_inner(self) -> (Option<Abi>, Option<Bytes>) {
fn into_inner(self) -> (Option<JsonAbi>, Option<Bytes>) {
let artifact = self.into_compact_contract();
(artifact.abi, artifact.bin.and_then(|bin| bin.into_bytes()))
}
Expand All @@ -580,7 +581,7 @@ where
self.into()
}

fn into_parts(self) -> (Option<Abi>, Option<Bytes>, Option<Bytes>) {
fn into_parts(self) -> (Option<JsonAbi>, Option<Bytes>, Option<Bytes>) {
self.into_compact_contract().into_parts()
}

Expand Down
2 changes: 1 addition & 1 deletion src/artifacts/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
sourcemap::{self, SourceMap, SyntaxError},
utils,
};
use ethers_core::{abi::Address, types::Bytes};
use alloy_primitives::{Address, Bytes};
use serde::{Deserialize, Serialize, Serializer};
use std::collections::BTreeMap;

Expand Down
3 changes: 2 additions & 1 deletion src/artifacts/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use crate::artifacts::{
serde_helpers, DevDoc, Evm, Ewasm, LosslessAbi, LosslessMetadata, Offsets, StorageLayout,
UserDoc,
};
use ethers_core::{abi::Contract as Abi, types::Bytes};
use alloy_json_abi::JsonAbi as Abi;
use alloy_primitives::Bytes;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap, convert::TryFrom};

Expand Down
14 changes: 4 additions & 10 deletions src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
compile::*, error::SolcIoError, remappings::Remapping, utils, ProjectPathsConfig, SolcError,
};
use ethers_core::abi::Abi;
use alloy_json_abi::JsonAbi as Abi;
use md5::Digest;
use semver::Version;
use serde::{de::Visitor, Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -1635,13 +1635,7 @@ impl OutputContracts {
}
}

/// A helper type that ensures lossless (de)serialisation unlike [`ethers_core::abi::Abi`] which
/// omits some information of (nested) components in a serde roundtrip. This is a problem for
/// abienconderv2 structs because [`ethers_core::abi::Contract`]'s representation of those are
/// [`ethers_core::abi::Param`] and the `kind` field of type [`ethers_core::abi::ParamType`] does
/// not support deeply nested components as it's the case for structs. This is not easily fixable in
/// ethabi as it would require a redesign of the overall `Param` and `ParamType` types. Instead,
/// this type keeps a copy of the [`serde_json::Value`] when deserialized from the `solc` json
/// This type keeps a copy of the [`serde_json::Value`] when deserialized from the `solc` json
/// compiler output and uses it to serialize the `abi` without loss.
#[derive(Clone, Debug, PartialEq)]
pub struct LosslessAbi {
Expand Down Expand Up @@ -1678,7 +1672,7 @@ impl<'de> Deserialize<'de> for LosslessAbi {
D: Deserializer<'de>,
{
let abi_value = serde_json::Value::deserialize(deserializer)?;
let abi = serde_json::from_value(abi_value.clone()).map_err(serde::de::Error::custom)?;
let abi = Abi::from_json_str(&abi_value.to_string()).map_err(serde::de::Error::custom)?;
Ok(Self { abi_value, abi })
}
}
Expand Down Expand Up @@ -2270,7 +2264,7 @@ impl SourceFiles {
mod tests {
use super::*;
use crate::AggregatedCompilerOutput;
use ethers_core::types::Address;
use alloy_primitives::Address;
use std::{fs, path::PathBuf};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/artifacts/serde_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! serde helpers

use ethers_core::types::Bytes;
use alloy_primitives::Bytes;
use serde::{Deserialize, Deserializer};

pub fn deserialize_bytes<'de, D>(d: D) -> Result<Bytes, D::Error>
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ mod tests {
let project = Project::builder().paths(paths).no_artifacts().ephemeral().build().unwrap();
let contracts = project.compile().unwrap().succeeded().output().contracts;
// Contracts A to F
assert_eq!(contracts.contracts().count(), 5);
assert_eq!(contracts.contracts().count(), 3);
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions test-data/test-contract-versions/pinned-0.4.14.sol
Copy link
Member

Choose a reason for hiding this comment

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

what's with these tests?

Copy link
Member Author

@Evalir Evalir Oct 4, 2023

Choose a reason for hiding this comment

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

Noticed alloy_json_abi::JsonAbi doesn't parse pre-0.5.x ABIs and we had these tests expecting this to be the case from ethabi. This shouldn't be an issue for foundry as ds-test is >=0.5.0 though, and we should prob just remove them00.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma solidity =0.4.14;
// pragma solidity =0.4.14;

contract D {
function foo() public {}
}
// contract D {
// function foo() public {}
// }
8 changes: 4 additions & 4 deletions test-data/test-contract-versions/simple-0.4.14.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pragma solidity =0.4.14;
// pragma solidity =0.4.14;

contract F {
function foo() public {}
}
// contract F {
// function foo() public {}
// }
8 changes: 4 additions & 4 deletions tests/project.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! project tests

use ethers_core::types::Address;
use alloy_primitives::Address;
use foundry_compilers::{
artifacts::{
BytecodeHash, DevDoc, ErrorDoc, EventDoc, Libraries, MethodDoc, ModelCheckerEngine::CHC,
Expand Down Expand Up @@ -1117,7 +1117,7 @@ library MyLib {
// provide the library settings to let solc link
tmp.project_mut().solc_config.settings.libraries = BTreeMap::from([(
lib,
BTreeMap::from([("MyLib".to_string(), format!("{:?}", Address::zero()))]),
BTreeMap::from([("MyLib".to_string(), format!("{:?}", Address::ZERO))]),
)])
.into();

Expand All @@ -1129,7 +1129,7 @@ library MyLib {
let bytecode = &contract.bytecode.as_ref().unwrap().object;
assert!(!bytecode.is_unlinked());

let libs = Libraries::parse(&[format!("./src/MyLib.sol:MyLib:{:?}", Address::zero())]).unwrap();
let libs = Libraries::parse(&[format!("./src/MyLib.sol:MyLib:{:?}", Address::ZERO)]).unwrap();
// provide the library settings to let solc link
tmp.project_mut().solc_config.settings.libraries = libs.with_applied_remappings(tmp.paths());

Expand Down Expand Up @@ -1187,7 +1187,7 @@ library MyLib {
assert!(bytecode.is_unlinked());

let libs =
Libraries::parse(&[format!("remapping/MyLib.sol:MyLib:{:?}", Address::zero())]).unwrap(); // provide the library settings to let solc link
Libraries::parse(&[format!("remapping/MyLib.sol:MyLib:{:?}", Address::ZERO)]).unwrap(); // provide the library settings to let solc link
tmp.project_mut().solc_config.settings.libraries = libs.with_applied_remappings(tmp.paths());

let compiled = tmp.compile().unwrap();
Expand Down