Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rest-api): Adds validSwap validation [SLT-321] #3247

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
abtestingalpha marked this conversation as resolved.
Show resolved Hide resolved

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

return fromTokenInfo.swappable.includes(toToken)
abtestingalpha marked this conversation as resolved.
Show resolved Hide resolved
}
Loading