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

changing native token address standard [SLT-210] #3157

Merged
merged 4 commits into from
Sep 21, 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
31 changes: 16 additions & 15 deletions packages/rest-api/src/constants/bridgeable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BridgeableToken } from '../types'
import { CHAINS } from './chains'
import { ZeroAddress } from '.'

const NativeTokenAddress = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'

export const GOHM: BridgeableToken = {
addresses: {
Expand Down Expand Up @@ -796,16 +797,16 @@ export const NETH: BridgeableToken = {

export const ETH: BridgeableToken = {
addresses: {
[CHAINS.ETHEREUM.id]: ZeroAddress,
[CHAINS.OPTIMISM.id]: ZeroAddress,
[CHAINS.BOBA.id]: ZeroAddress,
[CHAINS.ETHEREUM.id]: NativeTokenAddress,
[CHAINS.OPTIMISM.id]: NativeTokenAddress,
[CHAINS.BOBA.id]: NativeTokenAddress,
[CHAINS.CANTO.id]: '0x5FD55A1B9FC24967C4dB09C513C3BA0DFa7FF687',
[CHAINS.BASE.id]: ZeroAddress,
[CHAINS.ARBITRUM.id]: ZeroAddress,
[CHAINS.BASE.id]: NativeTokenAddress,
[CHAINS.ARBITRUM.id]: NativeTokenAddress,
[CHAINS.DFK.id]: '0xfBDF0E31808d0aa7b9509AA6aBC9754E48C58852',
[CHAINS.BLAST.id]: ZeroAddress,
[CHAINS.SCROLL.id]: ZeroAddress,
[CHAINS.LINEA.id]: ZeroAddress,
[CHAINS.BLAST.id]: NativeTokenAddress,
[CHAINS.SCROLL.id]: NativeTokenAddress,
[CHAINS.LINEA.id]: NativeTokenAddress,
},
decimals: {
[CHAINS.ETHEREUM.id]: 18,
Expand All @@ -832,7 +833,7 @@ export const ETH: BridgeableToken = {
export const MOVR: BridgeableToken = {
addresses: {
[CHAINS.MOONBEAM.id]: '0x1d4C2a246311bB9f827F4C768e277FF5787B7D7E',
[CHAINS.MOONRIVER.id]: ZeroAddress,
[CHAINS.MOONRIVER.id]: NativeTokenAddress,
},
decimals: {
[CHAINS.MOONBEAM.id]: 18,
Expand All @@ -852,7 +853,7 @@ export const AVAX: BridgeableToken = {
addresses: {
[CHAINS.MOONBEAM.id]: '0xA1f8890E39b4d8E33efe296D698fe42Fb5e59cC3',
[CHAINS.KLAYTN.id]: '0xCd8fE44A29Db9159dB36f96570d7A4d91986f528',
[CHAINS.AVALANCHE.id]: ZeroAddress,
[CHAINS.AVALANCHE.id]: NativeTokenAddress,
[CHAINS.DFK.id]: '0xB57B60DeBDB0b8172bb6316a9164bd3C695F133a',
[CHAINS.HARMONY.id]: '0xb12c13e66AdE1F72f71834f2FC5082Db8C091358',
},
Expand Down Expand Up @@ -909,7 +910,7 @@ export const WAVAX: BridgeableToken = {

export const JEWEL: BridgeableToken = {
addresses: {
[CHAINS.DFK.id]: ZeroAddress,
[CHAINS.DFK.id]: NativeTokenAddress,
[CHAINS.HARMONY.id]: '0x72cb10c6bfa5624dd07ef608027e366bd690048f',
[CHAINS.KLAYTN.id]: '0x30C103f8f5A3A732DFe2dCE1Cc9446f545527b43',
[CHAINS.AVALANCHE.id]: '0x997Ddaa07d716995DE90577C123Db411584E5E46',
Expand Down Expand Up @@ -1158,7 +1159,7 @@ export const DAIe: BridgeableToken = {

export const KLAY: BridgeableToken = {
addresses: {
[CHAINS.KLAYTN.id]: ZeroAddress,
[CHAINS.KLAYTN.id]: NativeTokenAddress,
[CHAINS.DFK.id]: '0x97855Ba65aa7ed2F65Ed832a776537268158B78a',
},
decimals: {
Expand Down Expand Up @@ -1194,7 +1195,7 @@ export const WKLAY: BridgeableToken = {

export const MATIC: BridgeableToken = {
addresses: {
[CHAINS.POLYGON.id]: ZeroAddress,
[CHAINS.POLYGON.id]: NativeTokenAddress,
[CHAINS.DFK.id]: '0xD17a41Cd199edF1093A9Be4404EaDe52Ec19698e',
},
decimals: {
Expand Down Expand Up @@ -1230,7 +1231,7 @@ export const WMATIC: BridgeableToken = {

export const FTM: BridgeableToken = {
addresses: {
[CHAINS.FANTOM.id]: ZeroAddress,
[CHAINS.FANTOM.id]: NativeTokenAddress,
[CHAINS.DFK.id]: '0x2Df041186C844F8a2e2b63F16145Bc6Ff7d23E25',
},
decimals: {
Expand Down
18 changes: 18 additions & 0 deletions packages/rest-api/src/middleware/normalizeNativeTokenAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Request, Response, NextFunction } from 'express'
import { isAddress, getAddress } from 'ethers/lib/utils'

import { NativeGasAddress, ZeroAddress } from '../constants'

export const normalizeNativeTokenAddress = (addressFields: string[]) => {
return (req: Request, _res: Response, next: NextFunction) => {
for (const field of addressFields) {
const address = req.query[field]
if (typeof address === 'string' && isAddress(address)) {
const checksumAddress = getAddress(address)
req.query[field] =
checksumAddress === ZeroAddress ? NativeGasAddress : checksumAddress
}
}
next()
}
}
2 changes: 2 additions & 0 deletions packages/rest-api/src/routes/bridgeRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { showFirstValidationError } from '../middleware/showFirstValidationError
import { bridgeController } from '../controllers/bridgeController'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()

Expand Down Expand Up @@ -187,6 +188,7 @@ const router = express.Router()
*/
router.get(
'/',
normalizeNativeTokenAddress(['fromToken', 'toToken']),
checksumAddresses(['fromToken', 'toToken']),
[
check('fromChain')
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/src/routes/bridgeTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { bridgeTxInfoController } from '../controllers/bridgeTxInfoController'
import { isTokenAddress } from '../utils/isTokenAddress'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()

Expand Down Expand Up @@ -123,6 +124,7 @@ const router = express.Router()
*/
router.get(
'/',
normalizeNativeTokenAddress(['fromToken', 'toToken']),
checksumAddresses(['fromToken', 'toToken']),
[
check('fromChain')
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/src/routes/destinationTokensRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { destinationTokensController } from '../controllers/destinationTokensCon
import { isTokenAddress } from '../utils/isTokenAddress'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()

Expand Down Expand Up @@ -98,6 +99,7 @@ const router = express.Router()

router.get(
'/',
normalizeNativeTokenAddress(['fromToken']),
checksumAddresses(['fromToken']),
[
check('fromChain')
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/src/routes/swapRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { CHAINS_ARRAY } from '../constants/chains'
import { isTokenAddress } from '../utils/isTokenAddress'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()

Expand Down Expand Up @@ -133,6 +134,7 @@ const router = express.Router()
*/
router.get(
'/',
normalizeNativeTokenAddress(['fromToken', 'toToken']),
checksumAddresses(['fromToken', 'toToken']),
[
check('chain')
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/src/routes/swapTxInfoRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { swapTxInfoController } from '../controllers/swapTxInfoController'
import { isTokenAddress } from '../utils/isTokenAddress'
import { isTokenSupportedOnChain } from '../utils/isTokenSupportedOnChain'
import { checksumAddresses } from '../middleware/checksumAddresses'
import { normalizeNativeTokenAddress } from '../middleware/normalizeNativeTokenAddress'

const router = express.Router()

Expand Down Expand Up @@ -115,6 +116,7 @@ const router = express.Router()
*/
router.get(
'/',
normalizeNativeTokenAddress(['fromToken', 'toToken']),
checksumAddresses(['fromToken', 'toToken']),
[
check('chain')
Expand Down
41 changes: 37 additions & 4 deletions packages/rest-api/src/tests/bridgeRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import request from 'supertest'
import express from 'express'

import bridgeRoute from '../routes/bridgeRoute'
import { NativeGasAddress, ZeroAddress } from '../constants'
import { USDC } from '../constants/bridgeable'

const app = express()
app.use('/bridge', bridgeRoute)
Expand All @@ -11,8 +13,8 @@ describe('Bridge Route with Real Synapse Service', () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Ethereum
toToken: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85', // USDC on Optimism
fromToken: USDC.addresses[1],
toToken: USDC.addresses[10],
amount: '1000',
})
expect(response.status).toBe(200)
Expand All @@ -22,6 +24,37 @@ describe('Bridge Route with Real Synapse Service', () => {
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',
toChain: '10',
fromToken: ZeroAddress,
toToken: ZeroAddress,
amount: '10',
})
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 NativeGasAddress', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: NativeGasAddress,
toToken: NativeGasAddress,
amount: '10',
})

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 400 for unsupported fromChain, with error message', async () => {
const response = await request(app).get('/bridge').query({
fromChain: '999',
Expand Down Expand Up @@ -83,8 +116,8 @@ describe('Bridge Route with Real Synapse Service', () => {
const response = await request(app).get('/bridge').query({
fromChain: '1',
toChain: '10',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[10],
})
expect(response.status).toBe(400)
expect(response.body.error).toHaveProperty('field', 'amount')
Expand Down
13 changes: 7 additions & 6 deletions packages/rest-api/src/tests/bridgeTxInfoRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import request from 'supertest'
import express from 'express'

import bridgeTxInfoRoute from '../routes/bridgeTxInfoRoute'
import { USDC } from '../constants/bridgeable'

const app = express()
app.use('/bridgeTxInfo', bridgeTxInfoRoute)
Expand All @@ -11,8 +12,8 @@ describe('Bridge TX Info Route', () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
toChain: '137',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC on Ethereum
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174', // USDC on Polygon
fromToken: USDC.addresses[1],
toToken: USDC.addresses[137],
amount: '1000',
destAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
})
Expand Down Expand Up @@ -79,8 +80,8 @@ describe('Bridge TX Info Route', () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
toChain: '137',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[137],
destAddress: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
})
expect(response.status).toBe(400)
Expand All @@ -91,8 +92,8 @@ describe('Bridge TX Info Route', () => {
const response = await request(app).get('/bridgeTxInfo').query({
fromChain: '1',
toChain: '137',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
toToken: '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174',
fromToken: USDC.addresses[1],
toToken: USDC.addresses[137],
amount: '1000',
destAddress: 'invalid_address',
})
Expand Down
28 changes: 22 additions & 6 deletions packages/rest-api/src/tests/destinationTokensRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import request from 'supertest'
import express from 'express'

import destinationTokensRoute from '../routes/destinationTokensRoute'
import { NativeGasAddress, ZeroAddress } from '../constants'
import { USDC, USDT } from '../constants/bridgeable'

const app = express()
app.use('/destinationTokens', destinationTokensRoute)
Expand All @@ -10,7 +12,7 @@ describe('destinatonTokens Route', () => {
it('should return destination tokens for valid input', async () => {
const response = await request(app).get('/destinationTokens').query({
fromChain: '1',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
fromToken: USDC.addresses[1],
})

expect(response.status).toBe(200)
Expand All @@ -24,7 +26,21 @@ describe('destinatonTokens Route', () => {
it('should return destination tokens for valid gas Tokens', async () => {
const response = await request(app).get('/destinationTokens').query({
fromChain: '1',
fromToken: '0x0000000000000000000000000000000000000000',
fromToken: NativeGasAddress,
})

expect(response.status).toBe(200)
expect(Array.isArray(response.body)).toBe(true)
expect(response.body.length).toBeGreaterThan(0)
expect(response.body[0]).toHaveProperty('symbol')
expect(response.body[0]).toHaveProperty('address')
expect(response.body[0]).toHaveProperty('chainId')
})

it('should return destination tokens for valid gas Tokens, ZeroAddress', async () => {
const response = await request(app).get('/destinationTokens').query({
fromChain: '1',
fromToken: ZeroAddress,
})

expect(response.status).toBe(200)
Expand All @@ -40,7 +56,7 @@ describe('destinatonTokens Route', () => {

const response = await request(app).get('/destinationTokens').query({
fromChain: '534352',
fromToken: '0x06eFdBFf2a14a7c8E15944D1F4A48F9F95F663A4',
fromToken: USDC.addresses[534352],
})

expect(response.status).toBe(200)
Expand All @@ -54,7 +70,7 @@ describe('destinatonTokens Route', () => {
it('should return destination tokens for non-checksummed address', async () => {
const response = await request(app).get('/destinationTokens').query({
fromChain: '43114',
fromToken: '0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7',
fromToken: USDT.addresses[43114].toLowerCase(),
})

expect(response.status).toBe(200)
Expand Down Expand Up @@ -107,7 +123,7 @@ describe('destinatonTokens Route', () => {
it('should return 400 for token not supported on specified chain', async () => {
const response = await request(app).get('/destinationTokens').query({
fromChain: '10',
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
fromToken: USDC.addresses[1],
})

expect(response.status).toBe(400)
Expand All @@ -119,7 +135,7 @@ describe('destinatonTokens Route', () => {

it('should return 400 for missing fromChain', async () => {
const response = await request(app).get('/destinationTokens').query({
fromToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
fromToken: USDC.addresses[1],
})

expect(response.status).toBe(400)
Expand Down
Loading
Loading