This repository was archived by the owner on Mar 14, 2025. It is now read-only.
File tree 4 files changed +61
-1
lines changed
4 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,14 @@ import { EtchingRoutes } from './routes/etchings';
7
7
import { AddressRoutes } from './routes/addresses' ;
8
8
import { TransactionRoutes } from './routes/transactions' ;
9
9
import { BlockRoutes } from './routes/blocks' ;
10
+ import { StatusRoutes } from './routes/status' ;
10
11
11
12
export const Api : FastifyPluginAsync <
12
13
Record < never , never > ,
13
14
Server ,
14
15
TypeBoxTypeProvider
15
16
> = async fastify => {
17
+ await fastify . register ( StatusRoutes ) ;
16
18
await fastify . register ( EtchingRoutes ) ;
17
19
await fastify . register ( AddressRoutes ) ;
18
20
await fastify . register ( TransactionRoutes ) ;
Original file line number Diff line number Diff line change
1
+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' ;
2
+ import { FastifyPluginCallback } from 'fastify' ;
3
+ import { Server } from 'http' ;
4
+ import { ApiStatusResponse } from '../schemas' ;
5
+ import { SERVER_VERSION } from '@hirosystems/api-toolkit' ;
6
+ import { handleCache } from '../util/cache' ;
7
+
8
+ export const StatusRoutes : FastifyPluginCallback <
9
+ Record < never , never > ,
10
+ Server ,
11
+ TypeBoxTypeProvider
12
+ > = ( fastify , options , done ) => {
13
+ fastify . addHook ( 'preHandler' , handleCache ) ;
14
+
15
+ fastify . get (
16
+ '/' ,
17
+ {
18
+ schema : {
19
+ operationId : 'getApiStatus' ,
20
+ summary : 'API Status' ,
21
+ description : 'Displays the status of the API' ,
22
+ tags : [ 'Status' ] ,
23
+ response : {
24
+ 200 : ApiStatusResponse ,
25
+ } ,
26
+ } ,
27
+ } ,
28
+ async ( request , reply ) => {
29
+ const result = await fastify . db . sqlTransaction ( async sql => {
30
+ const block_height = await fastify . db . getChainTipBlockHeight ( ) ;
31
+ return {
32
+ server_version : `runes-api ${ SERVER_VERSION . tag } (${ SERVER_VERSION . branch } :${ SERVER_VERSION . commit } )` ,
33
+ status : 'ready' ,
34
+ block_height : block_height ? parseInt ( block_height ) : undefined ,
35
+ } ;
36
+ } ) ;
37
+ await reply . send ( result ) ;
38
+ }
39
+ ) ;
40
+
41
+ done ( ) ;
42
+ } ;
Original file line number Diff line number Diff line change @@ -107,6 +107,15 @@ export type Block = Static<typeof BlockSchema>;
107
107
// Responses
108
108
// ==========================
109
109
110
+ export const ApiStatusResponse = Type . Object (
111
+ {
112
+ server_version : Type . String ( { examples : [ '' ] } ) ,
113
+ status : Type . String ( ) ,
114
+ block_height : Optional ( Type . Integer ( ) ) ,
115
+ } ,
116
+ { title : 'Api Status Response' }
117
+ ) ;
118
+
110
119
export const EtchingResponseSchema = Type . Object ( {
111
120
id : Type . String ( { examples : [ '840000:1' ] } ) ,
112
121
name : Type . String ( { examples : [ 'ZZZZZFEHUZZZZZ' ] } ) ,
Original file line number Diff line number Diff line change @@ -82,6 +82,13 @@ export class PgStore extends BasePgStore {
82
82
return result [ 0 ] ?. etag ;
83
83
}
84
84
85
+ async getChainTipBlockHeight ( ) : Promise < string | undefined > {
86
+ const result = await this . sql < { block_height : string } [ ] > `
87
+ SELECT block_height FROM ledger ORDER BY block_height DESC LIMIT 1
88
+ ` ;
89
+ return result [ 0 ] ?. block_height ;
90
+ }
91
+
85
92
async getEtching ( id : Rune ) : Promise < DbRuneWithChainTip | undefined > {
86
93
const result = await this . sql < DbRuneWithChainTip [ ] > `
87
94
SELECT *, (SELECT MAX(block_height) FROM ledger) AS chain_tip
@@ -95,7 +102,7 @@ export class PgStore extends BasePgStore {
95
102
const results = await this . sql < DbCountedQueryResult < DbRuneWithChainTip > [ ] > `
96
103
WITH
97
104
rune_count AS (SELECT COALESCE(MAX(number), 0) + 1 AS total FROM runes),
98
- max AS (SELECT MAX(block_height) AS max FROM ledger)
105
+ max AS (SELECT MAX(block_height) AS chain_tip FROM ledger)
99
106
SELECT *, (SELECT total FROM rune_count), (SELECT chain_tip FROM max)
100
107
FROM runes
101
108
ORDER BY block_height DESC, tx_index DESC
You can’t perform that action at this time.
0 commit comments