Skip to content

Commit

Permalink
Merge 8530122 into 265aff4
Browse files Browse the repository at this point in the history
  • Loading branch information
bigboydiamonds authored Sep 16, 2024
2 parents 265aff4 + 8530122 commit 714ad0d
Show file tree
Hide file tree
Showing 4 changed files with 815 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build": "tsc",
"build:go": " ",
"start": "node dist/app.js",
"dev": "nodemon --watch src --exec ts-node src/app.ts",
"lint:fix": "eslint src/**/*.ts --fix",
"lint:check": "eslint . --max-warnings=0",
"ci:lint": "npm run lint:check",
Expand All @@ -17,10 +18,12 @@
"postinstall": "npm run build"
},
"dependencies": {
"@ethersproject/address": "^5.7.0",
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@ethersproject/units": "5.7.0",
"@synapsecns/sdk-router": "^0.11.1",
"axios": "^1.7.7",
"bignumber": "^1.1.0",
"ethers": "5.7.2",
"express": "^4.18.2",
Expand Down
124 changes: 124 additions & 0 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { JsonRpcProvider } from '@ethersproject/providers'
import { SynapseSDK } from '@synapsecns/sdk-router'
import { BigNumber } from '@ethersproject/bignumber'
import { formatUnits, parseUnits } from '@ethersproject/units'
import { getAddress } from '@ethersproject/address'
import express from 'express'
import axios from 'axios'
import { isString } from 'lodash'

import * as tokensList from './constants/bridgeable'
import { CHAINS_ARRAY } from './constants/chains'
Expand Down Expand Up @@ -74,6 +77,127 @@ app.get('/', (_req, res) => {
)
})

app.get('/get-quote-max', async (req, res) => {
try {
const {
originChainId,
originTokenAddress,
originTokenSymbol,
destChainId,
destTokenAddress,
destTokenSymbol,
} = req.query

if (
!originChainId ||
!originTokenSymbol ||
!originTokenAddress ||
!destChainId ||
!destTokenSymbol ||
!destTokenAddress
) {
return res.status(400).json({ error: 'Missing required parameters' })
}

if (
!isString(originChainId) ||
!isString(originTokenAddress) ||
!isString(destChainId) ||
!isString(originTokenSymbol) ||
!isString(destTokenSymbol) ||
!isString(destTokenAddress)
) {
return res.status(400).json({ error: 'Invalid parameters type' })
}

const originTokenInfo = findTokenInfo(originChainId, originTokenSymbol)
const destTokenInfo = findTokenInfo(destChainId, destTokenSymbol)

if (!originTokenInfo || !destTokenInfo) {
return res
.status(404)
.json({ error: 'Invalid token symbols or chainids' })
}

const { decimals: originTokenDecimals } = originTokenInfo

const rfqResponse = await axios.get('https://rfq-api.omnirpc.io/quotes', {
params: {
originChainId,
originTokenAddress,
destChainId,
destTokenAddress,
},
})

const rfqQuotes = rfqResponse.data

let bestRfqQuote = null

if (Array.isArray(rfqQuotes) && rfqQuotes.length > 0) {
const filteredQuotes = rfqQuotes
.slice(0, 20)
.filter(
(quote) =>
Number(quote.origin_chain_id) === Number(originChainId) &&
getAddress(quote.origin_token_addr) === originTokenAddress &&
Number(quote.dest_chain_id) === Number(destChainId) &&
getAddress(quote.dest_token_addr) === destTokenAddress
)

bestRfqQuote = filteredQuotes.reduce((maxQuote, currentQuote) => {
const currentAmount = Number(currentQuote.max_origin_amount)
const maxAmount = maxQuote ? Number(maxQuote.max_origin_amount) : 0
return currentAmount > maxAmount ? currentQuote : maxQuote
}, null)
}

const amount = parseUnits('1000000', originTokenDecimals)

const bridgeQuotes = await Synapse.allBridgeQuotes(
Number(originChainId),
Number(destChainId),
originTokenAddress,
destTokenAddress,
amount
)

if (!Array.isArray(bridgeQuotes) || bridgeQuotes.length === 0) {
return res.status(404).json({ error: 'No bridge quotes found' })
}

const bestSDKQuote = bridgeQuotes[0]

let maxOriginQuote
if (bestRfqQuote) {
const bestRfqQuoteMaxAmountBN = BigNumber.from(
bestRfqQuote.max_origin_amount
)
maxOriginQuote = bestRfqQuoteMaxAmountBN.gt(bestSDKQuote.maxAmountOut)
? { source: 'RFQ', amount: bestRfqQuoteMaxAmountBN }
: {
source: bestSDKQuote.bridgeModuleName,
amount: bestSDKQuote.maxAmountOut,
}
} else {
// If no RFQ quote, simply use the SDK quote
maxOriginQuote = {
source: bestSDKQuote.bridgeModuleName,
amount: bestSDKQuote.maxAmountOut,
}
}

return res.json({
rfqBestQuote: bestRfqQuote,
sdkBestQuote: bestSDKQuote,
maxOriginQuote,
})
} catch (error) {
console.error('Error fetching quotes:', error)
return res.status(500).json({ error: 'Failed to fetch quotes' })
}
})

app.get('/tokenList', (_req, res) => {
res.send(tokensList)
})
Expand Down
2 changes: 2 additions & 0 deletions packages/synapse-interface/slices/bridgeQuote/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export const fetchBridgeQuote = createAsyncThunk(
}
)

console.log('allQuotes: ', allQuotes)

const pausedBridgeModules = new Set(
pausedModulesList
.filter((module) =>
Expand Down
Loading

0 comments on commit 714ad0d

Please sign in to comment.