-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
1 deletion.
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
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,37 @@ | ||
import { task, types } from 'hardhat/config' | ||
import { loadSolc } from '../utils/solc' | ||
|
||
task('codesize', 'Displays the codesize of the contracts') | ||
.addParam('skipcompile', 'should not compile before printing size', false, types.boolean, true) | ||
.addParam('contractname', 'name of the contract', undefined, types.string, true) | ||
.setAction(async (taskArgs, hre) => { | ||
if (!taskArgs.skipcompile) { | ||
await hre.run('compile') | ||
} | ||
const contracts = await hre.artifacts.getAllFullyQualifiedNames() | ||
for (const contract of contracts) { | ||
const artifact = await hre.artifacts.readArtifact(contract) | ||
if (taskArgs.contractname && taskArgs.contractname !== artifact.contractName) continue | ||
console.log(artifact.contractName, hre.ethers.dataLength(artifact.deployedBytecode), 'bytes (limit is 24576)') | ||
} | ||
}) | ||
|
||
task('yulcode', 'Outputs yul code for contracts') | ||
.addParam('contractname', 'name of the contract', undefined, types.string, true) | ||
.setAction(async (taskArgs, hre) => { | ||
const contracts = await hre.artifacts.getAllFullyQualifiedNames() | ||
for (const contract of contracts) { | ||
if (taskArgs.contractname && !contract.endsWith(taskArgs.contractname)) continue | ||
const buildInfo = await hre.artifacts.getBuildInfo(contract) | ||
if (!buildInfo) return | ||
console.log({ buildInfo }) | ||
buildInfo.input.settings.outputSelection['*']['*'].push('ir', 'evm.assembly') | ||
const solcjs = await loadSolc(buildInfo.solcLongVersion) | ||
const compiled = solcjs.compile(JSON.stringify(buildInfo.input)) | ||
const output = JSON.parse(compiled) | ||
console.log(output.contracts[contract.split(':')[0]]) | ||
console.log(output.errors) | ||
} | ||
}) | ||
|
||
export {} |
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,9 @@ | ||
declare module 'solc' { | ||
export type Compiler = { | ||
compile: (input: string) => string | ||
} | ||
|
||
export function compile(input: string): string | ||
|
||
export function loadRemoteVersion(version: string, callback: (err: Error, solc: Compiler) => void): void | ||
} |
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,16 @@ | ||
import solc from 'solc' | ||
import type { Compiler } from 'solc' | ||
|
||
const solcCache: Record<string, Compiler> = {} | ||
|
||
|
||
export const loadSolc = async (version: string): Promise<Compiler> => { | ||
return await new Promise((resolve, reject) => { | ||
if (solcCache[version] !== undefined) resolve(solcCache[version]) | ||
else | ||
solc.loadRemoteVersion(`v${version}`, (error, solcjs) => { | ||
solcCache[version] = solcjs | ||
return error ? reject(error) : resolve(solcjs) | ||
}) | ||
}) | ||
} |
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