Skip to content

Commit e1f832c

Browse files
authoredMar 23, 2022
Prevent abiParser from breaking too easily (#661)
* Prevent abiParser from breaking too easily * Improve test case name
1 parent 7931301 commit e1f832c

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed
 

Diff for: ‎.changeset/wet-turtles-type.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typechain': patch
3+
---
4+
5+
Make parsing JSON abi files more resilent. This should improve foundry integration.

Diff for: ‎packages/typechain/src/parser/abiParser.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -402,25 +402,31 @@ export function extractBytecode(rawContents: string): BytecodeWithLinkReferences
402402

403403
if (!json) return undefined
404404

405+
function tryMatchBytecode(obj: any | undefined): any | undefined {
406+
if (obj && obj.match instanceof Function) {
407+
return obj.match(bytecodeRegex)
408+
}
409+
}
410+
405411
// `json.evm.bytecode` often has more information than `json.bytecode`, needs to be checked first
406-
if (json.evm?.bytecode?.object?.match(bytecodeRegex)) {
412+
if (tryMatchBytecode(json.evm?.bytecode?.object)) {
407413
return extractLinkReferences(json.evm.bytecode.object, json.evm.bytecode.linkReferences)
408414
}
409415

410416
// handle json schema of @0x/sol-compiler
411-
if (json.compilerOutput?.evm?.bytecode?.object?.match(bytecodeRegex)) {
417+
if (tryMatchBytecode(json.compilerOutput?.evm?.bytecode?.object)) {
412418
return extractLinkReferences(
413419
json.compilerOutput.evm.bytecode.object,
414420
json.compilerOutput.evm.bytecode.linkReferences,
415421
)
416422
}
417423

418424
// handle json schema of @foundry/forge
419-
if (json.bytecode?.object?.match(bytecodeRegex)) {
425+
if (tryMatchBytecode(json.bytecode?.object)) {
420426
return extractLinkReferences(json.bytecode.object, json.bytecode.linkReferences)
421427
}
422428

423-
if (json.bytecode?.match(bytecodeRegex)) {
429+
if (tryMatchBytecode(json.bytecode)) {
424430
return extractLinkReferences(json.bytecode, json.linkReferences)
425431
}
426432

Diff for: ‎packages/typechain/test/parser/abiParser.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ describe('extractBytecode', () => {
145145
expect(extractBytecode(`{ "bytecode": { "object": "${sampleBytecode}" } }`)).toEqual(resultBytecode)
146146
})
147147

148+
it('should not throw when unexpected abi was passed', () => {
149+
expect(extractBytecode(`{ "bytecode": { "smt_else": "${sampleBytecode}" } }`)).toEqual(undefined)
150+
})
151+
148152
it('should return undefined when nested abi bytecode is malformed', () => {
149153
expect(extractBytecode(`{ "bytecode": "surely-not-bytecode" }`)).toEqual(undefined)
150154
})

0 commit comments

Comments
 (0)
Please sign in to comment.