-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Init etherscan client #8280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Init etherscan client #8280
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| package etherscan | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "github.com/ethereum-optimism/optimism/op-service/retry" | ||
| ) | ||
|
|
||
| type client struct { | ||
| baseUrl string | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| type apiResponse struct { | ||
| Status string `json:"status"` | ||
| Message string `json:"message"` | ||
| Result json.RawMessage `json:"result"` | ||
| } | ||
|
|
||
| type rpcResponse struct { | ||
| JsonRpc string `json:"jsonrpc"` | ||
| Id int `json:"id"` | ||
| Result json.RawMessage `json:"result"` | ||
| } | ||
|
|
||
| type TxInfo struct { | ||
| TxHash string `json:"txHash"` | ||
| To string `json:"to"` | ||
| Input string `json:"input"` | ||
| } | ||
|
|
||
| const apiMaxRetries = 3 | ||
| const apiRetryDelay = time.Duration(2) * time.Second | ||
| const errRateLimited = "Max rate limit reached" | ||
|
|
||
| func NewClient(baseUrl, apiKey string) *client { | ||
| return &client{ | ||
| baseUrl: baseUrl + "/api?apikey=" + apiKey + "&", | ||
| httpClient: &http.Client{ | ||
| Timeout: time.Second * 10, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func NewEthereumClient(apiKey string) *client { | ||
| return NewClient("https://api.etherscan.io", apiKey) | ||
| } | ||
|
|
||
| func NewOptimismClient(apiKey string) *client { | ||
| return NewClient("https://api-optimistic.etherscan.io", apiKey) | ||
| } | ||
|
|
||
| func (c *client) fetch(ctx context.Context, url string) ([]byte, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return body, nil | ||
| } | ||
|
|
||
| func (c *client) fetchEtherscanApi(ctx context.Context, url string) (apiResponse, error) { | ||
| return retry.Do[apiResponse](ctx, apiMaxRetries, retry.Fixed(apiRetryDelay), func() (apiResponse, error) { | ||
| body, err := c.fetch(ctx, url) | ||
| if err != nil { | ||
| return apiResponse{}, err | ||
| } | ||
|
|
||
| var response apiResponse | ||
| err = json.Unmarshal(body, &response) | ||
| if err != nil { | ||
| return apiResponse{}, fmt.Errorf("failed to unmarshal as apiResponse: %w", err) | ||
| } | ||
|
|
||
| if response.Message != "OK" { | ||
| var resultString string | ||
| err = json.Unmarshal(response.Result, &resultString) | ||
| if err != nil { | ||
| return apiResponse{}, fmt.Errorf("response for %s not OK, returned message: %s", url, response.Message) | ||
| } | ||
|
|
||
| if resultString == errRateLimited { | ||
| return apiResponse{}, errors.New(errRateLimited) | ||
| } | ||
|
|
||
| return apiResponse{}, fmt.Errorf("there was an issue with the Etherscan request to %s, received response: %v", url, response) | ||
Spacesai1or marked this conversation as resolved.
Show resolved
Hide resolved
Spacesai1or marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return response, nil | ||
| }) | ||
| } | ||
|
|
||
| func (c *client) fetchEtherscanRpc(ctx context.Context, url string) (rpcResponse, error) { | ||
| return retry.Do[rpcResponse](ctx, apiMaxRetries, retry.Fixed(apiRetryDelay), func() (rpcResponse, error) { | ||
| body, err := c.fetch(ctx, url) | ||
| if err != nil { | ||
| return rpcResponse{}, err | ||
| } | ||
|
|
||
| var response rpcResponse | ||
| err = json.Unmarshal(body, &response) | ||
| if err != nil { | ||
| return rpcResponse{}, fmt.Errorf("failed to unmarshal as rpcResponse: %w", err) | ||
| } | ||
|
|
||
| var resultString string | ||
| err = json.Unmarshal(response.Result, &resultString) | ||
| if err != nil { | ||
| return rpcResponse{}, fmt.Errorf("failed to unmarshal response.Result as a string: %w", err) | ||
| } | ||
| if resultString == errRateLimited { | ||
| return rpcResponse{}, errors.New(errRateLimited) | ||
| } | ||
|
|
||
| return response, nil | ||
| }) | ||
mslipper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (c *client) FetchAbi(ctx context.Context, address string) (string, error) { | ||
| params := url.Values{} | ||
| params.Set("address", address) | ||
| url := constructUrl(c.baseUrl, "getabi", "contract", params) | ||
| response, err := c.fetchEtherscanApi(ctx, url) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| var abi string | ||
| err = json.Unmarshal(response.Result, &abi) | ||
| if err != nil { | ||
| return "", fmt.Errorf("API response result is not expected ABI string: %w", err) | ||
| } | ||
|
|
||
| return abi, nil | ||
| } | ||
|
|
||
| func (c *client) FetchDeployedBytecode(ctx context.Context, address string) (string, error) { | ||
| params := url.Values{} | ||
| params.Set("address", address) | ||
| url := constructUrl(c.baseUrl, "eth_getCode", "proxy", params) | ||
| response, err := c.fetchEtherscanRpc(ctx, url) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error fetching deployed bytecode: %w", err) | ||
| } | ||
|
|
||
| var bytecode string | ||
| err = json.Unmarshal(response.Result, &bytecode) | ||
| if err != nil { | ||
| return "", errors.New("API response result is not expected bytecode string") | ||
| } | ||
|
|
||
| return bytecode, nil | ||
| } | ||
|
|
||
| func (c *client) FetchDeploymentTxHash(ctx context.Context, address string) (string, error) { | ||
| params := url.Values{} | ||
| params.Set("contractaddresses", address) | ||
| url := constructUrl(c.baseUrl, "getcontractcreation", "contract", params) | ||
| response, err := c.fetchEtherscanApi(ctx, url) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| var results []TxInfo | ||
| err = json.Unmarshal(response.Result, &results) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to unmarshal API response as []txInfo: %w", err) | ||
| } | ||
|
|
||
| if len(results) == 0 { | ||
| return "", fmt.Errorf("API response result is an empty array") | ||
| } | ||
|
|
||
| return results[0].TxHash, nil | ||
| } | ||
|
|
||
| func (c *client) FetchDeploymentTx(ctx context.Context, txHash string) (TxInfo, error) { | ||
| params := url.Values{} | ||
| params.Set("txHash", txHash) | ||
| params.Set("tag", "latest") | ||
| url := constructUrl(c.baseUrl, "eth_getTransactionByHash", "proxy", params) | ||
| response, err := c.fetchEtherscanRpc(ctx, url) | ||
| if err != nil { | ||
| return TxInfo{}, err | ||
| } | ||
|
|
||
| resultBytes, err := json.Marshal(response.Result) | ||
| if err != nil { | ||
| return TxInfo{}, fmt.Errorf("failed to marshal Result into JSON: %w", err) | ||
| } | ||
|
|
||
| var tx TxInfo | ||
| err = json.Unmarshal(resultBytes, &tx) | ||
| if err != nil { | ||
| return TxInfo{}, fmt.Errorf("API response result is not expected txInfo struct: %w", err) | ||
| } | ||
|
|
||
| return tx, nil | ||
| } | ||
|
|
||
| func constructUrl(baseUrl, action, module string, params url.Values) string { | ||
| params.Set("action", action) | ||
| params.Set("module", module) | ||
| queryFragment := params.Encode() | ||
| return baseUrl + queryFragment | ||
mslipper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.