From 93271b07116752c74419720a39c1fdbd460c078d Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Mon, 25 Nov 2024 22:37:48 +0000 Subject: [PATCH] Support parsing full path contract name from block explorers (#1771) * 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. --- .changeset/cyan-spoons-greet.md | 5 +++++ packages/cli/src/command-helpers/abi.ts | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changeset/cyan-spoons-greet.md diff --git a/.changeset/cyan-spoons-greet.md b/.changeset/cyan-spoons-greet.md new file mode 100644 index 000000000..c4161fd56 --- /dev/null +++ b/.changeset/cyan-spoons-greet.md @@ -0,0 +1,5 @@ +--- +'@graphprotocol/graph-cli': patch +--- + +Support parsing full path contract name from block explorers diff --git a/packages/cli/src/command-helpers/abi.ts b/packages/cli/src/command-helpers/abi.ts index b30dcb6de..024fcb4f5 100644 --- a/packages/cli/src/command-helpers/abi.ts +++ b/packages/cli/src/command-helpers/abi.ts @@ -151,7 +151,13 @@ export const getContractNameForAddress = async ( ): Promise => { try { const contractSourceCode = await fetchSourceCodeFromEtherscan(network, address); - const contractName = contractSourceCode.result[0].ContractName; + let contractName: string = contractSourceCode.result[0].ContractName; + + // Some explorers will return the full path of the contract instead of just the name + // Example: contracts/SyncSwapRouter.sol:SyncSwapRouter + if (contractName.includes(':')) + contractName = contractName.substring(contractName.lastIndexOf(':') + 1); + logger('Successfully getContractNameForAddress. contractName: %s', contractName); return contractName; } catch (error) {