Skip to content

Commit

Permalink
Adds validateRouteExists validation
Browse files Browse the repository at this point in the history
  • Loading branch information
abtestingalpha committed Sep 24, 2024
1 parent 67a04cd commit 1a5f4a0
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 26 deletions.
8 changes: 8 additions & 0 deletions packages/rest-api/src/routes/bridgeLimitsRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isTokenSupportedOnChain } from './../utils/isTokenSupportedOnChain'
import { isTokenAddress } from '../utils/isTokenAddress'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { validateRouteExists } from '../validations/validateRouteExists'

const router = express.Router()

Expand Down Expand Up @@ -130,6 +131,13 @@ router.get(
isTokenSupportedOnChain(value, req.query.toChain as string)
)
.withMessage('Token not supported on specified chain'),
check()
.custom((_value, { req }) => {
const { fromChain, toChain, fromToken, toToken } = req.query

return validateRouteExists(fromChain, fromToken, toChain, toToken)
})
.withMessage('No valid route exists for the chain/token combination'),
],
showFirstValidationError,
bridgeLimitsController
Expand Down
8 changes: 8 additions & 0 deletions packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { bridgeController } from '../controllers/bridgeController'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'
import { validateRouteExists } from '../validations/validateRouteExists'

const router = express.Router()

Expand Down Expand Up @@ -222,6 +223,13 @@ router.get(
)
.withMessage('Token not supported on specified chain'),
check('amount').isNumeric().exists().withMessage('amount is required'),
check()
.custom((_value, { req }) => {
const { fromChain, toChain, fromToken, toToken } = req.query

return validateRouteExists(fromChain, fromToken, toChain, toToken)
})
.withMessage('No valid route exists for the chain/token combination'),
],
showFirstValidationError,
bridgeController
Expand Down
15 changes: 15 additions & 0 deletions packages/rest-api/src/tests/bridgeLimitsRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import express from 'express'

import bridgeLimitsRoute from '../routes/bridgeLimitsRoute'
import { USDC, ETH } from '../constants/bridgeable'
import { NativeGasAddress } from '../constants'

const app = express()
app.use('/bridgeLimits', bridgeLimitsRoute)
Expand Down Expand Up @@ -34,6 +35,20 @@ describe('Get Bridge Limits Route', () => {
expect(response.body).toHaveProperty('minOriginAmount')
}, 10_000)

it('should return 400 for unsupported route', async () => {
const response = await request(app).get('/bridgeLimits').query({
fromChain: '1',
toChain: '10',
fromToken: NativeGasAddress,
toToken: USDC.addresses[10],
})
expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'No valid route exists for the chain/token combination'
)
}, 10_000)

it('should return 400 for unsupported fromChain', async () => {
const response = await request(app).get('/bridgeLimits').query({
fromChain: '999',
Expand Down
17 changes: 17 additions & 0 deletions packages/rest-api/src/tests/bridgeRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('Bridge Route with Real Synapse Service', () => {
toToken: USDC.addresses[10],
amount: '1000',
})

expect(response.status).toBe(200)
expect(Array.isArray(response.body)).toBe(true)
expect(response.body.length).toBeGreaterThan(0)
Expand Down Expand Up @@ -55,6 +56,22 @@ describe('Bridge Route with Real Synapse Service', () => {
expect(response.body[0]).toHaveProperty('bridgeFeeFormatted')
}, 15000)

it('should return 400 for unsupported route', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: NativeGasAddress,
toToken: USDC.addresses[10],
amount: '10',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'No valid route exists for the chain/token combination'
)
}, 15000)

it('should return 400 for unsupported fromChain, with error message', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '999',
Expand Down
10 changes: 6 additions & 4 deletions packages/rest-api/src/utils/bridgeRouteMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TransformedBridgeRoutes = Record<string, TokenData[]>
const constructJSON = (
swappableMap,
exclusionList
): TransformedBridgeRoutes => {
): StringifiedBridgeRoutes => {
const result = {}

// Iterate through the chains
Expand Down Expand Up @@ -56,8 +56,7 @@ const constructJSON = (
}
}
}

return transformBridgeRouteValues(result)
return result
}

const transformPair = (string: string): any => {
Expand Down Expand Up @@ -97,4 +96,7 @@ const transformBridgeRouteValues = (
)
}

export const BRIDGE_ROUTE_MAPPING = constructJSON(BRIDGE_MAP, [])
export const BRIDGE_ROUTE_MAPPING_SYMBOLS = constructJSON(BRIDGE_MAP, [])
export const BRIDGE_ROUTE_MAPPING = transformBridgeRouteValues(
BRIDGE_ROUTE_MAPPING_SYMBOLS
)
20 changes: 20 additions & 0 deletions packages/rest-api/src/validations/validateRouteExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { BRIDGE_ROUTE_MAPPING_SYMBOLS } from '../utils/bridgeRouteMapping'

export const validateRouteExists = (fromChain, fromToken, toChain, toToken) => {
const fromTokenInfo = tokenAddressToToken(fromChain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(toChain.toString(), toToken)

if (!fromTokenInfo || !toTokenInfo) {
return false
}

const key = `${fromTokenInfo.symbol}-${fromChain}`
const routes = BRIDGE_ROUTE_MAPPING_SYMBOLS[key]

if (!routes) {
return false
}

return routes.includes(`${toTokenInfo.symbol}-${toChain}`)
}
22 changes: 0 additions & 22 deletions packages/rest-api/src/validations/validateTokens.ts

This file was deleted.

0 comments on commit 1a5f4a0

Please sign in to comment.