1+ const express = require ( 'express' ) ;
2+ const router = express . Router ( ) ;
3+ const Block = require ( '../models/Block' ) ;
4+
5+ // 获取区块信息
6+ router . get ( '/block/:blockNumber' , async ( req , res ) => {
7+ try {
8+ const blockNumber = parseInt ( req . params . blockNumber ) ;
9+ const blockInfo = await Block . findOne ( { number : blockNumber } ) ;
10+
11+ if ( ! blockInfo ) {
12+ return res . status ( 404 ) . json ( {
13+ success : false ,
14+ error : '区块未找到'
15+ } ) ;
16+ }
17+
18+ res . json ( {
19+ success : true ,
20+ data : blockInfo
21+ } ) ;
22+ } catch ( error ) {
23+ res . status ( 500 ) . json ( {
24+ success : false ,
25+ error : error . message
26+ } ) ;
27+ }
28+ } ) ;
29+
30+ // 获取交易信息
31+ router . get ( '/transaction/:txHash' , async ( req , res ) => {
32+ try {
33+ const txHash = req . params . txHash ;
34+ const block = await Block . findOne ( { 'transactions.hash' : txHash } ) ;
35+
36+ if ( ! block ) {
37+ return res . status ( 404 ) . json ( {
38+ success : false ,
39+ error : '交易未找到'
40+ } ) ;
41+ }
42+
43+ const txInfo = block . transactions . find ( tx => tx . hash === txHash ) ;
44+ res . json ( {
45+ success : true ,
46+ data : txInfo
47+ } ) ;
48+ } catch ( error ) {
49+ res . status ( 500 ) . json ( {
50+ success : false ,
51+ error : error . message
52+ } ) ;
53+ }
54+ } ) ;
55+
56+ // 获取地址的交易历史
57+ router . get ( '/address/:address/transactions' , async ( req , res ) => {
58+ try {
59+ const address = req . params . address . toLowerCase ( ) ;
60+ const page = parseInt ( req . query . page ) || 1 ;
61+ const limit = parseInt ( req . query . limit ) || 10 ;
62+ const skip = ( page - 1 ) * limit ;
63+
64+ const query = {
65+ $or : [
66+ { 'transactions.from' : address } ,
67+ { 'transactions.to' : address }
68+ ]
69+ } ;
70+
71+ const [ blocks , total ] = await Promise . all ( [
72+ Block . find ( query )
73+ . sort ( { number : - 1 } )
74+ . skip ( skip )
75+ . limit ( limit ) ,
76+ Block . countDocuments ( query )
77+ ] ) ;
78+
79+ const transactions = blocks . reduce ( ( acc , block ) => {
80+ return acc . concat (
81+ block . transactions . filter ( tx =>
82+ tx . from . toLowerCase ( ) === address ||
83+ ( tx . to && tx . to . toLowerCase ( ) === address )
84+ )
85+ ) ;
86+ } , [ ] ) ;
87+
88+ res . json ( {
89+ success : true ,
90+ data : {
91+ transactions,
92+ pagination : {
93+ page,
94+ limit,
95+ total
96+ }
97+ }
98+ } ) ;
99+ } catch ( error ) {
100+ res . status ( 500 ) . json ( {
101+ success : false ,
102+ error : error . message
103+ } ) ;
104+ }
105+ } ) ;
106+
107+ // 获取最新的区块列表
108+ router . get ( '/blocks/latest' , async ( req , res ) => {
109+ try {
110+ const limit = parseInt ( req . query . limit ) || 10 ;
111+ const blocks = await Block . find ( )
112+ . sort ( { number : - 1 } )
113+ . limit ( limit )
114+ . select ( '-transactions' ) ;
115+
116+ res . json ( {
117+ success : true ,
118+ data : blocks
119+ } ) ;
120+ } catch ( error ) {
121+ res . status ( 500 ) . json ( {
122+ success : false ,
123+ error : error . message
124+ } ) ;
125+ }
126+ } ) ;
127+
128+ // 获取最新的交易列表
129+ router . get ( '/transactions/latest' , async ( req , res ) => {
130+ try {
131+ const limit = parseInt ( req . query . limit ) || 10 ;
132+ const blocks = await Block . find ( )
133+ . sort ( { number : - 1 } )
134+ . limit ( Math . ceil ( limit / 2 ) )
135+ . select ( 'transactions' ) ;
136+
137+ const transactions = blocks . reduce ( ( acc , block ) => {
138+ return acc . concat ( block . transactions ) ;
139+ } , [ ] ) . slice ( 0 , limit ) ;
140+
141+ res . json ( {
142+ success : true ,
143+ data : transactions
144+ } ) ;
145+ } catch ( error ) {
146+ res . status ( 500 ) . json ( {
147+ success : false ,
148+ error : error . message
149+ } ) ;
150+ }
151+ } ) ;
152+
153+ // 搜索功能(支持区块号、交易哈希、地址)
154+ router . get ( '/search/:query' , async ( req , res ) => {
155+ try {
156+ const query = req . params . query ;
157+ let result = {
158+ type : '' ,
159+ data : null
160+ } ;
161+
162+ // 尝试作为区块号搜索
163+ if ( / ^ \d + $ / . test ( query ) ) {
164+ const block = await Block . findOne ( { number : parseInt ( query ) } ) ;
165+ if ( block ) {
166+ result = { type : 'block' , data : block } ;
167+ }
168+ }
169+
170+ // 尝试作为交易哈希搜索
171+ if ( ! result . data && / ^ 0 x [ a - f A - F 0 - 9 ] { 64 } $ / . test ( query ) ) {
172+ const block = await Block . findOne ( { 'transactions.hash' : query } ) ;
173+ if ( block ) {
174+ const transaction = block . transactions . find ( tx => tx . hash === query ) ;
175+ if ( transaction ) {
176+ result = { type : 'transaction' , data : transaction } ;
177+ }
178+ }
179+ }
180+
181+ // 尝试作为地址搜索
182+ if ( ! result . data && / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( query ) ) {
183+ const address = query . toLowerCase ( ) ;
184+ const blocks = await Block . find ( {
185+ $or : [
186+ { 'transactions.from' : address } ,
187+ { 'transactions.to' : address }
188+ ]
189+ } ) . limit ( 10 ) ;
190+
191+ if ( blocks . length > 0 ) {
192+ const transactions = blocks . reduce ( ( acc , block ) => {
193+ return acc . concat (
194+ block . transactions . filter ( tx =>
195+ tx . from . toLowerCase ( ) === address ||
196+ ( tx . to && tx . to . toLowerCase ( ) === address )
197+ )
198+ ) ;
199+ } , [ ] ) ;
200+ result = { type : 'address' , data : transactions } ;
201+ }
202+ }
203+
204+ res . json ( {
205+ success : true ,
206+ data : result
207+ } ) ;
208+ } catch ( error ) {
209+ res . status ( 500 ) . json ( {
210+ success : false ,
211+ error : error . message
212+ } ) ;
213+ }
214+ } ) ;
215+
216+
217+ module . exports = router ;
0 commit comments