Skip to content

Commit

Permalink
Add codesize task
Browse files Browse the repository at this point in the history
  • Loading branch information
mmv08 committed Mar 27, 2024
1 parent a8928b7 commit ce81d03
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/passkey/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dotenv from 'dotenv'
import type { HardhatUserConfig } from 'hardhat/config'
import 'hardhat-deploy'
import { HttpNetworkUserConfig } from 'hardhat/types'
import './src/tasks/codesize'

dotenv.config()
const { CUSTOM_NODE_URL, MNEMONIC, ETHERSCAN_API_KEY, PK } = process.env
Expand Down
1 change: 1 addition & 0 deletions modules/passkey/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"build:sol": "rimraf build typechain-types && hardhat compile",
"build:ts": "rimraf dist && tsc",
"coverage": "hardhat coverage",
"codesize": "hardhat codesize",
"fmt": "prettier --write .",
"fmt:check": "prettier --check .",
"lint": "npm run lint:sol && npm run lint:ts",
Expand Down
37 changes: 37 additions & 0 deletions modules/passkey/src/tasks/codesize.ts
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 {}
41 changes: 41 additions & 0 deletions modules/passkey/src/types/solc.d.ts
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[]

Check warning on line 22 in modules/passkey/src/types/solc.d.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
evm: {
bytecode: {
object: string
}
}
}
}
}
sources: {
[fileName: string]: {
ast: any

Check warning on line 33 in modules/passkey/src/types/solc.d.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}
}
}

export function compile(input: string): string

export function loadRemoteVersion(version: string, callback: (err: Error, solc: any) => void): void

Check warning on line 40 in modules/passkey/src/types/solc.d.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}
18 changes: 18 additions & 0 deletions modules/passkey/src/utils/solc.ts
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)
})
})
}
3 changes: 2 additions & 1 deletion modules/passkey/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
},
"include": ["src/**/*.ts", "hardhat.config.ts", "test"]
}

0 comments on commit ce81d03

Please sign in to comment.