Skip to content

Commit

Permalink
🪲 Export ABIs as constants for better viem integration (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Feb 15, 2024
1 parent 0170fdd commit d7d1af1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/shiny-years-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@layerzerolabs/export-deployments-test": patch
"@layerzerolabs/export-deployments": patch
---

Export all ABIs as constants

This comment has been minimized.

Copy link
@cl3pl4t3

cl3pl4t3 Feb 25, 2024

12 changes: 11 additions & 1 deletion packages/export-deployments/src/generator/typescript/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createExportConst,
createIdentifier,
createStringLiteral,
creteAsConst,
normalizeIdentifierName,
printTSFile,
recordToObjectLiteral,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions packages/export-deployments/src/generator/typescript/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/export-deployments-test/test/export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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/)
})
})
})

0 comments on commit d7d1af1

Please sign in to comment.