Skip to content

Commit

Permalink
Prevent abiParser from breaking too easily (#661)
Browse files Browse the repository at this point in the history
* Prevent abiParser from breaking too easily

* Improve test case name
  • Loading branch information
krzkaczor committed Mar 23, 2022
1 parent 7931301 commit e1f832c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-turtles-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typechain': patch
---

Make parsing JSON abi files more resilent. This should improve foundry integration.
14 changes: 10 additions & 4 deletions packages/typechain/src/parser/abiParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,31 @@ export function extractBytecode(rawContents: string): BytecodeWithLinkReferences

if (!json) return undefined

function tryMatchBytecode(obj: any | undefined): any | undefined {
if (obj && obj.match instanceof Function) {
return obj.match(bytecodeRegex)
}
}

// `json.evm.bytecode` often has more information than `json.bytecode`, needs to be checked first
if (json.evm?.bytecode?.object?.match(bytecodeRegex)) {
if (tryMatchBytecode(json.evm?.bytecode?.object)) {
return extractLinkReferences(json.evm.bytecode.object, json.evm.bytecode.linkReferences)
}

// handle json schema of @0x/sol-compiler
if (json.compilerOutput?.evm?.bytecode?.object?.match(bytecodeRegex)) {
if (tryMatchBytecode(json.compilerOutput?.evm?.bytecode?.object)) {
return extractLinkReferences(
json.compilerOutput.evm.bytecode.object,
json.compilerOutput.evm.bytecode.linkReferences,
)
}

// handle json schema of @foundry/forge
if (json.bytecode?.object?.match(bytecodeRegex)) {
if (tryMatchBytecode(json.bytecode?.object)) {
return extractLinkReferences(json.bytecode.object, json.bytecode.linkReferences)
}

if (json.bytecode?.match(bytecodeRegex)) {
if (tryMatchBytecode(json.bytecode)) {
return extractLinkReferences(json.bytecode, json.linkReferences)
}

Expand Down
4 changes: 4 additions & 0 deletions packages/typechain/test/parser/abiParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ describe('extractBytecode', () => {
expect(extractBytecode(`{ "bytecode": { "object": "${sampleBytecode}" } }`)).toEqual(resultBytecode)
})

it('should not throw when unexpected abi was passed', () => {
expect(extractBytecode(`{ "bytecode": { "smt_else": "${sampleBytecode}" } }`)).toEqual(undefined)
})

it('should return undefined when nested abi bytecode is malformed', () => {
expect(extractBytecode(`{ "bytecode": "surely-not-bytecode" }`)).toEqual(undefined)
})
Expand Down

0 comments on commit e1f832c

Please sign in to comment.