Skip to content

Commit

Permalink
refactor: isolate logging utils for easier staging
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiTimesChi committed Dec 30, 2024
1 parent a70a4a6 commit fc67a32
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 30 deletions.
19 changes: 10 additions & 9 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 @@ -75,15 +76,15 @@ export const fetchWithTimeout = async (
...init,
})
if (!response.ok) {
console.info({ name, url, params, response }, `${name}: not OK`)
logger.info({ name, url, params, response }, `${name}: not OK`)
return null
}
return response
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
console.info({ name, url, timeout, params }, `${name}: timed out`)
logger.info({ name, url, timeout, params }, `${name}: timed out`)
} else {
console.error(
logger.error(
{ name, url, params, error },
`${name}: was not able to get a response`
)
Expand Down Expand Up @@ -147,13 +148,13 @@ export const getAllQuotes = async (): Promise<FastBridgeQuote[]> => {
try {
return unmarshallFastBridgeQuote(quote)
} catch (error) {
console.error({ quote, error }, 'Could not unmarshall quote')
logger.error({ quote, error }, 'Could not unmarshall quote')
return null
}
})
.filter((quote): quote is FastBridgeQuote => quote !== null)
} catch (error) {
console.error({ error }, 'Failed to fetch all quotes')
logger.error({ error }, 'Failed to fetch all quotes')
return []
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ export const getBestRelayerQuote = async (
// Check that response is successful, contains non-zero dest amount, and has a relayer address
const rfqResponse: PutRFQResponseAPI = await response.json()
if (!rfqResponse.success) {
console.info(
logger.info(
{
rfqRequest,
rfqResponse,
Expand All @@ -208,15 +209,15 @@ export const getBestRelayerQuote = async (
return ZeroQuote
}
if (!rfqResponse.dest_amount || !rfqResponse.relayer_address) {
console.error(
logger.error(
{ rfqRequest, rfqResponse },
'Error getting RFQ quote: missing dest_amount or relayer_address in response'
)
return ZeroQuote
}
const destAmount = BigNumber.from(rfqResponse.dest_amount)
if (destAmount.lte(0)) {
console.info({ rfqRequest, rfqResponse }, 'No RFQ quote returned')
logger.info({ rfqRequest, rfqResponse }, 'No RFQ quote returned')
return ZeroQuote
}
return {
Expand All @@ -225,7 +226,7 @@ export const getBestRelayerQuote = async (
quoteID: rfqResponse.quote_id,
}
} catch (error) {
console.error({ rfqRequest, error }, 'Failed to get RFQ quote')
logger.error({ rfqRequest, error }, 'Failed to get RFQ quote')
return ZeroQuote
}
}
3 changes: 2 additions & 1 deletion packages/sdk-router/src/rfq/engine/defaultEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ChainProvider } from '../../router'
import { SynapseIntentPreviewer as PreviewerContract } from '../../typechain/SynapseIntentPreviewer'
import { IDefaultActionsInterface } from '../../typechain/IDefaultActions'
import { isSameAddress } from '../../utils/addressUtils'
import { logger } from '../../utils/logger'
import {
SwapEngine,
SwapEngineRoute,
Expand Down Expand Up @@ -107,7 +108,7 @@ export class DefaultEngine implements SwapEngine {
quote: SwapEngineRoute
): Promise<SwapEngineRoute> {
if (quote.engineID !== this.id || !quote.steps) {
console.error({ quote }, 'DefaultEngine: unexpected quote')
logger.error({ quote }, 'DefaultEngine: unexpected quote')
return getEmptyRoute(this.id)
}
return quote
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk-router/src/rfq/engine/noOpEngine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber } from 'ethers'

import { isSameAddress } from '../../utils/addressUtils'
import { logger } from '../../utils/logger'
import {
SwapEngine,
SwapEngineRoute,
Expand Down Expand Up @@ -29,7 +30,7 @@ export class NoOpEngine implements SwapEngine {
quote: SwapEngineRoute
): Promise<SwapEngineRoute> {
if (quote.engineID !== this.id || !quote.steps) {
console.error({ quote }, 'NoOpEngine: unexpected quote')
logger.error({ quote }, 'NoOpEngine: unexpected quote')
return getEmptyRoute(this.id)
}
return quote
Expand Down
9 changes: 5 additions & 4 deletions packages/sdk-router/src/rfq/engine/odosEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from './swapEngine'
import { AddressMap } from '../../constants'
import { isSameAddress } from '../../utils/addressUtils'
import { logger } from '../../utils/logger'
import { isNativeToken } from '../../utils/handleNativeToken'

const ODOS_API_URL = 'https://api.odos.xyz/sor'
Expand Down Expand Up @@ -120,15 +121,15 @@ export class OdosEngine implements SwapEngine {
!odosQuoteResponse.pathId ||
odosQuoteResponse.outAmounts.length !== 1
) {
console.error(
logger.error(
{ request, odosQuoteResponse },
'Odos: invalid quote response'
)
return EmptyOdosQuote
}
const amountOut = odosQuoteResponse.outAmounts[0]
if (amountOut === '0') {
console.info({ request, odosQuoteResponse }, 'Odos: zero amount out')
logger.info({ request, odosQuoteResponse }, 'Odos: zero amount out')
return EmptyOdosQuote
}
return {
Expand All @@ -146,7 +147,7 @@ export class OdosEngine implements SwapEngine {
quote: OdosQuote
): Promise<SwapEngineRoute> {
if (quote.engineID !== this.id || !quote.assembleRequest) {
console.error({ quote }, 'Odos: unexpected quote')
logger.error({ quote }, 'Odos: unexpected quote')
return getEmptyRoute(this.id)
}
const response = await this.getAssembleResponse(quote.assembleRequest)
Expand All @@ -155,7 +156,7 @@ export class OdosEngine implements SwapEngine {
}
const odosAssembleResponse: OdosAssembleResponse = await response.json()
if (!odosAssembleResponse.transaction) {
console.error(
logger.error(
{ request: quote.assembleRequest, response },
'Odos: invalid assemble response'
)
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-router/src/rfq/sirSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { SynapseIntentRouter } from './sir'
import { ChainProvider } from '../router'
import { ONE_HOUR, TEN_MINUTES } from '../utils/deadlines'
import { logger } from '../utils/logger'
import { isSameAddress } from '../utils/addressUtils'
import { marshallTicker, Ticker } from './ticker'
import { getAllQuotes, getBestRelayerQuote, RelayerQuote } from './api'
Expand Down Expand Up @@ -252,7 +253,7 @@ 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(
logger.warn(
'No params saved in destQuery.rawParams, slippage is not applied'
)
return {
Expand All @@ -268,7 +269,7 @@ export class SynapseIntentRouterSet extends SynapseModuleSet {
isSameAddress(paramsV1.destRelayToken, AddressZero) ||
paramsV1.destRelayAmount.eq(0)
) {
console.warn(
logger.warn(
'No destToken or destAmount saved in destQuery.rawParams, slippage is not applied'
)
return {
Expand Down
13 changes: 7 additions & 6 deletions packages/sdk-router/src/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BigintIsh } from '../constants'
import { Query } from '../module/query'
import { DestRequest } from './types'
import { BridgeToken, FeeConfig, SynapseModule } from '../module'
import { logger } from '../utils/logger'

/**
* Abstract class for a router contract deployed on a chain.
Expand Down Expand Up @@ -117,9 +118,9 @@ export abstract class Router implements SynapseModule {
// Don't filter anything, as the amount of returned queries should match the amount of symbols
return this.getOriginAmountOut(tokenIn, tokenSymbols, amountIn)
} catch (error) {
console.error(
'[SynapseSDK: Router] Failed to fetch origin queries',
error
logger.error(
{ tokenIn, tokenSymbols, amountIn, error },
'[SynapseSDK: Router] Failed to fetch origin queries'
)
throw error
}
Expand All @@ -141,9 +142,9 @@ export abstract class Router implements SynapseModule {
// Don't filter anything, as the amount of returned queries should match the amount of requests
return this.getDestinationAmountOut(requests, tokenOut)
} catch (error) {
console.error(
'[SynapseSDK: Router] Failed to fetch destination queries',
error
logger.error(
{ requests, tokenOut, error },
'[SynapseSDK: Router] Failed to fetch destination queries'
)
throw error
}
Expand Down
15 changes: 12 additions & 3 deletions packages/sdk-router/src/router/routerSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
hasComplexBridgeAction,
} from '../module/query'
import { ONE_WEEK, TEN_MINUTES } from '../utils/deadlines'
import { logger } from '../utils/logger'

export type ChainProvider = {
chainId: number
Expand Down Expand Up @@ -143,9 +144,17 @@ export abstract class RouterSet extends SynapseModuleSet {
bridgeRoute.destQuery.minAmountOut.gt(0)
)
} catch (error) {
console.error(
`[SynapseSDK: RouterSet] Error when trying to calculate the best quote with bridge tokens: ${bridgeTokens} `,
error
logger.error(
{
originChainId,
destChainId,
tokenIn,
tokenOut,
amountIn,
bridgeTokens,
error,
},
'[SynapseSDK: RouterSet] Error when trying to calculate the best quote'
)
return []
}
Expand Down
18 changes: 14 additions & 4 deletions packages/sdk-router/src/utils/RouterCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/* @ts-ignore */
import NodeCache from 'node-cache'

import { logger } from './logger'

// export const CACHE_HYDRATION_DELAY = {
// RFQ: 0,
// CCTP: 0,
Expand Down Expand Up @@ -56,10 +58,18 @@ export const RouterCache = (maxAge: number) => {
cache.set(key, res)
return res
})
.catch((e) => {
console.error(
`[SynapseSDK: RouterCache] Error on ChainID ${this.chainId}: `,
e
.catch((error) => {
logger.error(
{
target,
args,
propertyKey,
name: target.constructor.name,
address: this.address,
chainId: this.chainId,
error,
},
'[SynapseSDK: RouterCache] Error'
)
})
return result
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-router/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const logger = console

0 comments on commit fc67a32

Please sign in to comment.