Skip to content

Commit

Permalink
adding originUserAddress for refunds [SLT-281] (#3206)
Browse files Browse the repository at this point in the history
* adding originUserAddress for refunds

* Adds validation & tests

* Reorder for required first

---------

Co-authored-by: abtestingalpha <[email protected]>
  • Loading branch information
Defi-Moses and abtestingalpha authored Sep 29, 2024
1 parent eae6830 commit 5dbc345
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 5 deletions.
14 changes: 12 additions & 2 deletions packages/rest-api/src/controllers/bridgeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ export const bridgeController = async (req, res) => {
return res.status(400).json({ errors: errors.array() })
}
try {
const { fromChain, toChain, amount, fromToken, toToken } = req.query
const {
fromChain,
toChain,
amount,
fromToken,
toToken,
originUserAddress,
} = req.query

const fromTokenInfo = tokenAddressToToken(fromChain.toString(), fromToken)
const toTokenInfo = tokenAddressToToken(toChain.toString(), toToken)
Expand All @@ -23,7 +30,10 @@ export const bridgeController = async (req, res) => {
Number(toChain),
fromToken,
toToken,
amountInWei
amountInWei,
originUserAddress
? { originUserAddress: originUserAddress.toString() }
: {}
)

const payload = resp.map((quote) => {
Expand Down
16 changes: 13 additions & 3 deletions packages/rest-api/src/controllers/bridgeTxInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ export const bridgeTxInfoController = async (req, res) => {
}

try {
const { fromChain, toChain, amount, destAddress, fromToken, toToken } =
req.query
const {
fromChain,
toChain,
amount,
destAddress,
fromToken,
toToken,
originUserAddress,
} = req.query

const fromTokenInfo = tokenAddressToToken(fromChain.toString(), fromToken)

Expand All @@ -23,7 +30,10 @@ export const bridgeTxInfoController = async (req, res) => {
Number(toChain),
fromToken,
toToken,
amountInWei
amountInWei,
originUserAddress
? { originUserAddress: originUserAddress.toString() }
: {}
)

const txInfoArray = await Promise.all(
Expand Down
11 changes: 11 additions & 0 deletions packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express'
import { check } from 'express-validator'
import { isAddress } from 'ethers/lib/utils'

import { isTokenAddress } from '../utils/isTokenAddress'
import { CHAINS_ARRAY } from '../constants/chains'
Expand Down Expand Up @@ -49,6 +50,12 @@ const router = express.Router()
* schema:
* type: number
* description: The amount of tokens to bridge
* - in: query
* name: originUserAddress
* required: false
* schema:
* type: string
* description: The address of the user on the origin chain
* responses:
* 200:
* description: Successful response
Expand Down Expand Up @@ -230,6 +237,10 @@ router.get(
return validateRouteExists(fromChain, fromToken, toChain, toToken)
})
.withMessage('No valid route exists for the chain/token combination'),
check('originUserAddress')
.optional()
.custom((value) => isAddress(value))
.withMessage('Invalid originUserAddress address'),
],
showFirstValidationError,
bridgeController
Expand Down
10 changes: 10 additions & 0 deletions packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ const router = express.Router()
* schema:
* type: string
* description: The destination address for the bridged tokens
* - in: query
* name: originUserAddress
* required: false
* schema:
* type: string
* description: The address of the user on the origin chain
* responses:
* 200:
* description: Successful response
Expand Down Expand Up @@ -171,6 +177,10 @@ router.get(
return validateRouteExists(fromChain, fromToken, toChain, toToken)
})
.withMessage('No valid route exists for the chain/token combination'),
check('originUserAddress')
.optional()
.custom((value) => isAddress(value))
.withMessage('Invalid originUserAddress address'),
],
showFirstValidationError,
bridgeTxInfoController
Expand Down
34 changes: 34 additions & 0 deletions packages/rest-api/src/tests/bridgeRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ describe('Bridge Route with Real Synapse Service', () => {
expect(response.body[0]).toHaveProperty('bridgeFeeFormatted')
}, 15000)

it('should return bridge quotes for valid originUserAddress', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[10],
amount: '1000',
originUserAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
})

expect(response.status).toBe(200)
expect(Array.isArray(response.body)).toBe(true)
expect(response.body.length).toBeGreaterThan(0)
expect(response.body[0]).toHaveProperty('maxAmountOutStr')
expect(response.body[0]).toHaveProperty('bridgeFeeFormatted')
}, 15000)

it('should return bridge quotes for ZeroAddress', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
Expand Down Expand Up @@ -56,6 +73,23 @@ describe('Bridge Route with Real Synapse Service', () => {
expect(response.body[0]).toHaveProperty('bridgeFeeFormatted')
}, 15000)

it('should return 400 for invalid originUserAddress', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[10],
amount: '1000',
originUserAddress: 'invalid_address',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'Invalid originUserAddress address'
)
}, 15000)

it('should return 400 for unsupported route', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
Expand Down
39 changes: 39 additions & 0 deletions packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,45 @@ describe('Bridge TX Info Route', () => {
)
}, 10_000)

it('should return bridge transaction info for valid input with valid originUserAddress', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
toChain: '137',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[137],
amount: '1000',
destAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
originUserAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
})

expect(response.status).toBe(200)
expect(Array.isArray(response.body)).toBe(true)
expect(response.body.length).toBeGreaterThan(0)
expect(response.body[0]).toHaveProperty('data')
expect(response.body[0]).toHaveProperty(
'to',
'0xd5a597d6e7ddf373a92C8f477DAAA673b0902F48'
)
}, 10_000)

it('should return 400 for invalid originUserAddress', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
toChain: '137',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[137],
amount: '1000',
destAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
originUserAddress: 'invalid_address',
})

expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty(
'message',
'Invalid originUserAddress address'
)
}, 10_000)

it('should return 400 for unsupported route', async () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
Expand Down

0 comments on commit 5dbc345

Please sign in to comment.