Skip to content

Commit

Permalink
rework api keys to fetch ABIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Aug 1, 2024
1 parent 18bef12 commit 14df7fb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
28 changes: 18 additions & 10 deletions ethfull/apicalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/tidwall/gjson"
)

var etherscanAPIKey = os.Getenv("SUBDEV_ETHERSCAN_API_KEY")

var httpClient = http.Client{
Transport: dhttp.NewLoggingRoundTripper(zlog, tracer, http.DefaultTransport),
Timeout: 30 * time.Second,
Expand All @@ -37,20 +35,20 @@ func getContractABIFollowingProxy(ctx context.Context, contractAddress string, c
}
return &ABI{abi, abiContent}, nil
}
abi, abiContent, wait, err := getContractABI(ctx, contractAddress, chain.ApiEndpoint)
abi, abiContent, wait, err := getContractABI(ctx, contractAddress, chain.ApiEndpoint, os.Getenv(chain.APIKeyEnvVar))
if err != nil {
return nil, err
}

<-wait.C
implementationAddress, wait, err := getProxyContractImplementation(ctx, contractAddress, chain.ApiEndpoint)
implementationAddress, wait, err := getProxyContractImplementation(ctx, contractAddress, chain.ApiEndpoint, os.Getenv(chain.APIKeyEnvVar))
if err != nil {
return nil, err
}
<-wait.C

if implementationAddress != "" {
implementationABI, implementationABIContent, wait, err := getContractABI(ctx, implementationAddress, chain.ApiEndpoint)
implementationABI, implementationABIContent, wait, err := getContractABI(ctx, implementationAddress, chain.ApiEndpoint, os.Getenv(chain.APIKeyEnvVar))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,8 +119,11 @@ func getContractABIDirect(ctx context.Context, address string, endpoint string)

}

func getContractABI(ctx context.Context, address string, 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, etherscanAPIKey), nil)
func getContractABI(ctx context.Context, address string, endpoint string, apiKey string) (*eth.ABI, string, *time.Timer, error) {
if apiKey != "" {
apiKey = fmt.Sprintf("&apiKey=%s", apiKey)
}
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=contract&action=getabi&address=%s%s", endpoint, address, apiKey), nil)
if err != nil {
return nil, "", nil, fmt.Errorf("new request: %w", err)
}
Expand Down Expand Up @@ -158,9 +159,12 @@ func getContractABI(ctx context.Context, address string, endpoint string) (*eth.
}

// getProxyContractImplementation returns the implementation address and a timer to wait before next call
func getProxyContractImplementation(ctx context.Context, address string, endpoint string) (string, *time.Timer, error) {
func getProxyContractImplementation(ctx context.Context, address string, endpoint string, apiKey string) (string, *time.Timer, error) {
if apiKey != "" {
apiKey = fmt.Sprintf("&apiKey=%s", apiKey)
}
// 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, etherscanAPIKey), nil)
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=contract&action=getsourcecode&address=%s%s", endpoint, address, apiKey), nil)

if err != nil {
return "", nil, fmt.Errorf("new request: %w", err)
Expand Down Expand Up @@ -236,7 +240,11 @@ func getContractInitialBlock(ctx context.Context, chain *ChainConfig, contractAd
return initBlock, nil
}

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, contractAddress, etherscanAPIKey), nil)
apiKey := ""
if key := os.Getenv(chain.APIKeyEnvVar); key != "" {
apiKey = fmt.Sprintf("&apiKey=%s", key)
}
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/api?module=account&action=txlist&address=%s&page=1&offset=1&sort=asc%s", chain.ApiEndpoint, contractAddress, apiKey), nil)
if err != nil {
return chain.FirstStreamableBlock, fmt.Errorf("new request: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions ethfull/chain_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ChainConfig struct {
FirstStreamableBlock uint64
Network string
SupportsCalls bool
APIKeyEnvVar string

abiCache map[string]*ABI
initialBlockCache map[string]uint64
Expand All @@ -36,6 +37,7 @@ var ChainConfigByID = map[string]*ChainConfig{
abiCache: make(map[string]*ABI),
initialBlockCache: make(map[string]uint64),
SupportsCalls: true,
APIKeyEnvVar: "CODEGEN_MAINNET_API_KEY",
},
"bnb": {
DisplayName: "BNB",
Expand All @@ -47,6 +49,7 @@ var ChainConfigByID = map[string]*ChainConfig{
abiCache: make(map[string]*ABI),
initialBlockCache: make(map[string]uint64),
SupportsCalls: true,
APIKeyEnvVar: "CODEGEN_BNB_API_KEY",
},
"polygon": {
DisplayName: "Polygon",
Expand All @@ -58,6 +61,7 @@ var ChainConfigByID = map[string]*ChainConfig{
abiCache: make(map[string]*ABI),
initialBlockCache: make(map[string]uint64),
SupportsCalls: true,
APIKeyEnvVar: "CODEGEN_POLYGON_API_KEY",
},
"amoy": {
DisplayName: "Polygon Amoy Testnet",
Expand Down Expand Up @@ -113,6 +117,7 @@ var ChainConfigByID = map[string]*ChainConfig{
abiCache: make(map[string]*ABI),
initialBlockCache: make(map[string]uint64),
SupportsCalls: false,
APIKeyEnvVar: "CODEGEN_OPTIMISM_API_KEY",
},
"avalanche": {
DisplayName: "Avalanche C-chain",
Expand Down
7 changes: 4 additions & 3 deletions ethfull/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/codemodus/kace"
"github.com/golang-cz/textcase"
"github.com/huandu/xstrings"
"math"
"regexp"
"strings"
"time"

"github.com/codemodus/kace"
"github.com/golang-cz/textcase"
"github.com/huandu/xstrings"

"github.com/streamingfast/eth-go"
)

Expand Down

0 comments on commit 14df7fb

Please sign in to comment.