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 95c51a1
Show file tree
Hide file tree
Showing 6 changed files with 66 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 {}
9 changes: 9 additions & 0 deletions modules/passkey/src/types/solc.d.ts
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
}
16 changes: 16 additions & 0 deletions modules/passkey/src/utils/solc.ts
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> = {}


Check failure on line 6 in modules/passkey/src/utils/solc.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
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)
})
})
}
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 95c51a1

Please sign in to comment.