@@ -2,7 +2,12 @@ import type { HttpResponse } from 'serverlet'
22import type { ExpressRequest } from 'serverlet/express'
33
44import { asExchangeRatesReq , getRedisMarkets } from '../exchangeRateRouter'
5- import { asCoinrankReq , type CoinrankReq } from '../types'
5+ import {
6+ asCoinrankAssetReq ,
7+ asCoinrankReq ,
8+ type CoinrankAssetReq ,
9+ type CoinrankReq
10+ } from '../types'
611import { ratesV3 , v2CurrencyCodeMapSyncDoc } from './router'
712import { asGetRatesParams } from './types'
813import { convertV2 , convertV3ToV2 } from './v2converter'
@@ -141,3 +146,71 @@ export const sendCoinranksV2 = async (
141146 }
142147 }
143148}
149+
150+ export const sendCoinrankAssetV2 = async (
151+ request : ExpressRequest
152+ ) : Promise < HttpResponse > => {
153+ if ( request . req . query == null ) {
154+ return {
155+ status : 400 ,
156+ headers : { 'content-type' : 'application/json' } ,
157+ body : JSON . stringify ( { error : 'Invalid request query' } )
158+ }
159+ }
160+
161+ let query : CoinrankAssetReq
162+ try {
163+ query = asCoinrankAssetReq ( request . req . query )
164+ } catch ( e ) {
165+ return {
166+ status : 400 ,
167+ headers : { 'content-type' : 'application/json' } ,
168+ body : JSON . stringify ( { error : `Invalid request query ${ String ( e ) } ` } )
169+ }
170+ }
171+ const { fiatCode } = query
172+ const pathParts = request . path . split ( '/' )
173+ const assetId = pathParts [ pathParts . length - 1 ]
174+ if ( assetId == null ) {
175+ return {
176+ status : 400 ,
177+ headers : { 'content-type' : 'application/json' } ,
178+ body : JSON . stringify ( { error : 'Invalid request path' } )
179+ }
180+ }
181+ try {
182+ const redisResult = await getRedisMarkets ( fiatCode )
183+
184+ if ( redisResult == null ) {
185+ return {
186+ status : 400 ,
187+ headers : { 'content-type' : 'application/json' } ,
188+ body : JSON . stringify ( {
189+ error : `Unable to get results for fiatCode ${ fiatCode } `
190+ } )
191+ }
192+ }
193+
194+ const { markets } = redisResult
195+ const market = markets . find ( m => m . assetId === assetId )
196+ if ( market == null ) {
197+ return {
198+ status : 404 ,
199+ headers : { 'content-type' : 'application/json' } ,
200+ body : JSON . stringify ( { error : `assetId ${ assetId } not found` } )
201+ }
202+ }
203+
204+ return {
205+ status : 200 ,
206+ headers : { 'content-type' : 'application/json' } ,
207+ body : JSON . stringify ( { data : market } )
208+ }
209+ } catch ( e : unknown ) {
210+ return {
211+ status : 500 ,
212+ headers : { 'content-type' : 'application/json' } ,
213+ body : JSON . stringify ( { error : `Internal server error ${ String ( e ) } ` } )
214+ }
215+ }
216+ }
0 commit comments