Skip to content
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

Add events endpoint #34

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ func (c *Client) Transactions(ids []types.TransactionID) (resp []explorer.Transa
}

// AddressUTXOs returns the specified address' unspent outputs.
func (c *Client) AddressUTXOs(address types.Address, limit, offset uint64) (resp AddressUTXOsResponse, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/addresses/%s/utxos?limit=%d&offset=%d", address, limit, offset), &resp)
func (c *Client) AddressUTXOs(address types.Address, offset, limit uint64) (resp AddressUTXOsResponse, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/addresses/%s/utxos?offset=%d&limit=%d", address, offset, limit), &resp)
return
}

// AddressEvents returns the specified address' events.
func (c *Client) AddressEvents(address types.Address, offset, limit uint64) (resp []explorer.Event, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/addresses/%s/events?offset=%d&limit=%d", address, offset, limit), &resp)
return
}

Expand Down
30 changes: 26 additions & 4 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ type (
BestTip(height uint64) (types.ChainIndex, error)
Transactions(ids []types.TransactionID) ([]explorer.Transaction, error)
Balance(address types.Address) (sc types.Currency, immatureSC types.Currency, sf uint64, err error)
UnspentSiacoinOutputs(address types.Address, limit, offset uint64) ([]explorer.SiacoinOutput, error)
UnspentSiafundOutputs(address types.Address, limit, offset uint64) ([]explorer.SiafundOutput, error)
UnspentSiacoinOutputs(address types.Address, offset, limit uint64) ([]explorer.SiacoinOutput, error)
UnspentSiafundOutputs(address types.Address, offset, limit uint64) ([]explorer.SiafundOutput, error)
AddressEvents(address types.Address, offset, limit uint64) (events []explorer.Event, err error)
Contracts(ids []types.FileContractID) (result []explorer.FileContract, err error)
}
)
Expand Down Expand Up @@ -208,11 +209,11 @@ func (s *server) explorerAddressessAddressUtxosHandler(jc jape.Context) {
return
}

unspentSiacoinOutputs, err := s.e.UnspentSiacoinOutputs(address, limit, offset)
unspentSiacoinOutputs, err := s.e.UnspentSiacoinOutputs(address, offset, limit)
if jc.Check("failed to get unspent siacoin outputs", err) != nil {
return
}
unspentSiafundOutputs, err := s.e.UnspentSiafundOutputs(address, limit, offset)
unspentSiafundOutputs, err := s.e.UnspentSiafundOutputs(address, offset, limit)
if jc.Check("failed to get unspent siafund outputs", err) != nil {
return
}
Expand Down Expand Up @@ -241,6 +242,26 @@ func (s *server) explorerAddressessAddressBalanceHandler(jc jape.Context) {
})
}

func (s *server) explorerAddressessAddressEventsHandler(jc jape.Context) {
var address types.Address
if jc.DecodeParam("address", &address) != nil {
return
}

limit := uint64(100)
offset := uint64(0)
if jc.DecodeForm("limit", &limit) != nil || jc.DecodeForm("offset", &offset) != nil {
return
}

events, err := s.e.AddressEvents(address, offset, limit)
if jc.Check("failed to get address events", err) != nil {
return
}

jc.Encode(events)
}

func (s *server) explorerContractIDHandler(jc jape.Context) {
errNotFound := errors.New("no contract found")

Expand Down Expand Up @@ -295,6 +316,7 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
"GET /explorer/transactions/:id": srv.explorerTransactionsIDHandler,
"POST /explorer/transactions": srv.explorerTransactionsHandler,
"GET /explorer/addresses/:address/utxos": srv.explorerAddressessAddressUtxosHandler,
"GET /explorer/addresses/:address/events": srv.explorerAddressessAddressEventsHandler,
"GET /explorer/addresses/:address/balance": srv.explorerAddressessAddressBalanceHandler,
"GET /explorer/contracts/:id": srv.explorerContractIDHandler,
"POST /explorer/contracts": srv.explorerContractsHandler,
Expand Down
Loading