-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create wagmi abis with contractor
- Loading branch information
Showing
8 changed files
with
230 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/** @internal */ | ||
/** Make sure to use all possible options in a switch statement */ | ||
export const assertExhaustiveSwitch = (x: never, msg: string): never => { | ||
throw new Error(`${msg}: ${x}`); | ||
}; | ||
|
||
/** @internal */ | ||
/** Use this to filter empty undefinied values from arrays in a type-safe way */ | ||
export const nonNullable = <T>(value: T): value is NonNullable<T> => { | ||
return value !== null && value !== undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"name": "@colony/wagmi-plugin", | ||
"version": "0.1", | ||
"description": "A plugin to generate wagmi/abitype compatible ABIs", | ||
"scripts": { | ||
"clean": "rimraf ./dist", | ||
"build": "npm run clean && tsc --declaration --outDir dist", | ||
"lint": "eslint --ext .ts src", | ||
"typecheck": "tsc --noEmit" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"default": "./dist/index.js" | ||
} | ||
}, | ||
"module": "./dist/esm/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
"files": [ | ||
"./dist", | ||
"README.md" | ||
], | ||
"type": "module", | ||
"engines": { | ||
"node": "^18 || ^20 || ^22", | ||
"pnpm": "^8" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/JoinColony/colonyJS.git" | ||
}, | ||
"author": "Christian Maniewski <[email protected]>", | ||
"license": "GPL-3.0-only", | ||
"bugs": { | ||
"url": "https://github.com/JoinColony/colonyJS/issues" | ||
}, | ||
"homepage": "https://docs.colony.io/develop", | ||
"dependencies": { | ||
"@colony/abis": "^1.8.1", | ||
"@colony/core": "^2.3.0", | ||
"@typechain/ethers-v5": "^11.1.0", | ||
"@typechain/ethers-v6": "^0.4.2", | ||
"@types/mkdirp": "^1.0.2", | ||
"@types/yargs": "^17.0.24", | ||
"abitype": "^1.0.6", | ||
"cross-fetch": "^4.0.0", | ||
"mkdirp": "^1.0.4", | ||
"rimraf": "^5.0.0", | ||
"typechain": "8.3.0", | ||
"yargs": "^17.7.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.15.11", | ||
"ts-node": "^10.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { createRequire } from 'node:module'; | ||
import { join as joinPath } from 'node:path'; | ||
import { readFile } from 'node:fs/promises'; | ||
import { fetch } from 'cross-fetch'; | ||
import { type Address } from 'abitype'; | ||
import { joinAbis, type NodeType } from '@colony/abis/utils'; | ||
import { nonNullable } from '@colony/core'; | ||
|
||
interface ContractConfig { | ||
name: string; | ||
path: string; | ||
address?: Address; | ||
merge?: NodeType; | ||
} | ||
|
||
interface ColonyConfig { | ||
baseUrl: string; | ||
contracts: ContractConfig[]; | ||
} | ||
|
||
// Capitalize the first letter of a string | ||
const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); | ||
|
||
const resolveAbiFile = async (path: string) => { | ||
const require = createRequire(import.meta.url); | ||
return require.resolve(path); | ||
}; | ||
|
||
const colony = (config: ColonyConfig) => { | ||
const { baseUrl, contracts } = config; | ||
return { | ||
name: 'Colony', | ||
async contracts() { | ||
const promises = contracts.map(async ({ name, path, address, merge }) => { | ||
try { | ||
if (merge) { | ||
const typeSuffix = capitalize(merge); | ||
const eventsAbiFile = await resolveAbiFile( | ||
`@colony/abis/${merge}/${name}${typeSuffix}`, | ||
); | ||
const contents = await readFile(eventsAbiFile); | ||
const { abi: historicalAbi } = JSON.parse(contents.toString()); | ||
const res = await fetch(joinPath(baseUrl, path)); | ||
const { abi } = await res.json(); | ||
// Join historical and current ABIs | ||
return { | ||
abi: joinAbis(historicalAbi, abi, merge), | ||
name: `${name}${typeSuffix}`, | ||
}; | ||
} | ||
const res = await fetch(joinPath(baseUrl, path)); | ||
const { abi } = await res.json(); | ||
return { abi, address, name }; | ||
} catch (e) { | ||
console.error( | ||
`Could not read contract file ${name}:`, | ||
(e as Error).message, | ||
); | ||
return undefined; | ||
} | ||
}); | ||
const results = await Promise.all(promises); | ||
return results.filter(nonNullable); | ||
}, | ||
}; | ||
}; | ||
|
||
export default colony; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": [ | ||
"es2022" | ||
], | ||
"module": "nodenext", | ||
"moduleResolution": "nodenext", | ||
"noImplicitAny": true, | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"strict": true, | ||
"target": "es2022", | ||
"types": [ | ||
"node" | ||
], | ||
"rootDir": "./src", | ||
"skipLibCheck": true | ||
}, | ||
"include": [ | ||
"./src" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.