11package handlers
22
33import (
4+ "fmt"
45 "net/http"
6+ "time"
57
68 "github.com/ethereum/go-ethereum/accounts/abi"
79 gethCommon "github.com/ethereum/go-ethereum/common"
@@ -28,6 +30,8 @@ import (
2830// @Param limit query int false "Number of items per page" default(5)
2931// @Param aggregate query []string false "List of aggregate functions to apply"
3032// @Param force_consistent_data query bool false "Force consistent data at the expense of query speed"
33+ // @Param from_time query int false "Start time for filtering (Unix timestamp)"
34+ // @Param to_time query int false "End time for filtering (Unix timestamp, defaults to current time)"
3135// @Success 200 {object} api.QueryResponse{data=[]common.TransactionModel}
3236// @Failure 400 {object} api.Error
3337// @Failure 401 {object} api.Error
@@ -53,6 +57,8 @@ func GetTransactions(c *gin.Context) {
5357// @Param limit query int false "Number of items per page" default(5)
5458// @Param force_consistent_data query bool false "Force consistent data at the expense of query speed"
5559// @Param decode query bool false "Decode transaction data"
60+ // @Param from_time query int false "Start time for filtering (Unix timestamp)"
61+ // @Param to_time query int false "End time for filtering (Unix timestamp, defaults to current time)"
5662// @Success 200 {object} api.QueryResponse{data=[]common.DecodedTransactionModel}
5763// @Failure 400 {object} api.Error
5864// @Failure 401 {object} api.Error
@@ -78,6 +84,8 @@ func GetWalletTransactions(c *gin.Context) {
7884// @Param limit query int false "Number of items per page" default(5)
7985// @Param aggregate query []string false "List of aggregate functions to apply"
8086// @Param force_consistent_data query bool false "Force consistent data at the expense of query speed"
87+ // @Param from_time query int false "Start time for filtering (Unix timestamp)"
88+ // @Param to_time query int false "End time for filtering (Unix timestamp, defaults to current time)"
8189// @Success 200 {object} api.QueryResponse{data=[]common.TransactionModel}
8290// @Failure 400 {object} api.Error
8391// @Failure 401 {object} api.Error
@@ -104,6 +112,8 @@ func GetTransactionsByContract(c *gin.Context) {
104112// @Param limit query int false "Number of items per page" default(5)
105113// @Param aggregate query []string false "List of aggregate functions to apply"
106114// @Param force_consistent_data query bool false "Force consistent data at the expense of query speed"
115+ // @Param from_time query int false "Start time for filtering (Unix timestamp)"
116+ // @Param to_time query int false "End time for filtering (Unix timestamp, defaults to current time)"
107117// @Success 200 {object} api.QueryResponse{data=[]common.DecodedTransactionModel}
108118// @Failure 400 {object} api.Error
109119// @Failure 401 {object} api.Error
@@ -129,6 +139,35 @@ func handleTransactionsRequest(c *gin.Context) {
129139 return
130140 }
131141
142+ // Parse and validate time parameters
143+ fromTime := queryParams .FromTime
144+ toTime := queryParams .ToTime
145+
146+ // If toTime is not provided, default to current time
147+ if toTime == 0 {
148+ toTime = time .Now ().Unix ()
149+ }
150+
151+ // Validate time range
152+ if fromTime > 0 && toTime > 0 {
153+ timeRange := toTime - fromTime
154+ maxTimeRange := int64 (config .Cfg .API .TransactionMaxTimeRangeSeconds )
155+ if maxTimeRange == 0 {
156+ // Default to 1 week if not configured
157+ maxTimeRange = 7 * 24 * 60 * 60 // 1 week in seconds
158+ }
159+
160+ if timeRange > maxTimeRange {
161+ api .BadRequestErrorHandler (c , fmt .Errorf ("time range cannot exceed %d seconds (approximately %d days)" , maxTimeRange , maxTimeRange / (24 * 60 * 60 )))
162+ return
163+ }
164+
165+ if fromTime >= toTime {
166+ api .BadRequestErrorHandler (c , fmt .Errorf ("from_time must be less than to_time" ))
167+ return
168+ }
169+ }
170+
132171 var functionABI * abi.Method
133172 signatureHash := ""
134173 if signature != "" {
@@ -158,6 +197,8 @@ func handleTransactionsRequest(c *gin.Context) {
158197 Page : queryParams .Page ,
159198 Limit : queryParams .Limit ,
160199 ForceConsistentData : queryParams .ForceConsistentData ,
200+ FromTime : fromTime ,
201+ ToTime : toTime ,
161202 }
162203
163204 // Initialize the QueryResult
0 commit comments