|
| 1 | +import { Logger, ErrorHandler } from '../common/decorators/index.js'; |
| 2 | + |
| 3 | +import { BusModule } from './bus-module.js'; |
| 4 | +import type { |
| 5 | + WithdrawalWaitingTimeByAmountResponse, |
| 6 | + WithdrawalWaitingTimeRequestInfo, |
| 7 | + WithdrawalWaitingTimeByAmountParams, |
| 8 | + WithdrawalWaitingTimeByRequestIdsParams, |
| 9 | + WqApiCustomUrlGetter, |
| 10 | +} from './types.js'; |
| 11 | +import { ERROR_CODE, WQ_API_URLS } from '../common/index.js'; |
| 12 | +import { formatEther } from 'viem'; |
| 13 | + |
| 14 | +const endpoints = { |
| 15 | + calculateByAmount: '/v2/request-time/calculate', |
| 16 | + calculateByRequestId: '/v2/request-time', |
| 17 | +}; |
| 18 | + |
| 19 | +export class LidoSDKWithdrawWaitingTime extends BusModule { |
| 20 | + // API call integrations |
| 21 | + @Logger('API:') |
| 22 | + @ErrorHandler() |
| 23 | + public async getWithdrawalWaitingTimeByAmount( |
| 24 | + props: WithdrawalWaitingTimeByAmountParams, |
| 25 | + ): Promise<WithdrawalWaitingTimeByAmountResponse> { |
| 26 | + const getCustomApiUrl = props?.getCustomApiUrl; |
| 27 | + |
| 28 | + const query = new URLSearchParams(); |
| 29 | + if (props.amount) { |
| 30 | + query.set('amount', formatEther(props.amount)); |
| 31 | + } |
| 32 | + |
| 33 | + const baseUrl = this.getBaseUrl(getCustomApiUrl); |
| 34 | + const url = `${baseUrl}${endpoints.calculateByAmount}?${query.toString()}`; |
| 35 | + |
| 36 | + const response = await fetch(url, { |
| 37 | + headers: { |
| 38 | + 'WQ-Request-Source': 'sdk', |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + return response.json(); |
| 43 | + } |
| 44 | + |
| 45 | + @Logger('API:') |
| 46 | + @ErrorHandler() |
| 47 | + public async getWithdrawalWaitingTimeByRequestIds( |
| 48 | + props: WithdrawalWaitingTimeByRequestIdsParams, |
| 49 | + ): Promise<readonly WithdrawalWaitingTimeRequestInfo[]> { |
| 50 | + const requestDelay = props?.requestDelay ?? 1000; |
| 51 | + const getCustomApiUrl = props?.getCustomApiUrl; |
| 52 | + |
| 53 | + if (!Array.isArray(props.ids) || props.ids.length === 0) { |
| 54 | + throw this.bus.core.error({ |
| 55 | + code: ERROR_CODE.INVALID_ARGUMENT, |
| 56 | + message: 'expected not empty array ids', |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + const idsPages = []; |
| 61 | + const pageSize = 20; |
| 62 | + const baseUrl = this.getBaseUrl(getCustomApiUrl); |
| 63 | + const path = `${baseUrl}${endpoints.calculateByRequestId}`; |
| 64 | + |
| 65 | + for (let i = 0; i < props.ids.length; i += pageSize) { |
| 66 | + idsPages.push(props.ids.slice(i, i + pageSize)); |
| 67 | + } |
| 68 | + |
| 69 | + const result = []; |
| 70 | + |
| 71 | + for (const page of idsPages) { |
| 72 | + const query = new URLSearchParams(); |
| 73 | + query.set('ids', page.toString()); |
| 74 | + |
| 75 | + const url = `${path}?${query.toString()}`; |
| 76 | + |
| 77 | + const response = await fetch(url, { |
| 78 | + headers: { |
| 79 | + 'WQ-Request-Source': 'sdk', |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const requests = await response.json(); |
| 84 | + result.push(...requests); |
| 85 | + |
| 86 | + if (idsPages.length > 1) { |
| 87 | + // avoid backend spam |
| 88 | + await new Promise((resolve) => setTimeout(resolve, requestDelay)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return result; |
| 93 | + } |
| 94 | + |
| 95 | + getBaseUrl(getCustomApiUrl?: WqApiCustomUrlGetter) { |
| 96 | + const defaultUrl = WQ_API_URLS[this.bus.core.chainId]; |
| 97 | + |
| 98 | + const baseUrl = |
| 99 | + getCustomApiUrl && typeof getCustomApiUrl === 'function' |
| 100 | + ? getCustomApiUrl(defaultUrl, this.bus.core.chainId) |
| 101 | + : defaultUrl; |
| 102 | + |
| 103 | + if (!baseUrl) { |
| 104 | + throw this.bus.core.error({ |
| 105 | + code: ERROR_CODE.INVALID_ARGUMENT, |
| 106 | + message: `wq-api URL is not found for chain ${this.bus.core.chainId}, use getCustomApiUrl prop to setup custom URL`, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + return baseUrl; |
| 111 | + } |
| 112 | +} |
0 commit comments