Skip to content

Commit 2ab4abb

Browse files
committed
Add legacy v2/coinrankList endpoint
1 parent 8dab1f6 commit 2ab4abb

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/v3/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
ratesV2,
99
rateV2,
1010
sendCoinrankAssetV2,
11+
sendCoinrankListV2,
1112
sendCoinranksV2
1213
} from './legacyRouter'
1314
import { heartbeatV3, ratesV3 } from './router'
@@ -23,6 +24,7 @@ function server(): void {
2324
'/v2/exchangeRates': pickMethod({ POST: ratesV2 }),
2425
'/v2/coinrank': pickMethod({ GET: sendCoinranksV2 }),
2526
'/v2/coinrankAsset/([^/]+)': pickMethod({ GET: sendCoinrankAssetV2 }),
27+
'/v2/coinrankList': pickMethod({ GET: sendCoinrankListV2 }),
2628
'/v3/rates': pickMethod({ POST: ratesV3 })
2729
})
2830

src/v3/legacyRouter.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type CoinrankAssetReq,
99
type CoinrankReq
1010
} from '../types'
11+
import { hgetallAsync } from '../utils/dbUtils'
1112
import { ratesV3, v2CurrencyCodeMapSyncDoc } from './router'
1213
import { asGetRatesParams } from './types'
1314
import { convertV2, convertV3ToV2 } from './v2converter'
@@ -214,3 +215,22 @@ export const sendCoinrankAssetV2 = async (
214215
}
215216
}
216217
}
218+
219+
export const sendCoinrankListV2 = async (
220+
request: ExpressRequest
221+
): Promise<HttpResponse> => {
222+
try {
223+
const data = await hgetallAsync('coingeckoassets')
224+
return {
225+
status: 200,
226+
headers: { 'content-type': 'application/json' },
227+
body: JSON.stringify({ data })
228+
}
229+
} catch (e: unknown) {
230+
return {
231+
status: 500,
232+
headers: { 'content-type': 'application/json' },
233+
body: JSON.stringify({ error: `Internal server error ${String(e)}` })
234+
}
235+
}
236+
}

src/v3/providers/coingecko/coingecko.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { asCouchDoc, syncedDocument } from 'edge-server-tools'
33

44
import { coinrankEngine } from '../../../coinrankEngine'
55
import { config } from '../../../config'
6+
import { coingeckoAssetsInternal } from '../../../providers/coingecko'
7+
import type { AssetMap } from '../../../rates'
8+
import { hsetAsync } from '../../../utils/dbUtils'
69
import { dateOnly, snooze } from '../../../utils/utils'
710
import { TOKEN_TYPES_KEY } from '../../constants'
811
import {
@@ -289,6 +292,17 @@ const getHistoricalRates = async (
289292
return out
290293
}
291294

295+
const coingeckoAssetsEngine: RateEngine = async () => {
296+
const assets = await coingeckoAssetsInternal(3000)
297+
const assetMap: AssetMap = {}
298+
for (const asset of assets) {
299+
const currencyCode = asset.symbol.toUpperCase()
300+
const { id } = asset
301+
assetMap[currencyCode] ??= id
302+
}
303+
await hsetAsync('coingeckoassets', assetMap)
304+
}
305+
292306
export const coingecko: RateProvider = {
293307
providerId: 'coingecko',
294308
type: 'api',
@@ -362,6 +376,10 @@ export const coingecko: RateProvider = {
362376
engine: async () => {
363377
await coinrankEngine(true)
364378
}
379+
},
380+
{
381+
frequency: 'day',
382+
engine: coingeckoAssetsEngine
365383
}
366384
]
367385
}

0 commit comments

Comments
 (0)