This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/spool-v2
- Loading branch information
Showing
5,072 changed files
with
799,418 additions
and
1,702,522 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,20 @@ | ||
/* eslint no-console: 0 */ | ||
import fs, { writeFileSync } from 'fs'; | ||
import path from 'path'; | ||
import util from 'util'; | ||
|
||
const exists = util.promisify(fs.exists); | ||
|
||
export const generateIndex = async (location: string) => { | ||
let content = ` | ||
/* Autogenerated file. Do not edit manually. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
`; | ||
|
||
if (await exists(path.join(location, `/contracts/viem.contract-factory.ts`))) { | ||
content += `export * from './viem.contract-factory';\n`; | ||
} | ||
|
||
writeFileSync(path.join(location, `/contracts/index.ts`), content); | ||
}; |
71 changes: 71 additions & 0 deletions
71
cli/commands/generate-contract-factory/generate-viem-contract-factory.ts
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,71 @@ | ||
/* eslint no-console: 0 */ | ||
import { writeFileSync } from 'fs'; | ||
import path from 'path'; | ||
|
||
import { readFile } from 'fs-extra'; | ||
import { camelCase, sortBy, upperFirst } from 'lodash'; | ||
|
||
import { getAbis } from './get-abis'; | ||
|
||
export const generateViemContractFactory = async (location: string) => { | ||
const abis = await getAbis(location); | ||
const maybeAppId = path.basename(location); | ||
|
||
const sortedAbis = sortBy(abis); | ||
const globalAppId = 'contract'; | ||
const globalClassName = 'ContractViemContractFactory'; | ||
const appClassName = `${upperFirst(camelCase(maybeAppId))}ViemContractFactory`; | ||
const className = maybeAppId === globalAppId ? globalClassName : appClassName; | ||
|
||
const moduleFile = await readFile(path.resolve(location, `${maybeAppId}.module.ts`), 'utf-8').catch(() => ''); | ||
const hasAppToolkitDep = moduleFile.includes(`extends AbstractApp()`); | ||
|
||
const renderer = { | ||
viem: async () => { | ||
writeFileSync( | ||
path.join(location, `/contracts/viem.contract-factory.ts`), | ||
` | ||
import { Injectable, Inject } from '@nestjs/common'; | ||
import { PublicClient } from 'viem'; | ||
import { Network } from '~types/network.interface'; | ||
${hasAppToolkitDep ? `import { IAppToolkit, APP_TOOLKIT } from '~app-toolkit/app-toolkit.interface';` : ''} | ||
${!hasAppToolkitDep ? `import { Web3Service } from '~web3/web3.service';` : ''} | ||
${ | ||
sortedAbis.length | ||
? `import { ${sortedAbis.map(abi => `${upperFirst(camelCase(abi))}__factory`).join(', ')} } from './viem';` | ||
: '' | ||
} | ||
${sortedAbis.length ? `type ContractOpts = {address: string, network: Network};` : ''} | ||
${!hasAppToolkitDep ? `type ViemNetworkProviderResolver = (network: Network) => PublicClient;` : ''} | ||
@Injectable() | ||
export class ${className} { | ||
${ | ||
hasAppToolkitDep | ||
? `constructor(@Inject(APP_TOOLKIT) protected readonly appToolkit: IAppToolkit) {}` | ||
: `constructor(protected readonly networkProviderResolver: ViemNetworkProviderResolver) {}` | ||
} | ||
${sortedAbis | ||
.map( | ||
abiName => | ||
`${camelCase(abiName)}({address, network}: ContractOpts) { return ${upperFirst( | ||
camelCase(abiName), | ||
)}__factory.connect(address, ${ | ||
hasAppToolkitDep | ||
? `this.appToolkit.getViemNetworkProvider(network)` | ||
: `this.networkProviderResolver(network)` | ||
}); }`, | ||
) | ||
.join('\n')} | ||
} | ||
`, | ||
); | ||
}, | ||
}; | ||
|
||
await renderer.viem(); | ||
}; |
Oops, something went wrong.