@@ -5,18 +5,22 @@ import (
55 "flare-indexer/services/api"
66 "flare-indexer/services/context"
77 "flare-indexer/services/utils"
8+ "flare-indexer/utils/staking"
89 "net/http"
10+ "strconv"
911
1012 "gorm.io/gorm"
1113)
1214
1315type transactionRouteHandlers struct {
14- db * gorm.DB
16+ db * gorm.DB
17+ epochs staking.EpochInfo
1518}
1619
17- func newTransactionRouteHandlers (ctx context.ServicesContext ) * transactionRouteHandlers {
20+ func newTransactionRouteHandlers (ctx context.ServicesContext , epochs staking. EpochInfo ) * transactionRouteHandlers {
1821 return & transactionRouteHandlers {
19- db : ctx .DB (),
22+ db : ctx .DB (),
23+ epochs : epochs ,
2024 }
2125}
2226
@@ -41,8 +45,37 @@ func (rh *transactionRouteHandlers) getTransaction() utils.RouteHandler {
4145 & api.ApiPChainTx {})
4246}
4347
44- func AddTransactionRoutes (router utils.Router , ctx context.ServicesContext ) {
45- vr := newTransactionRouteHandlers (ctx )
48+ func (rh * transactionRouteHandlers ) listTransactions () utils.RouteHandler {
49+ handler := func (params map [string ]string ) ([]api.ApiPChainTxListItem , * utils.ErrorHandler ) {
50+ epoch , err := strconv .ParseInt (params ["epoch" ], 10 , 64 )
51+ if err != nil {
52+ return nil , utils .HttpErrorHandler (http .StatusBadRequest , "Invalid epoch" )
53+ }
54+
55+ startTimestamp , endTimestamp := rh .epochs .GetTimeRange (epoch )
56+ txs , err := database .GetPChainTxsForEpoch (& database.GetPChainTxsForEpochInput {
57+ DB : rh .db ,
58+ StartTimestamp : startTimestamp ,
59+ EndTimestamp : endTimestamp ,
60+ })
61+ if err != nil {
62+ return nil , utils .InternalServerErrorHandler (err )
63+ }
64+
65+ return api .NewApiPChainTxList (txs ), nil
66+ }
67+
68+ return utils .NewParamRouteHandler (handler , http .MethodGet ,
69+ map [string ]string {"epoch:[0-9]+" : "Epoch" },
70+ []api.ApiPChainTxListItem {},
71+ )
72+ }
73+
74+ func AddTransactionRoutes (
75+ router utils.Router , ctx context.ServicesContext , epochs staking.EpochInfo ,
76+ ) {
77+ vr := newTransactionRouteHandlers (ctx , epochs )
4678 subrouter := router .WithPrefix ("/transactions" , "Transactions" )
4779 subrouter .AddRoute ("/get/{tx_id:[0-9a-zA-Z]+}" , vr .getTransaction ())
80+ subrouter .AddRoute ("/list/{epoch:[0-9]+}" , vr .listTransactions ())
4881}
0 commit comments