@@ -19,42 +19,37 @@ export function createTxRouter(db: DataStore): RouterWithAsync {
19
19
const router = addAsync ( express . Router ( ) ) ;
20
20
21
21
router . getAsync ( '/' , async ( req , res ) => {
22
- try {
23
- const limit = parseTxQueryLimit ( req . query . limit ?? 96 ) ;
24
- const offset = parsePagingQueryInput ( req . query . offset ?? 0 ) ;
25
-
26
- const typeQuery = req . query . type ;
27
- let txTypeFilter : TransactionType [ ] ;
28
- if ( Array . isArray ( typeQuery ) ) {
29
- txTypeFilter = parseTxTypeStrings ( typeQuery as string [ ] ) ;
30
- } else if ( typeof typeQuery === 'string' ) {
31
- txTypeFilter = parseTxTypeStrings ( [ typeQuery ] ) ;
32
- } else if ( typeQuery ) {
33
- throw new Error ( `Unexpected tx type query value: ${ JSON . stringify ( typeQuery ) } ` ) ;
34
- } else {
35
- txTypeFilter = [ ] ;
36
- }
22
+ const limit = parseTxQueryLimit ( req . query . limit ?? 96 ) ;
23
+ const offset = parsePagingQueryInput ( req . query . offset ?? 0 ) ;
24
+
25
+ const typeQuery = req . query . type ;
26
+ let txTypeFilter : TransactionType [ ] ;
27
+ if ( Array . isArray ( typeQuery ) ) {
28
+ txTypeFilter = parseTxTypeStrings ( typeQuery as string [ ] ) ;
29
+ } else if ( typeof typeQuery === 'string' ) {
30
+ txTypeFilter = parseTxTypeStrings ( [ typeQuery ] ) ;
31
+ } else if ( typeQuery ) {
32
+ throw new Error ( `Unexpected tx type query value: ${ JSON . stringify ( typeQuery ) } ` ) ;
33
+ } else {
34
+ txTypeFilter = [ ] ;
35
+ }
37
36
38
- const { results : txResults , total } = await db . getTxList ( { offset, limit, txTypeFilter } ) ;
37
+ const { results : txResults , total } = await db . getTxList ( { offset, limit, txTypeFilter } ) ;
39
38
40
- // TODO: fix these duplicate db queries
41
- const results = await Bluebird . mapSeries ( txResults , async tx => {
42
- const txQuery = await getTxFromDataStore ( tx . tx_id , db ) ;
43
- if ( ! txQuery . found ) {
44
- throw new Error ( 'unexpected tx not found -- fix tx enumeration query' ) ;
45
- }
46
- return txQuery . result ;
47
- } ) ;
48
- const response : TransactionResults = { limit, offset, total, results } ;
49
- const schemaPath = require . resolve (
50
- '@blockstack/stacks-blockchain-sidecar-types/api/transaction/get-transactions.schema.json'
51
- ) ;
52
- await validate ( schemaPath , response ) ;
53
- res . json ( response ) ;
54
- } catch ( error ) {
55
- logError ( 'error getting tx' , error ) ;
56
- res . status ( 500 ) ;
57
- }
39
+ // TODO: fix these duplicate db queries
40
+ const results = await Bluebird . mapSeries ( txResults , async tx => {
41
+ const txQuery = await getTxFromDataStore ( tx . tx_id , db ) ;
42
+ if ( ! txQuery . found ) {
43
+ throw new Error ( 'unexpected tx not found -- fix tx enumeration query' ) ;
44
+ }
45
+ return txQuery . result ;
46
+ } ) ;
47
+ const response : TransactionResults = { limit, offset, total, results } ;
48
+ const schemaPath = require . resolve (
49
+ '@blockstack/stacks-blockchain-sidecar-types/api/transaction/get-transactions.schema.json'
50
+ ) ;
51
+ await validate ( schemaPath , response ) ;
52
+ res . json ( response ) ;
58
53
} ) ;
59
54
60
55
router . getAsync ( '/stream' , async ( req , res ) => {
@@ -107,26 +102,22 @@ export function createTxRouter(db: DataStore): RouterWithAsync {
107
102
} ) ;
108
103
109
104
router . getAsync ( '/:tx_id' , async ( req , res ) => {
110
- try {
111
- const { tx_id } = req . params ;
105
+ const { tx_id } = req . params ;
112
106
113
- if ( ! has0xPrefix ( tx_id ) ) {
114
- return res . redirect ( '/sidecar/v1/tx/0x' + tx_id ) ;
115
- }
107
+ if ( ! has0xPrefix ( tx_id ) ) {
108
+ return res . redirect ( '/sidecar/v1/tx/0x' + tx_id ) ;
109
+ }
116
110
117
- const txQuery = await getTxFromDataStore ( tx_id , db ) ;
118
- if ( ! txQuery . found ) {
119
- res . status ( 404 ) . json ( { error : `could not find transaction by ID ${ tx_id } ` } ) ;
120
- return ;
121
- }
122
- const schemaPath = require . resolve (
123
- '@blockstack/stacks-blockchain-sidecar-types/entities/transactions/transaction.schema.json'
124
- ) ;
125
- await validate ( schemaPath , txQuery . result ) ;
126
- res . json ( txQuery . result ) ;
127
- } catch ( error ) {
128
- res . status ( 500 ) ;
111
+ const txQuery = await getTxFromDataStore ( tx_id , db ) ;
112
+ if ( ! txQuery . found ) {
113
+ res . status ( 404 ) . json ( { error : `could not find transaction by ID ${ tx_id } ` } ) ;
114
+ return ;
129
115
}
116
+ const schemaPath = require . resolve (
117
+ '@blockstack/stacks-blockchain-sidecar-types/entities/transactions/transaction.schema.json'
118
+ ) ;
119
+ await validate ( schemaPath , txQuery . result ) ;
120
+ res . json ( txQuery . result ) ;
130
121
} ) ;
131
122
132
123
return router ;
0 commit comments