Skip to content

Commit

Permalink
chore: add wagmi types generation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbrugneaux authored and aaronmgdr committed Oct 5, 2023
1 parent b997d18 commit 3584a83
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/protocol/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ deployedGrants.json
cache/
out/

wagmi.config.js
wagmi.config.js.map
5 changes: 3 additions & 2 deletions packages/protocol/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@celo/protocol",
"version": "9.0.0",
"version": "1.0.0",
"private": true,
"main": "index.ts",
"author": "Celo",
Expand Down Expand Up @@ -114,6 +114,7 @@
"@types/lodash": "^4.14.199",
"@types/tmp": "^0.1.0",
"@types/yargs": "^13.0.2",
"@wagmi/cli": "^1.0.1",
"cross-env": "^5.1.6",
"eth-gas-reporter": "^0.2.16",
"merkle-patricia-tree": "4.0.0",
Expand All @@ -127,4 +128,4 @@
"typechain-target-ethers-v5": "5.0.1",
"yargs": "^14.0.0"
}
}
}
8 changes: 4 additions & 4 deletions packages/protocol/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* tslint:disable no-console */
import Web3V1Celo from '@celo/typechain-target-web3-v1-celo'
import { execSync } from 'child_process'
import { readJSONSync } from 'fs-extra'
import { readFileSync } from 'fs'
import path from 'path'
import { tsGenerator } from 'ts-generator'
import { MENTO_PACKAGE } from '../contractPackages'
import minimist from 'minimist'

const ROOT_DIR = path.normalize(path.join(__dirname, '../'))
const BUILD_DIR = path.join(ROOT_DIR, process.env.BUILD_DIR ?? './build')
Expand Down Expand Up @@ -105,8 +106,8 @@ function compile(outdir: string) {

for (const contractName of ImplContracts) {
try {
const fileStr = readJSONSync(`${outdir}/contracts/${contractName}.json`)
if (hasEmptyBytecode(fileStr)) {
const fileStr = readFileSync(`${outdir}/contracts/${contractName}.json`)
if (hasEmptyBytecode(JSON.parse(fileStr.toString()))) {
console.error(
`${contractName} has empty bytecode. Maybe you forgot to fully implement an interface?`
)
Expand Down Expand Up @@ -212,7 +213,6 @@ async function main(buildTargets: typeof _buildTargets) {
}
}

const minimist = require('minimist')
const argv = minimist(process.argv.slice(2), {
string: Object.keys(_buildTargets),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as path from 'path'
import rimraf from 'rimraf'
import { version } from '../package.json'

const ROOT_DIR = path.join(__dirname, '../')
const SRC_DIR = path.join(__dirname, '../contracts')
const BUILD_DIR = path.join(__dirname, '../build/contracts')
const TYPES_DIR = path.join(BUILD_DIR, 'types')
Expand Down Expand Up @@ -38,6 +39,13 @@ allFiles.forEach((filePath) => {
}
})

const tsconfig = JSON.parse(fs.readFileSync(path.join(ROOT_DIR, 'tsconfig.json'), 'utf8'))
const tsconfigForWagmi = { ...tsconfig }
tsconfig.target = 'ES6'
fs.writeFileSync(path.join(ROOT_DIR, 'tsconfig.json'), JSON.stringify(tsconfigForWagmi))
child_process.execSync(`yarn wagmi generate`, { stdio: 'inherit' })
child_process.execSync(`git checkout ${path.join(ROOT_DIR, 'tsconfig.json')}`, { stdio: 'inherit' })

fs.copyFileSync(path.join(SRC_DIR, 'package.abis.json'), path.join(BUILD_DIR, 'package.json'))
fs.copyFileSync(path.join(SRC_DIR, 'README.abis.md'), path.join(BUILD_DIR, 'README.md'))

Expand Down
27 changes: 27 additions & 0 deletions packages/protocol/wagmi.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { defineConfig } from '@wagmi/cli'
import { react } from '@wagmi/cli/plugins'
import * as path from 'path'
import { ImplContracts } from './scripts/build'
import { readFileSync } from 'fs'

const BUILD_DIR = path.join(__dirname, 'build', 'contracts')
console.log(BUILD_DIR)

const contracts = []
for (const contractName of new Set(ImplContracts)) {
try {
const fileStr = readFileSync(path.join(BUILD_DIR, `${contractName}.json`))
contracts.push({
name: contractName,
abi: JSON.parse(fileStr.toString()).abi,
})
} catch (e) {
console.debug(`WARNING: ${contractName} artifact could not be fetched. Maybe it doesn't exist?`)
}
}

export default defineConfig({
out: path.join(BUILD_DIR, 'types', 'wagmi', 'index.ts'),
contracts: contracts,
plugins: [react()],
})

0 comments on commit 3584a83

Please sign in to comment.