Skip to content

Commit

Permalink
fix: 🐛 accept truffle-style ABI files without contractName
Browse files Browse the repository at this point in the history
Accept ABI files in truffle-build format even if they don't contain a
`contractName` property. Added basic check for ABI items to see if they
have a `type` property.
  • Loading branch information
ziegfried committed Apr 26, 2021
1 parent 26e18ef commit e2f3e28
Show file tree
Hide file tree
Showing 4 changed files with 1,918 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/abi/files.ts
Expand Up @@ -29,12 +29,16 @@ export interface SignatureFileContents {
entries: Array<[string, AbiItemDefinition[]]>;
}

export function isAbiItem(obj: any): obj is AbiItem {
return typeof obj.type === 'string';
}

export function isAbiItemArray(obj: any): obj is AbiItem[] {
return Array.isArray(obj);
return Array.isArray(obj) && obj.every(isAbiItem);
}

export function isTruffleBuildFile(obj: any): obj is TruffleBuild {
return typeof obj === 'object' && typeof obj.contractName === 'string' && Array.isArray(obj.abi);
return typeof obj === 'object' && isAbiItemArray(obj.abi);
}

export function extractDeployedContractAddresses(truffleBuild: TruffleBuild): Address[] | undefined {
Expand Down
74 changes: 74 additions & 0 deletions test/abi/__snapshots__/files.test.ts.snap
@@ -1,5 +1,79 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loadAbiFile loads EventEmitter.json 1`] = `
Object {
"contractAddress": undefined,
"contractFingerprint": "a9e90fbc1bf47877d0dd918a13af663585b3290b93c0cd53b0869705ae1137c3",
"contractName": "EventEmitter",
"entries": Array [
Object {
"abi": Object {
"contractAddress": undefined,
"contractFingerprint": "a9e90fbc1bf47877d0dd918a13af663585b3290b93c0cd53b0869705ae1137c3",
"contractName": "EventEmitter",
"inputs": Array [
Object {
"indexed": false,
"internalType": "address",
"name": "_to",
"type": "address",
},
Object {
"indexed": false,
"internalType": "uint256",
"name": "_amount",
"type": "uint256",
},
],
"name": "stored",
"type": "event",
},
"sig": "stored(address,uint256)",
},
Object {
"abi": Object {
"contractAddress": undefined,
"contractFingerprint": "a9e90fbc1bf47877d0dd918a13af663585b3290b93c0cd53b0869705ae1137c3",
"contractName": "EventEmitter",
"inputs": Array [],
"name": "sender",
"type": "function",
},
"sig": "sender()",
},
Object {
"abi": Object {
"contractAddress": undefined,
"contractFingerprint": "a9e90fbc1bf47877d0dd918a13af663585b3290b93c0cd53b0869705ae1137c3",
"contractName": "EventEmitter",
"inputs": Array [
Object {
"internalType": "uint256",
"name": "_amount",
"type": "uint256",
},
],
"name": "store",
"type": "function",
},
"sig": "store(uint256)",
},
Object {
"abi": Object {
"contractAddress": undefined,
"contractFingerprint": "a9e90fbc1bf47877d0dd918a13af663585b3290b93c0cd53b0869705ae1137c3",
"contractName": "EventEmitter",
"inputs": Array [],
"name": "value",
"type": "function",
},
"sig": "value()",
},
],
"fileName": "abis/EventEmitter.json",
}
`;

exports[`loadAbiFile loads raw ABI file 1`] = `
Object {
"contractAddress": undefined,
Expand Down
12 changes: 12 additions & 0 deletions test/abi/files.test.ts
Expand Up @@ -25,4 +25,16 @@ describe('loadAbiFile', () => {
})
).resolves.toMatchSnapshot();
});

it('loads EventEmitter.json', async () => {
await expect(
loadAbiFile(join(__dirname, '../abis/EventEmitter.json'), {
decodeAnonymous: false,
fingerprintContracts: true,
directory: join(__dirname, '..'),
requireContractMatch: true,
reconcileStructShapeFromTuples: false,
})
).resolves.toMatchSnapshot();
});
});

0 comments on commit e2f3e28

Please sign in to comment.