/
init.go
888 lines (740 loc) · 26.2 KB
/
init.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/streamingfast/cli"
"github.com/streamingfast/dhttp"
"github.com/streamingfast/eth-go"
"github.com/streamingfast/substreams/codegen"
"github.com/streamingfast/substreams/codegen/templates"
)
// Some developers centric environment override to make it faster to iterate on `substreams init` command
var (
devInitSourceDirectory = os.Getenv("SUBSTREAMS_DEV_INIT_SOURCE_DIRECTORY")
devInitProjectName = os.Getenv("SUBSTREAMS_DEV_INIT_PROJECT_NAME")
devInitProtocol = os.Getenv("SUBSTREAMS_DEV_INIT_PROTOCOL")
devInitEthereumTrackedContract = os.Getenv("SUBSTREAMS_DEV_INIT_ETHEREUM_TRACKED_CONTRACT")
devInitEthereumChain = os.Getenv("SUBSTREAMS_DEV_INIT_ETHEREUM_CHAIN")
)
var errInitUnsupportedChain = errors.New("unsupported chain")
var errInitUnsupportedProtocol = errors.New("unsupported protocol")
var initCmd = &cobra.Command{
Use: "init [<path>]",
Short: "Initialize a new, working Substreams project from scratch",
Long: cli.Dedent(`
Initialize a new, working Substreams project from scratch. The path parameter is optional,
with your current working directory being the default value.
If you have an Etherscan API Key, you can set it to "ETHERSCAN_API_KEY" environment variable, it will be used to
fetch the ABIs and contract information.
`),
RunE: runSubstreamsInitE,
Args: cobra.RangeArgs(0, 1),
SilenceUsage: true,
}
var etherscanAPIKey = "YourApiKeyToken"
func init() {
if x := os.Getenv("ETHERSCAN_API_KEY"); x != "" {
etherscanAPIKey = x
}
rootCmd.AddCommand(initCmd)
}
func runSubstreamsInitE(cmd *cobra.Command, args []string) error {
relativeWorkingDir := "."
if len(args) == 1 {
relativeWorkingDir = args[0]
}
if devInitSourceDirectory != "" {
relativeWorkingDir = devInitSourceDirectory
}
absoluteWorkingDir, err := filepath.Abs(relativeWorkingDir)
if err != nil {
return fmt.Errorf("getting absolute path of %q: %w", relativeWorkingDir, err)
}
projectName, moduleName, err := promptProjectName(absoluteWorkingDir)
if err != nil {
return fmt.Errorf("running project name prompt: %w", err)
}
absoluteProjectDir := filepath.Join(absoluteWorkingDir, projectName)
protocol, err := promptProtocol()
if err != nil {
return fmt.Errorf("running protocol prompt: %w", err)
}
switch protocol {
case codegen.ProtocolEthereum:
chainSelected, err := promptEthereumChain()
if err != nil {
return fmt.Errorf("running chain prompt: %w", err)
}
if chainSelected == codegen.EthereumChainOther {
fmt.Println()
fmt.Println("We haven't added any templates for your selected chain quite yet")
fmt.Println()
fmt.Println("Come join us in discord at https://discord.gg/u8amUbGBgF and suggest templates/chains you want to see!")
fmt.Println()
return errInitUnsupportedChain
}
chain := templates.EthereumChainsByID[chainSelected.String()]
if chain == nil {
return fmt.Errorf("unknown chain: %s", chainSelected.String())
}
ethereumContracts, err := promptEthereumVerifiedContracts(eth.MustNewAddress(chain.DefaultContractAddress), chain.DefaultContractName)
if err != nil {
return fmt.Errorf("running contract prompt: %w", err)
}
fmt.Printf("Tracking %d contract(s), let's define a short name for each contract\n", len(ethereumContracts))
ethereumContracts, err = promptEthereumContractShortNames(ethereumContracts)
if err != nil {
return fmt.Errorf("running short name contract prompt: %w", err)
}
fmt.Printf("Retrieving %s contract information (ABI & creation block)\n", chain.DisplayName)
// Get contract abiContents & parse them
ethereumContracts, err = getAndSetContractABIs(cmd.Context(), ethereumContracts, chain)
if err != nil {
return fmt.Errorf("getting %s contract ABI: %w", chain.DisplayName, err)
}
// Get contract creation block
lowestStartBlock, err := getContractCreationBlock(cmd.Context(), ethereumContracts, chain)
if err != nil {
// FIXME: not sure if we should simplify set the contract block num to zero by default
return fmt.Errorf("getting %s contract creating block: %w", chain.DisplayName, err)
}
for _, contract := range ethereumContracts {
fmt.Printf("Generating ABI Event models for %s\n", contract.GetName())
events, err := templates.BuildEventModels(contract.GetAbi())
if err != nil {
return fmt.Errorf("build ABI event models for contract [%s - %s]: %w", contract.GetAddress(), contract.GetName(), err)
}
contract.SetEvents(events)
if contract.GetWithCalls() {
fmt.Printf("Generating ABI Call models for %s\n", contract.GetName())
calls, err := templates.BuildCallModels(contract.GetAbi())
if err != nil {
return fmt.Errorf("build ABI call models for contract [%s - %s]: %w", contract.GetAddress(), contract.GetName(), err)
}
contract.SetCalls(calls)
}
}
// Ask for any dynamic datasources
ethereumContracts, err = promptEthereumDynamicDataSources(cmd.Context(), ethereumContracts, chain)
if err != nil {
return fmt.Errorf("running dynamic datasources prompt, %s, %w", chain.DisplayName, err)
}
fmt.Println("Writing project files")
project, err := templates.NewEthereumProject(
projectName,
moduleName,
chain,
ethereumContracts,
lowestStartBlock,
)
if err != nil {
return fmt.Errorf("new Ethereum %s project: %w", chain.DisplayName, err)
}
if err := renderProjectFilesIn(project, absoluteProjectDir); err != nil {
return fmt.Errorf("render Ethereum %s project: %w", chain.DisplayName, err)
}
case codegen.ProtocolOther:
fmt.Println()
fmt.Println("We haven't added any templates for your selected protocol quite yet")
fmt.Println()
fmt.Println("Come join us in discord at https://discord.gg/u8amUbGBgF and suggest templates/chains you want to see!")
fmt.Println()
return errInitUnsupportedProtocol
}
fmt.Println("Generating Protobuf Rust code")
if err := protogenSubstreams(absoluteProjectDir); err != nil {
return fmt.Errorf("protobuf generation: %w", err)
}
fmt.Printf("Project %q initialized at %q\n", projectName, absoluteWorkingDir)
fmt.Println()
fmt.Println("Run 'make build' to build the wasm code.")
fmt.Println()
fmt.Println("The following substreams.yaml files have been created with different sink targets:")
fmt.Println(" * substreams.yaml: no sink target")
fmt.Println(" * substreams.sql.yaml: PostgreSQL sink")
fmt.Println(" * substreams.clickhouse.yaml: Clickhouse sink")
fmt.Println(" * substreams.subgraph.yaml: Sink into Substreams-based subgraph")
return nil
}
func protogenSubstreams(absoluteProjectDir string) error {
cmd := exec.Command("substreams", "protogen", `--exclude-paths`, `sf/substreams,google`)
cmd.Dir = absoluteProjectDir
output, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(output))
return fmt.Errorf("running %q failed: %w", cmd, err)
}
return nil
}
func renderProjectFilesIn(project templates.Project, absoluteProjectDir string) error {
files, err := project.Render()
if err != nil {
return fmt.Errorf("render project: %w", err)
}
for relativeFile, content := range files {
file := filepath.Join(absoluteProjectDir, strings.ReplaceAll(relativeFile, "/", string(os.PathSeparator)))
directory := filepath.Dir(file)
if err := os.MkdirAll(directory, os.ModePerm); err != nil {
return fmt.Errorf("create directory %q: %w", directory, err)
}
if err := os.WriteFile(file, content, 0644); err != nil {
// remove directory, we want a complete e2e file generation
e := os.RemoveAll(directory)
if e != nil {
return fmt.Errorf("removing directory %s: %w and write file: %w", directory, e, err)
}
return fmt.Errorf("write file: %w", err)
}
}
return nil
}
// We accept _ here because they are used across developers. we sanitize it later when
// used within Substreams module.
var moduleNameRegexp = regexp.MustCompile(`^([a-z][a-z0-9_]{0,63})$`)
func promptProjectName(absoluteSrcDir string) (string, string, error) {
if name := devInitProjectName; name != "" {
return name, projectNameToModuleName(name), nil
}
projectName, err := prompt("Project name (lowercase, numbers, undescores)", &promptOptions{
Validate: func(input string) error {
ok := moduleNameRegexp.MatchString(input)
if !ok {
return fmt.Errorf("invalid name: must match %s", moduleNameRegexp)
}
if cli.DirectoryExists(input) {
return fmt.Errorf("project %q already exist in %q", input, absoluteSrcDir)
}
return nil
},
})
if err != nil {
return "", "", err
}
return projectName, projectNameToModuleName(projectName), nil
}
func projectNameToModuleName(in string) string {
return strings.ReplaceAll(in, "-", "_")
}
func promptEthereumVerifiedContracts(defaultAddress eth.Address, defaultContractName string) ([]*templates.EthereumContract, error) {
if devInitEthereumTrackedContract != "" {
// It's ok to panic, we expect the dev to put in a valid Ethereum address
return []*templates.EthereumContract{
templates.NewEthereumContract("", eth.MustNewAddress(devInitEthereumTrackedContract), nil, ""),
}, nil
}
var ethContracts []*templates.EthereumContract
inputOrDefaultFunc := func(input string) (eth.Address, error) {
if input == "" {
return defaultAddress, nil
}
return eth.NewAddress(input)
}
inputOrEmptyFunc := func(input string) (eth.Address, error) {
if input == "" {
return nil, nil
}
return eth.NewAddress(input)
}
firstContractAddress, err := promptContractAddress(fmt.Sprintf("Contract address to track (leave empty to use %q)", defaultContractName), inputOrDefaultFunc)
if err != nil {
return nil, err
}
ethContracts = append(ethContracts, templates.NewEthereumContract("", firstContractAddress, nil, ""))
if bytes.Equal(firstContractAddress, defaultAddress) {
return ethContracts, nil
}
for {
contractAddr, err := promptContractAddress("Would you like to track another contract? (Leave empty if not)", inputOrEmptyFunc)
if err != nil {
return nil, err
}
if contractAddr == nil {
return ethContracts, nil
}
ethContracts = append(ethContracts, templates.NewEthereumContract("", contractAddr, nil, ""))
}
}
func promptContractAddress(message string, inputFuncCheck func(input string) (eth.Address, error)) (eth.Address, error) {
return promptT(message, inputFuncCheck, &promptOptions{
Validate: func(input string) error {
if input == "" {
return nil
}
_, err := eth.NewAddress(input)
if err != nil {
return fmt.Errorf("invalid address: %w", err)
}
return nil
},
})
}
var shortNameRegexp = regexp.MustCompile(`^([a-z][a-z0-9]{0,63})$`)
func promptEthereumContractShortNames(ethereumContracts []*templates.EthereumContract) ([]*templates.EthereumContract, error) {
for _, contract := range ethereumContracts {
shortName, err := prompt(fmt.Sprintf("Choose a short name for %s (lowercase and numbers only)", contract.GetAddress()), &promptOptions{
Validate: func(input string) error {
ok := shortNameRegexp.MatchString(input)
if !ok {
return fmt.Errorf("invalid name: must match %s", shortNameRegexp)
}
return nil
},
})
if err != nil {
return nil, err
}
contract.SetName(shortName)
withCalls, err := promptCallOrEvents()
if err != nil {
return nil, err
}
contract.SetWithCalls(withCalls)
}
return ethereumContracts, nil
}
func promptEthereumDynamicDataSources(ctx context.Context, ethereumContracts []*templates.EthereumContract, chain *templates.EthereumChain) ([]*templates.EthereumContract, error) {
dds, err := promptConfirm("Would you like to track a dynamic datasource", &promptOptions{
PromptTemplates: &promptui.PromptTemplates{
Success: `{{ "Track a dynamic datasource:" | faint }} `,
},
})
if err != nil {
return nil, err
}
if !dds {
return ethereumContracts, nil
}
selected := ethereumContracts[0]
if len(ethereumContracts) != 1 {
contractNames := make([]string, len(ethereumContracts))
for i := range ethereumContracts {
contractNames[i] = ethereumContracts[i].GetName()
}
choice := promptui.Select{
Label: "Select the factory contract",
Items: contractNames,
Templates: &promptui.SelectTemplates{
Selected: `{{ "Contract:" | faint }} {{ . }}`,
},
HideHelp: true,
}
idx, _, err := choice.Run()
if err != nil {
return nil, err
}
selected = ethereumContracts[idx]
}
events := selected.GetEvents()
fmt.Println("Select the event on the factory that triggers the creation of a dynamic datasource:")
eventNames := make([]string, len(events))
for i := range events {
eventNames[i] = events[i].Rust.ABIStructName
}
choice := promptui.Select{
Label: "Select the event",
Items: eventNames,
Templates: &promptui.SelectTemplates{
Selected: `{{ "Event:" | faint }} {{ . }}`,
},
HideHelp: true,
}
idx, _, err := choice.Run()
if err != nil {
return nil, err
}
selectedEventName := events[idx].Rust.ABIStructName
fmt.Println("Select the field on the factory event that provides the address of the dynamic datasource:")
eventFields := events[idx].Proto.Fields
eventFieldNames := make([]string, len(eventFields))
for i, eventField := range eventFields {
eventFieldNames[i] = eventField.Name
}
choice = promptui.Select{
Label: "Select the event field",
Items: eventFieldNames,
Templates: &promptui.SelectTemplates{
Selected: `{{ "Field:" | faint }} {{ . }}`,
},
HideHelp: true,
}
_, selectedEventField, err := choice.Run()
if err != nil {
return nil, err
}
shortName, err := prompt("Choose a short name for the created datasource, (lowercase and numbers only)", &promptOptions{
Default: selectedEventField,
Validate: func(input string) error {
ok := shortNameRegexp.MatchString(input)
if !ok {
return fmt.Errorf("invalid name: must match %s", shortNameRegexp)
}
return nil
},
})
if err != nil {
return nil, err
}
withCalls, err := promptCallOrEvents()
if err != nil {
return nil, err
}
referenceContractAddress, err := promptContractAddress(
"Enter a reference contract address to fetch the ABI",
func(input string) (eth.Address, error) {
return eth.NewAddress(input)
})
if err != nil {
return nil, err
}
abi, abiContent, err := getContractABIFollowingProxy(ctx, referenceContractAddress, chain)
if err != nil {
return nil, fmt.Errorf("getting contract ABI for %s: %w", referenceContractAddress, err)
}
fmt.Println("adding dynamic datasource", shortName, selectedEventName, selectedEventField)
selected.AddDynamicDataSource(shortName, abi, abiContent, selectedEventName, selectedEventField, withCalls)
return ethereumContracts, nil
}
func promptCallOrEvents() (calls bool, err error) {
choice := promptui.Select{
Label: "Select the type of data that you want to extract from this contract:",
Items: []string{"Events only", "Events + Calls"},
HideHelp: true,
}
resp, _, err := choice.Run()
if err != nil {
return false, err
}
switch resp {
case 0:
return false, nil
case 1:
return true, nil
}
panic("impossible choice")
}
func promptProtocol() (codegen.Protocol, error) {
if devInitProtocol != "" {
// It's ok to panic, we expect the dev to put in a valid Ethereum address
protocol, err := codegen.ParseProtocol(devInitProtocol)
if err != nil {
panic(fmt.Errorf("invalid protocol: %w", err))
}
return protocol, nil
}
choice := promptui.Select{
Label: "Select protocol",
Items: codegen.ProtocolNames(),
Templates: &promptui.SelectTemplates{
Selected: `{{ "Protocol:" | faint }} {{ . }}`,
},
HideHelp: true,
}
_, selection, err := choice.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
// We received Ctrl-C, users wants to abort, nothing else to do, quit immediately
os.Exit(1)
}
return codegen.ProtocolOther, fmt.Errorf("running protocol prompt: %w", err)
}
var protocol codegen.Protocol
if err := protocol.UnmarshalText([]byte(selection)); err != nil {
panic(fmt.Errorf("impossible, selecting hard-coded value from enum itself, something is really wrong here"))
}
return protocol, nil
}
func promptEthereumChain() (codegen.EthereumChain, error) {
if devInitEthereumChain != "" {
// It's ok to panic, we expect the dev to put in a valid Ethereum chain
chain, err := codegen.ParseEthereumChain(devInitEthereumChain)
if err != nil {
panic(fmt.Errorf("invalid chain: %w", err))
}
return chain, nil
}
choice := promptui.Select{
Label: "Select Ethereum chain",
Items: codegen.EthereumChainNames(),
Templates: &promptui.SelectTemplates{
Selected: `{{ "Ethereum chain:" | faint }} {{ . }}`,
},
HideHelp: true,
}
_, selection, err := choice.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
// We received Ctrl-C, users wants to abort, nothing else to do, quit immediately
os.Exit(1)
}
return codegen.EthereumChainOther, fmt.Errorf("running chain prompt: %w", err)
}
var chain codegen.EthereumChain
if err := chain.UnmarshalText([]byte(selection)); err != nil {
panic(fmt.Errorf("impossible, selecting hard-coded value from enum itself, something is really wrong here"))
}
return chain, nil
}
type promptOptions struct {
Validate promptui.ValidateFunc
IsConfirm bool
PromptTemplates *promptui.PromptTemplates
Default string
}
var confirmPromptRegex = regexp.MustCompile("(y|Y|n|N|No|Yes|YES|NO)")
func prompt(label string, opts *promptOptions) (string, error) {
var templates *promptui.PromptTemplates
if opts != nil {
templates = opts.PromptTemplates
}
if templates == nil {
templates = &promptui.PromptTemplates{
Success: `{{ . | faint }}{{ ":" | faint}} `,
}
}
if opts != nil && opts.IsConfirm {
// We have no differences
templates.Valid = `{{ "?" | blue}} {{ . | bold }} {{ "[y/N]" | faint}} `
templates.Invalid = templates.Valid
}
def := ""
if opts != nil {
def = opts.Default
}
prompt := promptui.Prompt{
Label: label,
Templates: templates,
Default: def,
}
if opts != nil && opts.Validate != nil {
prompt.Validate = opts.Validate
}
if opts != nil && opts.IsConfirm {
prompt.Validate = func(in string) error {
if !confirmPromptRegex.MatchString(in) {
return errors.New("answer with y/yes/Yes or n/no/No")
}
return nil
}
}
choice, err := prompt.Run()
if err != nil {
if errors.Is(err, promptui.ErrInterrupt) {
// We received Ctrl-C, users wants to abort, nothing else to do, quit immediately
os.Exit(1)
}
if prompt.IsConfirm && errors.Is(err, promptui.ErrAbort) {
return "false", nil
}
return "", fmt.Errorf("running prompt: %w", err)
}
return choice, nil
}
// promptT is just like [prompt] but accepts a transformer that transform the `string` into the generic type T.
func promptT[T any](label string, transformer func(string) (T, error), opts *promptOptions) (T, error) {
choice, err := prompt(label, opts)
if err == nil {
return transformer(choice)
}
var empty T
return empty, err
}
// promptConfirm is just like [prompt] but enforce `IsConfirm` and returns a boolean which is either
// `true` for yes answer or `false` for a no answer.
func promptConfirm(label string, opts *promptOptions) (bool, error) {
if opts == nil {
opts = &promptOptions{}
}
opts.IsConfirm = true
transform := func(in string) (bool, error) {
in = strings.ToLower(in)
return in == "y" || in == "yes", nil
}
return promptT(label, transform, opts)
}
var httpClient = http.Client{
Transport: dhttp.NewLoggingRoundTripper(zlog, tracer, http.DefaultTransport),
Timeout: 30 * time.Second,
}
// getProxyContractImplementation returns the implementation address and a timer to wait before next call
func getProxyContractImplementation(ctx context.Context, address eth.Address, endpoint string) (*eth.Address, *time.Timer, error) {
// check for proxy contract's implementation
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=contract&action=getsourcecode&address=%s&apiKey=%s", endpoint, address.Pretty(), etherscanAPIKey), nil)
if err != nil {
return nil, nil, fmt.Errorf("new request: %w", err)
}
res, err := httpClient.Do(req)
if err != nil {
return nil, nil, fmt.Errorf("getting contract abi from etherscan: %w", err)
}
defer res.Body.Close()
type Response struct {
Message string `json:"message"` // ex: `OK-Missing/Invalid API Key, rate limit of 1/5sec applied`
Result []struct {
Implementation string `json:"Implementation"`
// ContractName string `json:"ContractName"`
} `json:"result"`
}
var response Response
bod, err := io.ReadAll(res.Body)
if err != nil {
return nil, nil, err
}
if err := json.NewDecoder(bytes.NewReader(bod)).Decode(&response); err != nil {
return nil, nil, fmt.Errorf("unmarshaling %s: %w", string(bod), err)
}
timer := timerUntilNextCall(response.Message)
if len(response.Result) == 0 {
return nil, timer, nil
}
if len(response.Result[0].Implementation) != 42 {
return nil, timer, nil
}
addr, err := eth.NewAddress(response.Result[0].Implementation)
if err != nil {
return nil, timer, err
}
return &addr, timer, nil
}
func timerUntilNextCall(msg string) *time.Timer {
// etherscan-specific
if strings.HasPrefix(msg, "OK-Missing/Invalid API Key") {
return time.NewTimer(time.Second * 5)
}
return time.NewTimer(time.Millisecond * 400)
}
func getContractABI(ctx context.Context, address eth.Address, endpoint string) (*eth.ABI, string, *time.Timer, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=contract&action=getabi&address=%s&apiKey=%s", endpoint, address.Pretty(), etherscanAPIKey), nil)
if err != nil {
return nil, "", nil, fmt.Errorf("new request: %w", err)
}
res, err := httpClient.Do(req)
if err != nil {
return nil, "", nil, fmt.Errorf("getting contract abi from etherscan: %w", err)
}
defer res.Body.Close()
type Response struct {
Message string `json:"message"` // ex: `OK-Missing/Invalid API Key, rate limit of 1/5sec applied`
Result interface{} `json:"result"`
}
var response Response
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return nil, "", nil, fmt.Errorf("unmarshaling: %w", err)
}
timer := timerUntilNextCall(response.Message)
abiContent, ok := response.Result.(string)
if !ok {
return nil, "", timer, fmt.Errorf(`invalid response "Result" field type, expected "string" got "%T"`, response.Result)
}
ethABI, err := eth.ParseABIFromBytes([]byte(abiContent))
if err != nil {
return nil, "", timer, fmt.Errorf("parsing abi %q: %w", abiContent, err)
}
return ethABI, abiContent, timer, err
}
func getContractABIFollowingProxy(ctx context.Context, contractAddress eth.Address, chain *templates.EthereumChain) (*eth.ABI, string, error) {
abi, abiContent, wait, err := getContractABI(ctx, contractAddress, chain.ApiEndpoint)
if err != nil {
return nil, "", err
}
<-wait.C
implementationAddress, wait, err := getProxyContractImplementation(ctx, contractAddress, chain.ApiEndpoint)
if err != nil {
return nil, "", err
}
<-wait.C
if implementationAddress != nil {
implementationABI, implementationABIContent, wait, err := getContractABI(ctx, *implementationAddress, chain.ApiEndpoint)
if err != nil {
return nil, "", err
}
for k, v := range implementationABI.LogEventsMap {
abi.LogEventsMap[k] = append(abi.LogEventsMap[k], v...)
}
for k, v := range implementationABI.LogEventsByNameMap {
abi.LogEventsByNameMap[k] = append(abi.LogEventsByNameMap[k], v...)
}
abiAsArray := []map[string]interface{}{}
if err := json.Unmarshal([]byte(abiContent), &abiAsArray); err != nil {
return nil, "", fmt.Errorf("unmarshalling abiContent as array: %w", err)
}
implementationABIAsArray := []map[string]interface{}{}
if err := json.Unmarshal([]byte(implementationABIContent), &implementationABIAsArray); err != nil {
return nil, "", fmt.Errorf("unmarshalling implementationABIContent as array: %w", err)
}
abiAsArray = append(abiAsArray, implementationABIAsArray...)
content, err := json.Marshal(abiAsArray)
if err != nil {
return nil, "", fmt.Errorf("re-marshalling ABI")
}
abiContent = string(content)
fmt.Printf("Fetched contract ABI for Implementation %s of Proxy %s\n", *implementationAddress, contractAddress)
<-wait.C
}
return abi, abiContent, nil
}
func getAndSetContractABIs(ctx context.Context, contracts []*templates.EthereumContract, chain *templates.EthereumChain) ([]*templates.EthereumContract, error) {
for _, contract := range contracts {
abi, abiContent, err := getContractABIFollowingProxy(ctx, contract.GetAddress(), chain)
if err != nil {
return nil, fmt.Errorf("getting contract ABI for %s: %w", contract.GetAddress(), err)
}
//fmt.Println("this is the complete abiContent after merge", abiContent)
contract.SetAbiContent(abiContent)
contract.SetAbi(abi)
fmt.Printf("Fetched contract ABI for %s\n", contract.GetAddress())
}
return contracts, nil
}
func getContractCreationBlock(ctx context.Context, contracts []*templates.EthereumContract, chain *templates.EthereumChain) (uint64, error) {
var lowestStartBlock uint64 = math.MaxUint64
for _, contract := range contracts {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=account&action=txlist&address=%s&page=1&offset=1&sort=asc&apikey=%s", chain.ApiEndpoint, contract.GetAddress().Pretty(), etherscanAPIKey), nil)
if err != nil {
return 0, fmt.Errorf("new request: %w", err)
}
res, err := httpClient.Do(req)
if err != nil {
return 0, fmt.Errorf("failed request to etherscan: %w", err)
}
defer res.Body.Close()
type Response struct {
Status string `json:"status"`
Message string `json:"message"`
Result []struct {
BlockNumber string `json:"blockNumber"`
} `json:"result"`
}
var response Response
if err := json.NewDecoder(res.Body).Decode(&response); err != nil {
return 0, fmt.Errorf("unmarshaling: %w", err)
}
if len(response.Result) == 0 {
return 0, fmt.Errorf("empty result from response %v", response)
}
<-timerUntilNextCall(response.Message).C
blockNum, err := strconv.ParseUint(response.Result[0].BlockNumber, 10, 64)
if err != nil {
return 0, fmt.Errorf("parsing block number: %w", err)
}
if blockNum < lowestStartBlock {
lowestStartBlock = blockNum
}
fmt.Printf("Fetched initial block %d for %s (lowest %d)\n", blockNum, contract.GetAddress(), lowestStartBlock)
}
return lowestStartBlock, nil
}