Skip to content

Commit

Permalink
add API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jun 4, 2024
1 parent 36b6236 commit 535d3da
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (c *Client) AddressBalance(address types.Address) (resp AddressBalanceRespo

// Contract returns the file contract with the specified ID.
func (c *Client) Contract(id types.FileContractID) (resp explorer.FileContract, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/contracts/%s", id), &resp)
err = c.c.GET(fmt.Sprintf("/explorer/contracts/id/%s", id), &resp)
return
}

Expand All @@ -116,6 +116,12 @@ func (c *Client) Contracts(ids []types.FileContractID) (resp []explorer.FileCont
return
}

// ContractsKey returns the contracts for a particular ed25519 key.
func (c *Client) ContractsKey(key types.UnlockKey) (resp []explorer.FileContract, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/contracts/key/%s", key), &resp)
return
}

// Metrics returns the most recent metrics about Sia.
func (c *Client) Metrics() (resp explorer.Metrics, err error) {
err = c.c.GET("/explorer/metrics", &resp)
Expand Down
21 changes: 20 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type (
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)
ContractsKey(key types.UnlockKey) (result []explorer.FileContract, err error)
}
)

Expand Down Expand Up @@ -305,6 +306,23 @@ func (s *server) explorerContractIDHandler(jc jape.Context) {
jc.Encode(fcs[0])
}

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

var key types.UnlockKey
if jc.DecodeParam("key", &key) != nil {
return
}
fcs, err := s.e.ContractsKey(key)
if jc.Check("failed to get contracts", err) != nil {
return
} else if len(fcs) == 0 {
jc.Error(errNotFound, http.StatusNotFound)
return
}
jc.Encode(fcs)
}

func (s *server) explorerContractsHandler(jc jape.Context) {
var ids []types.FileContractID
if jc.Decode(&ids) != nil {
Expand Down Expand Up @@ -346,7 +364,8 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
"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,
"GET /explorer/contracts/id/:id": srv.explorerContractIDHandler,
"GET /explorer/contracts/key/:key": srv.explorerContractKeyHandler,
"POST /explorer/contracts": srv.explorerContractsHandler,
})
}

0 comments on commit 535d3da

Please sign in to comment.