Skip to content

Commit

Permalink
Merge pull request #26 from peeramid-labs/add-viem-abi-export
Browse files Browse the repository at this point in the history
added viem compatible abi export during compilation
  • Loading branch information
peersky authored Dec 6, 2024
2 parents 5076f15 + 4ece252 commit 7801e72
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-sheep-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@peeramid-labs/multipass': patch
---

added viem compatible abi export during compilation
28 changes: 26 additions & 2 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import 'solidity-coverage';
import { task } from 'hardhat/config';
import { task, subtask } from 'hardhat/config';
import { TASK_COMPILE_SOLIDITY_EMIT_ARTIFACTS } from 'hardhat/builtin-tasks/task-names';
import { join } from 'path';
import { writeFile, mkdir } from 'fs/promises';
import { inspect } from 'util';
import '@nomicfoundation/hardhat-chai-matchers';
import '@nomicfoundation/hardhat-toolbox';
import 'hardhat-abi-exporter';
Expand All @@ -9,6 +13,26 @@ import 'hardhat-deploy';
import 'solidity-docgen';
import './playbook';

type ContractMap = Record<string, { abi: object }>;

subtask(TASK_COMPILE_SOLIDITY_EMIT_ARTIFACTS).setAction(async (args, env, next) => {
const output = await next();
const promises = Object.entries(args.output.contracts).map(async ([sourceName, contract]) => {
// Extract the contract name from the full path
const contractName = sourceName.split('/').pop()?.replace('.sol', '') || '';
const dirPath = join('./abi', sourceName);
await mkdir(dirPath, { recursive: true });
const file = join(dirPath, `${contractName}.ts`);
const { abi } = Object.values(contract as ContractMap)[0];
if (JSON.stringify(abi).length > 2) {
const data = `export const abi = ${inspect(abi, false, null)} as const; export default abi;`;
await writeFile(file, data);
}
});
await Promise.all(promises);
return output;
});

task('accounts', 'Prints the list of accounts', async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();

Expand Down Expand Up @@ -49,7 +73,7 @@ export default {
},
defaultPlayer: {
localhost: '0xF52E5dF676f51E410c456CC34360cA6F27959420',
}
},
},
defaultNetwork: 'hardhat',
networks: {
Expand Down

0 comments on commit 7801e72

Please sign in to comment.