Skip to content

Commit

Permalink
#98 Allow for descriminate-types options and other typechain flags (#99)
Browse files Browse the repository at this point in the history
Co-authored-by: Kris Kaczor <[email protected]>
  • Loading branch information
ShravanSunder and krzkaczor authored May 15, 2022
1 parent 5bb53ba commit cd56037
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-islands-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@dethcrypto/eth-sdk': patch
---

TypeChain flags can be via eth-sdk config.
4 changes: 3 additions & 1 deletion packages/eth-sdk/src/client/generateTsClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debug from 'debug'
import { join } from 'path'
import { CodegenConfig } from 'typechain'

import { EthSdkContracts } from '../config'
import { Fs, realFs } from '../peripherals/fs'
Expand All @@ -13,10 +14,11 @@ export async function generateTsClient(
outputRoot: string,
outputToAbiRelativePath: string,
fs: Fs = realFs,
typechainFlags: CodegenConfig,
) {
d(`Generating ts client to ${outputRoot}`)
const typesOutputPath = join(outputRoot, './types')
await generateTypes(abisRoot, typesOutputPath)
await generateTypes(abisRoot, typesOutputPath, typechainFlags)

const abisRootOut = join(outputRoot, 'abis')
await fs.copy(abisRoot, abisRootOut)
Expand Down
5 changes: 3 additions & 2 deletions packages/eth-sdk/src/client/generateTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path'
import { glob, runTypeChain } from 'typechain'
import { CodegenConfig, glob, runTypeChain } from 'typechain'

export async function generateTypes(abisRoot: string, outputPath: string) {
export async function generateTypes(abisRoot: string, outputPath: string, typechainFlags: CodegenConfig) {
const cwd = process.cwd()
const files = glob(cwd, [join(abisRoot, '/**/*.json')])

Expand All @@ -12,5 +12,6 @@ export async function generateTypes(abisRoot: string, outputPath: string) {
target: 'ethers-v5',
outDir: outputPath,
inputDir: abisRoot,
flags: typechainFlags,
})
}
15 changes: 13 additions & 2 deletions packages/eth-sdk/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { join, relative } from 'path'
import { CodegenConfig } from 'typechain'

import { EthSdkCtx } from '../types'
import { generateTsClient } from './generateTsClient'
Expand All @@ -7,7 +8,7 @@ import { transpileClient } from './transpileClient'
export async function generateSdk(ctx: EthSdkCtx): Promise<void> {
const {
cliArgs: { workingDirPath },
config: { contracts, outputPath },
config: { contracts, outputPath, typechainFlags },
fs,
} = ctx

Expand All @@ -19,7 +20,17 @@ export async function generateSdk(ctx: EthSdkCtx): Promise<void> {
const outputToAbiRelativePath = relative(outputPath, abisRoot).replace(/\\/g, '/')

const randomTmpDir = await fs.tmpDir('eth-sdk')
await generateTsClient(contracts, abisRoot, randomTmpDir, outputToAbiRelativePath, fs)

const shapedFlag: CodegenConfig = {
discriminateTypes: typechainFlags?.discriminateTypes ?? false,
alwaysGenerateOverloads: typechainFlags?.alwaysGenerateOverloads ?? false,
environment: undefined,
}
if (typechainFlags?.tsNocheck != null) {
shapedFlag.tsNocheck = typechainFlags.tsNocheck
}

await generateTsClient(contracts, abisRoot, randomTmpDir, outputToAbiRelativePath, fs, shapedFlag)
await transpileClient(randomTmpDir, outputPath, fs)
}

Expand Down
7 changes: 7 additions & 0 deletions packages/eth-sdk/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export type NetworkIds = { [key in NetworkSymbol | (string & {})]?: number }

export const networkIdsSchema: z.ZodSchema<NetworkIds> = z.record(z.number())

export const flagsSchema = z.object({
tsNocheck: z.optional(z.boolean()),
discriminateTypes: z.boolean(),
alwaysGenerateOverloads: z.boolean(),
})

const ethSdkConfigSchema = z
.object({
contracts: ethSdKContractsSchema,
Expand All @@ -83,6 +89,7 @@ const ethSdkConfigSchema = z
noFollowProxies: z.boolean().optional(),
abiSource: abiSourceSchema.default(DEFAULT_ABI_SOURCE),
networkIds: networkIdsSchema.default({}),
typechainFlags: flagsSchema.optional(),
})
.strict()

Expand Down

0 comments on commit cd56037

Please sign in to comment.