From f1cb79ea0103605cf3e8eee6e5ac3807c1a9c6a2 Mon Sep 17 00:00:00 2001 From: Manuel Wedler Date: Thu, 1 Aug 2024 16:20:27 +0200 Subject: [PATCH] Get source code with Etherscan lookup --- .../src/app/Verifiers/BlockscoutVerifier.ts | 4 +- .../src/app/Verifiers/EtherscanVerifier.ts | 43 +++++++++++++++---- .../src/app/Verifiers/SourcifyVerifier.ts | 6 +-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/apps/contract-verification/src/app/Verifiers/BlockscoutVerifier.ts b/apps/contract-verification/src/app/Verifiers/BlockscoutVerifier.ts index c408e7dab1..fd7b95563f 100644 --- a/apps/contract-verification/src/app/Verifiers/BlockscoutVerifier.ts +++ b/apps/contract-verification/src/app/Verifiers/BlockscoutVerifier.ts @@ -32,11 +32,11 @@ export class BlockscoutVerifier extends EtherscanVerifier { return url.href } - processReceivedFiles(source: unknown, contractAddress: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { + processReceivedFiles(source: unknown, contractAddress: string, chainId: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { const blockscoutSource = source as BlockscoutSource const result: SourceFile[] = [] - const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}` + const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}` const targetFilePath = `${filePrefix}/${blockscoutSource.FileName}` result.push({ content: blockscoutSource.SourceCode, path: targetFilePath }) diff --git a/apps/contract-verification/src/app/Verifiers/EtherscanVerifier.ts b/apps/contract-verification/src/app/Verifiers/EtherscanVerifier.ts index f5fc04eb21..526de822e2 100644 --- a/apps/contract-verification/src/app/Verifiers/EtherscanVerifier.ts +++ b/apps/contract-verification/src/app/Verifiers/EtherscanVerifier.ts @@ -235,8 +235,7 @@ export class EtherscanVerifier extends AbstractVerifier { } const lookupUrl = this.getContractCodeUrl(contractAddress) - console.log(lookupResponse) - const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.result[0], contractAddress) + const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.result[0], contractAddress, chainId) return { status: 'verified', lookupUrl, sourceFiles, targetFilePath } } @@ -246,12 +245,40 @@ export class EtherscanVerifier extends AbstractVerifier { return url.href } - processReceivedFiles(source: EtherscanSource, contractAddress: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { - const result: SourceFile[] = [] - const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}` - // TODO - const targetFilePath = '' + processReceivedFiles(source: EtherscanSource, contractAddress: string, chainId: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { + const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}` + + // Covers the cases: + // SourceFile: {[FileName]: [content]} + // SourceFile: {{sources: {[FileName]: [content]}}} + let parsedFiles: any + try { + parsedFiles = JSON.parse(source.SourceCode) + } catch (e) { + try { + // Etherscan wraps the Object in one additional bracket + parsedFiles = JSON.parse(source.SourceCode.substring(1, source.SourceCode.length - 1)).sources + } catch (e) {} + } + + if (parsedFiles) { + const result: SourceFile[] = [] + let targetFilePath = '' + for (const [fileName, fileObj] of Object.entries(parsedFiles)) { + const path = `${filePrefix}/${fileName}` + + result.push({ path, content: fileObj.content }) + + if (path.endsWith(`/${source.ContractName}.sol`)) { + targetFilePath = path + } + } + return { sourceFiles: result, targetFilePath } + } - return { sourceFiles: result, targetFilePath } + // Parsing to JSON failed, SourceCode is the code itself + const targetFilePath = `${filePrefix}/${source.ContractName}.sol` + const sourceFiles: SourceFile[] = [{ content: source.SourceCode, path: targetFilePath }] + return { sourceFiles, targetFilePath } } } diff --git a/apps/contract-verification/src/app/Verifiers/SourcifyVerifier.ts b/apps/contract-verification/src/app/Verifiers/SourcifyVerifier.ts index 351cd3e9a2..c9a8f53497 100644 --- a/apps/contract-verification/src/app/Verifiers/SourcifyVerifier.ts +++ b/apps/contract-verification/src/app/Verifiers/SourcifyVerifier.ts @@ -132,7 +132,7 @@ export class SourcifyVerifier extends AbstractVerifier { lookupUrl = this.getContractCodeUrl(contractAddress, chainId, false) } - const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.files, contractAddress) + const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.files, contractAddress, chainId) return { status, lookupUrl, sourceFiles, targetFilePath } } @@ -142,10 +142,10 @@ export class SourcifyVerifier extends AbstractVerifier { return url.href } - processReceivedFiles(files: SourcifyFile[], contractAddress: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { + processReceivedFiles(files: SourcifyFile[], contractAddress: string, chainId: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { const result: SourceFile[] = [] let targetFilePath: string - const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}` + const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}` for (const file of files) { let filePath: string