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

REST API Improvements [SLT-179] #3133

Merged
merged 7 commits into from
Sep 18, 2024
Merged
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/rest-api/src/controllers/swapTxInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const swapTxInfoController = async (req, res) => {
}

try {
const { chain, amount } = req.query
const { chain, amount, address } = req.query
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip

Codebase Verification

Add validation for the address parameter in the route setup

The address parameter has been correctly implemented in the swapTxInfoController function, which enhances the flexibility of the swap operation as intended. However, there's a potential issue in the route setup:

  • The route validation includes checks for chain, fromToken, and toToken, but there's no validation for the newly added address parameter.

To ensure data integrity and security, please add appropriate validation for the address parameter in the route setup. This should include:

  • Checking if the address is provided (required field)
  • Validating the format of the address (e.g., using a regex pattern for Ethereum addresses)

Example:

check('address')
  .exists().withMessage('address is required')
  .matches(/^0x[a-fA-F0-9]{40}$/).withMessage('Invalid Ethereum address format')

Adding this validation will prevent potential issues caused by missing or invalid address inputs.

Analysis chain

Verify the function usage across the codebase.

The addition of the address parameter enhances the flexibility of the swapTxInfoController function by allowing the caller to specify the address from which tokens are being swapped. This is a positive change.

However, it's crucial to ensure that all the callers of this function are updated to pass the address parameter to avoid runtime exceptions.

Run the following script to verify the function usage:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `swapTxInfoController` pass the `address` parameter.

# Test: Search for the function usage. Expect: Occurrences of the function call with the `address` parameter.
rg --type typescript -A 5 $'swapTxInfoController'

Length of output: 83


Script:

#!/bin/bash
# Description: Find the definition and usage of swapTxInfoController function

# Find the function definition
echo "Function Definition:"
fd -e ts -e tsx | xargs rg -i "function\s+swapTxInfoController" -A 10

echo "\nFunction Usage:"
# Search for function usage
fd -e ts -e tsx | xargs rg -i "swapTxInfoController" -A 5

Length of output: 1424


Script:

#!/bin/bash
# Description: Inspect the swapTxInfoController function definition and route setup

# View the full swapTxInfoController function definition
echo "Full swapTxInfoController Definition:"
rg -A 20 "export const swapTxInfoController" packages/rest-api/src/controllers/swapTxInfoController.ts

echo "\nRoute Setup and Validations:"
# Check for query parameter validations in the route file
rg -A 10 "router.get" packages/rest-api/src/routes/swapTxInfoRoute.ts

Length of output: 1289

const fromTokenInfo = res.locals.tokenInfo.fromToken
const toTokenInfo = res.locals.tokenInfo.toToken

Expand All @@ -25,7 +25,7 @@ export const swapTxInfoController = async (req, res) => {

const txInfo = await Synapse.swap(
Number(chain),
fromTokenInfo.address,
address,
toTokenInfo.address,
amountInWei,
quote.query
Expand Down
Loading