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(synapse-interface): Reorders validation to check existence first #3156

Merged
merged 2 commits into from
Sep 19, 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
12 changes: 6 additions & 6 deletions packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ router.get(
checksumAddresses(['fromToken', 'toToken']),
[
check('fromChain')
.exists()
.withMessage('fromChain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported fromChain')
.exists()
.withMessage('fromChain is required'),
.withMessage('Unsupported fromChain'),
check('toChain')
.exists()
.withMessage('toChain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported toChain')
.exists()
.withMessage('toChain is required'),
.withMessage('Unsupported toChain'),
check('fromToken')
.exists()
.withMessage('fromToken is required')
Expand Down
14 changes: 7 additions & 7 deletions packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ router.get(
checksumAddresses(['fromToken', 'toToken']),
[
check('fromChain')
.exists()
.withMessage('fromChain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported fromChain')
.exists()
.withMessage('fromChain is required'),
.withMessage('Unsupported fromChain'),
check('toChain')
.exists()
.withMessage('toChain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported toChain')
.exists()
.withMessage('toChain is required'),
.withMessage('Unsupported toChain'),
check('fromToken')
.exists()
.withMessage('fromToken is required')
Expand All @@ -45,7 +45,7 @@ router.get(
isTokenSupportedOnChain(value, req.query.toChain as string)
)
.withMessage('Token not supported on specified chain'),
check('amount').isNumeric().exists().withMessage('amount is required'),
check('amount').exists().withMessage('amount is required').isNumeric(),
check('destAddress')
.exists()
.withMessage('destAddress is required')
Expand Down
16 changes: 8 additions & 8 deletions packages/rest-api/src/routes/getBridgeTxStatusRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ router.get(
'/',
[
check('destChainId')
.exists()
.withMessage('destChainId is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported destChainId')
.exists()
.withMessage('destChainId is required'),
.withMessage('Unsupported destChainId'),
check('bridgeModule')
.exists()
.withMessage('bridgeModule is required')
.isString()
.isIn(VALID_BRIDGE_MODULES)
.withMessage(
'Invalid bridge module. Must be one of: ' +
VALID_BRIDGE_MODULES.join(', ')
)
.exists()
.withMessage('bridgeModule is required'),
),
check('synapseTxId')
.isString()
.exists()
.withMessage('synapseTxId is required'),
.withMessage('synapseTxId is required')
.isString(),
],
showFirstValidationError,
getBridgeTxStatusController
Expand Down
6 changes: 3 additions & 3 deletions packages/rest-api/src/routes/getDestinationTxRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ router.get(
'/',
[
check('originChainId')
.isNumeric()
.exists()
.withMessage('originChainId is required'),
check('txHash').isString().exists().withMessage('txHash is required'),
.withMessage('originChainId is required')
.isNumeric(),
check('txHash').exists().withMessage('txHash is required').isString(),
],
showFirstValidationError,
getDestinationTxController
Expand Down
12 changes: 6 additions & 6 deletions packages/rest-api/src/routes/getSynapseTxIdRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ router.get(
'/',
[
check('originChainId')
.isNumeric()
.exists()
.withMessage('originChainId is required'),
.withMessage('originChainId is required')
.isNumeric(),
check('bridgeModule')
.exists()
.withMessage('bridgeModule is required')
.isString()
.isIn(VALID_BRIDGE_MODULES)
.withMessage(
'Invalid bridge module. Must be one of: ' +
VALID_BRIDGE_MODULES.join(', ')
)
.exists()
.withMessage('bridgeModule is required'),
check('txHash').isString().exists().withMessage('txHash is required'),
),
check('txHash').exists().withMessage('txHash is required').isString(),
],
showFirstValidationError,
getSynapseTxIdController
Expand Down
8 changes: 4 additions & 4 deletions packages/rest-api/src/routes/swapRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ router.get(
checksumAddresses(['fromToken', 'toToken']),
[
check('chain')
.exists()
.withMessage('chain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported chain')
.exists()
.withMessage('chain is required'),
.withMessage('Unsupported chain'),
check('fromToken')
.exists()
.withMessage('fromToken is required')
Expand All @@ -38,7 +38,7 @@ router.get(
isTokenSupportedOnChain(value, req.query.chain as string)
)
.withMessage('Token not supported on specified chain'),
check('amount').isNumeric().exists().withMessage('amount is required'),
check('amount').exists().withMessage('amount is required').isNumeric(),
],
showFirstValidationError,
swapController
Expand Down
6 changes: 3 additions & 3 deletions packages/rest-api/src/routes/swapTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ router.get(
checksumAddresses(['fromToken', 'toToken']),
[
check('chain')
.exists()
.withMessage('chain is required')
.isNumeric()
.custom((value) => CHAINS_ARRAY.some((c) => c.id === Number(value)))
.withMessage('Unsupported chain')
.exists()
.withMessage('chain is required'),
.withMessage('Unsupported chain'),
check('fromToken')
.exists()
.withMessage('fromToken is required')
Expand Down
25 changes: 20 additions & 5 deletions packages/rest-api/src/utils/bridgeRouteMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,27 @@ const transformPair = (string: string): any => {
}
}

const transformBridgeRouteValues = (routes: StringifiedBridgeRoutes) => {
const transformBridgeRouteValues = (
routes: StringifiedBridgeRoutes
): TransformedBridgeRoutes => {
return Object.fromEntries(
Object.entries(routes).map(([key, values]) => [
key,
values.map(transformPair).filter((pair) => pair !== undefined),
])
Object.entries(routes).map(([key, values]) => {
const uniquePairs: TokenData[] = []
values.forEach((pairStr) => {
const transformedPair = transformPair(pairStr)
if (
transformedPair &&
!uniquePairs.some(
(pair) =>
pair.symbol === transformedPair.symbol &&
pair.chainId === transformedPair.chainId
)
) {
uniquePairs.push(transformedPair)
}
})
return [key, uniquePairs]
})
)
}

Expand Down
Loading