Skip to content

Commit

Permalink
api2: add /charts route
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Sep 13, 2024
1 parent 6e49774 commit 928ba87
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 49 deletions.
6 changes: 2 additions & 4 deletions api2/cron-task/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as rates from "../../src/getRates";
import sluggifyPegged from "../../src/peggedAssets/utils/sluggifyPegged";
import { storeRouteData } from "../file-cache";
import storePeggedPrices from "./storePeggedPrices";
import storeCharts, { craftChartsResponse } from "./storeCharts";
import storeCharts, { craftChartsResponse, storeChartsPart2 } from "./storeCharts";
import storeStablecoins from "./getStableCoins";
import { craftStablecoinPricesResponse } from "./getStablecoinPrices";
import { craftStablecoinChainsResponse } from "./getStablecoinChains";
Expand Down Expand Up @@ -40,8 +40,8 @@ async function run() {
const chainChartMap: any = {}
const recentProtocolData: any = {}


const timeWrapper = {
storeChartsPart2: () => storeChartsPart2(assetChainMap),
storePrices,
storeStablecoinChains,
storePeggedAssets,
Expand Down Expand Up @@ -96,8 +96,6 @@ async function run() {
}
}



async function storeChainChartData() {
const frontendKey = 'all-llama-app'
const allChartsStartTimestamp = 1617148800 // for /stablecoins page, charts begin on April 1, 2021, to reduce size of page
Expand Down
81 changes: 42 additions & 39 deletions api2/cron-task/storeCharts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,42 @@ export default async function handler() {
cache.rateTimestamps = rateTimestamps
console.timeEnd(timeKey)

/*
console.time('storeCharts')
const commonOptions = {
lastPrices,
historicalPrices,
historicalRates,
priceTimestamps,
rateTimestamps,
peggedAssetsData: cache.peggedAssetsData,
}
// store overall chart
const allData = await craftChartsResponse({ ...commonOptions, chain: "all" });
await storeRouteData('charts/all/all', allData)
// store chain charts
const chains = [Object.keys(chainCoingeckoIds), Object.values(normalizedChainReplacements)].flat()
for (let chain of chains) {
const normalizedChain = normalizeChain(chain);
const chainData = await craftChartsResponse({ ...commonOptions, chain, });
if (chainData.length) {
await storeRouteData(`charts/${normalizedChain}`, chainData)
} else {
console.log(`No data for ${chain}`)
}
}
// store pegged asset charts
for (const pegged of peggedAssets) {
const id = pegged.id;
const chart = await craftChartsResponse({ ...commonOptions, peggedID: id });
await storeRouteData(`charts/all/${id}`, chart)
}
console.timeEnd('storeCharts') */
}

export async function storeChartsPart2(assetChainMap: any) {
const { lastPrices, historicalRates, priceTimestamps, rateTimestamps, } = cache

const commonOptions = {
assetChainMap,
lastPrices,
historicalPrices: cache.historicalPrices,
historicalRates,
priceTimestamps,
rateTimestamps,
peggedAssetsData: cache.peggedAssetsData,
}
// store overall chart
const allData = await craftChartsResponse({ ...commonOptions, chain: "all" });
await storeRouteData('charts/all/all', allData)

// store chain charts
const chains = [Object.keys(chainCoingeckoIds), Object.values(normalizedChainReplacements)].flat()
for (let chain of chains) {
const normalizedChain = normalizeChain(chain);
const chainData = await craftChartsResponse({ ...commonOptions, chain: normalizedChain, });
// if (chainData.length) {
await storeRouteData(`charts/${normalizedChain}`, chainData)
// } else {
// console.log(`No data for ${chain} ${normalizedChain}`)
// }
}

// store pegged asset charts
for (const pegged of peggedAssets) {
const id = pegged.id;
const chart = await craftChartsResponse({ ...commonOptions, peggedID: id });
await storeRouteData(`charts/all/${id}`, chart)
}

}

Expand Down Expand Up @@ -115,19 +118,19 @@ async function getPeggedAssetsData() {

function replaceAvalanceAvax(obj) {
if (typeof obj !== 'object' || obj === null) {
return; // Not an object or is null, do nothing
return; // Not an object or is null, do nothing
}

if (obj.hasOwnProperty('avalanche')) {
obj['avax'] = obj['avalanche']; // Create 'avax' key with 'avalanche' value
delete obj['avalanche']; // Remove 'avalanche' key
obj['avax'] = obj['avalanche']; // Create 'avax' key with 'avalanche' value
delete obj['avalanche']; // Remove 'avalanche' key
}

// Recursively apply to all object values
Object.values(obj).forEach(value => {
if (typeof value === 'object') {
replaceAvalanceAvax(value); // Recursive call
}
if (typeof value === 'object') {
replaceAvalanceAvax(value); // Recursive call
}
});
}

Expand Down
13 changes: 13 additions & 0 deletions api2/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ export default function setRoutes(router: HyperExpress.Router) {
router.get("/stablecoinchains", defaultFileHandler);
router.get("/stablecoins", defaultFileHandler);
router.get("/stablecoin/:stablecoin", defaultFileHandler);
router.get("/stablecoin/:stablecoin", defaultFileHandler);

router.get("/charts/all/:peggedID", ew(async (req: any, res: any) => {
let { peggedID } = req.path_parameters;
peggedID = normalizeChain(peggedID)
return fileResponse('/charts/all/' + peggedID, res);
}))

router.get("/charts/:chain", ew(async (req: any, res: any) => {
let { chain } = req.path_parameters;
chain = normalizeChain(chain)
return fileResponse('/charts/' + chain, res);
}))

router.get("/stablecoindominance/:chain", ew(async (req: any, res: any) => {
let { chain } = req.path_parameters;
Expand Down
12 changes: 6 additions & 6 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ functions:
# memorySize: 3000
# events:
# - schedule: cron(15/30 * * * ? *)
storeGetStablecoinChart:
handler: src/storeGetStablecoinChart.default
timeout: 900
memorySize: 5120
events:
- schedule: cron(50 * * * ? *)
# storeGetStablecoinChart:
# handler: src/storeGetStablecoinChart.default
# timeout: 900
# memorySize: 5120
# events:
# - schedule: cron(50 * * * ? *)

resources:
# DynamoDB and api gateway errors
Expand Down

0 comments on commit 928ba87

Please sign in to comment.