Skip to content

Commit

Permalink
feat(rest-api): API gateway for rfq-indexer API and rfq API + swagger…
Browse files Browse the repository at this point in the history
… merge [SLT-339] (#3323)

Co-authored-by: defi-moses <[email protected]>
  • Loading branch information
golangisfun123 and Defi-Moses authored Oct 29, 2024
1 parent e590bd9 commit 6bdb316
Show file tree
Hide file tree
Showing 26 changed files with 4,349 additions and 1,161 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"editor.defaultFormatter": "JuanBlanco.solidity"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand All @@ -26,4 +26,4 @@
".*Class.*",
".*Properties.*",
]
}
}
2 changes: 1 addition & 1 deletion docs/bridge/docs/04-Routers/RFQ/API/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The RFQ API expects the signatures to have V values as 0/1 rather than 27/28. Th

### API Urls

- Mainnet: `rfq-api.omnirpc.io`
- Mainnet: `api.synapseprotocol.com/quotes`
- Testnet: `rfq-api-testnet.omnirpc.io`


Expand Down
2 changes: 1 addition & 1 deletion docs/bridge/docs/06-Services/06-RFQ-Indexer-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The RFQ Indexer API is a service designed to provide access to indexed RFQ bridg

## API-docs

[`https://triumphant-magic-production.up.railway.app/api-docs/`](https://triumphant-magic-production.up.railway.app/api-docs/)
[`api.synapseprotocol.com/api-docs/`](https://api.synapseprotocol.com/api-docs/)

## Key Features
1. **Real-Time and Historical Data** Indexes from a specified start block up to real-time events.
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
"copyfiles": "^2.3.0",
"depcheck": "^1.4.3",
"doctoc": "^2.2.0",
"eslint": "^8.16.0",
"eslint": "^8.53.0",
"eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^16.0.3",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^35.1.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^5.1.0",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-unicorn": "^42.0.0",
"lerna": "^4.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"ethers": "5.7.2",
"express": "^4.18.2",
"express-validator": "^7.2.0",
"http-proxy-middleware": "^3.0.3",
"jest": "^29.7.0",
"lodash": "^4.17.21",
"supertest": "^6.3.3",
Expand All @@ -48,6 +49,7 @@
"lodash": "^4.17.21",
"nodemon": "^3.0.1",
"swagger-jsdoc": "^6.2.8",
"swagger-merge": "^0.4.0",
"swagger-ui-express": "^5.0.1",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2"
Expand Down
17 changes: 15 additions & 2 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import swaggerUi from 'swagger-ui-express'
import { specs } from './swagger'
import routes from './routes'
import { logger } from './middleware/logger'
import {
isRFQAPIRequest,
isRFQIndexerRequest,
rfqApiProxy,
rfqIndexerProxy,
} from './utils/isGatewayRoute'

const app = express()
const port = process.env.PORT || 3000
Expand All @@ -20,7 +26,6 @@ app.use((req, res, next) => {
})

const originalPath = req.path

const originalJson = res.json
res.json = function (body) {
logger.info({
Expand All @@ -36,7 +41,15 @@ app.use((req, res, next) => {
return originalJson.call(this, body)
}

next()
if (isRFQAPIRequest(originalPath)) {
return rfqApiProxy(req, res, next)
}

if (isRFQIndexerRequest(originalPath)) {
return rfqIndexerProxy(req, res, next)
}

return next()
})

app.listen(port, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/bridgeLimitsRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenA
import { checksumAddresses } from '../middleware/checksumAddresses'
import { validateRouteExists } from '../validations/validateRouteExists'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'
import { validateRouteExists } from '../validations/validateRouteExists'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'
import { validateRouteExists } from '../validations/validateRouteExists'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/bridgeTxStatusRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CHAINS_ARRAY } from '../constants/chains'
import { VALID_BRIDGE_MODULES } from '../constants'
import { validateKappa } from '../validations/validateKappa'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/destinationTokensRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/destinationTxRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { check } from 'express-validator'
import { showFirstValidationError } from '../middleware/showFirstValidationError'
import { destinationTxController } from '../controllers/destinationTxController'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import tokenListRoute from './tokenListRoute'
import destinationTokensRoute from './destinationTokensRoute'
import bridgeLimitsRoute from './bridgeLimitsRoute'

const router = express.Router()
const router: express.Router = express.Router()

router.use('/', indexRoute)
router.use('/swap', swapRoute)
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/indexRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express'

import { indexController } from '../controllers/indexController'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/swapRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenA
import { validSwapTokens } from '../validations/validSwapTokens'
import { validSwapChain } from '../validations/validSwapChain'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/swapTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/synapseTxIdRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { showFirstValidationError } from '../middleware/showFirstValidationError
import { synapseTxIdController } from '../controllers/synapseTxIdController'
import { VALID_BRIDGE_MODULES } from '../constants'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-api/src/routes/tokenListRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express'

import { tokenListController } from '../controllers/tokenListController'

const router = express.Router()
const router: express.Router = express.Router()

/**
* @openapi
Expand Down
33 changes: 8 additions & 25 deletions packages/rest-api/src/swagger.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import swaggerJsdoc from 'swagger-jsdoc'
import fs from 'fs'
import path from 'path'

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require('../package.json')
// Define the path to your static swagger.json file
const swaggerFilePath = path.join(__dirname, '../swagger.json')

const isDevelopment = process.env.NODE_ENV === 'development'
const serverUrl = isDevelopment
? 'http://localhost:3000'
: 'https://api.synapseprotocol.com'
// Read the static swagger.json file
const specs = JSON.parse(fs.readFileSync(swaggerFilePath, 'utf8'))

const options: swaggerJsdoc.Options = {
definition: {
openapi: '3.0.0',
info: {
title: 'Synapse Protocol REST API',
version: packageJson.version,
description: 'API documentation for the Synapse Protocol REST API',
},
servers: [
{
url: serverUrl,
},
],
},
apis: ['./src/routes/*.ts', './src/*.ts'],
}

export const specs = swaggerJsdoc(options)
// Export the specs for use in your application
export { specs }
34 changes: 34 additions & 0 deletions packages/rest-api/src/utils/isGatewayRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createProxyMiddleware } from 'http-proxy-middleware'

export const isRFQIndexerRequest = (route: string): boolean => {
return (
route.includes('/conflicting-proofs') ||
route.includes('/disputes') ||
route.includes('/invalid-relaus') ||
route.includes('/pending-transactions') ||
route.includes('/refunded-and-relayed') ||
route.includes('/transaction-id')
)
}

export const isRFQAPIRequest = (route: string): boolean => {
return (
route.includes('/ack') ||
route.includes('/bulk_quotes') ||
route.includes('/contracts') ||
route.includes('/open_quote_requests') ||
route.includes('/quotes') ||
route.includes('/rfq') ||
route.includes('/rfq_stream')
)
}

export const rfqApiProxy = createProxyMiddleware({
target: 'https://rfq-api.omnirpc.io',
changeOrigin: true,
})

export const rfqIndexerProxy = createProxyMiddleware({
target: 'https://rfq-indexer.synapseprotocol.com/api',
changeOrigin: true,
})
Loading

0 comments on commit 6bdb316

Please sign in to comment.