@@ -8,88 +8,90 @@ import {
8
8
handleServiceResponse ,
9
9
validateRequest ,
10
10
} from "@/common/utils/httpHandlers"
11
- import { runPipeline } from "@/swapService/runner"
11
+ import { findSwaps } from "@/swapService/runner"
12
12
import type { SwapParams } from "@/swapService/types"
13
- import {
14
- ApiError ,
15
- addInOutDeposits ,
16
- findToken ,
17
- getSwapper ,
18
- } from "@/swapService/utils"
13
+ import { ApiError , findToken , getSwapper } from "@/swapService/utils"
19
14
import { StatusCodes } from "http-status-codes"
20
- import { InvalidAddressError , isHex } from "viem"
15
+ import { InvalidAddressError } from "viem"
21
16
import { z } from "zod"
22
17
import {
23
18
type SwapResponse ,
19
+ type SwapResponseSingle ,
24
20
getSwapSchema ,
25
21
swapResponseSchema ,
22
+ swapResponseSchemaSingle ,
26
23
} from "./swapModel"
27
24
28
25
export const swapRegistry = new OpenAPIRegistry ( )
29
26
export const swapRouter : Router = express . Router ( )
30
27
31
- swapRegistry . register ( "SwapQuote" , swapResponseSchema )
28
+ swapRegistry . register ( "SwapQuote" , swapResponseSchemaSingle )
32
29
swapRegistry . registerPath ( {
33
30
method : "get" ,
34
31
path : "/swap" ,
35
- tags : [ "Get swap quote" ] ,
32
+ tags : [ "Get the best swap quote" ] ,
33
+ request : { query : getSwapSchema . shape . query } ,
34
+ responses : createApiResponse ( swapResponseSchemaSingle , "Success" ) ,
35
+ } )
36
+
37
+ swapRegistry . register ( "SwapQuotes" , swapResponseSchema )
38
+ swapRegistry . registerPath ( {
39
+ method : "get" ,
40
+ path : "/swaps" ,
41
+ tags : [ "Get swap quotes ordered from best to worst" ] ,
36
42
request : { query : getSwapSchema . shape . query } ,
37
43
responses : createApiResponse ( swapResponseSchema , "Success" ) ,
38
44
} )
39
45
40
46
swapRouter . get (
41
- "/" ,
47
+ "/swap " ,
42
48
validateRequest ( getSwapSchema ) ,
43
49
async ( req : Request , res : Response ) => {
44
- const serviceResponse = await findSwap ( req )
45
- console . log ( "===== END =====" )
46
- return handleServiceResponse ( serviceResponse , res )
50
+ try {
51
+ const swaps = await findSwaps ( parseRequest ( req ) )
52
+ return handleServiceResponse (
53
+ ServiceResponse . success < SwapResponseSingle > ( swaps [ 0 ] ) ,
54
+ res ,
55
+ )
56
+ } catch ( error ) {
57
+ return handleServiceResponse ( createFailureResponse ( req , error ) , res )
58
+ } finally {
59
+ console . log ( "===== END =====" )
60
+ }
47
61
} ,
48
62
)
49
63
50
- async function findSwap (
51
- req : Request ,
52
- ) : Promise < ServiceResponse < SwapResponse | null > > {
53
- try {
54
- const swapParams = parseRequest ( req )
55
-
56
- let data = await runPipeline ( swapParams )
57
-
58
- // GLOBAL CHECKS
59
- data = addInOutDeposits ( swapParams , data )
60
-
61
- // make sure verify item includes at least a function selector
62
- if (
63
- ! isHex ( data . verify . verifierData ) ||
64
- data . verify . verifierData . length < 10
65
- )
66
- throw new ApiError (
67
- StatusCodes . INTERNAL_SERVER_ERROR ,
68
- "Verifier transaction is empty" ,
69
- )
70
-
71
- return ServiceResponse . success < SwapResponse > ( data )
72
- } catch ( error ) {
73
- console . log (
74
- "error: " ,
75
- error . statusCode ,
76
- error . message ,
77
- error . errorMessage ,
78
- JSON . stringify ( error . data ) ,
79
- req . url ,
80
- )
81
- if ( error instanceof ApiError ) {
82
- return ServiceResponse . failure (
83
- error . message ,
84
- error . statusCode ,
85
- error . data ,
64
+ swapRouter . get (
65
+ "/swaps" ,
66
+ validateRequest ( getSwapSchema ) ,
67
+ async ( req : Request , res : Response ) => {
68
+ try {
69
+ const swaps = await findSwaps ( parseRequest ( req ) )
70
+ return handleServiceResponse (
71
+ ServiceResponse . success < SwapResponse > ( swaps ) ,
72
+ res ,
86
73
)
74
+ } catch ( error ) {
75
+ return handleServiceResponse ( createFailureResponse ( req , error ) , res )
76
+ } finally {
77
+ console . log ( "===== END =====" )
87
78
}
88
- return ServiceResponse . failure (
89
- `${ error } ` ,
90
- StatusCodes . INTERNAL_SERVER_ERROR ,
91
- )
79
+ } ,
80
+ )
81
+
82
+ function createFailureResponse ( req : Request , error : any ) {
83
+ console . log (
84
+ "error: " ,
85
+ error . statusCode ,
86
+ error . message ,
87
+ error . errorMessage ,
88
+ JSON . stringify ( error . data ) ,
89
+ req . url ,
90
+ )
91
+ if ( error instanceof ApiError ) {
92
+ return ServiceResponse . failure ( error . message , error . statusCode , error . data )
92
93
}
94
+ return ServiceResponse . failure ( `${ error } ` , StatusCodes . INTERNAL_SERVER_ERROR )
93
95
}
94
96
95
97
function parseRequest ( request : Request ) : SwapParams {
0 commit comments