Skip to content

Commit 1e550f9

Browse files
committed
ad providers endpoint
1 parent db509e9 commit 1e550f9

16 files changed

+131
-10
lines changed

src/api/routes/swap/swapModel.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ extendZodWithOpenApi(z)
88
export type Meta = z.infer<typeof metaSchema>
99
export type SwapResponseSingle = z.infer<typeof swapResponseSchemaSingle>
1010
export type SwapResponse = z.infer<typeof swapResponseSchema>
11+
export type ProvidersResponse = z.infer<typeof providersResponseSchema>
1112

1213
const addressSchema = z
1314
.string()
@@ -343,4 +344,26 @@ const swapResponseSchemaSingle = z.object({
343344

344345
const swapResponseSchema = z.array(swapResponseSchemaSingle)
345346

346-
export { getSwapSchema, swapResponseSchemaSingle, swapResponseSchema }
347+
const getProvidersSchema = z.object({
348+
query: z.object({
349+
chainId: z
350+
.string()
351+
.transform(Number)
352+
.pipe(z.number().int().positive())
353+
.openapi({ example: "1", param: { description: "Chain id" } }),
354+
}),
355+
})
356+
357+
const providersResponseSchema = z
358+
.array(z.string())
359+
.openapi({
360+
description: "Array of providers for use with `swapWithProvider` endpoint",
361+
})
362+
363+
export {
364+
getSwapSchema,
365+
swapResponseSchemaSingle,
366+
swapResponseSchema,
367+
getProvidersSchema,
368+
providersResponseSchema,
369+
}

src/api/routes/swap/swapRouter.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
validateRequest,
1010
} from "@/common/utils/httpHandlers"
1111
import { log, logProd, logRouteTime } from "@/common/utils/logs"
12-
import { findSwaps } from "@/swapService/runner"
12+
import { findSwaps, reflectProviders } from "@/swapService/runner"
1313
import type { SwapParams } from "@/swapService/types"
1414
import {
1515
ApiError,
@@ -21,9 +21,12 @@ import { StatusCodes } from "http-status-codes"
2121
import { InvalidAddressError } from "viem"
2222
import { z } from "zod"
2323
import {
24+
type ProvidersResponse,
2425
type SwapResponse,
2526
type SwapResponseSingle,
27+
getProvidersSchema,
2628
getSwapSchema,
29+
providersResponseSchema,
2730
swapResponseSchema,
2831
swapResponseSchemaSingle,
2932
} from "./swapModel"
@@ -49,6 +52,35 @@ swapRegistry.registerPath({
4952
responses: createApiResponse(swapResponseSchema, "Success"),
5053
})
5154

55+
swapRegistry.register("Providers", providersResponseSchema)
56+
swapRegistry.registerPath({
57+
method: "get",
58+
path: "/providers",
59+
tags: ["Get providers available for the queried chain"],
60+
request: { query: getProvidersSchema.shape.query },
61+
responses: createApiResponse(providersResponseSchema, "Success"),
62+
})
63+
64+
swapRouter.get(
65+
"/providers",
66+
validateRequest(getProvidersSchema),
67+
async (req: Request, res: Response) => {
68+
try {
69+
const {
70+
query: { chainId },
71+
} = getProvidersSchema.parse(req)
72+
const providers = await reflectProviders(chainId)
73+
74+
return handleServiceResponse(
75+
ServiceResponse.success<ProvidersResponse>(providers),
76+
res,
77+
)
78+
} catch (error) {
79+
return handleServiceResponse(createFailureResponse(req, error), res)
80+
}
81+
},
82+
)
83+
5284
swapRouter.get(
5385
"/swap",
5486
validateRequest(getSwapSchema),

src/swapService/runner.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import { strategies } from "./strategies/index"
1212
import type { StrategyResult, SwapParams } from "./types"
1313
import { ApiError, addInOutDeposits } from "./utils"
1414

15-
function loadPipeline(swapParams: SwapParams) {
15+
function loadPipeline(chainId: number, routingOverride?: ChainRoutingConfig) {
1616
let routing: ChainRoutingConfig
17-
if (swapParams.routingOverride) {
18-
routing = swapParams.routingOverride
17+
if (routingOverride) {
18+
routing = routingOverride
1919
} else {
20-
routing = getRoutingConfig(swapParams.chainId)
20+
routing = getRoutingConfig(chainId)
2121
if (!routing)
2222
throw new ApiError(
2323
StatusCodes.NOT_FOUND,
@@ -36,7 +36,7 @@ function loadPipeline(swapParams: SwapParams) {
3636
export async function runPipeline(
3737
swapParams: SwapParams,
3838
): Promise<SwapApiResponse[]> {
39-
const pipeline = loadPipeline(swapParams)
39+
const pipeline = loadPipeline(swapParams.chainId, swapParams.routingOverride)
4040

4141
const allResults: StrategyResult[] = []
4242
for (const strategy of pipeline) {
@@ -104,3 +104,15 @@ export async function findSwaps(swapParams: SwapParams) {
104104

105105
return quotes
106106
}
107+
108+
export async function reflectProviders(chainId: number) {
109+
const pipeline = loadPipeline(chainId)
110+
111+
return [
112+
...new Set(
113+
(
114+
await Promise.all(pipeline.map((strategy) => strategy.providers()))
115+
).flat(),
116+
),
117+
]
118+
}

src/swapService/strategies/strategyBalmySDK.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export class StrategyBalmySDK {
9595
config.sourcesFilter.includeSources.push(
9696
...allPendleAggregators.map((aggregator) => `pendle-${aggregator}`),
9797
)
98+
config.sourcesFilter.includeSources =
99+
config.sourcesFilter.includeSources.filter((s) => s !== "pendle")
98100
}
99101

100102
this.config = { ...defaultConfig, ...(config || {}) }
@@ -127,9 +129,9 @@ export class StrategyBalmySDK {
127129
"li-fi": {
128130
apiKey: String(process.env.LIFI_API_KEY),
129131
},
130-
pendle: {
131-
apiKey: String(process.env.PENDLE_API_KEY),
132-
},
132+
// pendle: {
133+
// apiKey: String(process.env.PENDLE_API_KEY),
134+
// },
133135
"open-ocean": {
134136
apiKey: String(process.env.OPENOCEAN_API_KEY),
135137
},
@@ -197,6 +199,10 @@ export class StrategyBalmySDK {
197199
)
198200
}
199201

202+
async providers(): Promise<string[]> {
203+
return this.config.sourcesFilter?.includeSources || []
204+
}
205+
200206
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
201207
const result: StrategyResult = {
202208
strategy: StrategyBalmySDK.name(),

src/swapService/strategies/strategyCombinedUniswap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export class StrategyCombinedUniswap {
5353
)
5454
}
5555

56+
async providers(): Promise<string[]> {
57+
return [] // relies on providers of underlying strategies
58+
}
59+
5660
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
5761
const result: StrategyResult = {
5862
strategy: StrategyCombinedUniswap.name(),

src/swapService/strategies/strategyConnect2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export class StrategyConnect2 {
4747
)
4848
}
4949

50+
async providers(): Promise<string[]> {
51+
return [] // TODO
52+
}
53+
5054
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
5155
const result: StrategyResult = {
5256
strategy: StrategyConnect2.name(),

src/swapService/strategies/strategyCurveLPNG.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export class StrategyCurveLPNG {
7575
)
7676
}
7777

78+
async providers(): Promise<string[]> {
79+
return ["custom"]
80+
}
81+
7882
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
7983
const result: StrategyResult = {
8084
strategy: StrategyCurveLPNG.name(),

src/swapService/strategies/strategyERC4626Wrapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ export class StrategyERC4626Wrapper {
273273
)
274274
}
275275

276+
async providers(): Promise<string[]> {
277+
return ["custom"]
278+
}
279+
276280
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
277281
const result: StrategyResult = {
278282
strategy: StrategyERC4626Wrapper.name(),

src/swapService/strategies/strategyElixir.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export class StrategyElixir {
7878
)
7979
}
8080

81+
async providers(): Promise<string[]> {
82+
return ["custom"]
83+
}
84+
8185
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
8286
const result: StrategyResult = {
8387
strategy: StrategyElixir.name(),

src/swapService/strategies/strategyIdleCDOTranche.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export class StrategyIdleCDOTranche {
7878
)
7979
}
8080

81+
async providers(): Promise<string[]> {
82+
return ["custom"]
83+
}
84+
8185
async findSwap(swapParams: SwapParams): Promise<StrategyResult> {
8286
const result: StrategyResult = {
8387
strategy: StrategyIdleCDOTranche.name(),

0 commit comments

Comments
 (0)