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

feat(sdk): uuid BridgeQuote.id #2896

Merged
merged 14 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
8 changes: 8 additions & 0 deletions packages/sdk-router/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module.exports = {
maxConcurrency: 1,
testTimeout: 30000,
moduleNameMapper: {
/**
* Force module uuidv7 to resolve with the CJS entry point,
* because Jest does not support package.json.exports.
* See https://github.com/uuidjs/uuid/issues/451
*/
uuidv7: require.resolve('uuidv7'),
},
}
7 changes: 5 additions & 2 deletions packages/sdk-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,24 @@
},
"dependencies": {
"@babel/core": "^7.20.12",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@ethersproject/abi": "^5.7.0",
"@ethersproject/abstract-provider": "^5.7.0",
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/constants": "^5.7.0",
"@ethersproject/contracts": "^5.7.0",
"babel-jest": "^29.4.1",
"babel-jest": "^25.2.6",
"big.js": "^5.2.2",
"decimal.js-light": "^2.5.1",
"ethers": "^5.7.2",
"jest": "^29.7.0",
"jsbi": "^4.3.0",
"node-cache": "^5.1.2",
"tiny-invariant": "^1.2.0",
"toformat": "^2.0.0",
"ts-xor": "^1.1.0"
"ts-xor": "^1.1.0",
"uuidv7": "^1.0.1"
}
}
3 changes: 3 additions & 0 deletions packages/sdk-router/src/module/synapseModuleSet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BigNumber } from '@ethersproject/bignumber'
import { uuidv7 } from 'uuidv7'
import invariant from 'tiny-invariant'

import { BigintIsh } from '../constants'
Expand Down Expand Up @@ -199,13 +200,15 @@ export abstract class SynapseModuleSet {
bridgeRoute.bridgeModuleName === this.bridgeModuleName,
'Invalid bridge module name'
)
const uuid = uuidv7()
const { originQuery, destQuery } = bridgeRoute
const { originModuleDeadline, destModuleDeadline } =
this.getModuleDeadlines(originDeadline, destDeadline)
originQuery.deadline = originModuleDeadline
destQuery.deadline = destModuleDeadline
const { feeAmount, feeConfig } = await this.getFeeData(bridgeRoute)
return {
id: uuid,
feeAmount,
feeConfig,
routerAddress: originModule.address,
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-router/src/module/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export type SwapQuote = {
* Returned by SDK to the consumer.
*/
export type BridgeQuote = {
id: string
feeAmount: BigNumber
feeConfig: FeeConfig
routerAddress: string
Expand Down
31 changes: 31 additions & 0 deletions packages/sdk-router/src/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
SynapseModuleSet,
} from './module'
import * as operations from './operations'
import { validateUUID } from './utils/validateUUID'

// Override fetch to exclude RFQ from tests
global.fetch = jest.fn(() =>
Expand Down Expand Up @@ -101,6 +102,10 @@ const createBridgeQuoteTests = (
result = await resultPromise
})

it('Generates a bridge quote with valid uuid', async () => {
expect(validateUUID(result.id)).toBe(true)
})

it('Fetches a bridge quote', async () => {
expectCorrectBridgeQuote(result)
})
Expand Down Expand Up @@ -402,6 +407,14 @@ describe('SynapseSDK', () => {
amount
)

const secondResultPromise: Promise<BridgeQuote> = synapse.bridgeQuote(
SupportedChainId.ARBITRUM,
SupportedChainId.ETH,
ARB_USDC,
ETH_USDC,
amount
)

createBridgeQuoteTests(
synapse,
SupportedChainId.ARBITRUM,
Expand Down Expand Up @@ -429,6 +442,13 @@ describe('SynapseSDK', () => {
expect(result.originChainId).toEqual(SupportedChainId.ARBITRUM)
expect(result.destChainId).toEqual(SupportedChainId.ETH)
})

it('Fetches a second Synapse bridge quote with a different ID', async () => {
const firstQuote = await resultPromise
const secondQuote = await secondResultPromise

expect(firstQuote.id).not.toEqual(secondQuote.id)
})
})

describe('ARB USDC -> ETH USDC (using CCTP)', () => {
Expand Down Expand Up @@ -808,6 +828,17 @@ describe('SynapseSDK', () => {
expect(allQuotes[1].destChainId).toEqual(SupportedChainId.ARBITRUM)
})

it('Generates unique IDs for SynapseBridge and SynapseCCTP quotes for USDC', async () => {
const allQuotes = await synapse.allBridgeQuotes(
SupportedChainId.ETH,
SupportedChainId.ARBITRUM,
ETH_USDC,
ARB_USDT,
BigNumber.from(10).pow(9)
)
expect(allQuotes[0].id).not.toEqual(allQuotes[1].id)
})

it('Fetches only SynapseBridge quotes for ETH', async () => {
const allQuotes = await synapse.allBridgeQuotes(
SupportedChainId.ETH,
Expand Down
12 changes: 12 additions & 0 deletions packages/sdk-router/src/utils/validateUUID.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { validateUUID } from './validateUUID'

describe('#validateUUID', () => {
it('returns true if valid', () => {
expect(validateUUID('0190c847-f0f3-79cd-abbf-0f885153e5dc')).toBe(true)
})

it('returns false if not valid', () => {
expect(validateUUID('test-id')).toBe(false)
expect(validateUUID('0190c83b-d74a-7b0b-aa6e-57e1cdb2f84bcc')).toBe(false)
})
})
10 changes: 10 additions & 0 deletions packages/sdk-router/src/utils/validateUUID.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Validates an UUIDv7 string (time-ordered UUID encoding Unix timestamp)
*
* @param uuid the generated UUIDv7 string
*/
export const validateUUID = (uuid: string): boolean => {
const uuidv7Regex =
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
return uuidv7Regex.test(uuid)
}
Loading
Loading