Skip to content

Commit

Permalink
Match Foundry style contract name formats
Browse files Browse the repository at this point in the history
  • Loading branch information
ericglau committed Oct 20, 2023
1 parent 683ef22 commit fc1c6c0
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
67 changes: 67 additions & 0 deletions packages/core/src/cli/validate/find-contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import test from 'ava';

import { isMatch } from './find-contract';

test('fully qualified - match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.true(isMatch('contracts/MyContract.sol:Foo', sourceContract as any));
});

test('fully qualified without folder - match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'MyContract.sol:Foo',
};

t.true(isMatch('MyContract.sol:Foo', sourceContract as any));
});

test('short name - match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.true(isMatch('Foo', sourceContract as any));
});

test('short name - match without folder', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'MyContract.sol:Foo',
};

t.true(isMatch('Foo', sourceContract as any));
});

test('short name with .sol - no match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'contracts/MyContract.sol:Foo',
};

t.false(isMatch('MyContract.sol', sourceContract as any));
t.false(isMatch('Foo.sol', sourceContract as any));
});

test('short name with .sol - match', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'contracts/Foo.sol:Foo',
};

t.true(isMatch('Foo.sol', sourceContract as any));
});

test('short name with .sol - match without folder', async t => {
const sourceContract = {
name: 'Foo',
fullyQualifiedName: 'Foo.sol:Foo',
};

t.true(isMatch('Foo.sol', sourceContract as any));
});
37 changes: 36 additions & 1 deletion packages/core/src/cli/validate/find-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ReferenceContractNotFound extends Error {
}

export function findContract(contractName: string, origin: SourceContract | undefined, allContracts: SourceContract[]) {
const foundContracts = allContracts.filter(c => c.fullyQualifiedName === contractName || c.name === contractName);
const foundContracts = allContracts.filter(c => isMatch(contractName, c));

if (foundContracts.length > 1) {
const msg =
Expand All @@ -42,3 +42,38 @@ export function findContract(contractName: string, origin: SourceContract | unde
throw new ReferenceContractNotFound(contractName, origin?.fullyQualifiedName);
}
}

export function isMatch(contractName: string, contract: SourceContract) {
return (
contract.fullyQualifiedName === contractName || // contracts/MyContract.sol:MyContract
contract.name === contractName || // MyContract
matchesDotSolAndName(contractName, contract) || // MyContract.sol:MyContract
matchesDotSol(contractName, contract) // MyContract.sol
);
}

function matchesDotSolAndName(contractName: string, contract: SourceContract) {
if (contractName.includes('.sol:')) {
const [fileWithoutExtension, name] = contractName.split('.sol:');
return matchesFullyQualifiedName(fileWithoutExtension, name, contract);
} else {
return false;
}
}

function matchesDotSol(contractName: string, contract: SourceContract) {
if (contractName.endsWith('.sol')) {
const name = contractName.slice(0, contractName.length - 4);
return matchesFullyQualifiedName(name, name, contract);
} else {
return false;
}
}

function matchesFullyQualifiedName(fileNameWithoutExtension: string, name: string, contract: SourceContract) {
const lastSlash = contract.fullyQualifiedName.lastIndexOf('/');
const fullyQualifiedWithoutPath =
lastSlash >= 0 ? contract.fullyQualifiedName.slice(lastSlash + 1) : contract.fullyQualifiedName;

return contract.name === name && fullyQualifiedWithoutPath === `${fileNameWithoutExtension}.sol:${name}`;
}

0 comments on commit fc1c6c0

Please sign in to comment.