forked from dethcrypto/dethcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchFiles.ts
More file actions
174 lines (145 loc) · 4.28 KB
/
fetchFiles.ts
File metadata and controls
174 lines (145 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { join } from "path";
import { assert, StrictOmit } from "ts-essentials";
import { fetch as _fetch } from "../util/fetch";
import { makeSolidFetch } from "../util/solidFetch";
import { prettyStringify } from "../util/stringify";
import * as types from "./api-types";
import { apiUrlToWebsite } from "./apiUrlToWebsite";
import { fileExtension } from "./fileExtension";
import { ApiName, explorerApiKeys, explorerApiUrls } from "./networks";
const fetchEtherscanResponse = makeSolidFetch({
async verifyResponse(response: unknown): Promise<boolean> {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return (response as any)?.message === "OK" || false;
},
});
interface FetchFilesOptions {
/**
* For unit testing.
* @internal
*/
fetch?: typeof _fetch;
/**
* If more than 0, we fetch implementation contract and merge its files.
*/
proxyDepth?: number;
/**
* If true multiple files are not prefixed.
*/
skipPrefix?: boolean;
}
export async function fetchFiles(
apiName: ApiName,
contractAddress: string,
{
fetch = fetchEtherscanResponse,
proxyDepth = 3,
skipPrefix = false,
}: FetchFilesOptions = {}
): Promise<FetchFilesResult> {
const apiUrl = explorerApiUrls[apiName];
let url;
if (apiUrl.includes("chainid=")) {
url =
apiUrl +
"&module=contract" +
"&action=getsourcecode" +
`&address=${contractAddress}` +
`&apikey=${explorerApiKeys[apiName]}`;
} else {
url =
apiUrl +
"?module=contract" +
"&action=getsourcecode" +
`&address=${contractAddress}` +
`&apikey=${explorerApiKeys[apiName]}`;
}
const response = (await fetch(url)) as types.ContractSourceResponse;
assert(
response.message === "OK",
"Failed to fetch contract source\n" + prettyStringify(response)
);
const {
SourceCode: sourceCode,
ABI: abi,
Implementation: implementationAddr,
..._info
} = response.result[0];
const info: FetchFilesResult["info"] = _info;
let files: FileContents = {};
if (
!sourceCode ||
(!info.ContractName && abi === "Contract source code not verified")
) {
return {
files: {
"error.md": contractNotVerifiedErrorMsg(apiName, contractAddress),
},
info,
};
}
if (types.sourceHasSettings(sourceCode)) {
let parsed = types.parseSourceCode(sourceCode);
files["settings.json"] = prettyStringify(parsed.settings);
for (const [path, { content }] of Object.entries(parsed.sources)) {
files[path] = content;
}
if (!skipPrefix) files = prefixFiles(files, info.ContractName);
} else if (types.sourceHasMultipleFiles(sourceCode)) {
const parsed = types.parseSourceCode(sourceCode);
for (const [path, { content }] of Object.entries(parsed)) {
files[path] = content;
}
if (!skipPrefix) files = prefixFiles(files, info.ContractName);
} else {
files[info.ContractName + fileExtension(info)] = sourceCode;
}
if (
implementationAddr &&
proxyDepth > 0 &&
implementationAddr !== contractAddress
) {
const implementation = await fetchFiles(apiName, implementationAddr, {
fetch,
proxyDepth: proxyDepth - 1,
});
Object.assign(
files,
prefixFiles(implementation.files, implementation.info.ContractName)
);
info.implementation = implementation.info;
}
return { files, info };
}
function prefixFiles(files: FileContents, prefix: string): FileContents {
const res: any = {};
const keys = Object.keys(files);
for (const k of keys) {
res[join(prefix, k)] = files[k];
}
return res;
}
export interface FetchFilesResult {
files: FileContents;
info: ContractInfoWithImplementation;
}
export interface ContractInfoWithImplementation extends ContractInfo {
implementation?: ContractInfo;
}
export interface ContractInfo
extends StrictOmit<
types.ContractInfo,
"SourceCode" | "ABI" | "Implementation"
> {}
export interface FileContents
extends Record<types.FilePath, types.FileContent> {}
function contractNotVerifiedErrorMsg(
apiName: ApiName,
contractAddress: string
) {
const websiteUrl = apiUrlToWebsite(explorerApiUrls[apiName]);
return `\
Oops! It seems this contract source code is not verified on ${websiteUrl}.
Take a look at ${websiteUrl}/address/${contractAddress}.
`;
}