Skip to content

Commit

Permalink
Fix error handling and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
g1nt0ki committed Feb 5, 2024
1 parent 554a296 commit b98f948
Show file tree
Hide file tree
Showing 97 changed files with 3,128 additions and 3,614 deletions.
25 changes: 24 additions & 1 deletion adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,32 @@ export type FetchResultGeneric = FetchResultBase & {
[key: string]: FetchResponseValue | undefined;
}

export type FetchOptions = {
createBalances: () => Balances;
getBlock: (timestamp: number, chain: string, chainBlocks: ChainBlocks) => Promise<number>;
getLogs: (params: FetchGetLogsOptions) => Promise<any[]>;
toTimestamp: number;
fromTimestamp: number;
getFromBlock: () => Promise<number>;
getToBlock: () => Promise<number>;
chain: string,
}

export type FetchGetLogsOptions = {
eventAbi: string,
target?: string,
targets?: string[],
onlyArgs?: boolean,
fromBlock?: number,
toBlock?: number,
flatten?: boolean,
topics?: string[],
}

export type Fetch = (
timestamp: number,
chainBlocks: ChainBlocks
chainBlocks: ChainBlocks,
options?: FetchOptions,
) => Promise<FetchResult>;

export type IStartTimestamp = () => Promise<number>
Expand Down
41 changes: 36 additions & 5 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Balances } from '@defillama/sdk'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchResultGeneric, } from '../types'
import { Balances, getEventLogs } from '@defillama/sdk'
import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchResultGeneric, } from '../types'
import { getBlock } from "../../helpers/getBlock";

const ONE_DAY_IN_SECONDS = 60 * 60 * 24

Expand All @@ -19,16 +20,17 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
async function getChainResult(chain: string) {
const fetchFunction = volumeAdapter[chain].customBackfill ?? volumeAdapter[chain].fetch
try {
const result: FetchResultGeneric = await fetchFunction(cleanCurrentDayTimestamp - 1, chainBlocks);
const options = getOptionsObject(cleanCurrentDayTimestamp, chain, chainBlocks)
const result: FetchResultGeneric = await fetchFunction(options.toTimestamp, chainBlocks, options);
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`)
result[key] = +Number(result[key]).toFixed(0)
if (isNaN(result[key] as number)) throw new Error(`[${chain}]Value: ${value} ${key} is NaN`)
}
return {
chain,
Expand All @@ -41,6 +43,35 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
}
}

function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks) {
const createBalances: () => Balances = () => {
const closeToCurrentTime = Math.trunc(Date.now() / 1000) - timestamp < 12 * 60 * 60 // 12 hours
return new Balances({ timestamp: closeToCurrentTime ? timestamp : undefined, chain })
}
const toTimestamp = timestamp - 1
const fromTimestamp = toTimestamp - ONE_DAY_IN_SECONDS
const fromChainBlocks = {}
const getFromBlock = async () => await getBlock(fromTimestamp, chain, fromChainBlocks)
const getToBlock = async () => await getBlock(toTimestamp, chain, chainBlocks)
const getLogs = async ({ target, targets, onlyArgs = true, fromBlock, toBlock, flatten = true, eventAbi, topics, }: FetchGetLogsOptions) => {
fromBlock = fromBlock ?? await getFromBlock()
toBlock = toBlock ?? await getToBlock()

return getEventLogs({ fromBlock, toBlock, chain, target, targets, onlyArgs, flatten, eventAbi, topics, })
}

return {
createBalances,
getBlock,
toTimestamp,
fromTimestamp,
getFromBlock,
getToBlock,
getLogs,
chain,
}
}

async function setChainValidStart(chain: string) {
const _start = volumeAdapter[chain]?.start
if (_start === undefined) return;
Expand Down
57 changes: 23 additions & 34 deletions aggregators/aggre/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {Chain} from "@defillama/sdk/build/general";
import {FetchResultAggregators, SimpleAdapter} from "../../adapters/types";
import {getBlock} from "../../helpers/getBlock";
import {CHAIN} from "../../helpers/chains";
import { Chain } from "@defillama/sdk/build/general";
import { FetchResultAggregators, SimpleAdapter } from "../../adapters/types";
import { getBlock } from "../../helpers/getBlock";
import { CHAIN } from "../../helpers/chains";
import * as sdk from "@defillama/sdk";

let abi = [ "event SwapExecuted(address indexed user, address tokenIn, address tokenOut, uint amountIn, uint amountOut, uint swapType)" ];
let abi = ["event SwapExecuted(address indexed user, address tokenIn, address tokenOut, uint amountIn, uint amountOut, uint swapType)"];

type IContract = {
[c: string | Chain]: string;
Expand All @@ -20,38 +20,27 @@ const fetch = (chain: Chain) => {
const toTimestamp = timestamp;


try {
const api = new sdk.ChainApi({chain, timestamp});
const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const toBlock = (await getBlock(toTimestamp, chain, {}));
const logs = (await api.getLogs({
target: contract[chain],
toBlock: toBlock,
fromBlock: fromBlock,
chain,
eventAbi: abi[0],
onlyArgs: true,
}))
const api = new sdk.ChainApi({ chain, timestamp });
const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const toBlock = (await getBlock(toTimestamp, chain, {}));
const logs = (await api.getLogs({
target: contract[chain],
toBlock: toBlock,
fromBlock: fromBlock,
chain,
eventAbi: abi[0],
onlyArgs: true,
}))

logs.map((parsed: any) => {
api.add(parsed.tokenOut, parsed.amountOut)
});
const VUSD = Number(await api.getUSDValue()).toFixed(0);

return {
dailyVolume: VUSD,
timestamp,
};
} catch (e) {
console.log('err::', e);
}
logs.map((parsed: any) => {
api.add(parsed.tokenOut, parsed.amountOut)
});
const VUSD = Number(await api.getUSDValue()).toFixed(0);

return {
dailyVolume: '0',
block: 0,
timestamp: 0,
totalVolume: ""
}
dailyVolume: VUSD,
timestamp,
};
};
};

Expand Down
33 changes: 14 additions & 19 deletions aggregators/jumper-exchange/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,20 @@ const fetch = (chain: Chain) => {
const fromTimestamp = timestamp - 60 * 60 * 24;
const toTimestamp = timestamp;
const api = new sdk.ChainApi({ chain, timestamp });
try {
const data: IData[] = (await api.getLogs({
target: contract[chain],
fromTimestamp, toTimestamp,
chain: chain,
onlyArgs: true,
eventAbi: 'event LiFiGenericSwapCompleted(bytes32 indexed transactionId, string integrator, string referrer, address receiver, address fromAssetId, address toAssetId, uint256 fromAmount, uint256 toAmount)'
})) as any
console.log(data.length, chain)
data.forEach((e: IData) => api.add(e.toAssetId, e.toAmount));
const { usdTvl } = await api.getUSDJSONs()
return {
dailyVolume: Number(usdTvl).toFixed(0),
timestamp,
} as any;
} catch (error) {
console.error(error);
throw error;
}
const data: IData[] = (await api.getLogs({
target: contract[chain],
fromTimestamp, toTimestamp,
chain: chain,
onlyArgs: true,
eventAbi: 'event LiFiGenericSwapCompleted(bytes32 indexed transactionId, string integrator, string referrer, address receiver, address fromAssetId, address toAssetId, uint256 fromAmount, uint256 toAmount)'
})) as any
console.log(data.length, chain)
data.forEach((e: IData) => api.add(e.toAssetId, e.toAmount));
const { usdTvl } = await api.getUSDJSONs()
return {
dailyVolume: Number(usdTvl).toFixed(0),
timestamp,
} as any;
};
};

Expand Down
26 changes: 9 additions & 17 deletions aggregators/kyberswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,16 @@ const fetch = (chain: string) => async (timestamp: number) => {
new Date(timestamp * 1000)
);

try {
const data = (
await fetchURL(
`https://common-service.kyberswap.com/api/v1/aggregator/volume/daily?chainId=${chainToId[chain]}&timestamps=${unixTimestamp}`
)
).data?.volumes?.[0];
const data = (
await fetchURL(
`https://common-service.kyberswap.com/api/v1/aggregator/volume/daily?chainId=${chainToId[chain]}&timestamps=${unixTimestamp}`
)
).data?.volumes?.[0];

return {
dailyVolume: data?.value ?? "0",
timestamp: unixTimestamp,
};
} catch (e) {
console.log(e);
return {
dailyVolume: "0",
timestamp: unixTimestamp,
};
}
return {
dailyVolume: data.value,
timestamp: unixTimestamp,
};
};

const adapter: any = {
Expand Down
18 changes: 5 additions & 13 deletions aggregators/llamaswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ const chains = [

const fetch =
(chain: string) =>
async (timestamp: number): Promise<FetchResult> => {
const dayTimestamp = timestamp;
async (timestamp: number): Promise<FetchResult> => {
const dayTimestamp = timestamp;

try {
const dailyVolume = await fetchURL(
`${URL}getSwapDailyVolume/?timestamp=${dayTimestamp}&chain=${chain}`
);
Expand All @@ -52,18 +51,11 @@ const fetch =
);

return {
dailyVolume: dailyVolume.volume || "0",
totalVolume: totalVolume.volume || "0",
dailyVolume: dailyVolume.volume,
totalVolume: totalVolume.volume,
timestamp: dayTimestamp,
};
} catch (e) {
return {
dailyVolume: "0",
totalVolume: "0",
timestamp: dayTimestamp,
};
}
};
};

const adapter: SimpleAdapter = {
adapter: {},
Expand Down
24 changes: 8 additions & 16 deletions aggregators/openocean/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,22 @@ const chains = [

const fetch =
(chain: string) =>
async (timestamp: number): Promise<FetchResult> => {
const today = new Date();
const timestampDate = new Date(timestamp * 1000);
const unixTimestamp = getUniqStartOfTodayTimestamp(timestampDate);
const dayDiff = today.getTime() - timestampDate.getTime();
const daysPassed = (dayDiff / (1000 * 3600 * 24)).toFixed(0);
try {
async (timestamp: number): Promise<FetchResult> => {
const today = new Date();
const timestampDate = new Date(timestamp * 1000);
const unixTimestamp = getUniqStartOfTodayTimestamp(timestampDate);
const dayDiff = today.getTime() - timestampDate.getTime();
const daysPassed = (dayDiff / (1000 * 3600 * 24)).toFixed(0);
const data = await fetchURL(
`https://open-api.openocean.finance/v3/DefiLlama/volume?limit=${
daysPassed || 1
`https://open-api.openocean.finance/v3/DefiLlama/volume?limit=${daysPassed || 1
}&total=true`
);

return {
dailyVolume: data.data[chainsMap[chain] || chain]?.volume,
timestamp: unixTimestamp,
};
} catch (e) {
return {
dailyVolume: "0",
timestamp: unixTimestamp,
};
}
};
};

const adapter: any = {
adapter: {
Expand Down
27 changes: 10 additions & 17 deletions aggregators/zrx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,17 @@ const fetch = (chain: string) => async (timestamp: number) => {
new Date(timestamp * 1000)
);

try {
const data = await getVolumeByChain(chain);
const dayData = data.find(
({ timestamp }: { timestamp: number }) =>
getUniqStartOfTodayTimestamp(new Date(timestamp)) ===
unixTimestamp
);
const data = await getVolumeByChain(chain);
const dayData = data.find(
({ timestamp }: { timestamp: number }) =>
getUniqStartOfTodayTimestamp(new Date(timestamp)) ===
unixTimestamp
);

return {
dailyVolume: dayData?.volumeUSD ?? '0',
timestamp: unixTimestamp,
};
} catch (e) {
return {
dailyVolume: '0',
timestamp: unixTimestamp,
};
}
return {
dailyVolume: dayData.volumeUSD,
timestamp: unixTimestamp,
};
};

const adapter: any = {
Expand Down
Loading

0 comments on commit b98f948

Please sign in to comment.