1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
//! Implements artifact format generated by [hardhat-deploy] plugin.
//!
//! There are three distinct artifact formats.
//!
//! First is called "hardhat export", it is a JSON file that contains
//! information about a single network and all contracts deployed on it.
//! It can be generated with `hardhat export` command.
//!
//! Second is called "hardhat multi-export", it contains information about
//! multiple networks, for each network it contains information about
//! all contracts deployed on it. It can be generated with
//! `hardhat export --export-all` command.
//!
//! Third is hardhat's `deployments` directory. It contains more details about
//! contracts than the previous two formats. Specifically, it has info about
//! deployed bytecode, deployment transaction receipt, documentation for
//! contract methods, and some other things. Given that it is a directory,
//! there are obvious issues with loading it over network. For this reason,
//! we don't recommend this export format for public libraries that export
//! contracts.
//!
//! All three formats are supported by [`HardHatLoader`], see its documentation
//! for info and limitations.
//!
//! [hardhat-deploy]: https://github.com/wighawag/hardhat-deploy
use crate::artifact::Artifact;
use crate::contract::Network;
use crate::errors::ArtifactError;
use crate::{Address, Contract, DeploymentInformation, TransactionHash};
use serde::Deserialize;
use serde_json::{from_reader, from_slice, from_str, from_value, Value};
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
/// Loads hardhat artifacts generated via `--export` and `--export-all`.
///
/// # Limitations
///
/// In hardhat, a contract could have different ABIs on different networks.
/// This could happen when deploying test versions of contracts.
/// Ethcontract does not support this. Parsing such artifact will result
/// in an error. You'll have to rename contracts, or filter out networks
/// with [`networks_allow_list`].
///
/// Another limitation is that hardhat allows having multiple networks
/// with the same chain ID. For example, you can have `rinkeby`
/// and `rinkeby-testing`. Both have chain ID of `4`, but contract addresses
/// and ABIs can be different. Ethcontract does not support this, so you'll
/// have to filter such networks. See [#545] for more info.
///
/// [#545]: https://github.com/gnosis/ethcontract-rs/issues/545.
#[must_use = "hardhat loaders do nothing unless you load them"]
pub struct HardHatLoader {
/// Override for artifact's origin. If `None`, origin
/// will be derived automatically.
pub origin: Option<String>,
/// List of allowed network names and chain IDs.
///
/// When loading a contract, networks with names that aren't found
/// in this list will be completely ignored. Contracts from these networks
/// will not be loaded. You can use this mechanism to bypass
/// the requirement that a contract must have the same ABI on all networks.
///
/// Empty list means that all networks are allowed.
pub networks_allow_list: Vec<NetworkEntry>,
/// List of denied network names and chain IDs.
///
/// When loading a contract, networks with names that are found
/// in this list will be completely ignored.
///
/// Empty list means that no networks are denied.
///
/// Deny list takes precedence over allow list. That is, if network
/// appears in both, it will be denied.
pub networks_deny_list: Vec<NetworkEntry>,
/// List of allowed contract names.
///
/// When loading artifact, loader will only load contracts if their names
/// are present in this list.
///
/// Empty list means that all contracts are allowed.
pub contracts_allow_list: Vec<String>,
/// List of denied contract names.
///
/// When loading artifact, loader will not load contracts if their names
/// are present in this list.
///
/// Empty list means that no contracts are denied.
///
/// Deny list takes precedence over allow list. That is, if contract
/// appears in both, it will be denied.
pub contracts_deny_list: Vec<String>,
}
impl HardHatLoader {
/// Creates a new hardhat loader.
pub fn new() -> Self {
HardHatLoader {
origin: None,
networks_deny_list: Vec::new(),
networks_allow_list: Vec::new(),
contracts_allow_list: Vec::new(),
contracts_deny_list: Vec::new(),
}
}
/// Creates a new hardhat loader and sets an override for artifact's origins.z
pub fn with_origin(origin: impl Into<String>) -> Self {
HardHatLoader {
origin: Some(origin.into()),
networks_deny_list: Vec::new(),
networks_allow_list: Vec::new(),
contracts_allow_list: Vec::new(),
contracts_deny_list: Vec::new(),
}
}
/// Sets new override for artifact's origin. See [`origin`] for more info.
///
/// [`origin`]: #structfield.origin
pub fn origin(mut self, origin: impl Into<String>) -> Self {
self.origin = Some(origin.into());
self
}
/// Adds chain id to the list of [`allowed networks`].
///
/// [`allowed networks`]: #structfield.networks_allow_list
pub fn allow_network_by_chain_id(mut self, network: impl Into<String>) -> Self {
self.networks_allow_list
.push(NetworkEntry::ByChainId(network.into()));
self
}
/// Adds network name to the list of [`allowed networks`].
///
/// [`allowed networks`]: #structfield.networks_allow_list
pub fn allow_network_by_name(mut self, network: impl Into<String>) -> Self {
self.networks_allow_list
.push(NetworkEntry::ByName(network.into()));
self
}
/// Adds chain id to the list of [`denied networks`].
///
/// [`denied networks`]: #structfield.networks_deny_list
pub fn deny_network_by_chain_id(mut self, network: impl Into<String>) -> Self {
self.networks_deny_list
.push(NetworkEntry::ByChainId(network.into()));
self
}
/// Adds network name to the list of [`denied networks`].
///
/// [`denied networks`]: #structfield.networks_deny_list
pub fn deny_network_by_name(mut self, network: impl Into<String>) -> Self {
self.networks_deny_list
.push(NetworkEntry::ByName(network.into()));
self
}
/// Adds contract name to the list of [`allowed contracts`].
///
/// [`allowed contracts`]: #structfield.contracts_allow_list
pub fn allow_contract(mut self, contract: impl Into<String>) -> Self {
self.contracts_allow_list.push(contract.into());
self
}
/// Adds contract name to the list of [`denied contracts`].
///
/// [`denied contracts`]: #structfield.contracts_deny_list
pub fn deny_contract(mut self, contract: impl Into<String>) -> Self {
self.contracts_deny_list.push(contract.into());
self
}
/// Loads an artifact from a JSON value.
pub fn load_from_reader(&self, f: Format, v: impl Read) -> Result<Artifact, ArtifactError> {
self.load_artifact(f, "<unknown>", v, from_reader, from_reader)
}
/// Loads an artifact from bytes of JSON text.
pub fn load_from_slice(&self, f: Format, v: &[u8]) -> Result<Artifact, ArtifactError> {
self.load_artifact(f, "<unknown>", v, from_slice, from_slice)
}
/// Loads an artifact from string of JSON text.
pub fn load_from_str(&self, f: Format, v: &str) -> Result<Artifact, ArtifactError> {
self.load_artifact(f, "<unknown>", v, from_str, from_str)
}
/// Loads an artifact from a loaded JSON value.
pub fn load_from_value(&self, f: Format, v: Value) -> Result<Artifact, ArtifactError> {
self.load_artifact(f, "<unknown>", v, from_value, from_value)
}
/// Loads an artifact from disk.
pub fn load_from_file(
&self,
f: Format,
p: impl AsRef<Path>,
) -> Result<Artifact, ArtifactError> {
let path = p.as_ref();
let file = File::open(path)?;
let reader = BufReader::new(file);
self.load_artifact(f, path.display(), reader, from_reader, from_reader)
}
/// Loads an artifact from `deployments` directory.
pub fn load_from_directory(&self, p: impl AsRef<Path>) -> Result<Artifact, ArtifactError> {
self._load_from_directory(p.as_ref())
}
/// Helper for `load_from_directory`. We use this helper function to avoid
/// making a big chunk of code generic over `AsRef<Path>`.
///
/// # Implementation note
///
/// Layout of the `deployments` directory looks like this:
///
/// ```text
/// deployments
/// |
/// +-- main
/// | |
/// | +-- .chainId
/// | |
/// | +-- Contract1.json
/// | |
/// | +-- Contract2.json
/// | |
/// | ...
/// |
/// +-- rinkeby
/// | |
/// | +-- .chainId
/// | |
/// | +-- Contract1.json
/// | |
/// | +-- Contract2.json
/// | |
/// | ...
/// |
/// ...
/// ```
///
/// There's a directory for each network. Within network's directory,
/// there's a `.chainId` file containing chain identifier encoded
/// as plain text. Next to `.chainId` file, there are JSON files for each
/// contract deployed to this network.
fn _load_from_directory(&self, p: &Path) -> Result<Artifact, ArtifactError> {
let mut artifact = Artifact::with_origin(p.display().to_string());
let mut chain_id_buf = String::new();
for chain_entry in p.read_dir()? {
let chain_entry = chain_entry?;
let chain_path = chain_entry.path();
if !chain_path.is_dir() {
continue;
}
let chain_id_file = chain_path.join(".chainId");
if !chain_id_file.exists() {
continue;
}
chain_id_buf.clear();
File::open(chain_id_file)?.read_to_string(&mut chain_id_buf)?;
let chain_id = chain_id_buf.trim().to_string();
let chain_name = chain_path
.file_name()
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("unable to get directory name for path {:?}", chain_path),
)
})?
.to_string_lossy();
if !self.network_allowed(&chain_id, &chain_name) {
continue;
}
for contract_entry in chain_path.read_dir()? {
let contract_entry = contract_entry?;
let contract_path = contract_entry.path();
if !contract_path.is_file() {
continue;
}
let mut contract_name = contract_path
.file_name()
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("unable to get file name for path {:?}", contract_path),
)
})?
.to_string_lossy()
.into_owned();
if !contract_name.ends_with(".json") {
continue;
}
contract_name.truncate(contract_name.len() - ".json".len());
if !self.contract_allowed(&contract_name) {
continue;
}
let HardHatContract {
address,
transaction_hash,
mut contract,
} = {
let file = File::open(contract_path)?;
let reader = BufReader::new(file);
from_reader(reader)?
};
contract.name = contract_name;
self.add_contract_to_artifact(
&mut artifact,
contract,
chain_id.clone(),
address,
transaction_hash,
)?;
}
}
Ok(artifact)
}
fn load_artifact<T>(
&self,
format: Format,
origin: impl ToString,
source: T,
single_loader: impl FnOnce(T) -> serde_json::Result<HardHatExport>,
multi_loader: impl FnOnce(T) -> serde_json::Result<HardHatMultiExport>,
) -> Result<Artifact, ArtifactError> {
let origin = self.origin.clone().unwrap_or_else(|| origin.to_string());
let mut artifact = Artifact::with_origin(origin);
match format {
Format::SingleExport => {
let loaded = single_loader(source)?;
self.fill_artifact(&mut artifact, loaded)?
}
Format::MultiExport => {
let loaded = multi_loader(source)?;
self.fill_artifact_multi(&mut artifact, loaded)?
}
}
Ok(artifact)
}
fn fill_artifact(
&self,
artifact: &mut Artifact,
export: HardHatExport,
) -> Result<(), ArtifactError> {
if self.network_allowed(&export.chain_id, &export.chain_name) {
for (name, contract) in export.contracts {
let HardHatContract {
address,
transaction_hash,
mut contract,
} = contract;
if !self.contract_allowed(&name) {
continue;
}
contract.name = name;
self.add_contract_to_artifact(
artifact,
contract,
export.chain_id.clone(),
address,
transaction_hash,
)?;
}
}
Ok(())
}
fn fill_artifact_multi(
&self,
artifact: &mut Artifact,
export: HardHatMultiExport,
) -> Result<(), ArtifactError> {
for (_, export) in export.networks {
for (_, export) in export {
self.fill_artifact(artifact, export)?;
}
}
Ok(())
}
fn add_contract_to_artifact(
&self,
artifact: &mut Artifact,
contract: Contract,
chain_id: String,
address: Address,
transaction_hash: Option<TransactionHash>,
) -> Result<(), ArtifactError> {
let mut contract = match artifact.get_mut(&contract.name) {
Some(existing_contract) => {
if existing_contract.abi != contract.abi {
return Err(ArtifactError::AbiMismatch(contract.name));
}
existing_contract
}
None => artifact.insert(contract).inserted_contract,
};
let deployment_information = transaction_hash.map(DeploymentInformation::TransactionHash);
if contract.networks.contains_key(&chain_id) {
Err(ArtifactError::DuplicateChain(chain_id))
} else {
contract.networks_mut().insert(
chain_id,
Network {
address,
deployment_information,
},
);
Ok(())
}
}
fn contract_allowed(&self, name: &str) -> bool {
!self.contract_explicitly_denied(name)
&& (self.contracts_allow_list.is_empty() || self.contract_explicitly_allowed(name))
}
fn contract_explicitly_allowed(&self, name: &str) -> bool {
self.contracts_allow_list.iter().any(|x| x == name)
}
fn contract_explicitly_denied(&self, name: &str) -> bool {
self.contracts_deny_list.iter().any(|x| x == name)
}
fn network_allowed(&self, chain_id: &str, chain_name: &str) -> bool {
!self.network_explicitly_denied(chain_id, chain_name)
&& (self.networks_allow_list.is_empty()
|| self.network_explicitly_allowed(chain_id, chain_name))
}
fn network_explicitly_allowed(&self, chain_id: &str, chain_name: &str) -> bool {
self.networks_allow_list
.iter()
.any(|x| x.matches(chain_id, chain_name))
}
fn network_explicitly_denied(&self, chain_id: &str, chain_name: &str) -> bool {
self.networks_deny_list
.iter()
.any(|x| x.matches(chain_id, chain_name))
}
}
impl Default for HardHatLoader {
fn default() -> Self {
HardHatLoader::new()
}
}
/// Artifact format.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Format {
/// Contracts for a single network. Generated with `hardhat export`.
SingleExport,
/// Contracts for all networks. Generated with `hardhat export --export-all`.
MultiExport,
}
/// Network allow-deny entry.
#[derive(Clone, Debug)]
pub enum NetworkEntry {
/// Network identified by chain ID.
ByChainId(String),
/// Network identified by its name specified in `hardhat.config.js`.
ByName(String),
}
impl NetworkEntry {
fn matches(&self, chain_id: &str, chain_name: &str) -> bool {
match self {
NetworkEntry::ByChainId(id) => chain_id == id,
NetworkEntry::ByName(name) => chain_name == name,
}
}
}
#[derive(Deserialize)]
struct HardHatMultiExport {
#[serde(flatten)]
networks: HashMap<String, HashMap<String, HardHatExport>>,
}
#[derive(Deserialize)]
struct HardHatExport {
#[serde(rename = "name")]
chain_name: String,
#[serde(rename = "chainId")]
chain_id: String,
contracts: HashMap<String, HardHatContract>,
}
#[derive(Deserialize)]
struct HardHatContract {
address: Address,
#[serde(rename = "transactionHash")]
transaction_hash: Option<TransactionHash>,
#[serde(flatten)]
contract: Contract,
}
#[cfg(test)]
mod test {
use super::*;
use std::path::PathBuf;
use web3::ethabi::ethereum_types::BigEndianHash;
use web3::types::{H256, U256};
fn address(address: u8) -> Address {
Address::from(H256::from_uint(&U256::from(address)))
}
#[test]
fn load_single() {
let json = r#"
{
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A"
},
"B": {
"address": "0x000000000000000000000000000000000000000B"
}
}
}
"#;
let artifact = HardHatLoader::new()
.load_from_str(Format::SingleExport, json)
.unwrap();
assert_eq!(artifact.len(), 2);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 1);
assert_eq!(a.networks["1"].address, address(0xA));
let b = artifact.get("B").unwrap();
assert_eq!(b.name, "B");
assert_eq!(b.networks.len(), 1);
assert_eq!(b.networks["1"].address, address(0xB));
}
static MULTI_EXPORT: &str = r#"
{
"1": {
"mainnet": {
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A"
},
"B": {
"address": "0x000000000000000000000000000000000000000B"
}
}
}
},
"4": {
"rinkeby": {
"name": "rinkeby",
"chainId": "4",
"contracts": {
"A": {
"address": "0x00000000000000000000000000000000000000AA"
}
}
}
}
}
"#;
#[test]
fn load_multi() {
let artifact = HardHatLoader::new()
.load_from_str(Format::MultiExport, MULTI_EXPORT)
.unwrap();
assert_eq!(artifact.len(), 2);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 2);
assert_eq!(a.networks["1"].address, address(0xA));
assert_eq!(a.networks["4"].address, address(0xAA));
let b = artifact.get("B").unwrap();
assert_eq!(b.name, "B");
assert_eq!(b.networks.len(), 1);
assert_eq!(b.networks["1"].address, address(0xB));
}
#[test]
fn load_multi_duplicate_networks_ok() {
let json = r#"
{
"1": {
"mainnet": {
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A"
}
}
},
"mainnet_beta": {
"name": "mainnet_beta",
"chainId": "1",
"contracts": {
"B": {
"address": "0x000000000000000000000000000000000000000B"
}
}
}
}
}
"#;
let artifact = HardHatLoader::new()
.load_from_str(Format::MultiExport, json)
.unwrap();
assert_eq!(artifact.len(), 2);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 1);
assert_eq!(a.networks["1"].address, address(0xA));
let b = artifact.get("B").unwrap();
assert_eq!(b.name, "B");
assert_eq!(b.networks.len(), 1);
assert_eq!(b.networks["1"].address, address(0xB));
}
#[test]
fn load_multi_duplicate_networks_err() {
let json = r#"
{
"1": {
"mainnet": {
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A"
}
}
},
"mainnet_beta": {
"name": "mainnet_beta",
"chainId": "1",
"contracts": {
"A": {
"address": "0x00000000000000000000000000000000000000AA"
}
}
}
}
}
"#;
let err = HardHatLoader::new().load_from_str(Format::MultiExport, json);
match err {
Err(ArtifactError::DuplicateChain(chain_id)) => assert_eq!(chain_id, "1"),
Err(unexpected_err) => panic!("unexpected error {:?}", unexpected_err),
_ => panic!("didn't throw an error"),
}
}
#[test]
fn load_multi_mismatching_abi() {
let json = r#"
{
"1": {
"mainnet": {
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A",
"abi": [
{
"constant": false,
"inputs": [],
"name": "foo",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
}
}
}
},
"4": {
"rinkeby": {
"name": "rinkeby",
"chainId": "4",
"contracts": {
"A": {
"address": "0x00000000000000000000000000000000000000AA",
"abi": [
{
"constant": false,
"inputs": [],
"name": "bar",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
}
}
}
}
}
"#;
let err = HardHatLoader::new().load_from_str(Format::MultiExport, json);
match err {
Err(ArtifactError::AbiMismatch(name)) => assert_eq!(name, "A"),
Err(unexpected_err) => panic!("unexpected error {:?}", unexpected_err),
_ => panic!("didn't throw an error"),
}
}
static NETWORK_CONFLICTS: &str = r#"
{
"1": {
"mainnet": {
"name": "mainnet",
"chainId": "1",
"contracts": {
"A": {
"address": "0x000000000000000000000000000000000000000A"
}
}
},
"mainnet_beta": {
"name": "mainnet_beta",
"chainId": "1",
"contracts": {
"A": {
"address": "0x00000000000000000000000000000000000000AA",
"abi": [
{
"constant": false,
"inputs": [],
"name": "test_method",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
}
}
}
},
"4": {
"rinkeby": {
"name": "rinkeby",
"chainId": "4",
"contracts": {
"A": {
"address": "0x00000000000000000000000000000000000000BA"
}
}
}
}
}
"#;
#[test]
fn load_multi_allow_by_name() {
let artifact = HardHatLoader::new()
.allow_network_by_name("mainnet")
.allow_network_by_name("rinkeby")
.load_from_str(Format::MultiExport, NETWORK_CONFLICTS)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 2);
assert_eq!(a.networks["1"].address, address(0xA));
assert_eq!(a.networks["4"].address, address(0xBA));
}
#[test]
fn load_multi_allow_by_chain_id() {
let artifact = HardHatLoader::new()
.allow_network_by_chain_id("4")
.load_from_str(Format::MultiExport, NETWORK_CONFLICTS)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 1);
assert_eq!(a.networks["4"].address, address(0xBA));
}
#[test]
fn load_multi_deny_by_name() {
let artifact = HardHatLoader::new()
.deny_network_by_name("mainnet_beta")
.load_from_str(Format::MultiExport, NETWORK_CONFLICTS)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 2);
assert_eq!(a.networks["1"].address, address(0xA));
assert_eq!(a.networks["4"].address, address(0xBA));
}
#[test]
fn load_multi_deny_by_chain_id() {
let artifact = HardHatLoader::new()
.deny_network_by_chain_id("1")
.load_from_str(Format::MultiExport, NETWORK_CONFLICTS)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 1);
assert_eq!(a.networks["4"].address, address(0xBA));
}
#[test]
fn load_multi_allow_contract_name() {
let artifact = HardHatLoader::new()
.allow_contract("A")
.load_from_str(Format::MultiExport, MULTI_EXPORT)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 2);
assert_eq!(a.networks["1"].address, address(0xA));
assert_eq!(a.networks["4"].address, address(0xAA));
let artifact = HardHatLoader::new()
.allow_contract("X")
.load_from_str(Format::MultiExport, MULTI_EXPORT)
.unwrap();
assert_eq!(artifact.len(), 0);
}
#[test]
fn load_multi_deny_contract_name() {
let artifact = HardHatLoader::new()
.deny_contract("A")
.load_from_str(Format::MultiExport, MULTI_EXPORT)
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("B").unwrap();
assert_eq!(a.name, "B");
assert_eq!(a.networks.len(), 1);
assert_eq!(a.networks["1"].address, address(0xB));
let artifact = HardHatLoader::new()
.deny_contract("X")
.load_from_str(Format::MultiExport, MULTI_EXPORT)
.unwrap();
assert_eq!(artifact.len(), 2);
let a = artifact.get("A").unwrap();
assert_eq!(a.name, "A");
assert_eq!(a.networks.len(), 2);
assert_eq!(a.networks["1"].address, address(0xA));
assert_eq!(a.networks["4"].address, address(0xAA));
let b = artifact.get("B").unwrap();
assert_eq!(b.name, "B");
assert_eq!(b.networks.len(), 1);
assert_eq!(b.networks["1"].address, address(0xB));
}
fn hardhat_dir() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("../examples/hardhat/deployments");
path
}
#[test]
fn load_from_directory() {
let artifact = HardHatLoader::new()
.load_from_directory(hardhat_dir())
.unwrap();
assert_eq!(artifact.len(), 1);
let a = artifact.get("DeployedContract").unwrap();
assert_eq!(a.name, "DeployedContract");
assert_eq!(a.networks.len(), 2);
assert_eq!(
a.networks["4"].address,
"0x4E29B76eC7d20c58A6B156CB464594a4ae39FdEd"
.parse()
.unwrap()
);
assert_eq!(
a.networks["4"].deployment_information,
Some(DeploymentInformation::TransactionHash(
"0x0122d15a8d394b8f9e45c15b7d3e5365bbf7122a15952246676e2fe7eb858f35"
.parse()
.unwrap()
))
);
assert_eq!(
a.networks["1337"].address,
"0x29BE0588389993e7064C21f00761303eb51373F5"
.parse()
.unwrap()
);
assert_eq!(
a.networks["1337"].deployment_information,
Some(DeploymentInformation::TransactionHash(
"0xe0631d7f749fe73f94e59f6e25ff9b925980e8e29ed67b8f862ec76a783ea06e"
.parse()
.unwrap()
))
);
}
}