MCPcopy Create free account
hub / github.com/dethcrypto/dethcode / fetchFiles

Function fetchFiles

packages/ethereum-viewer/src/explorer/fetchFiles.ts:35–132  ·  view source on GitHub ↗
(
  apiName: ApiName,
  contractAddress: string,
  {
    fetch = fetchEtherscanResponse,
    proxyDepth = 3,
    skipPrefix = false,
  }: FetchFilesOptions = {}
)

Source from the content-addressed store, hash-verified

33}
34
35export async function fetchFiles(
36 apiName: ApiName,
37 contractAddress: string,
38 {
39 fetch = fetchEtherscanResponse,
40 proxyDepth = 3,
41 skipPrefix = false,
42 }: FetchFilesOptions = {}
43): Promise<FetchFilesResult> {
44 const apiUrl = explorerApiUrls[apiName];
45 let url;
46 if (apiUrl.includes("chainid=")) {
47 url =
48 apiUrl +
49 "&module=contract" +
50 "&action=getsourcecode" +
51 `&address=${contractAddress}` +
52 `&apikey=${explorerApiKeys[apiName]}`;
53 } else {
54 url =
55 apiUrl +
56 "?module=contract" +
57 "&action=getsourcecode" +
58 `&address=${contractAddress}` +
59 `&apikey=${explorerApiKeys[apiName]}`;
60 }
61
62 const response = (await fetch(url)) as types.ContractSourceResponse;
63
64 assert(
65 response.message === "OK",
66 "Failed to fetch contract source\n" + prettyStringify(response)
67 );
68
69 const {
70 SourceCode: sourceCode,
71 ABI: abi,
72 Implementation: implementationAddr,
73 ..._info
74 } = response.result[0];
75
76 const info: FetchFilesResult["info"] = _info;
77
78 let files: FileContents = {};
79
80 if (
81 !sourceCode ||
82 (!info.ContractName && abi === "Contract source code not verified")
83 ) {
84 return {
85 files: {
86 "error.md": contractNotVerifiedErrorMsg(apiName, contractAddress),
87 },
88 info,
89 };
90 }
91
92 if (types.sourceHasSettings(sourceCode)) {

Callers 1

fetchFiles.test.tsFile · 0.90

Calls 4

prettyStringifyFunction · 0.90
fileExtensionFunction · 0.90
prefixFilesFunction · 0.85

Tested by

no test coverage detected