From d7d1af1d64da0bf957a719aae981ff07d7c84995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Jakub=20Nani=C5=A1ta?= Date: Thu, 15 Feb 2024 15:00:34 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=AA=B2=20Export=20ABIs=20as=20constants?= =?UTF-8?q?=20for=20better=20viem=20integration=20(#410)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/shiny-years-join.md | 6 ++++++ .../src/generator/typescript/generate.ts | 12 +++++++++++- .../src/generator/typescript/typescript.ts | 12 ++++++++++++ tests/export-deployments-test/test/export.test.ts | 10 ++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 .changeset/shiny-years-join.md diff --git a/.changeset/shiny-years-join.md b/.changeset/shiny-years-join.md new file mode 100644 index 000000000..752dab806 --- /dev/null +++ b/.changeset/shiny-years-join.md @@ -0,0 +1,6 @@ +--- +"@layerzerolabs/export-deployments-test": patch +"@layerzerolabs/export-deployments": patch +--- + +Export all ABIs as constants diff --git a/packages/export-deployments/src/generator/typescript/generate.ts b/packages/export-deployments/src/generator/typescript/generate.ts index d091067ab..a9b3734d5 100644 --- a/packages/export-deployments/src/generator/typescript/generate.ts +++ b/packages/export-deployments/src/generator/typescript/generate.ts @@ -11,6 +11,7 @@ import { createExportConst, createIdentifier, createStringLiteral, + creteAsConst, normalizeIdentifierName, printTSFile, recordToObjectLiteral, @@ -128,7 +129,16 @@ const transformGroupedContractInformation = ({ addresses, abis, transactionHashe uniqueAbis, // Take the array of unique ABIs and turn them into a list of variable declarations A.mapWithIndex((index, abi) => - pipe(abi, runtimeObjectToExpressionSafe, E.map(createConst()(createAbiIdentifier(index)))) + pipe( + abi, + runtimeObjectToExpressionSafe, + // Add "as const" to the exported ABIs + // + // This is very useful for e.g. viem that infers a lot of the information + // based on the shape of the ABI + E.map(creteAsConst), + E.map(createConst()(createAbiIdentifier(index))) + ) ), A.sequence(E.Applicative), // With the declarations ready, we can construct an object that maps a network name diff --git a/packages/export-deployments/src/generator/typescript/typescript.ts b/packages/export-deployments/src/generator/typescript/typescript.ts index 5be7e65d7..cd021f064 100644 --- a/packages/export-deployments/src/generator/typescript/typescript.ts +++ b/packages/export-deployments/src/generator/typescript/typescript.ts @@ -31,6 +31,18 @@ export const createConst = ) ) +/** + * Wraps an expression with "as const" + * + * @param {Expression} expression + * @returns {Expression} + */ +export const creteAsConst = (expression: Expression): Expression => + factory.createAsExpression( + expression, + factory.createTypeReferenceNode(factory.createIdentifier('const'), undefined) + ) + export const createIdentifier = factory.createIdentifier export const createStringLiteral = factory.createStringLiteral diff --git a/tests/export-deployments-test/test/export.test.ts b/tests/export-deployments-test/test/export.test.ts index 3903b7f35..ceedd85ef 100644 --- a/tests/export-deployments-test/test/export.test.ts +++ b/tests/export-deployments-test/test/export.test.ts @@ -4,6 +4,7 @@ import { spawnSync } from 'child_process' import { join } from 'path' import { isDirectory } from '@layerzerolabs/io-devtools' +import { readFileSync } from 'fs' describe(`export`, () => { describe('expectations', () => { @@ -102,5 +103,14 @@ describe(`export`, () => { }, }) }) + + it('should include "as const" when exporting the ABIs', async () => { + const result = runExpect('export-all') + + expect(result.status).toBe(0) + + const generated = readFileSync(join(__dirname, '..', 'generated', 'Test.ts'), 'utf8') + expect(generated).toMatch(/as const/) + }) }) })