Skip to content

Commit

Permalink
fix(rest-api): Adds validSwap validation [SLT-321] (#3247)
Browse files Browse the repository at this point in the history
* Adds validSwap validation
  • Loading branch information
abtestingalpha authored Oct 9, 2024
1 parent aecc24c commit 0738536
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
8 changes: 8 additions & 0 deletions packages/rest-api/src/routes/swapRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { isTokenAddress } from '../utils/isTokenAddress'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'
import { validSwap } from '../validations/validSwap'

const router = express.Router()

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

return validSwap(chain, fromToken, toToken)
})
.withMessage('Swap not supported for given tokens'),
],
showFirstValidationError,
swapController
Expand Down
17 changes: 16 additions & 1 deletion packages/rest-api/src/tests/swapRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import express from 'express'

import swapRoute from '../routes/swapRoute'
import { NativeGasAddress, ZeroAddress } from '../constants'
import { DAI, NETH, USDC } from '../constants/bridgeable'
import { DAI, ETH, NETH, USDC } from '../constants/bridgeable'

const app = express()
app.use('/swap', swapRoute)
Expand Down Expand Up @@ -78,6 +78,21 @@ describe('Swap Route with Real Synapse Service', () => {
)
}, 10_000)

it('should return 400 for invalid fromToken + toToken combo', async () => {
const response = await request(app).get('/swap').query({
chain: '1',
fromToken: ETH.addresses[1],
toToken: USDC.addresses[1],
amount: '1000',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'Swap not supported for given tokens'
)
})

it('should return 400 for token not supported on specified chain', async () => {
const response = await request(app).get('/swap').query({
chain: '1',
Expand Down
1 change: 1 addition & 0 deletions packages/rest-api/src/utils/tokenAddressToToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export const tokenAddressToToken = (chain: string, tokenAddress: string) => {
address: tokenAddress,
symbol: tokenInfo.symbol,
decimals: tokenInfo.decimals,
swappable: tokenInfo.swappable,
}
}
16 changes: 16 additions & 0 deletions packages/rest-api/src/validations/validSwap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { tokenAddressToToken } from '../utils/tokenAddressToToken'

export const validSwap = (
chain: number | string,
fromToken: string,
toToken: string
) => {
const fromTokenInfo = tokenAddressToToken(chain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(chain.toString(), toToken)

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

return fromTokenInfo.swappable.includes(toToken)
}

0 comments on commit 0738536

Please sign in to comment.