|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Block, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package xyz.block.augurref.api |
| 18 | + |
| 19 | +import io.ktor.http.ContentType |
| 20 | +import io.ktor.http.HttpStatusCode |
| 21 | +import io.ktor.server.application.call |
| 22 | +import io.ktor.server.response.respond |
| 23 | +import io.ktor.server.response.respondText |
| 24 | +import io.ktor.server.routing.Route |
| 25 | +import io.ktor.server.routing.get |
| 26 | +import org.slf4j.LoggerFactory |
| 27 | +import xyz.block.augurref.service.MempoolCollector |
| 28 | +import java.time.LocalDateTime |
| 29 | +import java.time.format.DateTimeParseException |
| 30 | + |
| 31 | +/** |
| 32 | + * Configure historical fee estimate endpoint |
| 33 | + */ |
| 34 | +fun Route.configureHistoricalFeesEndpoint(mempoolCollector: MempoolCollector) { |
| 35 | + val logger = LoggerFactory.getLogger("xyz.block.augurref.api.HistoricalFeeEstimateEndpoint") |
| 36 | + |
| 37 | + get("/historical_fee") { |
| 38 | + logger.info("Received request for historical fee estimates") |
| 39 | + |
| 40 | + // Extract date param from query parameters |
| 41 | + val dateParam = call.request.queryParameters["date"] |
| 42 | + |
| 43 | + if (dateParam == null) { |
| 44 | + call.respondText( |
| 45 | + "Date parameter is required", |
| 46 | + status = HttpStatusCode.BadRequest, |
| 47 | + contentType = ContentType.Text.Plain, |
| 48 | + ) |
| 49 | + return@get |
| 50 | + } |
| 51 | + |
| 52 | + // Validate and parse the date |
| 53 | + val date = try { |
| 54 | + dateParam?.let { LocalDateTime.parse(it) } |
| 55 | + } catch (e: DateTimeParseException) { |
| 56 | + logger.warn("Invalid date format: $dateParam") |
| 57 | + call.respondText( |
| 58 | + "Invalid date format. Use YYYY-MM-DDTHH:MM:SS", |
| 59 | + status = HttpStatusCode.BadRequest, |
| 60 | + contentType = ContentType.Text.Plain, |
| 61 | + ) |
| 62 | + return@get |
| 63 | + } |
| 64 | + |
| 65 | + // Fetch historical fee estimate based on date |
| 66 | + val feeEstimate = if (date != null) { |
| 67 | + logger.info("Fetching historical fee estimate for date: $date") |
| 68 | + mempoolCollector.getFeeEstimateForDate(date) |
| 69 | + } else { |
| 70 | + logger.warn("date is null") |
| 71 | + call.respondText( |
| 72 | + "Failed to parse date", |
| 73 | + status = HttpStatusCode.InternalServerError, |
| 74 | + contentType = ContentType.Text.Plain, |
| 75 | + ) |
| 76 | + return@get |
| 77 | + } |
| 78 | + |
| 79 | + if (feeEstimate == null) { |
| 80 | + logger.warn("No historical fee estimates available for $date") |
| 81 | + call.respondText( |
| 82 | + "No historical fee estimates available for $date", |
| 83 | + status = HttpStatusCode.ServiceUnavailable, |
| 84 | + contentType = ContentType.Text.Plain, |
| 85 | + ) |
| 86 | + } else { |
| 87 | + logger.info("Transforming historical fee estimates for response") |
| 88 | + val response = transformFeeEstimate(feeEstimate) |
| 89 | + logger.debug("Returning historical fee estimates with ${response.estimates.size} targets") |
| 90 | + call.respond(response) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments