|
1 |
| -import type { IFetchService, IProviderService } from "@balmy/sdk" |
| 1 | +import { LOG_SLOW_QUERY_TIMEOUT_SECONDS } from "@/swapService/config/constants" |
| 2 | +import type { |
| 3 | + IFetchService, |
| 4 | + IProviderService, |
| 5 | + SourceId, |
| 6 | + SourceListQuoteRequest, |
| 7 | + SourceListQuoteResponse, |
| 8 | +} from "@balmy/sdk" |
2 | 9 | import { LocalSourceList } from "@balmy/sdk/dist/services/quotes/source-lists/local-source-list"
|
3 | 10 | import { CustomKyberswapQuoteSource } from "./sources/kyberswapQuoteSource"
|
4 | 11 | import { CustomLiFiQuoteSource } from "./sources/lifiQuoteSource"
|
@@ -45,5 +52,40 @@ export class CustomSourceList extends LocalSourceList {
|
45 | 52 | ...customSources,
|
46 | 53 | }
|
47 | 54 | delete mutableThis.sources.balmy
|
| 55 | + |
| 56 | + // wrap getQuote in timer |
| 57 | + const getQuoteSuper = mutableThis.getQuote.bind(this) |
| 58 | + |
| 59 | + mutableThis.getQuote = async ( |
| 60 | + request: SourceListQuoteRequest, |
| 61 | + sourceId: SourceId, |
| 62 | + ): Promise<SourceListQuoteResponse> => { |
| 63 | + const startTime = process.hrtime() |
| 64 | + const result = await getQuoteSuper(request, sourceId) |
| 65 | + const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)) |
| 66 | + if (elapsedSeconds > LOG_SLOW_QUERY_TIMEOUT_SECONDS) { |
| 67 | + const { chainId, sellToken, buyToken, order } = request |
| 68 | + const requestGist = { |
| 69 | + chainId, |
| 70 | + sellToken, |
| 71 | + buyToken, |
| 72 | + order, |
| 73 | + } |
| 74 | + console.log( |
| 75 | + `SLOW QUERY: ${sourceId} ${elapsedSeconds}s ${stringify(requestGist)}`, |
| 76 | + ) |
| 77 | + } |
| 78 | + return result |
| 79 | + } |
48 | 80 | }
|
49 | 81 | }
|
| 82 | + |
| 83 | +function parseHrtimeToSeconds(hrtime: [number, number]) { |
| 84 | + return Number((hrtime[0] + hrtime[1] / 1e9).toFixed(3)) |
| 85 | +} |
| 86 | + |
| 87 | +function stringify(obj: object) { |
| 88 | + return JSON.stringify(obj, (_, v) => |
| 89 | + typeof v === "bigint" ? v.toString() : v, |
| 90 | + ) |
| 91 | +} |
0 commit comments