Get source code with Etherscan lookup

pull/5285/head
Manuel Wedler 4 months ago committed by Aniket
parent 03746cf574
commit f1cb79ea01
  1. 4
      apps/contract-verification/src/app/Verifiers/BlockscoutVerifier.ts
  2. 43
      apps/contract-verification/src/app/Verifiers/EtherscanVerifier.ts
  3. 6
      apps/contract-verification/src/app/Verifiers/SourcifyVerifier.ts

@ -32,11 +32,11 @@ export class BlockscoutVerifier extends EtherscanVerifier {
return url.href 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 blockscoutSource = source as BlockscoutSource
const result: SourceFile[] = [] const result: SourceFile[] = []
const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}` const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}`
const targetFilePath = `${filePrefix}/${blockscoutSource.FileName}` const targetFilePath = `${filePrefix}/${blockscoutSource.FileName}`
result.push({ content: blockscoutSource.SourceCode, path: targetFilePath }) result.push({ content: blockscoutSource.SourceCode, path: targetFilePath })

@ -235,8 +235,7 @@ export class EtherscanVerifier extends AbstractVerifier {
} }
const lookupUrl = this.getContractCodeUrl(contractAddress) const lookupUrl = this.getContractCodeUrl(contractAddress)
console.log(lookupResponse) const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.result[0], contractAddress, chainId)
const { sourceFiles, targetFilePath } = this.processReceivedFiles(lookupResponse.result[0], contractAddress)
return { status: 'verified', lookupUrl, sourceFiles, targetFilePath } return { status: 'verified', lookupUrl, sourceFiles, targetFilePath }
} }
@ -246,12 +245,40 @@ export class EtherscanVerifier extends AbstractVerifier {
return url.href return url.href
} }
processReceivedFiles(source: EtherscanSource, contractAddress: string): { sourceFiles: SourceFile[]; targetFilePath?: string } { processReceivedFiles(source: EtherscanSource, contractAddress: string, chainId: string): { sourceFiles: SourceFile[]; targetFilePath?: string } {
const result: SourceFile[] = [] const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}`
const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}`
// TODO // Covers the cases:
const targetFilePath = '' // 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<any>(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 }
} }
} }

@ -132,7 +132,7 @@ export class SourcifyVerifier extends AbstractVerifier {
lookupUrl = this.getContractCodeUrl(contractAddress, chainId, false) 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 } return { status, lookupUrl, sourceFiles, targetFilePath }
} }
@ -142,10 +142,10 @@ export class SourcifyVerifier extends AbstractVerifier {
return url.href 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[] = [] const result: SourceFile[] = []
let targetFilePath: string let targetFilePath: string
const filePrefix = `/${this.LOOKUP_STORE_DIR}/${contractAddress}` const filePrefix = `/${this.LOOKUP_STORE_DIR}/${chainId}/${contractAddress}`
for (const file of files) { for (const file of files) {
let filePath: string let filePath: string

Loading…
Cancel
Save