Skip to content

Commit

Permalink
viem: abigen from optimism repo snapshots (#619)
Browse files Browse the repository at this point in the history
* abigen

* abigen

* lint & remove abi export

* gen

* final touches

* changeset

* update imports

* gen abi from forge artifacts

* fix wagmi imports

* changeset

* temporarily remove relayMessage output
  • Loading branch information
hamdiallam authored Jan 17, 2025
1 parent d07cb41 commit bba9176
Show file tree
Hide file tree
Showing 29 changed files with 375 additions and 461 deletions.
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

0 comments on commit bba9176

Please sign in to comment.