Skip to content

Commit

Permalink
fix: better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 20, 2024
1 parent b73f836 commit 03af5bd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
21 changes: 11 additions & 10 deletions packages/sdk-router/src/rfq/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FastBridgeQuoteAPI,
unmarshallFastBridgeQuote,
} from './quote'
import { logger } from '../utils/logger'

const API_URL = 'https://rfq-api-stage.omnirpc.io'
const API_TIMEOUT = 2000
Expand Down Expand Up @@ -83,24 +84,24 @@ export const getAllQuotes = async (): Promise<FastBridgeQuote[]> => {
try {
const response = await fetchWithTimeout(`${API_URL}/quotes`, API_TIMEOUT)
if (!response.ok) {
console.error('Error fetching quotes:', response.statusText)
logger.error('Error fetching quotes', { response })
return []
}
// The response is a list of quotes in the FastBridgeQuoteAPI format
const quotes: FastBridgeQuoteAPI[] = await response.json()
console.log(`${API_URL}/quotes`, { quotes })
logger.info(`${API_URL}/quotes`, { quotes })
return quotes
.map((quote) => {
try {
return unmarshallFastBridgeQuote(quote)
} catch (error) {
console.error('Error unmarshalling quote:', error)
logger.error('Error unmarshalling quote', { quote, error })
return null
}
})
.filter((quote): quote is FastBridgeQuote => quote !== null)
} catch (error) {
console.error('Error fetching quotes:', error)
logger.error('Error fetching quotes', { error })
return []
}
}
Expand Down Expand Up @@ -142,28 +143,28 @@ export const getBestRelayerQuote = async (
},
})
if (!response.ok) {
console.error('Error fetching quote', { response })
logger.error('Error fetching quote', { response })
return ZeroQuote
}
// Check that response is successful, contains non-zero dest amount, and has a relayer address
const rfqResponse: PutRFQResponseAPI = await response.json()
console.log(`${API_URL}/rfq`, { rfqRequest, rfqResponse })
logger.info(`${API_URL}/rfq`, { rfqRequest, rfqResponse })
if (!rfqResponse.success) {
console.error('No RFQ quote returned', {
logger.error('No RFQ quote returned', {
reason: rfqResponse.reason ?? 'Unknown reason',
})
return ZeroQuote
}
if (!rfqResponse.dest_amount || !rfqResponse.relayer_address) {
console.error(
logger.error(
'Error fetching quote: missing dest_amount or relayer_address in response:',
{ rfqResponse }
)
return ZeroQuote
}
const destAmount = BigNumber.from(rfqResponse.dest_amount)
if (destAmount.lte(0)) {
console.error('No RFQ quote returned', { rfqResponse })
logger.error('No RFQ quote returned', { rfqResponse })
return ZeroQuote
}
return {
Expand All @@ -172,7 +173,7 @@ export const getBestRelayerQuote = async (
quoteID: rfqResponse.quote_id,
}
} catch (error) {
console.error('Error fetching quote:', { error })
logger.error('Error fetching quote', { error })
return ZeroQuote
}
}
13 changes: 7 additions & 6 deletions packages/sdk-router/src/rfq/engine/paraSwapEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { StepParams } from '../steps'
import { AMOUNT_NOT_PRESENT, encodeZapData } from '../zapData'
import { ChainProvider } from '../../router'
import { isNativeToken } from '../../utils/handleNativeToken'
import { logger } from '../../utils/logger'

const PARASWAP_API_URL = 'https://api.paraswap.io/swap'
const PARASWAP_API_TIMEOUT = 2000
Expand Down Expand Up @@ -148,7 +149,7 @@ export class ParaSwapEngine implements SwapEngine {
if (request.slippage > MAX_SLIPPAGE) {
request.slippage = MAX_SLIPPAGE
}
console.log('Fetching ParaSwap response', { request })
logger.info('Fetching ParaSwap response', { request })
// Stringify every value in the request
const params = new URLSearchParams(
Object.entries(request).map(([k, v]) => {
Expand All @@ -158,14 +159,14 @@ export class ParaSwapEngine implements SwapEngine {
const url = `${PARASWAP_API_URL}?${params.toString()}`
const response = await fetchWithTimeout(url, PARASWAP_API_TIMEOUT)
if (!response.ok) {
console.error('Error fetching ParaSwap response', { url, response })
logger.error('Error fetching ParaSwap response', { url, response })
return EmptyParaSwapResponse
}
const paraSwapResponse: ParaSwapResponse = await response.json()
console.log('Fetched ParaSwap response', { url, paraSwapResponse })
logger.info('Fetched ParaSwap response', { url, paraSwapResponse })
return paraSwapResponse
} catch (error) {
console.error('Error fetching ParaSwap response', { error })
logger.error('Error fetching ParaSwap response', { error })
return EmptyParaSwapResponse
}
}
Expand Down Expand Up @@ -207,14 +208,14 @@ export class ParaSwapEngine implements SwapEngine {
}
const provider = this.providers[chainId]
if (!provider) {
console.error('No provider found for chainId', chainId)
logger.error('No provider found', { chainId })
return 0
}
const tokenContract = new Contract(token, erc20ABI, provider) as ERC20
try {
return tokenContract.decimals()
} catch (error) {
console.error('Error fetching token decimals:', error)
logger.error('Error fetching token decimals', { token, error })
return 0
}
}
Expand Down
19 changes: 15 additions & 4 deletions packages/sdk-router/src/rfq/sirSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SynapseIntentRouter } from './sir'
import { ChainProvider } from '../router'
import { ONE_HOUR, TEN_MINUTES } from '../utils/deadlines'
import { isSameAddress } from '../utils/addressUtils'
import { logger } from '../utils/logger'
import { marshallTicker, Ticker } from './ticker'
import { getAllQuotes, getBestRelayerQuote } from './api'
import {
Expand Down Expand Up @@ -231,8 +232,12 @@ export class SynapseIntentRouterSet extends SynapseModuleSet {
): { originQuery: Query; destQuery: Query } {
// We should have saved neccessary params within dstQuery.rawParams
if (hexDataLength(destQueryPrecise.rawParams) === 0) {
console.warn(
'No params saved in destQuery.rawParams, slippage is not applied'
logger.warn(
'No params saved in destQuery.rawParams, slippage is not applied',
{
originQuery: originQueryPrecise,
destQuery: destQueryPrecise,
}
)
return {
originQuery: originQueryPrecise,
Expand All @@ -247,8 +252,14 @@ export class SynapseIntentRouterSet extends SynapseModuleSet {
isSameAddress(paramsV1.destRelayToken, AddressZero) ||
paramsV1.destRelayAmount.eq(0)
) {
console.warn(
'No destToken or destAmount saved in destQuery.rawParams, slippage is not applied'
logger.warn(
'No destToken or destAmount saved in destQuery.rawParams, slippage is not applied',
{
originQuery: originQueryPrecise,
destQuery: destQueryPrecise,
paramsV1,
paramsV2,
}
)
return {
originQuery: originQueryPrecise,
Expand Down
7 changes: 7 additions & 0 deletions packages/sdk-router/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import winston from 'winston'

Check failure on line 1 in packages/sdk-router/src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / lint

'winston' should be listed in the project's dependencies. Run 'npm i -S winston' to add it

Check failure on line 1 in packages/sdk-router/src/utils/logger.ts

View workflow job for this annotation

GitHub Actions / lint

'winston' should be listed in the project's dependencies. Run 'npm i -S winston' to add it

export const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [new winston.transports.Console()],
})

0 comments on commit 03af5bd

Please sign in to comment.