Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viem: abigen from optimism repo snapshots #619

Merged
merged 11 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/old-paws-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@eth-optimism/wagmi": patch
"@eth-optimism/viem": patch
---

update generated abi and fix imports. abi will be exported temporarily
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/superchain-registry"]
path = lib/superchain-registry
url = https://github.com/ethereum-optimism/superchain-registry
[submodule "lib/optimism"]
path = lib/optimism
url = https://github.com/ethereum-optimism/optimism
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"create:app": "cd apps && pnpm create vite --template=react-ts ",
"create:react:library": "cd packages && pnpm create vite --template=react-ts ",
"release:publish": "pnpm install --frozen-lockfile && pnpm nx run-many --target=build && changeset publish",
"release:version": "changeset version && pnpm install --lockfile-only",
"fetch:artifacts": "bash ./scripts/pull-contract-artifacts.sh"
"release:version": "changeset version && pnpm install --lockfile-only"
},
"private": true,
"devDependencies": {
Expand Down
9 changes: 0 additions & 9 deletions packages/viem/abigen.json

This file was deleted.

81 changes: 48 additions & 33 deletions packages/viem/scripts/abigen.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,65 @@
import { execSync } from 'child_process'
import { Eta } from 'eta'
import fs from 'fs'
import path from 'path'

import abigen from '../abigen.json'
// Hardcoded. Can take a more elaborate approach if needed.
const OPTIMISM_PATH = path.join('..', '..', 'lib', 'optimism')
const CONTRACTS = [
'CrossL2Inbox',
'L2ToL2CrossDomainMessenger',
'SuperchainERC20',
'SuperchainWETH',
'SuperchainTokenBridge',
]

type JSONValue =
| string
| number
| boolean
| { [x: string]: JSONValue }
| JSONValue[]
/** Utility Functions */

type AutogenContract = {
name: string
abi: { [x: string]: JSONValue }
function camelCase(str: string): string {
const [start, ...rest] = str
return start.toLowerCase() + rest.join('')
}

const ARTIFACT_DIRECTORY = '../../lib/forge-artifacts'
const SRC_DIR = './src'
/** Abi Generation */

const eta = new Eta({ views: './scripts/templates', debug: true })

async function generateAbis() {
const contracts = [] as AutogenContract[]
for (const name of abigen.contracts) {
const artifact = JSON.parse(
fs.readFileSync(`${ARTIFACT_DIRECTORY}/${name}.sol/${name}.json`),
)
contracts.push({ name, abi: artifact.abi })
async function main() {
console.log('Generating forge artifacts...')
const contractsBedrockPath = path.join(
OPTIMISM_PATH,
'packages',
'contracts-bedrock',
)
try {
execSync('forge build', { cwd: contractsBedrockPath, stdio: 'inherit' })
} catch (error) {
throw new Error(`Failed to generate forge artifacts: ${error}`)
}

const generateAbiFileContents = eta.render('abis', {
contracts,
camelCase,
prettyPrintJSON: (json: JSONValue) => JSON.stringify(json, null, 2),
console.log('Extracting abi generation...')
const eta = new Eta({
views: './scripts/templates',
debug: true,
autoTrim: [false, false],
})

fs.writeFileSync(`${SRC_DIR}/abis.ts`, generateAbiFileContents)
}
const contracts = CONTRACTS.map((contract) => {
console.log(`Generating Abi for ${contract}`)
const abiPath = path.join(
contractsBedrockPath,
'forge-artifacts',
`${contract}.sol`,
`${contract}.json`,
)
const abi = JSON.parse(fs.readFileSync(abiPath, 'utf8')).abi
return { name: contract, exportName: camelCase(contract), abi }
})

function camelCase(str: string): string {
const [start, ...rest] = str
return start.toLowerCase() + rest.join('')
}
const fileContents = eta.render('abis', {
contracts,
prettyPrintJSON: (obj: any) => JSON.stringify(obj, null, 2),
})

async function main() {
await generateAbis()
fs.writeFileSync(`src/abis.ts`, fileContents)
}

;(async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/viem/scripts/templates/abis.eta
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
* ABI for the OP Stack contract `<%= contract.name %>`
* @category ABI
*/
export const <%= it.camelCase(contract.name) %>ABI = <%~ it.prettyPrintJSON(contract.abi) %> as const

export const <%= contract.exportName %>Abi = <%~ it.prettyPrintJSON(contract.abi) %> as const
<% }) %>
Loading
Loading