Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rver into add-stellar-glo-dollar
  • Loading branch information
ElliotFriend committed Jul 18, 2024
2 parents c78fb10 + 995efce commit 3f38977
Show file tree
Hide file tree
Showing 171 changed files with 1,510 additions and 18,027 deletions.
19 changes: 12 additions & 7 deletions api2/cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { getLastRecord, historicalRates } from "../src/peggedAssets/utils/getLastRecord";
import { readFromPGCache, writeToPGCache } from "./file-cache";

export enum CacheType {
CRON,
API_SERVER
}

type Prices = {
[coinGeckoId: string]: number;
Expand All @@ -23,17 +27,18 @@ export const cache: {
historicalPrices?: DailyPeggedPrices[]
} = {}

const MINUTES = 60 * 1000
const HOUR = 60 * MINUTES
const cacheFile = 'stablecoin-cache'

export async function initCache() {
export async function initCache(cacheType = CacheType.API_SERVER) {
console.time('Cache initialized')
const _cache = await readFromPGCache('cron-cache') ?? {}
Object.keys(_cache).forEach(key => cache[key] = _cache[key])
cache.rates = await getLastRecord(historicalRates);
if (cacheType === CacheType.CRON) {
const _cache = await readFromPGCache(cacheFile) ?? {}
Object.keys(_cache).forEach(key => cache[key] = _cache[key])
cache.rates = await getLastRecord(historicalRates);
}
console.timeEnd('Cache initialized')
}

export async function saveCache() {
await writeToPGCache('cron-cache', cache)
await writeToPGCache(cacheFile, cache)
}
173 changes: 87 additions & 86 deletions api2/cron-task/getStableCoins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function getTVLOfRecordClosestToTimestamp(
for (let i = 0; i < items.length; i++) {
if (items[i].SK <= endSK && items[i].SK >= endSK - searchWidth) {
if (!record || items[i].SK > record.SK)
record = items[i]
record = items[i]
}
}

Expand All @@ -32,101 +32,102 @@ function craftProtocolsResponse(
let prices = cache.peggedPrices!

const response = (
peggedAssets.map((pegged) => {
const pegType = pegged.pegType;
const { balances, lastBalance } = cache.peggedAssetsData?.[pegged.id] ?? {}
const lastHourlyRecord = lastBalance
if (lastHourlyRecord === undefined) {
return null;
peggedAssets.map((pegged) => {
const pegType = pegged.pegType;
const { balances, lastBalance } = cache.peggedAssetsData?.[pegged.id] ?? {}
const lastHourlyRecord = lastBalance
if (lastHourlyRecord === undefined) {
return null;
}
const lastSK = lastHourlyRecord.SK;
if (lastSK === undefined) {
return null;
}
const lastDailyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInDay,
secondsInDay // temporary, update later
);
const lastWeeklyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInWeek,
secondsInDay // temporary, update later
);
const lastMonthlyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInDay * 30,
secondsInDay // temporary, update later
);
const chainCirculating = {} as {
[chain: string]: any;
};
const chains: string[] = [];
Object.entries(lastHourlyRecord).forEach(([chain, issuances]: any) => {
if (nonChains.includes(chain)) {
return;
}
const lastSK = lastHourlyRecord.SK;
if (lastSK === undefined) {
return null;
}
const lastDailyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInDay,
secondsInDay // temporary, update later
);
const lastWeeklyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInWeek,
secondsInDay // temporary, update later
);
const lastMonthlyPeggedRecord = getTVLOfRecordClosestToTimestamp(
balances,
lastSK - secondsInDay * 30,
secondsInDay // temporary, update later
);
const chainCirculating = {} as {
[chain: string]: any;
};
const chains: string[] = [];
Object.entries(lastHourlyRecord).forEach(([chain, issuances]: any) => {
if (nonChains.includes(chain)) {
return;
}
const chainDisplayName = getChainDisplayName(chain, useNewChainNames);
chainCirculating[chainDisplayName] =
chainCirculating[chainDisplayName] || {};
chainCirculating[chainDisplayName].current = issuances.circulating;
chainCirculating[chainDisplayName].circulatingPrevDay =
lastDailyPeggedRecord && lastDailyPeggedRecord[chain]
? lastDailyPeggedRecord[chain].circulating ?? 0
: 0;
chainCirculating[chainDisplayName].circulatingPrevWeek =
lastWeeklyPeggedRecord && lastWeeklyPeggedRecord[chain]
? lastWeeklyPeggedRecord[chain].circulating ?? 0
: 0;
chainCirculating[chainDisplayName].circulatingPrevMonth =
lastMonthlyPeggedRecord && lastMonthlyPeggedRecord[chain]
? lastMonthlyPeggedRecord[chain].circulating ?? 0
: 0;
addToChains(chains, chainDisplayName);
});
const chainDisplayName = getChainDisplayName(chain, useNewChainNames);
chainCirculating[chainDisplayName] =
chainCirculating[chainDisplayName] || {};
chainCirculating[chainDisplayName].current = issuances.circulating;
chainCirculating[chainDisplayName].circulatingPrevDay =
lastDailyPeggedRecord && lastDailyPeggedRecord[chain]
? lastDailyPeggedRecord[chain].circulating ?? 0
: 0;
chainCirculating[chainDisplayName].circulatingPrevWeek =
lastWeeklyPeggedRecord && lastWeeklyPeggedRecord[chain]
? lastWeeklyPeggedRecord[chain].circulating ?? 0
: 0;
chainCirculating[chainDisplayName].circulatingPrevMonth =
lastMonthlyPeggedRecord && lastMonthlyPeggedRecord[chain]
? lastMonthlyPeggedRecord[chain].circulating ?? 0
: 0;
addToChains(chains, chainDisplayName);
});

const dataToReturn = {
id: pegged.id,
name: pegged.name,
symbol: pegged.symbol,
gecko_id: pegged.gecko_id,
pegType: pegged.pegType,
priceSource: pegged.priceSource,
pegMechanism: pegged.pegMechanism,
circulating: lastHourlyRecord.totalCirculating.circulating,
circulatingPrevDay: lastDailyPeggedRecord
? lastDailyPeggedRecord.totalCirculating.circulating
: 0,
circulatingPrevWeek: lastWeeklyPeggedRecord
? lastWeeklyPeggedRecord.totalCirculating.circulating
: 0,
circulatingPrevMonth: lastMonthlyPeggedRecord
? lastMonthlyPeggedRecord.totalCirculating.circulating
: 0,
chainCirculating,
chains: chains.sort(
(a, b) =>
chainCirculating[b].current[pegType] -
chainCirculating[a].current[pegType]
),
} as any;
dataToReturn.price = prices[pegged.gecko_id] ?? null;
if (pegged.delisted) dataToReturn.delisted = true;
return dataToReturn;
})
)
const dataToReturn = {
id: pegged.id,
name: pegged.name,
symbol: pegged.symbol,
gecko_id: pegged.gecko_id,
pegType: pegged.pegType,
priceSource: pegged.priceSource,
pegMechanism: pegged.pegMechanism,
circulating: lastHourlyRecord.totalCirculating.circulating,
circulatingPrevDay: lastDailyPeggedRecord
? lastDailyPeggedRecord.totalCirculating.circulating
: 0,
circulatingPrevWeek: lastWeeklyPeggedRecord
? lastWeeklyPeggedRecord.totalCirculating.circulating
: 0,
circulatingPrevMonth: lastMonthlyPeggedRecord
? lastMonthlyPeggedRecord.totalCirculating.circulating
: 0,
chainCirculating,
chains: chains.sort(
(a, b) =>
chainCirculating[b].current[pegType] -
chainCirculating[a].current[pegType]
),
} as any;
dataToReturn.price = prices[pegged.gecko_id] ?? null;
if (pegged.delisted) dataToReturn.delisted = true;
return dataToReturn;
})
)
.filter((pegged) => pegged !== null)
.sort((a, b) => b.circulating - a.circulating);
return response;
}

export default async function handler() {
export default async function handler({ peggedPrices }: { peggedPrices?: any; } = {}) {
const pegged = craftProtocolsResponse(true);
let response: any = {
peggedAssets: pegged,
};
const chainData = await craftStablecoinChainsResponse();
response.chains = chainData;
await storeRouteData('stablecoins', response)
const chainData = await craftStablecoinChainsResponse({ peggedPrices });
response.chains = chainData;
await storeRouteData('stablecoins', response)
return response;
};

Loading

0 comments on commit 3f38977

Please sign in to comment.