Skip to content

Commit 7988dcf

Browse files
committed
Get ContractFileName from Etherscan API directly
1 parent 4d342c2 commit 7988dcf

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

app/utils/etherscanApi.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface EtherscanResult {
5252
Proxy: string;
5353
Implementation: string;
5454
SwarmSource: string;
55+
ContractFileName?: string;
5556
}
5657

5758
export interface ProcessedEtherscanResult {
@@ -89,7 +90,7 @@ export const isVyperResult = (etherscanResult: EtherscanResult): boolean => {
8990
return etherscanResult.CompilerVersion.startsWith("vyper");
9091
};
9192

92-
export const getContractPathFromSources = (contractName: string, sources: any): string | undefined => {
93+
export const getContractPathFromSoliditySources = (contractName: string, sources: any): string | undefined => {
9394
// Look for a file that contains the contract definition
9495
for (const [filePath, source] of Object.entries(sources)) {
9596
const content = typeof source === "string" ? source : (source as any).content;
@@ -180,12 +181,16 @@ export const processEtherscanResult = async (
180181
verificationMethod = "std-json";
181182
const jsonInput = parseEtherscanJsonInput(sourceCodeObject);
182183

183-
// Find contract path from sources
184-
const foundPath = getContractPathFromSources(contractName, jsonInput.sources);
185-
if (!foundPath) {
186-
throw new Error("Could not find contract path in sources");
184+
// Use ContractFileName if available, otherwise search in sources
185+
if (etherscanResult.ContractFileName) {
186+
contractPath = etherscanResult.ContractFileName;
187+
} else {
188+
const foundPath = getContractPathFromSoliditySources(contractName, jsonInput.sources);
189+
if (!foundPath) {
190+
throw new Error("Could not find contract path in sources");
191+
}
192+
contractPath = foundPath;
187193
}
188-
contractPath = foundPath;
189194

190195
// Create a single JSON file
191196
const jsonContent = JSON.stringify(jsonInput, null, 2);
@@ -199,12 +204,16 @@ export const processEtherscanResult = async (
199204
verificationMethod = "multiple-files";
200205
const sourcesObject = JSON.parse(sourceCodeObject) as { [key: string]: { content: string } };
201206

202-
// Find contract path from sources
203-
const foundPath = getContractPathFromSources(contractName, sourcesObject);
204-
if (!foundPath) {
205-
throw new Error("Could not find contract path in sources");
207+
// Use ContractFileName if available, otherwise search in sources
208+
if (etherscanResult.ContractFileName) {
209+
contractPath = etherscanResult.ContractFileName;
210+
} else {
211+
const foundPath = getContractPathFromSoliditySources(contractName, sourcesObject);
212+
if (!foundPath) {
213+
throw new Error("Could not find contract path in sources");
214+
}
215+
contractPath = foundPath;
206216
}
207-
contractPath = foundPath;
208217

209218
// Create files from sources object
210219
files = Object.entries(sourcesObject).map(([filename, object]) => {
@@ -217,10 +226,15 @@ export const processEtherscanResult = async (
217226
// single-file method
218227
verificationMethod = "single-file";
219228
const extension = language === "vyper" ? "vy" : "sol";
220-
const filename = `${contractName}.${extension}`;
221-
contractPath = filename;
229+
230+
// Use ContractFileName if available, otherwise construct filename
231+
if (etherscanResult.ContractFileName) {
232+
contractPath = etherscanResult.ContractFileName;
233+
} else {
234+
contractPath = `${contractName}.${extension}`;
235+
}
222236

223-
const sourceFile = new File([sourceCodeObject], filename, { type: "text/plain" });
237+
const sourceFile = new File([sourceCodeObject], contractPath, { type: "text/plain" });
224238
files = [sourceFile];
225239

226240
// Generate compiler settings for single-file method

0 commit comments

Comments
 (0)