# @truffle/fetch-and-compile

> Used to obtain external verified sources and compile them

Latest version **0.5.56** (published 2023-09-13) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @truffle/fetch-and-compile
pnpm add @truffle/fetch-and-compile
yarn add @truffle/fetch-and-compile
bun add @truffle/fetch-and-compile
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.5.56 |
| Published | 2023-09-13 |
| First published | 2021-08-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | ^16.20 \|\| ^18.16 \|\| >=20 |
| Dependencies | 7 |
| Unpacked size | 47.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13899 |
| Author | Harry Altman |
| Maintainers | rizedr, joshuafernandes, cliffoo, kevinbluer, gnidan, haltman, eggplantzzz, fainashalts, cds-amal |
| Keywords | compile, ethereum, etherscan, ipfs, solidity, sourcify, verify-source |

## Links

- npm: https://www.npmjs.com/package/@truffle/fetch-and-compile
- Repository: https://github.com/trufflesuite/truffle
- Homepage: https://github.com/trufflesuite/truffle/tree/master/packages/fetch-and-compile#readme
- Issues: https://github.com/trufflesuite/truffle/issues
- npm.io page: https://npm.io/package/@truffle/fetch-and-compile

## Dependencies (7)

- [debug](https://npm.io/package/debug.md) ^4.3.2
- [semver](https://npm.io/package/semver.md) ^7.5.4
- [web3-utils](https://npm.io/package/web3-utils.md) 1.10.0
- [@truffle/codec](https://npm.io/package/@truffle/codec.md) ^0.17.3
- [@truffle/config](https://npm.io/package/@truffle/config.md) ^1.3.61
- [@truffle/source-fetcher](https://npm.io/package/@truffle/source-fetcher.md) ^1.0.44
- [@truffle/compile-solidity](https://npm.io/package/@truffle/compile-solidity.md) ^6.0.79

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.5.56 (latest) — 2023-09-13
- 0.5.42-visual-debugger.0 (visual-debugger) — 2023-05-12
- 0.5.17-hardhat-error.0 (hardhat-error) — 2022-08-25
- 0.5.11-typescript-migrations.0 (typescript-migrations) — 2022-07-21
- 0.5.7-alpha.2 (signTypedData_v4) — 2022-06-24
- 0.3.1-dashboard.1 (dashboard) — 2022-02-01
- 0.1.10-fc.0 (fc) — 2021-10-04
- 0.1.9-decoder-beta.0 (decoder-beta) — 2021-09-29
- 0.1.6-alpha.1 (eip1559-beta) — 2021-09-13
- 0.5.55 — 2023-09-07
- 0.5.54 — 2023-08-29
- 0.5.53 — 2023-08-04
- 0.5.52 — 2023-07-14
- 0.5.51 — 2023-06-30
- 0.5.50 — 2023-06-23
- … 96 more at https://npm.io/package/@truffle/fetch-and-compile/versions

## README

# `@truffle/fetch-and-compile`

This is used to obtain externally verified sources and compile them.

Note: If you import this into your TS project, you may need to enable `skipLibCheck` in your tsconfig due to an indirect dependency on @truffle/contract-schema.

## Usage

### `fetchAndCompile`

```ts
import * as dotenv from "dotenv";
dotenv.config();

import * as Codec from "@truffle/codec";
import Config from "@truffle/config";
//  🔨 set up by importing the fetch-and-compile.
import { fetchAndCompile } from "@truffle/fetch-and-compile";
import * as Decoder from "@truffle/decoder";

const config = Config.default().merge({
  networks: {
    mainnet: {
      url: process.env.RPC_URL,
      network_id: 1
    }
  },
  network: "mainnet",
  etherscan: {
    apiKey: process.env.ETHERSCAN_KEY
  }
});

async function decode(address: string) {
  // 👇 Pass in a contract address and a network config to fetchAndCompile 👇.
  const { compileResult } = await fetchAndCompile(address, config);

  // 🪄 Now that we have the result from fetch-and-compile, we can use it with @truffle/decoder for some more magic ✨.
  const projectInfo = {
    commonCompilations: compileResult.compilations
  };
  const projectDecoder = await Decoder.forProject({
    provider: config.provider,
    projectInfo
  });
  const decoder = await projectDecoder.forAddress(address);
  const decoding = await decoder.decodeTransaction({
    to: "0x00000000000c2e074ec69a0dfb2997ba6c7d2e1e",
    blockNumber: 13336592,
    from: "0xbc1be5ac62ca0637676f2b592bcd0a29bbf4e427",
    input:
      "0x1896f70af96f66c6c5e18dc3da0e5d238ee5ff0f56ad7876717492cfcbb3421db607e44c0000000000000000000000004976fb03c32e5b8cfe2b6ccb31c09ba78ebaba41"
  });
  if (decoding.kind !== "function") {
    throw new Error("Unsupported decoding kind");
  }
  console.log("%s(", decoding.abi.name);
  console.group();
  for (const { name, value: result } of decoding.arguments) {
    console.log(
      `${name}:`,
      new Codec.Format.Utils.Inspect.ResultInspector(result)
    );
  }
  console.groupEnd();
  console.log(")");
}
// 🥳 Try it out with a contract - this is the address for the ENS registry
decode("0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e");
// --- yields the following output ---
// setResolver(
//   node: 0xf96f66c6c5e18dc3da0e5d238ee5ff0f56ad7876717492cfcbb3421db607e44c
//   resolver: 0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41
// )
```

### `fetchAndCompileMultiple`

If you want to fetch and compile multiple contracts from the same network, you can use `fetchAndCompileMultiple`:

```ts
import { fetchAndCompileMultiple } from "@truffle/fetch-and-compile";
const config = /* set up config */;
const addresses: string[] = /* set addresses */;
const { results, failures } = await fetchAndCompileMultiple(addresses, config);
for (const address in results) {
  const compileResult = results[address];
  /* do things with compileResult as above */
}
for (const address in failures) {
  /* do things with failed addresses */
}
```

### Alternate input format

Instead of using a Truffle Config as input, you can pass in a `FetchAndCompileOptions` anywhere that
a `config` is used above. The format is as follows:

```ts
export interface FetchAndCompileOptions {
  network: {
    networkId: number;
  };
  fetch?: {
    precedence?: string[]; //which fetchers to use and in what order; defaults
    //to ["etherscan", "sourcify"]
    fetcherOptions?: {
      etherscan?: {
        apiKey: string; //etherscan API key if you have one to speed things up
      };
      sourcify?: {
        //nothing to go here at present
      };
    };
  };
  compile?: {
    docker?: boolean; //indicates that compilation should use dockerized solc;
    //note this won't work with contracts compiled with prerelease versions
    //of Solidity
  };
}
```

### `getSupportedNetworks`

If you want a (potentially partial) list of supported networks, you can call `getSupportedNetworks`:

```ts
import { getSupportedNetworks } from "@truffle/fetch-and-compile";
const networks = getSupportedNetworks();
// networks = {
//   mainnet: {
//     name: "mainnet",
//     networkId: 1,
//     chainId: 1,
//     fetchers: ["etherscan", "sourcify"] //which fetchers support this network?
//   },
//   ...
// }
```

Note that there may be additional unlisted supported networks.

You can also pass in a list of fetchers if you want to restrict the output to the networks
supported by the fetchers you list. (You can also pass in a config and it will use the `sourceFetchers`
property if set, or a `FetchAndCompileOptions` and it will use the `fetch.precedence` field if set.)

```ts
import { getSupportedNetworks } from "@truffle/fetch-and-compile";
import Config from "@truffle/config";
const networks = getSupportedNetworks(["etherscan"]); //will only list those supported by etherscan fetcher
// networks = {
//   mainnet: {
//     name: "mainnet",
//     networkId: 1,
//     chainId: 1,
//     fetchers: ["etherscan"] //sourcify is not listed since it's not being checked
//   },
//   ...
// }
```

Please see the [README for @truffle/decoder](https://github.com/trufflesuite/truffle/tree/develop/packages/decoder)
for more information on using Truffle decoder.

---
_Source: https://npm.io/package/@truffle/fetch-and-compile · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
