Skip to content

Commit

Permalink
migrating to using token addresses instead of symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Defi-Moses committed Sep 16, 2024
1 parent e2bab25 commit d35d50a
Show file tree
Hide file tree
Showing 15 changed files with 229 additions and 100 deletions.
23 changes: 18 additions & 5 deletions packages/rest-api/src/controllers/bridgeController.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
import { validationResult } from 'express-validator'
import { parseUnits } from '@ethersproject/units'
import { isAddress } from 'ethers/lib/utils'

import { formatBNToString } from '../utils/formatBNToString'
import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'

export const bridgeController = async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
try {
const { fromChain, toChain, amount } = req.query
const fromTokenInfo = res.locals.tokenInfo.fromToken
const toTokenInfo = res.locals.tokenInfo.toToken
const { fromChain, toChain, amount, fromToken, toToken } = req.query

if (!isAddress(fromToken) || !isAddress(toToken)) {
return res.status(400).json({ error: 'Invalid token address' })
}

const fromTokenInfo = tokenAddressToToken(fromChain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(toChain.toString(), toToken)

if (!fromTokenInfo || !toTokenInfo) {
return res
.status(400)
.json({ error: 'Token not supported on specified chain' })
}

const amountInWei = parseUnits(amount.toString(), fromTokenInfo.decimals)

const resp = await Synapse.allBridgeQuotes(
Number(fromChain),
Number(toChain),
fromTokenInfo.address,
toTokenInfo.address,
fromToken,
toToken,
amountInWei
)
const payload = resp.map((quote) => ({
Expand Down
25 changes: 19 additions & 6 deletions packages/rest-api/src/controllers/bridgeTxInfoController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { validationResult } from 'express-validator'
import { parseUnits } from '@ethersproject/units'
import { isAddress } from 'ethers/lib/utils'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'

export const bridgeTxInfoController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -10,17 +12,28 @@ export const bridgeTxInfoController = async (req, res) => {
}

try {
const { fromChain, toChain, amount, destAddress } = req.query
const fromTokenInfo = res.locals.tokenInfo.fromToken
const toTokenInfo = res.locals.tokenInfo.toToken
const { fromChain, toChain, amount, destAddress, fromToken, toToken } = req.query

Check warning on line 15 in packages/rest-api/src/controllers/bridgeTxInfoController.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·····`

Check warning on line 15 in packages/rest-api/src/controllers/bridgeTxInfoController.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·····`

if (!isAddress(fromToken) || !isAddress(toToken)) {
return res.status(400).json({ error: 'Invalid token address' })
}

const fromTokenInfo = tokenAddressToToken(fromChain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(toChain.toString(), toToken)

if (!fromTokenInfo || !toTokenInfo) {
return res
.status(400)
.json({ error: 'Token not supported on specified chain' })
}

const amountInWei = parseUnits(amount.toString(), fromTokenInfo.decimals)

const quotes = await Synapse.allBridgeQuotes(
Number(fromChain),
Number(toChain),
fromTokenInfo.address,
toTokenInfo.address,
fromToken,
toToken,
amountInWei
)

Expand All @@ -31,7 +44,7 @@ export const bridgeTxInfoController = async (req, res) => {
quote.routerAddress,
Number(fromChain),
Number(toChain),
fromTokenInfo.address,
fromToken,
amountInWei,
quote.originQuery,
quote.destQuery
Expand Down
23 changes: 18 additions & 5 deletions packages/rest-api/src/controllers/swapController.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
import { validationResult } from 'express-validator'
import { formatUnits, parseUnits } from '@ethersproject/units'
import { isAddress } from 'ethers/lib/utils'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'

export const swapController = async (req, res) => {
const errors = validationResult(req)
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() })
}
try {
const { chain, amount } = req.query
const fromTokenInfo = res.locals.tokenInfo.fromToken
const toTokenInfo = res.locals.tokenInfo.toToken
const { chain, amount, fromToken, toToken } = req.query

if (!isAddress(fromToken) || !isAddress(toToken)) {
return res.status(400).json({ error: 'Invalid token address' })
}

const fromTokenInfo = tokenAddressToToken(chain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(chain.toString(), toToken)

if (!fromTokenInfo || !toTokenInfo) {
return res
.status(400)
.json({ error: 'Token not supported on specified chain' })
}

const amountInWei = parseUnits(amount.toString(), fromTokenInfo.decimals)
const quote = await Synapse.swapQuote(
Number(chain),
fromTokenInfo.address,
toTokenInfo.address,
fromToken,
toToken,
amountInWei
)
res.json({
Expand Down
25 changes: 19 additions & 6 deletions packages/rest-api/src/controllers/swapTxInfoController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { validationResult } from 'express-validator'
import { parseUnits } from '@ethersproject/units'
import { isAddress } from 'ethers/lib/utils'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'

export const swapTxInfoController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -10,23 +12,34 @@ export const swapTxInfoController = async (req, res) => {
}

try {
const { chain, amount, address } = req.query
const fromTokenInfo = res.locals.tokenInfo.fromToken
const toTokenInfo = res.locals.tokenInfo.toToken
const { chain, amount, address, fromToken, toToken } = req.query

if (!isAddress(fromToken) || !isAddress(toToken)) {
return res.status(400).json({ error: 'Invalid token address' })
}

const fromTokenInfo = tokenAddressToToken(chain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(chain.toString(), toToken)

if (!fromTokenInfo || !toTokenInfo) {
return res
.status(400)
.json({ error: 'Token not supported on specified chain' })
}

const amountInWei = parseUnits(amount.toString(), fromTokenInfo.decimals)

const quote = await Synapse.swapQuote(
Number(chain),
fromTokenInfo.address,
toTokenInfo.address,
fromToken,
toToken,
amountInWei
)

const txInfo = await Synapse.swap(
Number(chain),
address,
toTokenInfo.address,
toToken,
amountInWei,
quote.query
)
Expand Down
16 changes: 12 additions & 4 deletions packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from 'express'
import { check } from 'express-validator'
import { isAddress } from 'ethers/lib/utils'

import { CHAINS_ARRAY } from '../constants/chains'
import { validateTokens } from '../validations/validateTokens'
import { showFirstValidationError } from '../middleware/showFirstValidationError'
import { bridgeController } from '../controllers/bridgeController'

Expand All @@ -23,9 +23,17 @@ router.get(
.withMessage('Unsupported toChain')
.exists()
.withMessage('toChain is required'),
validateTokens('fromChain', 'fromToken', 'fromToken'),
validateTokens('toChain', 'toToken', 'toToken'),
check('amount').isNumeric(),
check('fromToken')
.exists()
.withMessage('fromToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid fromToken address'),
check('toToken')
.exists()
.withMessage('toToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid toToken address'),
check('amount').isNumeric().exists().withMessage('amount is required'),
],
showFirstValidationError,
bridgeController
Expand Down
22 changes: 17 additions & 5 deletions packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from 'express'
import { check } from 'express-validator'
import { isAddress } from 'ethers/lib/utils'

import { CHAINS_ARRAY } from '../constants/chains'
import { validateTokens } from '../validations/validateTokens'
import { showFirstValidationError } from '../middleware/showFirstValidationError'
import { bridgeTxInfoController } from '../controllers/bridgeTxInfoController'

Expand All @@ -23,10 +23,22 @@ router.get(
.withMessage('Unsupported toChain')
.exists()
.withMessage('toChain is required'),
validateTokens('fromChain', 'fromToken', 'fromToken'),
validateTokens('toChain', 'toToken', 'toToken'),
check('amount').isNumeric(),
check('destAddress').isString(),
check('fromToken')
.exists()
.withMessage('fromToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid fromToken address'),
check('toToken')
.exists()
.withMessage('toToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid toToken address'),
check('amount').isNumeric().exists().withMessage('amount is required'),
check('destAddress')
.exists()
.withMessage('destAddress is required')
.custom((value) => isAddress(value))
.withMessage('Invalid destination address'),
],
showFirstValidationError,
bridgeTxInfoController
Expand Down
14 changes: 11 additions & 3 deletions packages/rest-api/src/routes/swapRoute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express'
import { check } from 'express-validator'
import { isAddress } from 'ethers/lib/utils'

import { validateTokens } from '../validations/validateTokens'
import { showFirstValidationError } from '../middleware/showFirstValidationError'
import { swapController } from '../controllers/swapController'
import { CHAINS_ARRAY } from '../constants/chains'
Expand All @@ -17,8 +17,16 @@ router.get(
.withMessage('Unsupported chain')
.exists()
.withMessage('chain is required'),
validateTokens('chain', 'fromToken', 'fromToken'),
validateTokens('chain', 'toToken', 'toToken'),
check('fromToken')
.exists()
.withMessage('fromToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid fromToken address'),
check('toToken')
.exists()
.withMessage('toToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid toToken address'),
check('amount').isNumeric().exists().withMessage('amount is required'),
],
showFirstValidationError,
Expand Down
21 changes: 17 additions & 4 deletions packages/rest-api/src/routes/swapTxInfoRoute.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import express from 'express'
import { check } from 'express-validator'
import { isAddress } from 'ethers/lib/utils'

import { CHAINS_ARRAY } from '../constants/chains'
import { validateTokens } from '../validations/validateTokens'
import { showFirstValidationError } from '../middleware/showFirstValidationError'
import { swapTxInfoController } from '../controllers/swapTxInfoController'

Expand All @@ -17,9 +17,22 @@ router.get(
.withMessage('Unsupported chain')
.exists()
.withMessage('chain is required'),
validateTokens('chain', 'fromToken', 'fromToken'),
validateTokens('chain', 'toToken', 'toToken'),
check('amount').isNumeric(),
check('fromToken')
.exists()
.withMessage('fromToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid fromToken address'),
check('toToken')
.exists()
.withMessage('toToken is required')
.custom((value) => isAddress(value))
.withMessage('Invalid toToken address'),
check('amount').isNumeric().exists().withMessage('amount is required'),
check('address')
.exists()
.withMessage('address is required')
.custom((value) => isAddress(value))
.withMessage('Invalid Ethereum address'),
],
showFirstValidationError,
swapTxInfoController
Expand Down
28 changes: 15 additions & 13 deletions packages/rest-api/src/tests/bridgeRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('Bridge Route with Real Synapse Service', () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '137',
fromToken: 'USDC',
toToken: 'USDC',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Ethereum
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC.e on Polygon
amount: '1000',
})
expect(response.status).toBe(200)
Expand All @@ -26,8 +26,8 @@ describe('Bridge Route with Real Synapse Service', () => {
const response = await request(app).get('/bridge').query({
fromChain: '999',
toChain: '137',
fromToken: 'USDC',
toToken: 'USDC',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
amount: '1000',
})
expect(response.status).toBe(400)
Expand All @@ -41,34 +41,36 @@ describe('Bridge Route with Real Synapse Service', () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '999',
fromToken: 'USDC',
toToken: 'USDC',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
amount: '1000',
})
expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty('message', 'Unsupported toChain')
}, 10000)

it('should return 400 for missing fromToken, with error message', async () => {
it('should return 400 for invalid fromToken address, with error message', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '137',
toToken: 'USDC',
fromToken: 'invalid_address',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
amount: '1000',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty('field', 'fromToken')
expect(response.body.error).toHaveProperty(
'message',
'Invalid fromToken address'
)
}, 10000)

it('should return 400 for missing amount, with error message', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '137',
fromToken: 'USDC',
toToken: 'USDC',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty('field', 'amount')
}, 10000)
Expand Down
Loading

0 comments on commit d35d50a

Please sign in to comment.