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

feat(contract, abigen): support artifact format in proc macro #480

Merged
merged 2 commits into from
Oct 2, 2021
Merged
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
28 changes: 20 additions & 8 deletions ethers-contract/ethers-contract-abigen/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use super::Abigen;
use crate::contract::structs::InternalStructs;
use crate::rawabi::RawAbi;
use anyhow::{anyhow, Context as _, Result};
use ethers_core::abi::AbiParser;
use ethers_core::{
abi::{parse_abi, Abi},
abi::{parse_abi, Abi, AbiParser},
types::Address,
};
use inflector::Inflector;
use proc_macro2::{Ident, Literal, TokenStream};
use quote::quote;
use serde::Deserialize;
use std::collections::BTreeMap;
use syn::{Path, Visibility};

Expand Down Expand Up @@ -121,13 +121,25 @@ impl Context {
// get the actual ABI string
let abi_str = args.abi_source.get().context("failed to get ABI JSON")?;
let mut abi_parser = AbiParser::default();
// parse it
let (abi, human_readable): (Abi, _) = if let Ok(abi) = serde_json::from_str(&abi_str) {
// normal abi format
(abi, false)

let (abi, human_readable): (Abi, _) = if let Ok(abi) = abi_parser.parse_str(&abi_str) {
(abi, true)
} else {
// heuristic for parsing the human readable format
(abi_parser.parse_str(&abi_str)?, true)
// a best-effort coercion of an ABI or an artifact JSON into an artifact JSON.
let json_abi_str = if abi_str.trim().starts_with('[') {
format!(r#"{{"abi":{}}}"#, abi_str.trim())
} else {
abi_str.clone()
};

#[derive(Deserialize)]
struct Contract {
abi: Abi,
}

let contract = serde_json::from_str::<Contract>(&json_abi_str)?;

(contract.abi, false)
};

// try to extract all the solidity structs from the normal JSON ABI
Expand Down