-
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
100 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,41 @@ | ||
declare module 'solc' { | ||
export interface CompilerInput { | ||
language: string | ||
sources: { | ||
[fileName: string]: { | ||
content: string | ||
} | ||
} | ||
settings: { | ||
outputSelection: { | ||
[fileName: string]: { | ||
[contractName: string]: string[] | ||
} | ||
} | ||
} | ||
} | ||
|
||
export interface CompilerOutput { | ||
contracts: { | ||
[fileName: string]: { | ||
[contractName: string]: { | ||
abi: any[] | ||
evm: { | ||
bytecode: { | ||
object: string | ||
} | ||
} | ||
} | ||
} | ||
} | ||
sources: { | ||
[fileName: string]: { | ||
ast: any | ||
} | ||
} | ||
} | ||
|
||
export function compile(input: string): string | ||
|
||
export function loadRemoteVersion(version: string, callback: (err: Error, solc: any) => 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,18 @@ | ||
import solc from 'solc' | ||
|
||
const solcCache: Record<string, Compiler> = {} | ||
|
||
export interface Compiler { | ||
compile: (input: string) => string | ||
} | ||
|
||
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: unknown, solcjs: Compiler) => { | ||
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