Skip to content

Commit

Permalink
Rename truffle::Artifact to contract::Contract (#543)
Browse files Browse the repository at this point in the history
In preparation for #512, we're separating artifacts (that is, JSON files that contain contracts) from actual contracts data.
  • Loading branch information
Tamika Nomara authored Jun 22, 2021
1 parent 882fa4c commit f4ca8b5
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use std::fs::File;
use std::path::Path;
use web3::types::Address;

/// Represents a truffle artifact.
/// Represents a contract data.
#[derive(Clone, Debug, Deserialize)]
#[serde(default = "Artifact::empty")]
pub struct Artifact {
#[serde(default = "Contract::empty")]
pub struct Contract {
/// The contract name
#[serde(rename = "contractName")]
pub contract_name: String,
Expand All @@ -29,10 +29,10 @@ pub struct Artifact {
pub userdoc: Documentation,
}

impl Artifact {
/// Creates an empty artifact instance.
impl Contract {
/// Creates an empty contract instance.
pub fn empty() -> Self {
Artifact {
Contract {
contract_name: String::new(),
abi: Abi {
constructor: None,
Expand Down Expand Up @@ -100,7 +100,7 @@ mod tests {

#[test]
fn parse_empty() {
if let Err(err) = Artifact::from_json("{}") {
if let Err(err) = Contract::from_json("{}") {
panic!("error parsing empty artifact: {:?}", err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ethcontract-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
pub mod abiext;
pub mod bytecode;
pub mod contract;
pub mod errors;
pub mod hash;
pub mod truffle;

pub use crate::abiext::FunctionExt;
pub use crate::bytecode::Bytecode;
pub use crate::truffle::Artifact;
pub use crate::contract::Contract;
pub use ethabi::{self as abi, Contract as Abi};
use serde::Deserialize;
pub use web3::types::Address;
Expand Down
20 changes: 10 additions & 10 deletions ethcontract-generate/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod types;
use crate::util;
use crate::Args;
use anyhow::{anyhow, Context as _, Result};
use ethcontract_common::{Address, Artifact, DeploymentInformation};
use ethcontract_common::{Address, Contract, DeploymentInformation};
use inflector::Inflector;
use proc_macro2::{Ident, Literal, TokenStream};
use quote::quote;
Expand All @@ -29,8 +29,8 @@ pub(crate) struct Deployment {
pub(crate) struct Context {
/// The artifact JSON as string literal.
artifact_json: Literal,
/// The parsed artifact.
artifact: Artifact,
/// The parsed contract.
contract: Contract,
/// The identifier for the runtime crate. Usually this is `ethcontract` but
/// it can be different if the crate was renamed in the Cargo manifest for
/// example.
Expand All @@ -54,13 +54,13 @@ pub(crate) struct Context {
impl Context {
/// Create a context from the code generation arguments.
fn from_args(args: Args) -> Result<Self> {
let (artifact_json, artifact) = {
let (artifact_json, contract) = {
let artifact_json = args
.artifact_source
.artifact_json()
.context("failed to get artifact JSON")?;

let artifact = Artifact::from_json(&artifact_json)
let contract = Contract::from_json(&artifact_json)
.with_context(|| format!("invalid artifact JSON '{}'", artifact_json))
.with_context(|| {
format!(
Expand All @@ -69,13 +69,13 @@ impl Context {
)
})?;

(Literal::string(&artifact_json), artifact)
(Literal::string(&artifact_json), contract)
};

let raw_contract_name = if let Some(name) = args.contract_name_override.as_ref() {
name
} else if !artifact.contract_name.is_empty() {
&artifact.contract_name
} else if !contract.contract_name.is_empty() {
&contract.contract_name
} else {
return Err(anyhow!(
"contract artifact is missing a name, this can happen when \
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Context {

Ok(Context {
artifact_json,
artifact,
contract,
runtime_crate,
visibility,
contract_mod,
Expand All @@ -137,7 +137,7 @@ impl Default for Context {
fn default() -> Self {
Context {
artifact_json: Literal::string("{}"),
artifact: Artifact::empty(),
contract: Contract::empty(),
runtime_crate: util::ident("ethcontract"),
visibility: Visibility::Inherited,
contract_mod: util::ident("contract"),
Expand Down
12 changes: 6 additions & 6 deletions ethcontract-generate/src/contract/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) fn expand(cx: &Context) -> TokenStream {
let contract_name = &cx.contract_name;

let doc_str = cx
.artifact
.contract
.devdoc
.details
.as_deref()
Expand All @@ -25,7 +25,7 @@ pub(crate) fn expand(cx: &Context) -> TokenStream {
quote! {
artifact.networks.insert(
#network_id.to_owned(),
self::ethcontract::common::truffle::Network {
self::ethcontract::common::contract::Network {
address: #address,
deployment_information: #deployment_information,
},
Expand All @@ -43,14 +43,14 @@ pub(crate) fn expand(cx: &Context) -> TokenStream {
impl Contract {
/// Retrieves the truffle artifact used to generate the type safe
/// API for this contract.
pub fn artifact() -> &'static self::ethcontract::Artifact {
pub fn artifact() -> &'static self::ethcontract::Contract {
use self::ethcontract::private::lazy_static;
use self::ethcontract::Artifact;
use self::ethcontract::Contract;

lazy_static! {
pub static ref ARTIFACT: Artifact = {
pub static ref ARTIFACT: Contract = {
#[allow(unused_mut)]
let mut artifact = Artifact::from_json(#artifact_json)
let mut artifact = Contract::from_json(#artifact_json)
.expect("valid artifact JSON");
#( #deployments )*

Expand Down
8 changes: 4 additions & 4 deletions ethcontract-generate/src/contract/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn expand(cx: &Context) -> Result<TokenStream> {
}

fn expand_deployed(cx: &Context) -> TokenStream {
if cx.artifact.networks.is_empty() && cx.deployments.is_empty() {
if cx.contract.networks.is_empty() && cx.deployments.is_empty() {
return quote! {};
}

Expand Down Expand Up @@ -51,7 +51,7 @@ fn expand_deployed(cx: &Context) -> TokenStream {
}

fn expand_deploy(cx: &Context) -> Result<TokenStream> {
if cx.artifact.bytecode.is_empty() {
if cx.contract.bytecode.is_empty() {
// do not generate deploy method for contracts that have empty bytecode
return Ok(quote! {});
}
Expand All @@ -60,7 +60,7 @@ fn expand_deploy(cx: &Context) -> Result<TokenStream> {
// can't seem to get truffle to output it
let doc = util::expand_doc("Generated by `ethcontract`");

let (input, arg) = match cx.artifact.abi.constructor() {
let (input, arg) = match cx.contract.abi.constructor() {
Some(constructor) => (
methods::expand_inputs(&constructor.inputs)?,
methods::expand_inputs_call_arg(&constructor.inputs),
Expand All @@ -69,7 +69,7 @@ fn expand_deploy(cx: &Context) -> Result<TokenStream> {
};

let libs: Vec<_> = cx
.artifact
.contract
.bytecode
.undefined_libraries()
.map(|name| (name, util::safe_ident(&name.to_snake_case())))
Expand Down
20 changes: 10 additions & 10 deletions ethcontract-generate/src/contract/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn expand(cx: &Context) -> Result<TokenStream> {
/// Expands into a module containing all the event data structures from the ABI.
fn expand_structs_mod(cx: &Context) -> Result<TokenStream> {
let data_types = cx
.artifact
.contract
.abi
.events()
.map(|event| expand_data_type(event, &cx.event_derives))
Expand Down Expand Up @@ -180,7 +180,7 @@ fn expand_data_tuple(
/// streams for all non-anonymous contract events in the ABI.
fn expand_filters(cx: &Context) -> Result<TokenStream> {
let standard_events = cx
.artifact
.contract
.abi
.events()
.filter(|event| !event.anonymous)
Expand Down Expand Up @@ -369,7 +369,7 @@ fn expand_builder_name(event: &Event) -> TokenStream {
/// Expands into the `all_events` method on the root contract type if it
/// contains events. Expands to nothing otherwise.
fn expand_all_events(cx: &Context) -> TokenStream {
if cx.artifact.abi.events.is_empty() {
if cx.contract.abi.events.is_empty() {
return quote! {};
}

Expand Down Expand Up @@ -397,7 +397,7 @@ fn expand_all_events(cx: &Context) -> TokenStream {
/// including anonymous types.
fn expand_event_enum(cx: &Context) -> TokenStream {
let variants = {
let mut events = cx.artifact.abi.events().collect::<Vec<_>>();
let mut events = cx.contract.abi.events().collect::<Vec<_>>();

// NOTE: We sort the events by name so that the generated enum is
// consistent. This also facilitates testing as so that the same ABI
Expand Down Expand Up @@ -430,7 +430,7 @@ fn expand_event_enum(cx: &Context) -> TokenStream {
fn expand_event_parse_log(cx: &Context) -> TokenStream {
let all_events = {
let mut all_events = cx
.artifact
.contract
.abi
.events()
.map(|event| {
Expand Down Expand Up @@ -705,7 +705,7 @@ mod tests {
fn expand_enum_for_all_events() {
let context = {
let mut context = Context::default();
context.artifact.abi.events.insert(
context.contract.abi.events.insert(
"Foo".into(),
vec![Event {
name: "Foo".into(),
Expand All @@ -717,7 +717,7 @@ mod tests {
anonymous: false,
}],
);
context.artifact.abi.events.insert(
context.contract.abi.events.insert(
"Bar".into(),
vec![Event {
name: "Bar".into(),
Expand Down Expand Up @@ -750,7 +750,7 @@ mod tests {
fn expand_parse_log_impl_for_all_events() {
let context = {
let mut context = Context::default();
context.artifact.abi.events.insert(
context.contract.abi.events.insert(
"Foo".into(),
vec![Event {
name: "Foo".into(),
Expand All @@ -762,7 +762,7 @@ mod tests {
anonymous: false,
}],
);
context.artifact.abi.events.insert(
context.contract.abi.events.insert(
"Bar".into(),
vec![Event {
name: "Bar".into(),
Expand All @@ -777,7 +777,7 @@ mod tests {
context
};

let foo_signature = expand_hash(context.artifact.abi.event("Foo").unwrap().signature());
let foo_signature = expand_hash(context.contract.abi.event("Foo").unwrap().signature());
let invalid_data = expand_invalid_data();

assert_quote!(expand_event_parse_log(&context), {
Expand Down
8 changes: 4 additions & 4 deletions ethcontract-generate/src/contract/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn expand(cx: &Context) -> Result<TokenStream> {
fn expand_functions(cx: &Context) -> Result<TokenStream> {
let mut aliases = cx.method_aliases.clone();
let functions = cx
.artifact
.contract
.abi
.functions()
.map(|function| {
Expand Down Expand Up @@ -92,11 +92,11 @@ fn expand_function(cx: &Context, function: &Function, alias: Option<Ident>) -> R
let selector = expand_selector(function.selector());

let doc_str = cx
.artifact
.contract
.devdoc
.methods
.get(&signature)
.or_else(|| cx.artifact.userdoc.methods.get(&signature))
.or_else(|| cx.contract.userdoc.methods.get(&signature))
.and_then(|entry| entry.details.as_ref())
.map(String::as_str)
.unwrap_or("Generated by `ethcontract`");
Expand Down Expand Up @@ -165,7 +165,7 @@ fn expand_selector(selector: H32) -> TokenStream {
/// Expands a context into fallback method when the contract implements one,
/// and an empty token stream otherwise.
fn expand_fallback(cx: &Context) -> TokenStream {
if cx.artifact.abi.fallback {
if cx.contract.abi.fallback {
quote! {
impl Contract {
/// Returns a method builder to setup a call to a smart
Expand Down
14 changes: 6 additions & 8 deletions ethcontract-generate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::path::Path;
/// Internal global arguments passed to the generators for each individual
/// component that control expansion.
pub(crate) struct Args {
/// The source of the truffle artifact JSON for the contract whose bindings
/// The source of the artifact JSON for the contract whose bindings
/// are being generated.
artifact_source: Source,
/// The runtime crate name to use.
Expand All @@ -51,7 +51,7 @@ pub(crate) struct Args {
}

impl Args {
/// Creates a new builder given the path to a contract's truffle artifact
/// Creates a new builder given the path to a contract's artifact
/// JSON file.
pub fn new(source: Source) -> Self {
Args {
Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct Builder {
}

impl Builder {
/// Creates a new builder given the path to a contract's truffle artifact
/// Creates a new builder given the path to a contract's artifact
/// JSON file.
pub fn new<P>(artifact_path: P) -> Self
where
Expand Down Expand Up @@ -158,13 +158,11 @@ impl Builder {

/// Manually adds specifies the deployed address and deployment transaction
/// hash or block of a contract for a given network. Note that manually specified
/// deployments take precedence over deployments in the Truffle artifact (in
/// the `networks` property of the artifact).
/// deployments take precedence over deployments in the artifact.
///
/// This is useful for integration test scenarios where the address of a
/// contract on the test node is deterministic (for example using
/// `ganache-cli -d`) but the contract address is not part of the Truffle
/// artifact; or to override a deployment included in a Truffle artifact.
/// contract on the test node is deterministic, but the contract address
/// is not in the artifact.
pub fn add_deployment(
mut self,
network_id: u32,
Expand Down
Loading

0 comments on commit f4ca8b5

Please sign in to comment.