Skip to content

Commit

Permalink
Adds route validation to bridgeTxInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
abtestingalpha committed Sep 24, 2024
1 parent 07a08f4 commit fe54496
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
8 changes: 8 additions & 0 deletions packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isTokenAddress } from '../utils/isTokenAddress'
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 @@ -163,6 +164,13 @@ router.get(
.withMessage('destAddress is required')
.custom((value) => isAddress(value))
.withMessage('Invalid destination address'),
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,
bridgeTxInfoController
Expand Down
27 changes: 22 additions & 5 deletions packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import express from 'express'

import bridgeTxInfoRoute from '../routes/bridgeTxInfoRoute'
import { USDC } from '../constants/bridgeable'
import { NativeGasAddress } from '../constants'

const app = express()
app.use('/bridgeTxInfo', bridgeTxInfoRoute)
Expand All @@ -28,6 +29,22 @@ describe('Bridge TX Info Route', () => {
)
}, 10_000)

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

it('should return 400 for unsupported fromChain', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '999',
Expand All @@ -42,7 +59,7 @@ describe('Bridge TX Info Route', () => {
'message',
'Unsupported fromChain'
)
}, 10_000)
})

it('should return 400 for invalid fromToken address', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
Expand All @@ -58,7 +75,7 @@ describe('Bridge TX Info Route', () => {
'message',
'Invalid fromToken address'
)
}, 10_000)
})

it('should return 400 for token not supported on specified chain', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
Expand All @@ -74,7 +91,7 @@ describe('Bridge TX Info Route', () => {
'message',
'Invalid fromToken address'
)
}, 10_000)
})

it('should return 400 for missing amount', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
Expand All @@ -86,7 +103,7 @@ describe('Bridge TX Info Route', () => {
})
expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty('field', 'amount')
}, 10_000)
})

it('should return 400 for invalid destAddress', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
Expand All @@ -102,5 +119,5 @@ describe('Bridge TX Info Route', () => {
'message',
'Invalid destination address'
)
}, 10_000)
})
})

0 comments on commit fe54496

Please sign in to comment.