-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: zksync support for core solidity package (#5205)
### Description ZKSync PR 1 This PR introduces ZKSync support for Solidity contracts and restructures build files directory structure. ### Drive-by changes - Added ZKSync compilation support using zksolc via `@matterlabs/hardhat-zksync-solc` - Restructured typechain directory location to `core-utils/typechain` - Decoupled Hardhat configuration for ZKSync from EVM. - Updated build process to handle both standard and ZKSync contract artifacts ### Related issues None ### Backward compatibility Yes ### Testing Testing was previously in [feat: zksync support](#4725) PR --------- Co-authored-by: Morteza Shojaei <[email protected]> Co-authored-by: mshojaei-txfusion <[email protected]> Co-authored-by: Le Yu <[email protected]>
- Loading branch information
1 parent
a352bc8
commit db8c090
Showing
16 changed files
with
705 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
'@hyperlane-xyz/core': minor | ||
--- | ||
|
||
Add ZKSync support and restructure build artifacts: | ||
|
||
- Add ZKSync compilation support | ||
- Restructure typechain directory location to core-utils/typechain | ||
- Add ZKSync-specific artifact generation and exports | ||
- Update build process to handle both standard and ZKSync artifacts | ||
- Add new exports for ZKSync build artifacts and contract types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './typechain/index.js'; | ||
export * from './zksync/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { promises as fsPromises } from 'fs'; | ||
import path, { join } from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
/** | ||
* @dev Represents a ZkSync artifact. | ||
*/ | ||
export type ZKSyncArtifact = { | ||
contractName: string; | ||
sourceName: string; | ||
abi: any; | ||
bytecode: string; | ||
deployedBytecode: string; | ||
factoryDeps?: Record<string, string>; | ||
}; | ||
|
||
/** | ||
* @dev A mapping of artifact names to their corresponding ZkSync artifacts. | ||
*/ | ||
export type ArtifactMap = { | ||
[key: string]: ZKSyncArtifact; | ||
}; | ||
|
||
// Get the resolved path to the current file | ||
const currentFilePath = fileURLToPath(import.meta.url); | ||
const currentDirectory = path.dirname(currentFilePath); | ||
|
||
/** | ||
* @dev Reads artifact files from the specified directory. | ||
* @param directory The directory to read artifact files from. | ||
* @return An array of artifact file names that end with '.json'. | ||
*/ | ||
async function getArtifactFiles(directory: string): Promise<string[]> { | ||
return fsPromises | ||
.readdir(directory) | ||
.then((files) => files.filter((file) => file.endsWith('.json'))); | ||
} | ||
|
||
/** | ||
* @dev Exports the list of artifact names without the .json extension. | ||
* @return An array of artifact names without the .json extension. | ||
*/ | ||
export async function getZKSyncArtifactNames(): Promise<string[]> { | ||
return getArtifactFiles(join(currentDirectory, 'artifacts')).then((files) => | ||
files.map((file) => file.replace('.json', '')), | ||
); | ||
} | ||
|
||
/** | ||
* @dev Checks if a ZkSync artifact exists by its name. | ||
* @param name The name of the artifact to check. | ||
* @return True if the artifact exists, false otherwise. | ||
*/ | ||
export async function artifactExists(name: string): Promise<boolean> { | ||
const artifactNames = await getZKSyncArtifactNames(); | ||
return artifactNames.includes(name); | ||
} | ||
|
||
/** | ||
* @dev Loads a ZkSync artifact by its name. | ||
* @param name The name of the artifact to load. | ||
* @return The loaded ZKSyncArtifact or undefined if it cannot be loaded. | ||
*/ | ||
export async function loadZKSyncArtifact( | ||
name: string, | ||
): Promise<ZKSyncArtifact | undefined> { | ||
try { | ||
const artifactPath = join(currentDirectory, 'artifacts', `${name}.json`); | ||
const artifactContent = await fsPromises.readFile(artifactPath, 'utf-8'); | ||
return JSON.parse(artifactContent); | ||
} catch (error) { | ||
console.error(`Error loading artifact: ${name}`, error); | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* @dev Loads all ZkSync artifacts into a map. | ||
* @return A map of artifact names to their corresponding ZkSync artifacts. | ||
*/ | ||
export async function loadAllZKSyncArtifacts(): Promise<ArtifactMap> { | ||
const zkSyncArtifactMap: ArtifactMap = {}; | ||
const zksyncArtifactNames = await getZKSyncArtifactNames(); | ||
for (const artifactName of zksyncArtifactNames) { | ||
const artifact = await loadZKSyncArtifact(artifactName); | ||
if (artifact) { | ||
zkSyncArtifactMap[artifactName] = artifact; | ||
} | ||
} | ||
|
||
return zkSyncArtifactMap; | ||
} | ||
|
||
/** | ||
* @dev Retrieves a specific ZkSync artifact by its file name. | ||
* @param name The name of the artifact to retrieve. | ||
* @return The loaded ZkSyncArtifact or undefined if it cannot be loaded. | ||
*/ | ||
export async function getZKSyncArtifactByName( | ||
name: string, | ||
): Promise<ZKSyncArtifact | undefined> { | ||
return loadZKSyncArtifact(name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { promises as fsPromises } from 'fs'; | ||
import { basename, dirname, join } from 'path'; | ||
import { glob } from 'typechain'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const cwd = process.cwd(); | ||
|
||
/** | ||
* @dev Only includes primary JSON artifacts & excludes debug files and build-info directory | ||
*/ | ||
const zksyncArtifacts = glob(cwd, [ | ||
`!./artifacts-zk/!(build-info)/**/*.dbg.json`, | ||
`./artifacts-zk/!(build-info)/**/+([a-zA-Z0-9_]).json`, | ||
]); | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = dirname(__filename); | ||
|
||
const srcOutputDir = join(__dirname, 'core-utils/zksync/artifacts'); | ||
|
||
// Ensure output directory exists | ||
await fsPromises.mkdir(srcOutputDir, { recursive: true }); | ||
|
||
/** | ||
* @dev Processes a single artifact file | ||
*/ | ||
async function processArtifactFile(file) { | ||
const fileName = `${basename(file, '.json')}`; | ||
const outputFile = join(srcOutputDir, `${fileName}.json`); | ||
|
||
// Check if file already exists | ||
const fileExists = await fsPromises | ||
.access(outputFile) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
if (fileExists) { | ||
// File already exists, skipping... | ||
// NOTE: Hardhat compiler produces duplicate artifacts when | ||
// shared interfaces/libraries are used across different contracts | ||
// This is expected behavior and we only need one copy of each artifact | ||
return; | ||
} | ||
|
||
const fileContent = await fsPromises.readFile(file, { encoding: 'utf-8' }); | ||
await fsPromises.writeFile(outputFile, fileContent); | ||
} | ||
|
||
/** | ||
* @dev Reads each artifact file and writes it to srcOutputDir concurrently | ||
*/ | ||
await Promise.all( | ||
zksyncArtifacts.map(async (file) => { | ||
try { | ||
await processArtifactFile(file); | ||
} catch (error) { | ||
console.error(`Error processing file ${file}:`, error); | ||
} | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.