diff --git a/ethcontract-common/src/truffle.rs b/ethcontract-common/src/contract.rs similarity index 94% rename from ethcontract-common/src/truffle.rs rename to ethcontract-common/src/contract.rs index ce35dd3b..4abc1ef1 100644 --- a/ethcontract-common/src/truffle.rs +++ b/ethcontract-common/src/contract.rs @@ -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, @@ -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, @@ -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); } } diff --git a/ethcontract-common/src/lib.rs b/ethcontract-common/src/lib.rs index 9aa47e47..8ca95566 100644 --- a/ethcontract-common/src/lib.rs +++ b/ethcontract-common/src/lib.rs @@ -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; diff --git a/ethcontract-generate/src/contract.rs b/ethcontract-generate/src/contract.rs index 79dd329d..d79234b5 100644 --- a/ethcontract-generate/src/contract.rs +++ b/ethcontract-generate/src/contract.rs @@ -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; @@ -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. @@ -54,13 +54,13 @@ pub(crate) struct Context { impl Context { /// Create a context from the code generation arguments. fn from_args(args: Args) -> Result { - 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!( @@ -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 \ @@ -120,7 +120,7 @@ impl Context { Ok(Context { artifact_json, - artifact, + contract, runtime_crate, visibility, contract_mod, @@ -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"), diff --git a/ethcontract-generate/src/contract/common.rs b/ethcontract-generate/src/contract/common.rs index c3971f4f..3a00cdb7 100644 --- a/ethcontract-generate/src/contract/common.rs +++ b/ethcontract-generate/src/contract/common.rs @@ -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() @@ -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, }, @@ -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 )* diff --git a/ethcontract-generate/src/contract/deployment.rs b/ethcontract-generate/src/contract/deployment.rs index 6671c199..0e2eccf7 100644 --- a/ethcontract-generate/src/contract/deployment.rs +++ b/ethcontract-generate/src/contract/deployment.rs @@ -17,7 +17,7 @@ pub(crate) fn expand(cx: &Context) -> Result { } 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! {}; } @@ -51,7 +51,7 @@ fn expand_deployed(cx: &Context) -> TokenStream { } fn expand_deploy(cx: &Context) -> Result { - 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! {}); } @@ -60,7 +60,7 @@ fn expand_deploy(cx: &Context) -> Result { // 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), @@ -69,7 +69,7 @@ fn expand_deploy(cx: &Context) -> Result { }; let libs: Vec<_> = cx - .artifact + .contract .bytecode .undefined_libraries() .map(|name| (name, util::safe_ident(&name.to_snake_case()))) diff --git a/ethcontract-generate/src/contract/events.rs b/ethcontract-generate/src/contract/events.rs index 3e52006e..a9103b94 100644 --- a/ethcontract-generate/src/contract/events.rs +++ b/ethcontract-generate/src/contract/events.rs @@ -23,7 +23,7 @@ pub(crate) fn expand(cx: &Context) -> Result { /// Expands into a module containing all the event data structures from the ABI. fn expand_structs_mod(cx: &Context) -> Result { let data_types = cx - .artifact + .contract .abi .events() .map(|event| expand_data_type(event, &cx.event_derives)) @@ -180,7 +180,7 @@ fn expand_data_tuple( /// streams for all non-anonymous contract events in the ABI. fn expand_filters(cx: &Context) -> Result { let standard_events = cx - .artifact + .contract .abi .events() .filter(|event| !event.anonymous) @@ -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! {}; } @@ -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::>(); + let mut events = cx.contract.abi.events().collect::>(); // NOTE: We sort the events by name so that the generated enum is // consistent. This also facilitates testing as so that the same ABI @@ -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| { @@ -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(), @@ -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(), @@ -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(), @@ -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(), @@ -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), { diff --git a/ethcontract-generate/src/contract/methods.rs b/ethcontract-generate/src/contract/methods.rs index f5196ef3..b8d96838 100644 --- a/ethcontract-generate/src/contract/methods.rs +++ b/ethcontract-generate/src/contract/methods.rs @@ -24,7 +24,7 @@ pub(crate) fn expand(cx: &Context) -> Result { fn expand_functions(cx: &Context) -> Result { let mut aliases = cx.method_aliases.clone(); let functions = cx - .artifact + .contract .abi .functions() .map(|function| { @@ -92,11 +92,11 @@ fn expand_function(cx: &Context, function: &Function, alias: Option) -> 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`"); @@ -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 diff --git a/ethcontract-generate/src/lib.rs b/ethcontract-generate/src/lib.rs index a1b3122a..092bc1a7 100644 --- a/ethcontract-generate/src/lib.rs +++ b/ethcontract-generate/src/lib.rs @@ -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. @@ -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 { @@ -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

(artifact_path: P) -> Self where @@ -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, diff --git a/ethcontract-generate/src/source.rs b/ethcontract-generate/src/source.rs index 34c00081..45fc34ba 100644 --- a/ethcontract-generate/src/source.rs +++ b/ethcontract-generate/src/source.rs @@ -10,17 +10,17 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use url::Url; -/// A source of a Truffle artifact JSON. +/// A source of an artifact JSON. #[derive(Clone, Debug, Eq, PartialEq)] pub enum Source { - /// A Truffle artifact or ABI located on the local file system. + /// An artifact or ABI located on the local file system. Local(PathBuf), - /// A truffle artifact or ABI to be retrieved over HTTP(S). + /// An artifact or ABI to be retrieved over HTTP(S). Http(Url), /// An address of a mainnet contract that has been verified on Etherscan.io. Etherscan(Address), - /// The package identifier of an npm package with a path to a Truffle - /// artifact or ABI to be retrieved from `unpkg.io`. + /// The package identifier of an npm package with a path to an artifact + /// or ABI to be retrieved from `unpkg.io`. Npm(String), } @@ -30,14 +30,14 @@ impl Source { /// Contract artifacts can be retrieved from the local filesystem or online /// from `etherscan.io`, this method parses artifact source URLs and accepts /// the following: - /// - `relative/path/to/Contract.json`: a relative path to a truffle + /// - `relative/path/to/Contract.json`: a relative path to an /// artifact JSON file. This relative path is rooted in the current /// working directory. To specify the root for relative paths, use /// `Source::with_root`. /// - `/absolute/path/to/Contract.json` or /// `file:///absolute/path/to/Contract.json`: an absolute path or file URL - /// to a truffle artifact JSON file. - /// - `http(s)://...` an HTTP url to a contract ABI or Truffle artifact. + /// to an artifact JSON file. + /// - `http(s)://...` an HTTP url to a contract ABI or a artifact. /// - `etherscan:0xXX..XX` or `https://etherscan.io/address/0xXX..XX`: a /// address or URL of a verified contract on Etherscan. /// - `npm:@org/package@1.0.0/path/to/contract.json` an npmjs package with @@ -136,7 +136,7 @@ impl FromStr for Source { } } -/// Reads a Truffle artifact JSON file from the local filesystem. +/// Reads an artifact JSON file from the local filesystem. fn get_local_contract(path: &Path) -> Result { let path = if path.is_relative() { let absolute_path = path.canonicalize().with_context(|| { @@ -157,7 +157,7 @@ fn get_local_contract(path: &Path) -> Result { Ok(abi_or_artifact(json)) } -/// Retrieves a Truffle artifact or ABI from an HTTP URL. +/// Retrieves an artifact or ABI from an HTTP URL. fn get_http_contract(url: &Url) -> Result { let json = util::http_get(url.as_str()) .with_context(|| format!("failed to retrieve JSON from {}", url))?; @@ -193,7 +193,7 @@ fn get_etherscan_contract(address: Address) -> Result { Ok(json) } -/// Retrieves a Truffle artifact or ABI from an npm package through `unpkg.com`. +/// Retrieves an artifact or ABI from an npm package through `unpkg.com`. fn get_npm_contract(package: &str) -> Result { let unpkg_url = format!("https://unpkg.com/{}", package); let json = util::http_get(&unpkg_url) @@ -202,16 +202,16 @@ fn get_npm_contract(package: &str) -> Result { Ok(abi_or_artifact(json)) } -/// A best-effort coercion of an ABI or Truffle artifact JSON document into a -/// Truffle artifact JSON document. +/// A best-effort coercion of an ABI or an artifact JSON document into an +/// artifact JSON document. /// -/// This method uses the fact that ABIs are arrays and Truffle artifacts are +/// This method uses the fact that ABIs are arrays and artifacts are /// objects to guess at what type of document this is. Note that no parsing or /// validation is done at this point as the document gets parsed and validated /// at generation time. /// /// This needs to be done as currently the contract generation infrastructure -/// depends on having a Truffle artifact. +/// depends on having an artifact. fn abi_or_artifact(json: String) -> String { if json.trim().starts_with('[') { format!(r#"{{"abi":{}}}"#, json.trim()) diff --git a/ethcontract/src/contract.rs b/ethcontract/src/contract.rs index 1cf4cd9e..3e7e3533 100644 --- a/ethcontract/src/contract.rs +++ b/ethcontract/src/contract.rs @@ -13,7 +13,7 @@ use crate::{ use ethcontract_common::abi::{Error as AbiError, Result as AbiResult}; use ethcontract_common::abiext::FunctionExt; use ethcontract_common::hash::H32; -use ethcontract_common::{Abi, Artifact, Bytecode, DeploymentInformation}; +use ethcontract_common::{Abi, Bytecode, Contract, DeploymentInformation}; use std::collections::HashMap; use std::hash::Hash; use web3::api::Web3; @@ -86,44 +86,44 @@ impl Instance { } /// Locates a deployed contract based on the current network ID reported by - /// the `web3` provider from the given `Artifact`'s ABI and networks. + /// the `web3` provider from the given `Contract`'s ABI and networks. /// - /// Note that this does not verify that a contract with a matchin `Abi` is + /// Note that this does not verify that a contract with a matching `Abi` is /// actually deployed at the given address. - pub async fn deployed(web3: Web3, artifact: Artifact) -> Result { + pub async fn deployed(web3: Web3, contract: Contract) -> Result { let network_id = web3.net().version().await?; - let network = artifact + let network = contract .networks .get(&network_id) .ok_or(DeployError::NotFound(network_id))?; Ok(Instance::with_deployment_info( web3, - artifact.abi, + contract.abi, network.address, network.deployment_information, )) } /// Creates a contract builder with the specified `web3` provider and the - /// given `Artifact` byte code. This allows the contract deployment + /// given `Contract` byte code. This allows the contract deployment /// transaction to be configured before deploying the contract. pub fn builder

( web3: Web3, - artifact: Artifact, + contract: Contract, params: P, ) -> Result, DeployError> where P: Tokenize, { - Linker::new(artifact).deploy(web3, params) + Linker::new(contract).deploy(web3, params) } /// Deploys a contract with the specified `web3` provider with the given - /// `Artifact` byte code and linking libraries. + /// `Contract` byte code and linking libraries. pub fn link_and_deploy<'a, P, I>( web3: Web3, - artifact: Artifact, + contract: Contract, params: P, libraries: I, ) -> Result, DeployError> @@ -131,7 +131,7 @@ impl Instance { P: Tokenize, I: Iterator, { - let mut linker = Linker::new(artifact); + let mut linker = Linker::new(contract); for (name, address) in libraries { linker = linker.library(name, address)?; } @@ -259,11 +259,11 @@ pub struct Linker { } impl Linker { - /// Create a new linker for a contract artifact. - pub fn new(artifact: Artifact) -> Linker { + /// Create a new linker for a contract. + pub fn new(contract: Contract) -> Linker { Linker { - abi: artifact.abi, - bytecode: artifact.bytecode, + abi: contract.abi, + bytecode: contract.bytecode, } } @@ -350,8 +350,8 @@ where mod tests { use super::*; use crate::test::prelude::*; - use ethcontract_common::truffle::Network; - use ethcontract_common::Artifact; + use ethcontract_common::contract::Network; + use ethcontract_common::Contract; use web3::types::H256; #[test] @@ -361,20 +361,20 @@ mod tests { let network_id = "42"; let address = addr!("0x0102030405060708091011121314151617181920"); - let artifact = { - let mut artifact = Artifact::empty(); - artifact.networks.insert( + let contract = { + let mut contract = Contract::empty(); + contract.networks.insert( network_id.to_string(), Network { address, deployment_information: Some(H256::repeat_byte(0x42).into()), }, ); - artifact + contract }; transport.add_response(json!(network_id)); // get network ID response - let instance = Instance::deployed(web3, artifact) + let instance = Instance::deployed(web3, contract) .immediate() .expect("successful deployment"); @@ -398,7 +398,7 @@ mod tests { let network_id = "42"; transport.add_response(json!(network_id)); // get network ID response - let err = Instance::deployed(web3, Artifact::empty()) + let err = Instance::deployed(web3, Contract::empty()) .immediate() .expect_err("unexpected success getting deployed contract"); diff --git a/ethcontract/src/contract/deploy.rs b/ethcontract/src/contract/deploy.rs index ba948780..489b6ca3 100644 --- a/ethcontract/src/contract/deploy.rs +++ b/ethcontract/src/contract/deploy.rs @@ -58,7 +58,7 @@ where T: Transport, I: Deploy, { - /// Create a new deploy builder from a `web3` provider, artifact data and + /// Create a new deploy builder from a `web3` provider, contract data and /// deployment (constructor) parameters. pub fn new

(web3: Web3, context: I::Context, params: P) -> Result where @@ -168,7 +168,7 @@ mod tests { use super::*; use crate::contract::{Instance, Linker}; use crate::test::prelude::*; - use ethcontract_common::{Artifact, Bytecode}; + use ethcontract_common::{Bytecode, Contract}; type InstanceDeployBuilder = DeployBuilder>; @@ -179,11 +179,11 @@ mod tests { let from = addr!("0x9876543210987654321098765432109876543210"); let bytecode = Bytecode::from_hex_str("0x42").unwrap(); - let artifact = Artifact { + let contract = Contract { bytecode: bytecode.clone(), - ..Artifact::empty() + ..Contract::empty() }; - let linker = Linker::new(artifact); + let linker = Linker::new(contract); let tx = InstanceDeployBuilder::new(web3, linker, ()) .expect("error creating deploy builder") .from(Account::Local(from, None)) @@ -214,8 +214,8 @@ mod tests { let transport = TestTransport::new(); let web3 = Web3::new(transport.clone()); - let artifact = Artifact::empty(); - let linker = Linker::new(artifact); + let contract = Contract::empty(); + let linker = Linker::new(contract); let error = InstanceDeployBuilder::new(web3, linker, ()).err().unwrap(); assert_eq!(error.to_string(), DeployError::EmptyBytecode.to_string()); diff --git a/ethcontract/src/lib.rs b/ethcontract/src/lib.rs index df2b608d..1db1ec44 100644 --- a/ethcontract/src/lib.rs +++ b/ethcontract/src/lib.rs @@ -106,7 +106,7 @@ pub mod transport; pub use crate::contract::Instance; pub use crate::prelude::*; pub use ethcontract_common as common; -pub use ethcontract_common::truffle::Artifact; +pub use ethcontract_common::contract::Contract; #[cfg(feature = "derive")] pub use ethcontract_derive::contract; pub use futures;