import { Integrator } from '@0x/asset-swapper' ; import { MetricsProxy } from '@0x/asset-swapper/lib/src/utils/quote_requestor' ; import { BigNumber } from '@0x/utils' ; import { Counter , Histogram , Summary } from 'prom-client' ; const ORDER_EXPIRED_TOO_SOON = new Counter ( { name : 'rfq_order_expired_too_soon' , help : 'RFQ Order expired too soon' , labelNames : [ 'maker' , 'isLastLook' ] , } ) ; const EXPIRATION_FOR_VALID_ORDER_SECONDS = new Histogram ( { name : 'rfq_expiration_for_valid_order' , help : 'Order expiration in seconds for ' , labelNames : [ 'maker' , 'isLastLook' ] , buckets : [ 0 , 30 , 45 , 60 , 75 , 90 , 105 , 120 , 135 , 150 , 165 , 180 , 195 , 210 , 225 , 240 , 255 , 270 , 285 , 300 ] , } ) ; const ORDER_FILL_RATIO_WARNING_RANGE = new Counter ( { name : 'rfq_fill_ratio_warning' , help : 'Fill ratio is in warning range (99%) usually due to a bug' , labelNames : [ 'maker' , 'isLastLook' ] , } ) ; const RFQ_MAKER_NETWORK_INTERACTION_COUNTER = new Counter ( { name : 'rfq_maker_network_interaction_counter' , help : 'Provides stats around market maker network interactions' , labelNames : [ 'isLastLook' , 'integratorLabel' , 'url' , 'quoteType' , 'included' , 'statusCode' , 'market' ] , } ) ; const RFQ_MAKER_NETWORK_INTERACTION_SUMMARY = new Summary ( { name : 'rfq_maker_network_interaction_summary' , help : 'Provides stats around market maker network interactions' , labelNames : [ 'isLastLook' , 'integratorLabel' , 'url' , 'quoteType' , 'included' , 'statusCode' , 'market' ] , } ) ; const KNOWN_TOKENS = { '0x6b175474e89094c44da98b954eedeac495271d0f' : 'DAI' , '0xdac17f958d2ee523a2206206994597c13d831ec7' : 'USDT' , '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48' : 'USDC' , '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' : 'WBTC' , '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2' : 'WETH' , } ; param tokenSold param tokenPurchased returns function getMarketLabel ( tokenSold , tokenPurchased ) { const items = [ tokenSold . toLowerCase ( ) , tokenPurchased . toLowerCase ( ) ] ; items . sort ( ) ; const tokenA = KNOWN_TOKENS [ items [ 0 ] ] ; const tokenB = KNOWN_TOKENS [ items [ 1 ] ] ; if ( ! tokenA || ! tokenB ) { return 'Other' ; } return `${ tokenA }-${ tokenB }` ; } export const METRICS_PROXY = { incrementExpirationToSoonCounter : ( isLastLook , maker ) => { ORDER_EXPIRED_TOO_SOON . labels ( maker , isLastLook . toString ( ) ) . inc ( ) ; } , measureExpirationForValidOrder : ( isLastLook , maker , expirationTimeSeconds ) => { EXPIRATION_FOR_VALID_ORDER_SECONDS . labels ( maker , isLastLook . toString ( ) ) . observe ( new BigNumber ( expirationTimeSeconds ) . toNumber ( ) ) ; } , incrementFillRatioWarningCounter : ( isLastLook , maker ) => { ORDER_FILL_RATIO_WARNING_RANGE . labels ( maker , isLastLook . toString ( ) ) . inc ( ) ; } , logRfqMakerNetworkInteraction : ( interaction ) => { const { isLastLook , integrator , url , quoteType , included : isIncluded , statusCode , sellTokenAddress , buyTokenAddress , latencyMs , } = interaction ; const market = getMarketLabel ( sellTokenAddress , buyTokenAddress ) ; const payload = [ isLastLook . toString ( ) , integrator . label , url , quoteType , isIncluded . toString ( ) , `${ statusCode || 'N/A' }` , market , ] ; RFQ_MAKER_NETWORK_INTERACTION_COUNTER . labels ( ... payload ) . inc ( ) ; RFQ_MAKER_NETWORK_INTERACTION_SUMMARY . labels ( ... payload ) . observe ( latencyMs ) ; } , } ;