forked from DefiLlama/dimension-adapters
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update dependencies and fix error handling in interactive.js
- Loading branch information
Showing
8 changed files
with
141 additions
and
1,179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,63 @@ | ||
import allSettled, { PromiseRejection, PromiseResolution, PromiseResult } from 'promise.allsettled' | ||
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchResult, FetchResultGeneric, IJSON } from '../types' | ||
import { Balances } from '@defillama/sdk' | ||
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchResultGeneric, } from '../types' | ||
|
||
const ONE_DAY_IN_SECONDS = 60 * 60 * 24 | ||
|
||
export type IRunAdapterResponseFulfilled = FetchResult & { | ||
chain: string | ||
startTimestamp: number | ||
} | ||
export interface IRunAdapterResponseRejected { | ||
chain: string | ||
timestamp: number | ||
error: Error | ||
} | ||
|
||
export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurrentDayTimestamp: number, chainBlocks: ChainBlocks, id?: string, version?: string) { | ||
const cleanPreviousDayTimestamp = cleanCurrentDayTimestamp - ONE_DAY_IN_SECONDS | ||
const chains = Object.keys(volumeAdapter).filter(c => c !== DISABLED_ADAPTER_KEY) | ||
const validStart = ((await Promise.all(chains.map(async (chain) => { | ||
const start = await volumeAdapter[chain]?.start().catch(() => { | ||
console.error(`Failed to get start time for ${id} ${version} ${chain}`) | ||
return Math.trunc(Date.now() / 1000) | ||
}) | ||
return [chain, start !== undefined && (start <= cleanPreviousDayTimestamp), start] | ||
}))) as [string, boolean, number][]).reduce((acc, curr) => ({ ...acc, [curr[0]]: [curr[1], curr[2]] }), {} as IJSON<(boolean | number)[]>) | ||
return allSettled(chains | ||
.filter(chain => validStart[chain][0]) | ||
.map(async (chain) => { | ||
const fetchFunction = volumeAdapter[chain].customBackfill ?? volumeAdapter[chain].fetch | ||
try { | ||
const startTimestamp = validStart[chain][1] | ||
const result: FetchResultGeneric = await fetchFunction(cleanCurrentDayTimestamp - 1, chainBlocks); | ||
if (id) | ||
console.log("Result before cleaning", id, version, cleanCurrentDayTimestamp, chain, result, JSON.stringify(chainBlocks ?? {})) | ||
cleanResult(result) | ||
return Promise.resolve({ | ||
chain, | ||
startTimestamp, | ||
...result | ||
}) | ||
} catch (e) { | ||
console.error(`Failed to get value on ${chain}: ${e}`) | ||
return Promise.reject({ chain, error: e, timestamp: cleanPreviousDayTimestamp }); | ||
} | ||
})) as Promise<PromiseResult<IRunAdapterResponseFulfilled, IRunAdapterResponseRejected>[]> | ||
} | ||
const cleanPreviousDayTimestamp = cleanCurrentDayTimestamp - ONE_DAY_IN_SECONDS | ||
const chains = Object.keys(volumeAdapter).filter(c => c !== DISABLED_ADAPTER_KEY) | ||
const validStart = {} as { | ||
[chain: string]: { | ||
canRun: boolean, | ||
startTimestamp: number | ||
} | ||
} | ||
await Promise.all(chains.map(setChainValidStart)) | ||
|
||
const cleanResult = (obj: any) => { | ||
Object.keys(obj).forEach(key => { | ||
if (typeof obj[key] === 'object') cleanResult(obj[key]) | ||
else if (!okAttribute(obj[key])) { | ||
console.log("Wrong value", obj[key], "with key", key) | ||
delete obj[key] | ||
} | ||
}) | ||
} | ||
return Promise.all(chains.filter(chain => validStart[chain]?.canRun).map(getChainResult)) | ||
|
||
const okAttribute = (value: any) => { | ||
return !(value && Number.isNaN(+value)) | ||
} | ||
async function getChainResult(chain: string) { | ||
const fetchFunction = volumeAdapter[chain].customBackfill ?? volumeAdapter[chain].fetch | ||
try { | ||
const result: FetchResultGeneric = await fetchFunction(cleanCurrentDayTimestamp - 1, chainBlocks); | ||
const ignoreKeys = ['timestamp', 'block'] | ||
if (id) | ||
console.log("Result before cleaning", id, version, cleanCurrentDayTimestamp, chain, result, JSON.stringify(chainBlocks ?? {})) | ||
for (const [key, value] of Object.entries(result)) { | ||
if (ignoreKeys.includes(key)) continue; | ||
if (value === undefined || value === null) throw new Error(`Value: ${value} ${key} is undefined or null`) | ||
if (value instanceof Balances) result[key] = await value.getUSDString() | ||
result[key] = +Number(value).toFixed(0) | ||
if (isNaN(result[key] as number)) throw new Error(`Value: ${value} ${key} is NaN`) | ||
} | ||
return { | ||
chain, | ||
startTimestamp: validStart[chain].startTimestamp, | ||
...result | ||
} | ||
} catch (error) { | ||
throw { chain, error } | ||
} | ||
} | ||
|
||
const isFulfilled = <T,>(p: PromiseResult<T>): p is PromiseResolution<T> => p.status === 'fulfilled'; | ||
const isRejected = <T, E>(p: PromiseResult<T, E>): p is PromiseRejection<E> => p.status === 'rejected'; | ||
export const getFulfilledResults = <T,>(results: PromiseResult<T>[]) => results.filter(isFulfilled).map(r => r.value) | ||
export const getRejectedResults = <T, E>(results: PromiseResult<T, E>[]) => results.filter(isRejected).map(r => r.reason) | ||
async function setChainValidStart(chain: string) { | ||
const _start = volumeAdapter[chain]?.start | ||
if (_start === undefined) return; | ||
if (typeof _start === 'number') { | ||
validStart[chain] = { | ||
canRun: _start <= cleanPreviousDayTimestamp, | ||
startTimestamp: _start | ||
} | ||
} else if (_start) { | ||
const defaultStart = Math.trunc(Date.now() / 1000) | ||
const start = await (_start as any)().catch(() => { | ||
console.error(`Failed to get start time for ${id} ${version} ${chain}`) | ||
return defaultStart | ||
}) | ||
validStart[chain] = { | ||
canRun: typeof start === 'number' && start <= cleanPreviousDayTimestamp, | ||
startTimestamp: start | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.