Skip to content

Commit 87e1334

Browse files
committed
Replace regex with index and substring
In case name of the file doesn't match the actual contract name, the regex would not be valid. Instead, we just pick the contract name following colon.
1 parent 6306e75 commit 87e1334

File tree

1 file changed

+5
-4
lines changed
  • packages/cli/src/command-helpers

1 file changed

+5
-4
lines changed

packages/cli/src/command-helpers/abi.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ export const getContractNameForAddress = async (
151151
): Promise<string> => {
152152
try {
153153
const contractSourceCode = await fetchSourceCodeFromEtherscan(network, address);
154-
let contractName = contractSourceCode.result[0].ContractName;
155-
// Some explorers will return the full path of the contract instead of just the name
156-
const regex = /(?<contract>.+)\.sol:\1$/;
154+
let contractName: string = contractSourceCode.result[0].ContractName;
157155

158-
if (regex.test(contractName)) contractName = regex.exec(contractName)?.groups?.contract;
156+
// Some explorers will return the full path of the contract instead of just the name
157+
// Example: contracts/SyncSwapRouter.sol:SyncSwapRouter
158+
if (contractName.includes(':'))
159+
contractName = contractName.substring(contractName.lastIndexOf(':') + 1);
159160

160161
logger('Successfully getContractNameForAddress. contractName: %s', contractName);
161162
return contractName;

0 commit comments

Comments
 (0)