Skip to content

Commit

Permalink
Merge pull request #34 from SiaFoundation/add-events-endpoint
Browse files Browse the repository at this point in the history
Add events endpoint
  • Loading branch information
ChrisSchinnerl authored May 21, 2024
2 parents 25bfc41 + f7a1f1f commit 8c3746d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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

0 comments on commit 8c3746d

Please sign in to comment.