Commit bc4974ec authored by Ethen Pociask's avatar Ethen Pociask

[indexer.client] Lightweight Indexer Client

parent 3a62bccd
......@@ -2,6 +2,7 @@ package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/ethereum-optimism/optimism/indexer/database"
......@@ -9,6 +10,14 @@ import (
"github.com/go-chi/chi/v5"
)
const (
depositPath = "/api/v0/deposits/"
withdrawalPath = "/api/v0/withdrawals/"
healthzPath = "/healthz"
addressParam = "{address:.+}"
)
type PaginationResponse struct {
// TODO type this better
Data interface{} `json:"data"`
......@@ -97,9 +106,9 @@ func NewApi(bv database.BridgeView) *Api {
// these regex are .+ because I wasn't sure what they should be
// don't want a regex for addresses because would prefer to validate the address
// with go-ethereum and throw a friendly error message
r.Get("/api/v0/deposits/{address:.+}", api.DepositsHandler)
r.Get("/api/v0/withdrawals/{address:.+}", api.WithdrawalsHandler)
r.Get("/healthz", api.HealthzHandler)
r.Get(fmt.Sprintf("%s%s", depositPath, addressParam), api.DepositsHandler)
r.Get(fmt.Sprintf("%s%s", withdrawalPath, addressParam), api.WithdrawalsHandler)
r.Get(healthzPath, api.HealthzHandler)
return api
......
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/ethereum-optimism/optimism/indexer/database"
"github.com/ethereum-optimism/optimism/indexer/db"
"github.com/ethereum/go-ethereum/common"
)
// Client defines the methods for interfacing with the indexer API.
type Client struct {
url string
c *http.Client
}
// NewClient creates a client that uses the given RPC client.
func NewClient(c *http.Client, url string) *Client {
return &Client{
url: url,
c: c,
}
}
// Close closes the underlying RPC connection.
func (ec *Client) Close() {
ec.c.CloseIdleConnections()
}
// GetDepositsByAddress returns all associated (L1->L2) deposits for a provided L1 address.
func (ic *Client) GetDepositsByAddress(addr common.Address) ([]*database.DepositWithTransactionHash, error) {
var deposits []*database.DepositWithTransactionHash
resp, err := ic.c.Get(ic.url + depositPath + addr.Hex())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if err := json.NewDecoder(resp.Body).Decode(&deposits); err != nil {
return nil, err
}
return deposits, nil
}
// GetWithdrawalsByAddress returns all associated (L2->L1) withdrawals for a provided L2 address.
func (ic *Client) GetWithdrawalsByAddress(addr common.Address) (*db.PaginatedWithdrawals, error) {
var deposits *db.PaginatedWithdrawals
resp, err := ic.c.Get(ic.url + depositPath + addr.Hex())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if err := json.NewDecoder(resp.Body).Decode(&deposits); err != nil {
return nil, err
}
return deposits, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment