Skip to content

Commit

Permalink
feat: create wagmi abis with contractor
Browse files Browse the repository at this point in the history
  • Loading branch information
chmanie committed Dec 20, 2024
1 parent ddc163e commit 7cc6781
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './constants.js';
export * from './helpers/index.js';
export * from './types.js';
export * from './versions/index.js';
export * from './utils/index.js';
4 changes: 2 additions & 2 deletions packages/core/src/utils/index.ts
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;
};
10 changes: 8 additions & 2 deletions packages/events/src/ColonyEventManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
type EventFilter,
type BaseContract,
} from 'ethers';
import { addressesAreEqual, type SignerOrProvider } from '@colony/core';

import {
addressesAreEqual,
nonNullable,
type SignerOrProvider,
} from '@colony/core';

import {
type IpfsAdapter,
Expand All @@ -16,8 +21,9 @@ import {
IpfsMetadata,
type MetadataType,
} from './ipfs/index.js';

import type { Ethers6Filter } from './types.js';
import { getLogs, nonNullable } from './utils.js';
import { getLogs } from './utils.js';

/**
* A valid eventsource (currently just an ethers.js {@link BaseContract})
Expand Down
5 changes: 0 additions & 5 deletions packages/events/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,3 @@ export const getLogs = async (
const usedFilter = await filter;
return provider.send('eth_getLogs', [usedFilter]);
};

/** 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;
};
57 changes: 57 additions & 0 deletions packages/wagmi-plugin/package.json
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"
}
}
68 changes: 68 additions & 0 deletions packages/wagmi-plugin/src/index.ts
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;
22 changes: 22 additions & 0 deletions packages/wagmi-plugin/tsconfig.json
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"
]
}
77 changes: 72 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7cc6781

Please sign in to comment.