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

REST API Migration #3049

Merged
merged 5 commits into from
Aug 29, 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
17 changes: 8 additions & 9 deletions packages/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
"node": ">=18.17.0"
},
"scripts": {
"test": "tsdx test",
"start": "node ./dist/app.js",
"dev": "yarn add @synapsecns/sdk-router && concurrently \"npx tsc --watch\" \"nodemon -q dist/app.js\"",
"test:coverage": "echo 'No tests defined.'",
"lint:fix": "eslint src/**.ts --fix",
"build": "tsc",
"start": "node dist/app.js",
"lint:fix": "eslint src/**/*.ts --fix",
"lint:check": "eslint . --max-warnings=0",
"ci:lint": "npm run lint:check",
"build:go": " ",
"build:slither": " ",
"build": "echo 'please run build api'",
"build:api": "yarn add @synapsecns/sdk-router && tsc",
"test": "tsdx test",
"test:coverage": "echo 'No tests defined.'",
"postinstall": "npm run build"
},
"dependencies": {
Expand All @@ -27,11 +23,14 @@
"bignumber": "^1.1.0",
"ethers": "5.7.2",
"express": "^4.18.2",
"lodash": "^4.17.21",
"supertest": "^6.3.3"
},
"description": "A node.js project exposing a rest api for synapse sdk quotes",
"devDependencies": {
"@types/supertest": "^6.0.2",
"concurrently": "^8.2.0",
"lodash": "^4.17.21",
"nodemon": "^3.0.1",
"typescript": "^4.8.3"
}
Expand Down
38 changes: 0 additions & 38 deletions packages/rest-api/src/app.test.ts

This file was deleted.

157 changes: 57 additions & 100 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,19 @@ import { JsonRpcProvider } from '@ethersproject/providers'
import { SynapseSDK } from '@synapsecns/sdk-router'
import { BigNumber } from '@ethersproject/bignumber'
import { formatUnits, parseUnits } from '@ethersproject/units'
import * as express from 'express'
import express from 'express'

import * as tokensList from './config/bridgeable'
import * as chainsData from './config/chains.json'
import { BRIDGE_MAP } from './config/bridgeMap'
import * as tokensList from './constants/bridgeable'
import { CHAINS_ARRAY } from './constants/chains'
import { Chain } from './types'
import { BRIDGE_MAP } from './constants/bridgeMap'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Token {
addresses: {
[key: string]: string
}
decimals: number | { [key: string]: number }
symbol: string
name: string
swapableType: string
color: string
visibilityRank: number
priorityRank: number
routeSymbol: string
swapableOn?: number[]
wrapperAddresses?: {
[key: string]: string
}
isNative?: boolean
docUrl?: string
}
// interface Tokens {
// [key: string]: Token
// }

// const tokens: Tokens = tokensList as any

interface Chains {
id: number
name: string
rpc: string
}

const chains: Chains[] = chainsData as any
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
function findTokenInfo(chain: string, tokenSymbol: string) {
const chains: Chain[] = CHAINS_ARRAY
const findTokenInfo = (chain: string, tokenSymbol: string) => {
const chainData = BRIDGE_MAP[chain]
if (!chainData) {
return null
}

for (const tokenAddress in chainData) {
if (chainData[tokenAddress].symbol === tokenSymbol) {
return {
Expand All @@ -62,24 +29,18 @@ function findTokenInfo(chain: string, tokenSymbol: string) {

const tokenHtml = Object.values(tokensList)
.map((token: any) => {
return (
'<b>Token: ' +
token.symbol +
'</b><br/>' +
Object.keys(token.addresses)
.map((chainId) => {
const tokenAddress = token.addresses[chainId]
return (
'<li>Chain Id: ' +
String(chainId) +
', Address: ' +
String(tokenAddress) +
'</li>'
)
})
.join('') +
'</br>'
)
const chainList = Object.entries(token.addresses)
.map(
([chainId, tokenAddress]) =>
`<li>Chain Id: ${chainId}, Address: ${tokenAddress}</li>`
)
.join('')

return `
<b>Token: ${token.symbol}</b><br/>
${chainList}
<br/>
`
})
.join('')

Expand All @@ -88,7 +49,7 @@ const providers = []
const chainIds = []

for (const chain of chains) {
providers.push(new JsonRpcProvider(chain.rpc))
providers.push(new JsonRpcProvider(chain.rpcUrls.primary))
chainIds.push(chain.id)
}
// Define the sdk
Expand All @@ -106,12 +67,7 @@ app.get('/', (_req, res) => {
<hr/>
<h2>Available Chains</h2>
<ul>
${chains
.map(
(chain) =>
'<li>' + String(chain.name) + ' (' + String(chain.id) + ')' + '</li>'
)
.join('')}
${chains.map((chain) => `<li>${chain.name} (${chain.id})</li>`).join('')}
</ul>
<h2>Available Tokens (symbols to use)</h2>
${tokenHtml}`
Expand All @@ -129,7 +85,7 @@ app.get('/swap', async (req, res) => {
const query = req.query

// Chain
const chainId = query.chain
const chainId = String(query.chain)

// Symbols
const fromTokenSymbol = String(query.fromToken)
Expand Down Expand Up @@ -218,8 +174,8 @@ app.get('/bridge', async (req, res) => {
const query = req.query

// Chains
const fromChain = query.fromChain
const toChain = query.toChain
const fromChain = String(query.fromChain)
const toChain = String(query.toChain)

// Symbols
const fromTokenSymbol = String(query.fromToken)
Expand Down Expand Up @@ -278,16 +234,17 @@ app.get('/bridge', async (req, res) => {
BigNumber.from(amount)
)
.then((resp) => {
const firstQuote = resp[0]
const payload: any = firstQuote
payload.maxAmountOutStr = formatBNToString(
firstQuote.maxAmountOut,
toTokenDecimals
)
payload.bridgeFeeFormatted = formatBNToString(
firstQuote.feeAmount,
toTokenDecimals
)
const payload = resp.map((quote) => ({
...quote,
maxAmountOutStr: formatBNToString(
quote.maxAmountOut,
toTokenDecimals
),
bridgeFeeFormatted: formatBNToString(
quote.feeAmount,
toTokenDecimals
),
}))
res.json(payload)
})
.catch((err) => {
Expand Down Expand Up @@ -316,7 +273,7 @@ app.get('/swapTxInfo', async (req, res) => {
const query = req.query

// Chain
const chainId = query.chain
const chainId = String(query.chain)

// Symbols
const fromTokenSymbol = String(query.fromToken)
Expand All @@ -331,7 +288,7 @@ app.get('/swapTxInfo', async (req, res) => {
`
<h1>Invalid Params</h1>
<hr/>
<b>Ensure that your request matches the following format: /swapTxInfo?chain=1&fromToken=USDC&toToken=DAI&amount=100
<b>Ensure that your request matches the following format: /bridge?fromChain=1&toChain=42161&fromToken=USDC&toToken=USDC&amount=1000000
</b>
<h2>Available Tokens (symbols to use)</h2>
${tokenHtml}`
Expand Down Expand Up @@ -411,8 +368,8 @@ app.get('/bridgeTxInfo', async (req, res) => {
const query = req.query

// Chains
const fromChain = query.fromChain
const toChain = query.toChain
const fromChain = String(query.fromChain)
const toChain = String(query.toChain)

// Symbols
const fromTokenSymbol = String(query.fromToken)
Expand All @@ -427,7 +384,7 @@ app.get('/bridgeTxInfo', async (req, res) => {
`
<h1>Invalid Params</h1>
<hr/>
<b>Ensure that your request matches the following format: /bridgeTxInfo?fromChain=1&toChain=42161&fromToken=USDC&toToken=USDC&amount=1000000&destAddress=0xcc78d2f004c9de9694ff6a9bbdee4793d30f3842
<b>Ensure that your request matches the following format: /bridge?fromChain=1&toChain=42161&fromToken=USDC&toToken=USDC&amount=1000000
</b>
<h2>Available Tokens (symbols to use)</h2>
${tokenHtml}`
Expand All @@ -443,9 +400,6 @@ app.get('/bridgeTxInfo', async (req, res) => {
//Get to Address on destination chain
const destAddress = String(query.destAddress)

//Router Address:
const routerAddress = '0x7e7a0e201fd38d3adaa9523da6c109a07118c96a'

// Handle invalid params (either token symbols or chainIDs)
// TODO: add error handling for missing params
if (
Expand Down Expand Up @@ -479,20 +433,23 @@ app.get('/bridgeTxInfo', async (req, res) => {
toTokenAddress,
BigNumber.from(amount)
)
.then((resp) => {
const firstQuote = resp[0]
Synapse.bridge(
destAddress,
routerAddress,
Number(fromChain),
Number(toChain),
fromTokenAddress,
BigNumber.from(amount),
firstQuote.originQuery,
firstQuote.destQuery
).then((txInfo) => {
res.json(txInfo)
})
.then(async (resp) => {
const txInfoArray = await Promise.all(
resp.map(async (quote) => {
const txInfo = await Synapse.bridge(
destAddress,
quote.routerAddress,
Number(fromChain),
Number(toChain),
fromTokenAddress,
BigNumber.from(amount),
quote.originQuery,
quote.destQuery
)
return txInfo
})
)
res.json(txInfoArray)
})
.catch((err) => {
// TODO: do a better return here
Expand Down
27 changes: 0 additions & 27 deletions packages/rest-api/src/config/Chains.ts

This file was deleted.

Loading
Loading