Skip to content

Commit

Permalink
add basic transaction API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Jan 9, 2024
1 parent ab66e8d commit bc1b19d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ func (c *Client) BlockHeight(height uint64) (resp types.Block, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/block/height/%d", height), &resp)
return
}

// Transaction returns the transaction with the specified ID.
func (c *Client) Transaction(id types.TransactionID) (resp types.Transaction, err error) {
err = c.c.GET(fmt.Sprintf("/explorer/transactions/%s", id), &resp)
return
}
14 changes: 14 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type (
Tip() (types.ChainIndex, error)
BlockByID(id types.BlockID) (types.Block, error)
BlockByHeight(height uint64) (types.Block, error)
Transaction(id types.TransactionID) (types.Transaction, error)
}
)

Expand Down Expand Up @@ -164,6 +165,18 @@ func (s *server) explorerBlockHeightHandler(jc jape.Context) {
jc.Encode(block)
}

func (s *server) explorerTransactionsIDHandler(jc jape.Context) {
var id types.TransactionID
if jc.DecodeParam("id", &id) != nil {
return
}
txn, err := s.e.Transaction(id)
if jc.Check("failed to get transaction", err) != nil {
return
}
jc.Encode(txn)
}

// NewServer returns an HTTP handler that serves the explored API.
func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
srv := server{
Expand All @@ -183,5 +196,6 @@ func NewServer(e Explorer, cm ChainManager, s Syncer) http.Handler {
"GET /explorer/tip": srv.explorerTipHandler,
"GET /explorer/block/id/:id": srv.explorerBlockHandler,
"GET /explorer/block/height/:height": srv.explorerBlockHeightHandler,
"GET /explorer/transactions/:id": srv.explorerTransactionsIDHandler,
})
}

0 comments on commit bc1b19d

Please sign in to comment.