Skip to content

Commit 7a59242

Browse files
🪲 Fix issues with type literal size limits when exporting deployments (LayerZero-Labs#953)
1 parent 05340d7 commit 7a59242

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

‎.changeset/wise-onions-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@layerzerolabs/export-deployments": patch
3+
---
4+
5+
Work around the compiler type literal limits

‎.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ docker-*.yaml
1111
.github
1212
.husky
1313
.turbo
14+
.netrc
15+
.env
16+
.env*
1417

1518
# Misc
1619
*ignore

‎packages/export-deployments/src/generator/typescript/generate.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ import {
1111
createExportConst,
1212
createIdentifier,
1313
createStringLiteral,
14+
createTypeDeclaration,
15+
createTypeOf,
16+
createTypeReferenceNode,
1417
creteAsConst,
1518
normalizeIdentifierName,
1619
printTSFile,
1720
recordToObjectLiteral,
21+
recordToRecordType,
1822
runtimeObjectToExpressionSafe,
1923
} from './typescript'
2024
import { OutputFile } from '../types'
@@ -111,6 +115,8 @@ const contractsInformationSafe = flow(
111115
*/
112116
const createAbiIdentifier = (index: number) => createIdentifier(`abi${index}`)
113117

118+
const createAbiTypeIdentifier = (index: number) => createIdentifier(`Abi${index}`)
119+
114120
/**
115121
* Converts GroupedContractInformation into a TypeScript file contents string.
116122
*
@@ -146,12 +152,30 @@ const transformGroupedContractInformation = ({ addresses, abis, transactionHashe
146152
E.map((declarations) => [
147153
// We'll return all the variable declarations
148154
...declarations,
155+
// Then we'll add their types
156+
...pipe(
157+
declarations,
158+
A.mapWithIndex((index) =>
159+
pipe(
160+
createTypeDeclaration()(createAbiTypeIdentifier(index))(
161+
createTypeOf(createAbiIdentifier(index))
162+
)
163+
)
164+
)
165+
),
166+
// Then we'll add the type of the 'abis' object
167+
pipe(
168+
abiIndexesByNetworkName,
169+
R.map(createAbiTypeIdentifier),
170+
recordToRecordType,
171+
createTypeDeclaration()(createIdentifier('Abis'))
172+
),
149173
// And we'll add an object export to map network names to the ABIs
150174
pipe(
151175
abiIndexesByNetworkName,
152176
R.map(createAbiIdentifier),
153177
recordToObjectLiteral,
154-
createExportConst('abis')
178+
createExportConst('abis', createTypeReferenceNode('Abis'))
155179
),
156180
])
157181
),

‎packages/export-deployments/src/generator/typescript/typescript.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,42 @@ import {
1515
SyntaxKind,
1616
createPrinter,
1717
ListFormat,
18+
Identifier,
19+
TypeNode,
1820
} from 'typescript'
1921
import { stringify } from 'fp-ts/lib/Json'
2022
import { Ord } from 'fp-ts/lib/string'
2123

2224
export const createConst =
2325
(modifiers: ModifierLike[] = []) =>
24-
(name: string | BindingName) =>
26+
(name: string | BindingName, type?: TypeNode) =>
2527
(expression: Expression) =>
2628
factory.createVariableStatement(
2729
modifiers,
2830
factory.createVariableDeclarationList(
29-
[factory.createVariableDeclaration(name, undefined, undefined, expression)],
31+
[factory.createVariableDeclaration(name, undefined, type, expression)],
3032
NodeFlags.Const
3133
)
3234
)
3335

36+
export const createTypeOf = factory.createTypeQueryNode
37+
38+
export const createTypeReferenceNode = factory.createTypeReferenceNode
39+
40+
export const createTypeDeclaration =
41+
(modifiers: ModifierLike[] = []) =>
42+
(name: Identifier) =>
43+
(type: TypeNode) =>
44+
factory.createTypeAliasDeclaration(modifiers, name, undefined, type)
45+
3446
/**
3547
* Wraps an expression with "as const"
3648
*
3749
* @param {Expression} expression
3850
* @returns {Expression}
3951
*/
4052
export const creteAsConst = (expression: Expression): Expression =>
41-
factory.createAsExpression(
42-
expression,
43-
factory.createTypeReferenceNode(factory.createIdentifier('const'), undefined)
44-
)
53+
factory.createAsExpression(expression, createTypeReferenceNode(createIdentifier('const'), undefined))
4554

4655
export const createIdentifier = factory.createIdentifier
4756

@@ -70,6 +79,18 @@ export const recordToObjectLiteral = flow(
7079
(properties) => factory.createObjectLiteralExpression(properties, true)
7180
)
7281

82+
export const recordToRecordType = flow(
83+
R.collect(Ord)((key: string, value: Identifier) =>
84+
factory.createPropertySignature(
85+
undefined,
86+
factory.createStringLiteral(key),
87+
undefined,
88+
createTypeReferenceNode(value)
89+
)
90+
),
91+
(properties) => factory.createTypeLiteralNode(properties)
92+
)
93+
7394
/**
7495
* Parses a JSON string into TypeScript AST
7596
*

0 commit comments

Comments
 (0)