48 lines
1.9 KiB
JavaScript
Executable File
48 lines
1.9 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
||
|
||
// 此脚本为 `hardhat/common-contracts.js` 脚本快照字节码和 ABI。
|
||
// - 字节码通过查询提供的客户端端点直接从区块链获取。如果没有提供端点,则使用 ethers 默认提供者。
|
||
// - ABI 使用提供的 etherscan API 密钥从 etherscan 的 API 获取。如果没有提供 API 密钥,则不会获取和保存 ABI。
|
||
//
|
||
// 生成的工件存储在 `output` 文件夹中(默认为 'test/bin')。对于每个合约,生成两个文件:
|
||
// - `<name>.bytecode` 包含合约字节码(二进制编码)
|
||
// - `<name>.abi` 包含 ABI(utf-8 编码)
|
||
|
||
const fs = require('fs');
|
||
const path = require('path');
|
||
const { ethers } = require('ethers');
|
||
const { request } = require('undici');
|
||
const { hideBin } = require('yargs/helpers');
|
||
const { argv } = require('yargs/yargs')(hideBin(process.argv))
|
||
.env('')
|
||
.options({
|
||
output: { type: 'string', default: 'test/bin/' },
|
||
client: { type: 'string' },
|
||
etherscan: { type: 'string' },
|
||
});
|
||
|
||
// 要获取的合约名称和地址列表
|
||
const config = {
|
||
EntryPoint070: '0x0000000071727De22E5E9d8BAf0edAc6f37da032',
|
||
SenderCreator070: '0xEFC2c1444eBCC4Db75e7613d20C6a62fF67A167C',
|
||
EntryPoint080: '0x4337084D9E255Ff0702461CF8895CE9E3b5Ff108',
|
||
SenderCreator080: '0x449ED7C3e6Fee6a97311d4b55475DF59C44AdD33',
|
||
};
|
||
|
||
Promise.all(
|
||
Object.entries(config).flatMap(([name, addr]) =>
|
||
Promise.all([
|
||
argv.etherscan &&
|
||
request(`https://api.etherscan.io/api?module=contract&action=getabi&address=${addr}&apikey=${argv.etherscan}`)
|
||
.then(({ body }) => body.json())
|
||
.then(({ result: abi }) => fs.writeFile(path.join(argv.output, `${name}.abi`), abi, 'utf-8', () => {})),
|
||
ethers
|
||
.getDefaultProvider(argv.client)
|
||
.getCode(addr)
|
||
.then(bytecode =>
|
||
fs.writeFile(path.join(argv.output, `${name}.bytecode`), ethers.getBytes(bytecode), 'binary', () => {}),
|
||
),
|
||
]),
|
||
),
|
||
);
|