Skip to content

Commit

Permalink
chore: add artifact convenience read and change contracts fn (gakonst#72
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mattsse authored Oct 1, 2021
1 parent f48ae61 commit 434b49d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 19 additions & 10 deletions dapp/src/artifacts.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use ethers::core::{types::Bytes, utils::CompiledContract};
use eyre::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{
collections::{BTreeMap, HashMap},
path::Path,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DapptoolsArtifact {
contracts: HashMap<String, HashMap<String, serde_json::Value>>,
contracts: BTreeMap<String, BTreeMap<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -40,11 +43,18 @@ where
}

impl DapptoolsArtifact {
pub fn contracts(&self) -> Result<HashMap<String, CompiledContract>> {
let mut map = HashMap::new();
for (key, value) in &self.contracts {
for (contract, data) in value.iter() {
let data: Contract = serde_json::from_value(data.clone())?;
/// Convenience function to read from a file
pub fn read(file: impl AsRef<Path>) -> Result<Self> {
let file = std::fs::File::open(file)?;
Ok(serde_json::from_reader::<_, _>(file)?)
}

/// Returns all the contract from the artifacts
pub fn into_contracts(self) -> Result<HashMap<String, CompiledContract>> {
let mut map = HashMap::with_capacity(self.contracts.len());
for (key, value) in self.contracts {
for (contract, data) in value {
let data: Contract = serde_json::from_value(data)?;
let data = CompiledContract {
abi: data.abi,
bytecode: data.evm.bytecode.object,
Expand All @@ -65,9 +75,8 @@ mod tests {
#[test]
fn parses_dapptools_artifact() {
let path = std::fs::canonicalize("testdata/dapp-artifact.json").unwrap();
let file = std::fs::File::open(path).unwrap();
let data = serde_json::from_reader::<_, DapptoolsArtifact>(file).unwrap();
let contracts = data.contracts().unwrap();
let data = DapptoolsArtifact::read(path).unwrap();
let contracts = data.into_contracts().unwrap();
let mut expected = [
"src/test/Greeter.t.sol:Greet",
"lib/ds-test/src/test.sol:DSTest",
Expand Down
3 changes: 1 addition & 2 deletions dapp/src/multi_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ impl<'a> MultiContractRunnerBuilder<'a> {
// 2. parallel compilation
// 3. Hardhat / Truffle-style artifacts
let contracts = if self.no_compile {
let out_file = std::fs::read_to_string(&self.out_path)?;
serde_json::from_str::<DapptoolsArtifact>(&out_file)?.contracts()?
DapptoolsArtifact::read(self.out_path)?.into_contracts()?
} else {
SolcBuilder::new(self.contracts, self.remappings, self.libraries)?.build_all()?
};
Expand Down

0 comments on commit 434b49d

Please sign in to comment.