Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,61 @@ fun Route.configureHistoricalFeesEndpoint(mempoolCollector: MempoolCollector) {
call.respond(response)
}
}

get("/historical_fees") {
logger.info("Received request for historical fee estimates")

// Extract params from query start_timestamp, end_timestamp, interval (seconds)
val startTimestampParam = call.request.queryParameters["start_timestamp"]?.toLongOrNull()
val endTimestampParam = call.request.queryParameters["end_timestamp"]?.toLongOrNull()
val intervalParam = call.request.queryParameters["interval"]
val interval: Int = intervalParam?.toIntOrNull() ?: 3600

if (startTimestampParam == null) {
call.respondText(
"start_timestamp parameter is required",
status = HttpStatusCode.BadRequest,
contentType = ContentType.Text.Plain,
)
return@get
}

if (endTimestampParam == null) {
call.respondText(
"end_timestamp parameter is required",
status = HttpStatusCode.BadRequest,
contentType = ContentType.Text.Plain,
)
return@get
}

// Fetch historical fee estimate based on date
logger.info(
"Fetching historical fee estimate for start_timestamp: $startTimestampParam to " +
"end_timestamp: $endTimestampParam for intervals: $interval seconds",
)
val feeEstimate = mempoolCollector.getFeeEstimateForTimestampRange(startTimestampParam, endTimestampParam, interval)

if (feeEstimate == null) {
logger.warn(
"No historical fee estimates available for start_timestamp $startTimestampParam to " +
"end_timestamp: $endTimestampParam for interval: $interval seconds",
)
call.respondText(
"No historical fee estimates available for start_timestamp $startTimestampParam to " +
"end_timestamp: $endTimestampParam for interval: $interval seconds",
status = HttpStatusCode.ServiceUnavailable,
contentType = ContentType.Text.Plain,
)
} else {
var mutableResponse = mutableListOf<FeeEstimateResponse>()
logger.info("Transforming historical fee estimates for response")
for (estimate in feeEstimate) {
val response = transformFeeEstimate(estimate)
mutableResponse.add(response)
}
logger.debug("Returning historical fee estimates")
call.respond(mutableResponse)
}
}
}
36 changes: 36 additions & 0 deletions app/src/main/kotlin/xyz/block/augurref/service/MempoolCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,42 @@ class MempoolCollector(
return latestFeeEstimate.get()
}

fun getFeeEstimateForTimestampRange(start_date: Long, end_date: Long, interval: Int): List<FeeEstimate>? {
// Ensure start_date is not after end_date
if (start_date > end_date) {
logger.warn("Start date is after end date")
return null
}

// Initialize result list to store fee estimates
val estimates = mutableListOf<FeeEstimate>()
var currentDate = start_date

// Iterate through the date range with the given interval
while (currentDate < end_date) {
logger.debug("Processing fee estimate for $currentDate")
val feeEstimate = getFeeEstimateForTimestamp(currentDate)

// If fee estimate is non-null and has estimates, merge them
if (feeEstimate != null && feeEstimate.estimates.isNotEmpty()) {
estimates.add(feeEstimate)
} else {
logger.debug("No valid fee estimate for $currentDate")
}

currentDate = currentDate + interval
}

// Return null if no estimates were collected
if (estimates.isEmpty()) {
logger.warn("No fee estimates collected for the date range")
return null
}

// Return a FeeEstimate with the aggregated estimates
return estimates
}

/**
* Get the fee estimate for specific date
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class FeeEstimateEndpointTest {
verify { mockMempoolCollector.getLatestFeeEstimateForBlockTarget(2.0) }
}


private fun createMockFeeEstimate(timestamp: Instant): FeeEstimate {
// Create mock data using the actual augur library structure
val mockFeeEstimate = mockk<FeeEstimate>()
Expand Down