|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Generate interface ID constants by deploying and calling InterfaceIdExtractor contract |
| 5 | +""" |
| 6 | + |
| 7 | +import json |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +def log(*args): |
| 16 | + """Print log message if not in silent mode""" |
| 17 | + if "--silent" not in sys.argv: |
| 18 | + print(*args) |
| 19 | + |
| 20 | + |
| 21 | +def run_hardhat_task(): |
| 22 | + """Run hardhat script to extract interface IDs""" |
| 23 | + hardhat_script = """ |
| 24 | +const hre = require('hardhat') |
| 25 | +
|
| 26 | +async function main() { |
| 27 | + const InterfaceIdExtractor = await hre.ethers.getContractFactory('InterfaceIdExtractor') |
| 28 | + const extractor = await InterfaceIdExtractor.deploy() |
| 29 | + await extractor.deployed() |
| 30 | + |
| 31 | + const results = { |
| 32 | + IRewardsManager: await extractor.getIRewardsManagerId(), |
| 33 | + IIssuanceTarget: await extractor.getIIssuanceTargetId(), |
| 34 | + IERC165: await extractor.getIERC165Id(), |
| 35 | + } |
| 36 | + |
| 37 | + console.log(JSON.stringify(results)) |
| 38 | +} |
| 39 | +
|
| 40 | +main().catch((error) => { |
| 41 | + console.error(error) |
| 42 | + process.exit(1) |
| 43 | +}) |
| 44 | +""" |
| 45 | + |
| 46 | + script_dir = Path(__file__).parent |
| 47 | + project_dir = script_dir.parent.parent |
| 48 | + |
| 49 | + # Write temporary script |
| 50 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.js', delete=False) as temp_file: |
| 51 | + temp_file.write(hardhat_script) |
| 52 | + temp_script = temp_file.name |
| 53 | + |
| 54 | + try: |
| 55 | + # Run the script with hardhat |
| 56 | + result = subprocess.run( |
| 57 | + ['npx', 'hardhat', 'run', temp_script, '--network', 'hardhat'], |
| 58 | + cwd=project_dir, |
| 59 | + capture_output=True, |
| 60 | + text=True, |
| 61 | + check=False |
| 62 | + ) |
| 63 | + |
| 64 | + if result.returncode != 0: |
| 65 | + raise RuntimeError(f"Hardhat script failed with code {result.returncode}: {result.stderr}") |
| 66 | + |
| 67 | + # Extract JSON from output |
| 68 | + for line in result.stdout.split('\n'): |
| 69 | + line = line.strip() |
| 70 | + if line: |
| 71 | + try: |
| 72 | + data = json.loads(line) |
| 73 | + if isinstance(data, dict): |
| 74 | + return data |
| 75 | + except json.JSONDecodeError: |
| 76 | + # Not JSON, continue - this is expected for non-JSON output lines |
| 77 | + continue |
| 78 | + |
| 79 | + raise RuntimeError("Could not parse interface IDs from output") |
| 80 | + |
| 81 | + finally: |
| 82 | + # Clean up temp script |
| 83 | + try: |
| 84 | + os.unlink(temp_script) |
| 85 | + except OSError: |
| 86 | + # Ignore cleanup errors - temp file may not exist |
| 87 | + pass |
| 88 | + |
| 89 | + |
| 90 | +def extract_interface_ids(): |
| 91 | + """Extract interface IDs using the InterfaceIdExtractor contract""" |
| 92 | + script_dir = Path(__file__).parent |
| 93 | + extractor_path = script_dir.parent.parent / "artifacts" / "contracts" / "tests" / "InterfaceIdExtractor.sol" / "InterfaceIdExtractor.json" |
| 94 | + |
| 95 | + if not extractor_path.exists(): |
| 96 | + print("❌ InterfaceIdExtractor artifact not found") |
| 97 | + print("Run: pnpm compile to build the extractor contract") |
| 98 | + raise RuntimeError("InterfaceIdExtractor not compiled") |
| 99 | + |
| 100 | + log("Deploying InterfaceIdExtractor contract to extract interface IDs...") |
| 101 | + |
| 102 | + try: |
| 103 | + results = run_hardhat_task() |
| 104 | + |
| 105 | + # Convert from ethers BigNumber format to hex strings |
| 106 | + processed = {} |
| 107 | + for name, value in results.items(): |
| 108 | + if isinstance(value, str): |
| 109 | + processed[name] = value |
| 110 | + else: |
| 111 | + # Convert number to hex string |
| 112 | + processed[name] = f"0x{int(value):08x}" |
| 113 | + log(f"✅ Extracted {name}: {processed[name]}") |
| 114 | + |
| 115 | + return processed |
| 116 | + |
| 117 | + except Exception as error: |
| 118 | + print(f"Error extracting interface IDs: {error}") |
| 119 | + raise |
| 120 | + |
| 121 | + |
| 122 | +def main(): |
| 123 | + """Main function to generate interface IDs TypeScript file""" |
| 124 | + log("Extracting interface IDs from Solidity compilation...") |
| 125 | + |
| 126 | + results = extract_interface_ids() |
| 127 | + |
| 128 | + # Generate TypeScript content |
| 129 | + content = f"""// Auto-generated interface IDs from Solidity compilation |
| 130 | +export const INTERFACE_IDS = {{ |
| 131 | +{chr(10).join(f" {name}: '{id_value}'," for name, id_value in results.items())} |
| 132 | +}} as const |
| 133 | +
|
| 134 | +// Individual exports for convenience |
| 135 | +{chr(10).join(f"export const {name} = '{id_value}'" for name, id_value in results.items())} |
| 136 | +""" |
| 137 | + |
| 138 | + # Write to output file |
| 139 | + script_dir = Path(__file__).parent |
| 140 | + output_file = script_dir.parent / "helpers" / "interfaceIds.ts" |
| 141 | + |
| 142 | + # Create helpers directory if it doesn't exist |
| 143 | + output_file.parent.mkdir(exist_ok=True) |
| 144 | + |
| 145 | + with open(output_file, 'w') as f: |
| 146 | + f.write(content) |
| 147 | + |
| 148 | + log(f"✅ Generated {output_file}") |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == "__main__": |
| 152 | + main() |
0 commit comments