fetch-common-contracts.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env node
  2. // This script snapshots the bytecode and ABI for the `hardhat/common-contracts.js` script.
  3. // - Bytecode is fetched directly from the blockchain by querying the provided client endpoint. If no endpoint is
  4. // provided, ethers default provider is used instead.
  5. // - ABI is fetched from etherscan's API using the provided etherscan API key. If no API key is provided, ABI will not
  6. // be fetched and saved.
  7. //
  8. // The produced artifacts are stored in the `output` folder ('test/bin' by default). For each contract, two files are
  9. // produced:
  10. // - `<name>.bytecode` containing the contract bytecode (in binary encoding)
  11. // - `<name>.abi` containing the ABI (in utf-8 encoding)
  12. const fs = require('fs');
  13. const path = require('path');
  14. const { ethers } = require('ethers');
  15. const { request } = require('undici');
  16. const { hideBin } = require('yargs/helpers');
  17. const { argv } = require('yargs/yargs')(hideBin(process.argv))
  18. .env('')
  19. .options({
  20. output: { type: 'string', default: 'test/bin/' },
  21. client: { type: 'string' },
  22. etherscan: { type: 'string' },
  23. });
  24. // List of contract names and addresses to fetch
  25. const config = {
  26. EntryPoint070: '0x0000000071727De22E5E9d8BAf0edAc6f37da032',
  27. SenderCreator070: '0xEFC2c1444eBCC4Db75e7613d20C6a62fF67A167C',
  28. EntryPoint080: '0x4337084D9E255Ff0702461CF8895CE9E3b5Ff108',
  29. SenderCreator080: '0x449ED7C3e6Fee6a97311d4b55475DF59C44AdD33',
  30. };
  31. Promise.all(
  32. Object.entries(config).flatMap(([name, addr]) =>
  33. Promise.all([
  34. argv.etherscan &&
  35. request(`https://api.etherscan.io/api?module=contract&action=getabi&address=${addr}&apikey=${argv.etherscan}`)
  36. .then(({ body }) => body.json())
  37. .then(({ result: abi }) => fs.writeFile(path.join(argv.output, `${name}.abi`), abi, 'utf-8', () => {})),
  38. ethers
  39. .getDefaultProvider(argv.client)
  40. .getCode(addr)
  41. .then(bytecode =>
  42. fs.writeFile(path.join(argv.output, `${name}.bytecode`), ethers.getBytes(bytecode), 'binary', () => {}),
  43. ),
  44. ]),
  45. ),
  46. );