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(rest-api): Adds winston logger [SLT-271] #3216

Merged
merged 8 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions packages/rest-api/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {
rules: {
'guard-for-in': 'off',
'jsdoc/check-indentation': 'off',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-unused-vars': 'off',
},
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/rest-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"jest": "^29.7.0",
"lodash": "^4.17.21",
"supertest": "^6.3.3",
"typescript": "^4.8.3"
"typescript": "^4.8.3",
"winston": "^3.14.2"
},
"description": "A node.js project exposing a rest api for synapse sdk quotes",
"devDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions packages/rest-api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import swaggerUi from 'swagger-ui-express'

import { specs } from './swagger'
import routes from './routes'
import { logger } from './middleware/logger'

const app = express()
const port = process.env.PORT || 3000

app.use(express.json())

app.listen(port, () => {
logger.info(`Server is listening on port ${port}`)
})

app.use(
'/api-docs',
(_req, res, next) => {
Expand All @@ -27,6 +32,16 @@ app.use(

app.use('/', routes)

export const server = app.listen(port, () => {
console.log(`Server listening at ${port}`)
app.use((err, _req, res, _next) => {
logger.error(`Express error: ${err.message}`, { stack: err.stack })
res.status(500).json({ error: 'Something went wrong', details: err.message })
})

process.on('uncaughtException', (err) => {
logger.error(`Uncaught Exception: ${err.message}`, { stack: err.stack })
process.exit(1)
})

process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection at:', promise, 'reason:', reason)
})
7 changes: 7 additions & 0 deletions packages/rest-api/src/controllers/bridgeController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { parseUnits } from '@ethersproject/units'
import { formatBNToString } from '../utils/formatBNToString'
import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { logger } from '../middleware/logger'

export const bridgeController = async (req, res) => {
const errors = validationResult(req)
Expand Down Expand Up @@ -53,8 +54,14 @@ export const bridgeController = async (req, res) => {
),
}
})

logger.info(`Successful bridgeController response`, { payload })
res.json(payload)
} catch (err) {
logger.error(`Error in bridgeController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error: 'An unexpected error occurred in /bridge. Please try again later.',
details: err.message,
Expand Down
12 changes: 10 additions & 2 deletions packages/rest-api/src/controllers/bridgeLimitsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseUnits } from '@ethersproject/units'
import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { formatBNToString } from '../utils/formatBNToString'
import { logger } from '../middleware/logger'

export const bridgeLimitsController = async (req, res) => {
const errors = validationResult(req)
Expand Down Expand Up @@ -94,11 +95,18 @@ export const bridgeLimitsController = async (req, res) => {
minAmountOriginQueryTokenOutInfo.decimals
)

return res.json({
const payload = {
maxOriginAmount,
minOriginAmount,
})
}

logger.info(`Succesful bridgeLimitsController response`, { payload })
return res.json(payload)
} catch (err) {
logger.error(`Error in bridgeLimitsController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /bridgeLimits. Please try again later.',
Expand Down
7 changes: 7 additions & 0 deletions packages/rest-api/src/controllers/bridgeTxInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseUnits } from '@ethersproject/units'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { logger } from '../middleware/logger'

export const bridgeTxInfoController = async (req, res) => {
const errors = validationResult(req)
Expand Down Expand Up @@ -51,8 +52,14 @@ export const bridgeTxInfoController = async (req, res) => {
return txInfo
})
)

logger.info(`Successful bridgeTxInfoController response`, { txInfoArray })
res.json(txInfoArray)
} catch (err) {
logger.error(`Error in bridgeTxInfoController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /bridgeTxInfo. Please try again later.',
Expand Down
25 changes: 21 additions & 4 deletions packages/rest-api/src/controllers/bridgeTxStatusController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'ethers'

import { Synapse } from '../services/synapseService'
import { getTokenDecimals } from '../utils/getTokenDecimals'
import { logger } from '../middleware/logger'

export const bridgeTxStatusController = async (req, res) => {
const errors = validationResult(req)
Expand Down Expand Up @@ -56,20 +57,36 @@ export const bridgeTxStatusController = async (req, res) => {
const tokenDecimals = getTokenDecimals(toInfo.chainID, tokenAddress)
const formattedValue = ethers.utils.formatUnits(value, tokenDecimals)

res.json({
const payload = {
status,
toInfo: {
...restToInfo,
formattedValue: `${formattedValue}`,
},
})
}

logger.info(`Successful bridgeTxStatusController response`, { payload })
res.json(payload)
} else {
res.json({ status, toInfo: null })
const payload = {
status,
toInfo: null,
}

logger.info(`Successful bridgeTxStatusController response`, { payload })
res.json(payload)
}
} else {
res.json({ status })
const payload = { status }

logger.info(`Successful bridgeTxStatusController response`, { payload })
res.json(payload)
}
} catch (err) {
logger.error(`Error in bridgeTxStatusController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /bridgeTxStatus. Please try again later.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { validationResult } from 'express-validator'

import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { BRIDGE_ROUTE_MAPPING } from '../utils/bridgeRouteMapping'
import { logger } from '../middleware/logger'

export const destinationTokensController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -16,10 +17,16 @@ export const destinationTokensController = async (req, res) => {

const constructedKey = `${fromTokenInfo.symbol}-${fromChain}`

const options = BRIDGE_ROUTE_MAPPING[constructedKey]
const payload = BRIDGE_ROUTE_MAPPING[constructedKey]

res.json(options)
logger.info(`Successful destinationTokensController response`, { payload })

res.json(payload)
} catch (err) {
logger.error(`Error in destinationTokensController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /destinationTokens. Please try again later.',
Expand Down
20 changes: 17 additions & 3 deletions packages/rest-api/src/controllers/destinationTxController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ethers } from 'ethers'

import { getTokenDecimals } from '../utils/getTokenDecimals'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { logger } from '../middleware/logger'

export const destinationTxController = async (req, res) => {
const errors = validationResult(req)
Expand Down Expand Up @@ -54,19 +55,32 @@ export const destinationTxController = async (req, res) => {
const tokenDecimals = getTokenDecimals(chainID, tokenAddress)
const formattedValue = ethers.utils.formatUnits(value, tokenDecimals)

res.json({
const payload = {
status: 'completed',
toInfo: {
chainID,
...restToInfo,
tokenSymbol: tokenInfo ? tokenInfo?.symbol : null,
formattedValue: `${formattedValue}`,
},
})
}

logger.info(`Successful destinationTxController response`, { payload })
res.json(payload)
} else {
res.json({ status: 'pending', toInfo: null })
const payload = {
status: 'pending',
toInfo: null,
}

logger.info(`Successful destinationTxController response`, { payload })
res.json(payload)
}
} catch (err) {
logger.error(`Error in destinationTxController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /destinationTx. Please try again later.',
Expand Down
12 changes: 10 additions & 2 deletions packages/rest-api/src/controllers/indexController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as tokensList from '../constants/bridgeable'
import { CHAINS_ARRAY } from '../constants/chains'
import { logger } from '../middleware/logger'

export const indexController = async (_req, res) => {
try {
Expand All @@ -13,15 +14,22 @@ export const indexController = async (_req, res) => {
),
}))

res.json({
const payload = {
message: 'Welcome to the Synapse REST API for swap and bridge quotes',
availableChains: CHAINS_ARRAY.map((chain) => ({
name: chain.name,
id: chain.id,
})),
availableTokens: tokensWithChains,
})
}

logger.info(`Successful indexController response`)
res.json(payload)
} catch (err) {
logger.error(`Error in indexController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error: 'An unexpected error occurred in /. Please try again later.',
details: err.message,
Expand Down
12 changes: 10 additions & 2 deletions packages/rest-api/src/controllers/swapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BigNumber } from '@ethersproject/bignumber'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { logger } from '../middleware/logger'

export const swapController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -29,11 +30,18 @@ export const swapController = async (req, res) => {
toTokenInfo.decimals
)

res.json({
const payload = {
...quote,
maxAmountOut: formattedMaxAmountOut,
})
}

logger.info(`Successful swapController response`, { payload })
res.json(payload)
} catch (err) {
logger.error(`Error in swapController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error: 'An unexpected error occurred in /swap. Please try again later.',
details: err.message,
Expand Down
10 changes: 8 additions & 2 deletions packages/rest-api/src/controllers/swapTxInfoController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { parseUnits } from '@ethersproject/units'

import { Synapse } from '../services/synapseService'
import { tokenAddressToToken } from '../utils/tokenAddressToToken'
import { logger } from '../middleware/logger'

export const swapTxInfoController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -24,16 +25,21 @@ export const swapTxInfoController = async (req, res) => {
amountInWei
)

const txInfo = await Synapse.swap(
const payload = await Synapse.swap(
Number(chain),
address,
fromToken,
amountInWei,
quote.query
)

res.json(txInfo)
logger.info(`Successful swapTxInfoController response`, payload)
res.json(payload)
} catch (err) {
logger.error(`Error in swapTxInfoController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /swapTxInfo. Please try again later.',
Expand Down
12 changes: 11 additions & 1 deletion packages/rest-api/src/controllers/synapseTxIdController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { validationResult } from 'express-validator'

import { Synapse } from '../services/synapseService'
import { logger } from '../middleware/logger'

export const synapseTxIdController = async (req, res) => {
const errors = validationResult(req)
Expand All @@ -17,8 +18,17 @@ export const synapseTxIdController = async (req, res) => {
txHash
)

res.json({ synapseTxId })
const payload = {
synapseTxId,
}

logger.info(`Successful synapseTxIdController response`, payload)
res.json(payload)
} catch (err) {
logger.error(`Error in synapseTxIdController`, {
error: err.message,
stack: err.stack,
})
res.status(500).json({
error:
'An unexpected error occurred in /synapseTxId. Please try again later.',
Expand Down
2 changes: 2 additions & 0 deletions packages/rest-api/src/controllers/tokenListController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as tokenList from '../constants/bridgeable'
import { logger } from '../middleware/logger'

export const tokenListController = async (_req, res) => {
logger.info(`Successful tokenListController response`)
res.json(tokenList)
}
Loading
Loading