-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
469 lines (405 loc) · 13.4 KB
/
main.go
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/textproto"
"os"
"regexp"
"strconv"
"strings"
"time"
)
type AutoGeneratedV2 struct {
IsVerified bool `json:"is_verified"`
}
type VerifySmartContract struct {
Items []string `json:"items"`
NextPagePath string `json:"next_page_path"`
}
type BeforeV2 struct {
Message string `json:"message"`
Result []struct {
ABI string `json:"ABI"`
AdditionalSources []struct {
Filename string `json:"Filename"`
SourceCode string `json:"SourceCode"`
} `json:"AdditionalSources"`
CompilerSettings struct {
EvmVersion string `json:"evmVersion"`
Libraries Libraries `json:"libraries"`
Metadata struct {
UseLiteralContent bool `json:"useLiteralContent"`
} `json:"metadata"`
Optimizer struct {
Details struct {
Yul bool `json:"yul"`
YulDetails struct {
OptimizerSteps string `json:"optimizerSteps"`
} `json:"yulDetails"`
} `json:"details"`
Enabled bool `json:"enabled"`
Runs int `json:"runs"`
} `json:"optimizer"`
OutputSelection struct {
Star struct {
Empty []string `json:""`
Star []string `json:"*"`
} `json:"*"`
} `json:"outputSelection"`
ViaIR bool `json:"viaIR"`
} `json:"CompilerSettings"`
CompilerVersion string `json:"CompilerVersion"`
ContractName string `json:"ContractName"`
EVMVersion string `json:"EVMVersion"`
ExternalLibraries []struct {
AddressHash string `json:"address_hash"`
Name string `json:"name"`
} `json:"ExternalLibraries"`
FileName string `json:"FileName"`
IsProxy string `json:"IsProxy"`
OptimizationRuns int `json:"OptimizationRuns"`
OptimizationUsed string `json:"OptimizationUsed"`
SourceCode string `json:"SourceCode"`
Address string `json:"Address"`
} `json:"result"`
Status string `json:"status"`
}
type ContractUltimate struct {
Language string `json:"language"`
Sources Sources `json:"sources"`
Settings UltimateSettings `json:"settings"`
}
type Sources map[string]Source
type Source struct {
Content string `json:"content"`
}
type UltimateSettings struct {
EvmVersion string `json:"evmVersion"`
Metadata struct {
UseLiteralContent bool `json:"useLiteralContent"`
BytecodeHash string `json:"bytecodeHash"`
} `json:"metadata"`
Optimizer struct {
Enabled bool `json:"enabled"`
Runs int `json:"runs"`
Details Details `json:"details"`
} `json:"optimizer"`
OutputSelection OutputSel `json:"outputSelection`
Libraries Libraries `json:"libraries"`
ViaIR bool `json:"viaIR"`
}
type OutputSel map[string]map[string][]string
type Libraries struct {
ContractsLibsIexecLibOrdersV5Sol struct {
IexecLibOrdersV5 string `json:"IexecLibOrders_v5"`
} `json:"contracts/libs/IexecLibOrders_v5.sol"`
}
type Library struct {
Content string `json:"content"`
}
type Details struct {
ConstantOptimizer bool `json:"constantOptimizer"`
Cse bool `json:"cse"`
Deduplicate bool `json:"deduplicate"`
Inliner bool `json:"inliner"`
JumpdestRemover bool `json:"jumpdestRemover"`
OrderLiterals bool `json:"orderLiterals"`
Peephole bool `json:"peephole"`
Yul bool `json:"yul"`
YulDetails YulDetails `json:"yulDetails"`
}
type YulDetails struct {
OptimizerSteps string `json:"optimizerSteps"`
StackAllocation bool `json:"stackAllocation"`
}
// ///////////////////////QUERRY VERIFIED SMART CONTRACT ON BLOKSCOUT V5 AND VERIFY ON V6 ///////////////////////////////////////
func QuerySmartContractVerify(smartContractAddress string) {
client := &http.Client{}
url := "https://blockscout-v5.bellecour.iex.ec/api?module=contract&action=getsourcecode&address="
fmt.Println(url + smartContractAddress)
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, smartContractAddress), nil)
if err != nil {
panic(err)
}
// fmt.Fprintf(req.URL)
// Execute the request and get the response.
resp, err := client.Do(req)
if err != nil {
// Handle the error.
panic(err)
}
defer resp.Body.Close()
// var resultPage AutoGenerated
var resultPage BeforeV2
if err := json.NewDecoder(resp.Body).Decode(&resultPage); err != nil {
// Handle the error.
panic(err)
}
filename := "before.json"
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // Set indentation for pretty-printing
err = encoder.Encode(resultPage)
if err != nil {
panic(err)
}
fmt.Println("resultPage saved as", filename)
verifiedSmartContractNewBlockscout(smartContractAddress, resultPage)
}
func verifiedSmartContractNewBlockscout(smartContractAddress string, nonStandardJSON BeforeV2) {
url := "https://blockscout.bellecour.iex.ec/api/v2/smart-contracts/"
verification := "/verification/via/standard-input"
// Create the multipart/form-data request
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
fmt.Println("address_hash", nonStandardJSON.Result[0].Address)
err := writer.WriteField("address_hash", nonStandardJSON.Result[0].Address)
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
err = writer.WriteField("compiler_version", nonStandardJSON.Result[0].CompilerVersion)
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
fmt.Println("compiler_version", nonStandardJSON.Result[0].CompilerVersion)
err = writer.WriteField("constructor_args", "")
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
err = writer.WriteField("autodetect_constructor_args", "false")
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
err = writer.WriteField("contract_name", nonStandardJSON.Result[0].ContractName)
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
fmt.Println("contract_name", nonStandardJSON.Result[0].ContractName)
// transformIntoStandardJSON(nonStandardJSON)
var jsonValue []byte
jsonValue, err = json.Marshal(transformIntoStandardJSON(nonStandardJSON))
if err != nil {
fmt.Println(err)
return
}
part, err := createFormFileWithContentType(writer, "files[0]", "name.json", "application/json")
// // part, err := writer.CreateFormFile("files[0]", "name.json")
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
_, err = part.Write(jsonValue)
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
err = writer.Close()
if err != nil {
log.Fatalf("Failed to close writer: %v", err)
}
// Create the HTTP request
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s%s", url, smartContractAddress, verification), body)
if err != nil {
panic(err)
}
// Set the appropriate headers
req.Header.Set("Content-Type", writer.FormDataContentType())
// Execute the HTTP request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to execute HTTP request: %v", err)
}
defer resp.Body.Close()
// Read the response
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
}
// Print the response
if resp.StatusCode == 200 {
fmt.Printf("Check : https://blockscout.bellecour.iex.ec/api/v2/smart-contracts/%s\n", smartContractAddress)
fmt.Println("Response Status:", resp.Status)
fmt.Println("Response Body:", string(respBody))
} else {
fmt.Println("========================================================================", smartContractAddress)
fmt.Println("Response Status:", resp.Status)
fmt.Println("Response Body:", string(respBody))
}
}
func transformIntoStandardJSON(nonStandardJSON BeforeV2) ContractUltimate {
var contract ContractUltimate
contract.Language = "Solidity"
contract.Settings.EvmVersion = nonStandardJSON.Result[0].EVMVersion
contract.Settings.Metadata.UseLiteralContent = nonStandardJSON.Result[0].CompilerSettings.Metadata.UseLiteralContent
contract.Settings.Metadata.BytecodeHash = "ipfs"
boolVal, _ := strconv.ParseBool(nonStandardJSON.Result[0].OptimizationUsed)
contract.Settings.Optimizer.Enabled = boolVal
contract.Settings.Optimizer.Runs = nonStandardJSON.Result[0].OptimizationRuns
contract.Settings.ViaIR = nonStandardJSON.Result[0].CompilerSettings.ViaIR
var yulDetails = YulDetails{
OptimizerSteps: "u:fDnTOc",
StackAllocation: true,
}
var optimizerDetails Details
optimizerDetails = Details{
Peephole: true,
JumpdestRemover: true,
OrderLiterals: true,
Deduplicate: true,
Cse: true,
ConstantOptimizer: true,
Inliner: true,
}
optimizerDetails.Yul = true
optimizerDetails.YulDetails = yulDetails
contract.Settings.Optimizer.Details = optimizerDetails
// contract.Settings.OutputSelection = nonStandardJSON.Result[0].CompilerSettings.OutputSelection
// contract.Settings.OutputSelection.Star.Star = []string{"*"}
// contract.Settings.OutputSelection.Star.Empty = []string{"*"}
outputSelection := OutputSel{
"*": {
"": []string{"*"},
"*": []string{"*"},
},
}
contract.Settings.OutputSelection = outputSelection
contract.Settings.Libraries = nonStandardJSON.Result[0].CompilerSettings.Libraries
contract.Sources = make(map[string]Source)
contract.Sources[nonStandardJSON.Result[0].FileName] = Source{Content: nonStandardJSON.Result[0].SourceCode}
for _, source := range nonStandardJSON.Result[0].AdditionalSources {
contract.Sources[source.Filename] = Source{Content: source.SourceCode}
}
filename := "contract.json"
file, err := os.Create(filename)
if err != nil {
return contract
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ") // Set indentation for pretty-printing
err = encoder.Encode(contract)
if err != nil {
return contract
}
// fmt.Println("Contract saved as", filename)
return contract
}
// ///////////////////////QUERRY VERIFIED SMART CONTRACT ADDRESSES ON BLOKSCOUT V5 ///////////////////////////////////////
func GetAllSmartContractsVerifedAddress() []string {
var smartContractAddressResponse []string
// Start with the initial URL
first := "https://blockscout-bellecour.iex.ec"
verified := "/verified-contracts?"
end := "type=JSON"
url := first + verified + end
for url != "" {
client := &http.Client{}
// fmt.Println(url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
panic(err)
}
// Execute the request and get the response.
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var resultPage VerifySmartContract
if err := json.NewDecoder(resp.Body).Decode(&resultPage); err != nil {
panic(err)
}
// fmt.Println(resultPage.NextPagePath)
// Extract contract addresses from the current page
for _, v := range resultPage.Items {
smartContractAddressResponse = append(smartContractAddressResponse, extractContractAddress(v))
}
if resultPage.NextPagePath == "" {
url = ""
} else {
// Update the URL with the next page path
url = first + resultPage.NextPagePath + "&" + end
}
}
return smartContractAddressResponse
}
func extractContractAddress(itemsStringValue string) string {
re := regexp.MustCompile(`data-address-hash="(0x[0-9a-fA-F]{40})"`)
match := re.FindStringSubmatch(itemsStringValue)
if len(match) > 1 {
contractAddress := match[1]
return contractAddress
} else {
return ""
}
}
func verifyIfSmartContractIsVerifed(smartContractAddress string) bool {
client := &http.Client{}
url := "https://blockscout.bellecour.iex.ec/api/v2/smart-contracts/"
// fmt.Println(url + smartContractAddress)
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", url, smartContractAddress), nil)
if err != nil {
panic(err)
}
// fmt.Fprintf(req.URL)
// Execute the request and get the response.
resp, err := client.Do(req)
if err != nil {
// Handle the error.
panic(err)
}
defer resp.Body.Close()
var resultPage AutoGeneratedV2
if err := json.NewDecoder(resp.Body).Decode(&resultPage); err != nil {
// Handle the error.
panic(err)
}
if resultPage.IsVerified == true {
fmt.Println(resultPage.IsVerified)
return true
} else {
fmt.Println(resultPage.IsVerified)
return false
}
}
// /////////////////////// HELPER FUNCTIONS ///////////////////////////////////////
func createFormFileWithContentType(w *multipart.Writer, fieldname, filename, contentType string) (io.Writer, error) {
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition",
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
escapeQuotes(fieldname), escapeQuotes(filename)))
h.Set("Content-Type", contentType)
return w.CreatePart(h)
}
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
func escapeQuotes(s string) string {
return quoteEscaper.Replace(s)
}
func main() {
// listOfContractAddresses := GetAllSmartContractsVerifedAddress()
// listOfContractAddresses := []string{
// "0xe39E2aDd731b449bAB187f34f0dd39c4AfCd4f97",
// "0xf2F8eBD6DF066f93dc012CC94FBfA4592727a910",
// }
// fmt.Printf("%v\n", len(listOfContractAddresses))
// for _, smartContractAddress := range listOfContractAddresses {
// if !verifyIfSmartContractIsVerifed(smartContractAddress) {
// fmt.Printf("smart contract not verified %v\n", smartContractAddress)
// // QuerySmartContractVerify(smartContractAddress)
// // time.Sleep(10 * time.Second)
// } else {
// }
// }
// 0x7eCf076343FBe296Da2D39f20B2a01AaBB68CC27
// 0x1eE1cceF893DF6c4D3FC4eCaF315F09183f3048c
QuerySmartContractVerify("0x1eE1cceF893DF6c4D3FC4eCaF315F09183f3048c")
}