From 36c898f59825ef510da0cb8eef69a35ece213a56 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Sat, 10 Feb 2024 10:11:39 +0100 Subject: [PATCH] replace usage of getPrices --- adapters/types.ts | 1 + adapters/utils/runAdapter.ts | 7 ++- cli/interactive.js | 2 +- dexs/ambient/index.ts | 98 +++++++++++++----------------------- dexs/aux-exchange/index.ts | 36 +++++-------- dexs/bisq/index.ts | 32 ++++++------ dexs/contango/index.ts | 88 ++++++++++++++++---------------- dexs/danogo/index.ts | 43 ++++------------ dexs/kava-swap/index.ts | 73 +++------------------------ dexs/maverick/maverick.ts | 92 +++++++++++++++------------------ dexs/minswap/index.ts | 31 +++++------- dexs/muesliswap/index.ts | 28 +++++------ dexs/paint-swap/index.ts | 29 +++++------ 13 files changed, 206 insertions(+), 354 deletions(-) diff --git a/adapters/types.ts b/adapters/types.ts index e7f9d0667d..cb2d4b3377 100644 --- a/adapters/types.ts +++ b/adapters/types.ts @@ -23,6 +23,7 @@ export type FetchOptions = { getLogs: (params: FetchGetLogsOptions) => Promise; toTimestamp: number; fromTimestamp: number; + startOfDay: number; getFromBlock: () => Promise; getToBlock: () => Promise; chain: string, diff --git a/adapters/utils/runAdapter.ts b/adapters/utils/runAdapter.ts index e076cbc924..87d3aea59b 100644 --- a/adapters/utils/runAdapter.ts +++ b/adapters/utils/runAdapter.ts @@ -1,6 +1,7 @@ import { Balances, ChainApi, getEventLogs, getProvider } from '@defillama/sdk' -import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchResultGeneric, } from '../types' +import { BaseAdapter, ChainBlocks, DISABLED_ADAPTER_KEY, FetchGetLogsOptions, FetchOptions, FetchResultGeneric, } from '../types' import { getBlock } from "../../helpers/getBlock"; +import { getUniqStartOfTodayTimestamp } from '../../helpers/getUniSubgraphFees'; const ONE_DAY_IN_SECONDS = 60 * 60 * 24 @@ -47,7 +48,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren } } - async function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks) { + async function getOptionsObject(timestamp: number, chain: string, chainBlocks: ChainBlocks): Promise { const withinTwoHours = Math.trunc(Date.now() / 1000) - timestamp < 2 * 60 * 60 // 2 hours const createBalances: () => Balances = () => { return new Balances({ timestamp: closeToCurrentTime ? undefined : timestamp, chain }) @@ -75,6 +76,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren } const fromApi = new ChainApi({ chain, timestamp: fromTimestamp, block: fromBlock }) const api = new ChainApi({ chain, timestamp: withinTwoHours ? undefined : timestamp, block: toBlock }) + const startOfDay = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) return { createBalances, @@ -87,6 +89,7 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren chain, fromApi, api, + startOfDay, } } diff --git a/cli/interactive.js b/cli/interactive.js index de31a46d9c..028f918093 100644 --- a/cli/interactive.js +++ b/cli/interactive.js @@ -54,7 +54,7 @@ async function runAdapter(adapterPath, debugMode) { } const startTime = Date.now() - const child = childProcess.spawn('ts-node', ['--transpile-only', 'cli/testAdapter.ts', ...adapterPath.split('/')], { + const child = childProcess.spawn('npx', ['ts-node', '--transpile-only', 'cli/testAdapter.ts', ...adapterPath.split('/')], { env, }) diff --git a/dexs/ambient/index.ts b/dexs/ambient/index.ts index d63ae3299e..5ca77bdce0 100644 --- a/dexs/ambient/index.ts +++ b/dexs/ambient/index.ts @@ -1,18 +1,13 @@ -import {Adapter, FetchResultVolume} from "../../adapters/types"; -import {CHAIN} from "../../helpers/chains"; -import {Chain} from "@defillama/sdk/build/general"; -import {request, gql} from "graphql-request"; -import {getTimestampAtStartOfDayUTC} from "../../utils/date"; -import { getPrices } from "../../utils/prices"; -import { getBlock } from "../../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; -import { type } from "os"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultVolume } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { Chain } from "@defillama/sdk/build/general"; +import { request, } from "graphql-request"; type TEndpoint = { [s: Chain | string]: string; } const endpoints: TEndpoint = { - [CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/crocswap/croc-mainnet", + [CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/crocswap/croc-mainnet", } interface IPool { quote: string; @@ -22,12 +17,12 @@ interface ISwap { pool: IPool; dex: string; } + +const toPositive = (n: any) => +n > 0 ? +n : n * -1 + const graphs = (chain: Chain) => { - return async (timestamp: number): Promise => { - const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp) - const fromTimestamp = todaysTimestamp - 60 * 60 * 24 - const toTimestamp = todaysTimestamp - const query = gql` + return async (timestamp: number, _: ChainBlocks, { fromTimestamp, toTimestamp, createBalances, }: FetchOptions): Promise => { + const query = ` { swaps(where: { time_gte: ${fromTimestamp} @@ -42,19 +37,13 @@ const graphs = (chain: Chain) => { } } ` - const graphRes: ISwap[] = (await request(endpoints[chain], query)).swaps; - const coins = [...new Set(graphRes.map((e: ISwap) => `${chain}:${e.pool.quote.toLowerCase()}`))] - const prices = await getPrices(coins, todaysTimestamp); - const dailyVolume = graphRes.map((e: ISwap) => { - const decimals = prices[`${chain}:${e.pool.quote.toLowerCase()}`]?.decimals || 0; - const price = prices[`${chain}:${e.pool.quote.toLowerCase()}`]?.price || 0; - return (Number(e.quoteFlow.replace('-','')) / 10 ** decimals) * price - }).reduce((a: number, b: number) => a + b, 0) - return { - dailyVolume: `${dailyVolume}`, - timestamp, - }; - } + const graphRes: ISwap[] = (await request(endpoints[chain], query)).swaps; + const dailyVolume = createBalances() + graphRes.map((e: ISwap) => { + dailyVolume.add(e.pool.quote, toPositive(e.quoteFlow)) + }) + return { dailyVolume, timestamp, } + } } const swapEvent = 'event CrocSwap (address indexed base, address indexed quote, uint256 poolIdx, bool isBuy, bool inBaseQty, uint128 qty, uint16 tip, uint128 limitPrice, uint128 minOut, uint8 reserveFlags, int128 baseFlow, int128 quoteFlow)'; @@ -67,48 +56,29 @@ interface ILog { quoteFlow: string; } const contract_address: TContractAddress = { - [CHAIN.SCROLL]: '0xaaaaaaaacb71bf2c8cae522ea5fa455571a74106' + [CHAIN.SCROLL]: '0xaaaaaaaacb71bf2c8cae522ea5fa455571a74106', } const fetchVolume = (chain: Chain) => { - return async (timestamp: number): Promise => { - const toTimestamp = getTimestampAtStartOfDayUTC(timestamp) - const fromTimestamp = toTimestamp - 60 * 60 * 24 - const balances = new sdk.Balances({ chain, timestamp }) - const fromBlock = await getBlock(fromTimestamp, chain, {}) - const toBlock = await getBlock(toTimestamp, chain, {}) - - const logs: ILog[] = (await sdk.getEventLogs({ - target: contract_address[chain], - toBlock: toBlock, - fromBlock: fromBlock, - chain, - eventAbi: swapEvent, - flatten: false, - onlyArgs: true, - })) as ILog[]; - logs.forEach((log: ILog) => { - balances.add(log.quote, log.quoteFlow) - }); - return { - dailyVolume: await balances.getUSDString(), - timestamp, - } - } + return async (timestamp: number, _: ChainBlocks, { getLogs, createBalances, }: FetchOptions): Promise => { + const dailyVolume = createBalances() + const logs: ILog[] = await getLogs({ target: contract_address[chain], eventAbi: swapEvent, }) + logs.forEach((log: ILog) => dailyVolume.add(log.quote, Number(log.quoteFlow) < 0 ? 0 : log.quoteFlow)); + return { dailyVolume, timestamp, } + } } - const adapter: Adapter = { - adapter: { - [CHAIN.ETHEREUM]: { - fetch: graphs(CHAIN.ETHEREUM), - start: 1685232000, - }, - [CHAIN.SCROLL]: { - fetch: fetchVolume(CHAIN.SCROLL), - start: async () => 1685232000, - }, - } + adapter: { + [CHAIN.ETHEREUM]: { + fetch: graphs(CHAIN.ETHEREUM), + start: 1685232000, + }, + [CHAIN.SCROLL]: { + fetch: fetchVolume(CHAIN.SCROLL), + start: 1685232000, + }, + } } export default adapter; diff --git a/dexs/aux-exchange/index.ts b/dexs/aux-exchange/index.ts index 599f10d5d5..5dbb6470b6 100644 --- a/dexs/aux-exchange/index.ts +++ b/dexs/aux-exchange/index.ts @@ -1,4 +1,4 @@ -import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getPrices } from "../../utils/prices"; import { httpGet } from "../../utils/fetchURL"; @@ -30,9 +30,8 @@ const getResources = async (account: string): Promise => { return data } -const fetchVolume = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 86400; - const toTimestamp = timestamp; +const fetchVolume = async (timestamp: number, _: ChainBlocks, { fromTimestamp, toTimestamp, createBalances, }: FetchOptions): Promise => { + const dailyVolume = createBalances(); const account_resource: any[] = (await getResources(account)) const pools = account_resource.filter(e => e.type?.includes('amm::Pool')) .map((e: any) => { @@ -49,24 +48,13 @@ const fetchVolume = async (timestamp: number): Promise => { } }) - const logs_swap: ISwapEventData[] = (await Promise.all(pools.map(p => getSwapEvent(p, fromTimestamp)))).flat() - .filter(e => toUnixTime(e.timestamp) > fromTimestamp && toUnixTime(e.timestamp) < toTimestamp) - const coins = [...new Set([...logs_swap.map(p => `${CHAIN.APTOS}:${p.in_coin_type}`), ...logs_swap.map(p => `${CHAIN.APTOS}:${p.out_coin_type}`)])] - const price = (await getPrices(coins, timestamp)); - const dailyVolume = logs_swap.map((e: ISwapEventData) => { - const token0Price = price[`${CHAIN.APTOS}:${e.in_coin_type}`]?.price || 0; - const token1Price = price[`${CHAIN.APTOS}:${e.out_coin_type}`]?.price || 0; - const token0Decimals = price[`${CHAIN.APTOS}:${e.in_coin_type}`]?.decimals || 0; - const token1Decimals = price[`${CHAIN.APTOS}:${e.out_coin_type}`]?.decimals || 0; - const in_au = (Number(e.in_au) / 10 ** token0Decimals) * token0Price; - const out_au = (Number(e.out_au) / 10 ** token1Decimals) * token1Price; - return token0Price ? in_au : out_au; - }).reduce((a: number, b: number) => a + b, 0) + const logs_swap: ISwapEventData[] = (await Promise.all(pools.map(p => getSwapEvent(p, fromTimestamp)))).flat() + .filter(e => toUnixTime(e.timestamp) > fromTimestamp && toUnixTime(e.timestamp) < toTimestamp) + logs_swap.map((e: ISwapEventData) => { + dailyVolume.add(e.out_coin_type, e.out_au) + }) - return { - timestamp, - dailyVolume: dailyVolume.toString(), - } + return { timestamp, dailyVolume, } } const getSwapEvent = async (pool: any, fromTimestamp: number): Promise => { @@ -77,12 +65,12 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise Number(e.sequence_number)) + const listSequence: number[] = event.map(e => Number(e.sequence_number)) swap_events.push(...event) const lastMin = Math.min(...listSequence) if (lastMin >= Infinity || lastMin <= -Infinity) break; const lastTimestamp = event.find(e => Number(e.sequence_number) === lastMin)?.data.timestamp - const lastTimestampNumber = Number((Number(lastTimestamp)/1e6).toString().split('.')[0]) + const lastTimestampNumber = Number((Number(lastTimestamp) / 1e6).toString().split('.')[0]) if (lastTimestampNumber < fromTimestamp) break; start = lastMin - 26 > 0 ? lastMin - 26 : 0; } catch { @@ -92,7 +80,7 @@ const getSwapEvent = async (pool: any, fromTimestamp: number): Promise e.data) } -const toUnixTime = (timestamp: string) => Number((Number(timestamp)/1e6).toString().split('.')[0]) +const toUnixTime = (timestamp: string) => Number((Number(timestamp) / 1e6).toString().split('.')[0]) const adapter: SimpleAdapter = { adapter: { diff --git a/dexs/bisq/index.ts b/dexs/bisq/index.ts index ec35283590..9eecf14924 100644 --- a/dexs/bisq/index.ts +++ b/dexs/bisq/index.ts @@ -1,8 +1,6 @@ import fetchURL from "../../utils/fetchURL" -import { SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import { getPrices } from "../../utils/prices"; const historicalVolumeEndpoint = "https://bisq.markets/bisq/api/markets/volumes?interval=day" @@ -11,24 +9,22 @@ interface IVolumeall { period_start: number; } -const fetch = async (timestamp: number) => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint)); - const totalVolume = historicalVolume - .filter(volItem => volItem.period_start <= dayTimestamp) - .reduce((acc, { volume }) => acc + Number(volume), 0) +const fetch = async (timestamp: number, _: ChainBlocks, {startOfDay, createBalances, }: FetchOptions) => { + const totalVolume = createBalances() + const dailyVolume = createBalances() - const dailyVolume = historicalVolume - .find(dayItem => dayItem.period_start === dayTimestamp)?.volume + const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint)); + historicalVolume + .filter(volItem => volItem.period_start <= startOfDay) + .map(({ volume }) => totalVolume.addCGToken('bitcoin', +volume)) - const coinId = "coingecko:bitcoin"; - const prices = await getPrices([coinId], dayTimestamp) + const dailyVol = historicalVolume + .find(dayItem => dayItem.period_start === startOfDay)?.volume + dailyVolume.addCGToken('bitcoin', +(dailyVol as any)) - return { - totalVolume: totalVolume ? String(Number(totalVolume) * prices[coinId].price) : "0", - dailyVolume: dailyVolume ? String(Number(dailyVolume) * prices[coinId].price) : "0", - timestamp: dayTimestamp, - }; + return { + // totalVolume, + dailyVolume, timestamp: startOfDay }; }; const adapter: SimpleAdapter = { diff --git a/dexs/contango/index.ts b/dexs/contango/index.ts index 1dd00d4b8e..6f330f5055 100644 --- a/dexs/contango/index.ts +++ b/dexs/contango/index.ts @@ -1,8 +1,8 @@ import request from "graphql-request"; -import { SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getBlock } from "../../helpers/getBlock"; import { getPrices } from "../../utils/prices"; +import { wrapGraphError } from "../../helpers/getUniSubgraph"; import { Chain } from "@defillama/sdk/build/general"; type IEndpoint = { @@ -31,26 +31,22 @@ interface IResponse { } interface IAsset { id: string; - volume: string; - openInterest: string; - fees: string; + volume: number; + openInterest: number; + fees: number; } -const fetchVolume = (chain: Chain) => { - return async (timestamp: number) => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - const toBlock = (await getBlock(toTimestamp, chain, {})); - const fromBlock = (await getBlock(fromTimestamp, chain, {})); +const fetchVolume = (chain: Chain) => { + return async (timestamp: number, _: ChainBlocks, { getFromBlock, getToBlock, createBalances, api, }: FetchOptions) => { const query = ` { - today:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${toBlock}}) { + today:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${await getToBlock()}}) { id symbol totalVolume openInterest totalFees }, - yesterday:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${fromBlock}}) { + yesterday:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${await getFromBlock()}}) { id symbol totalVolume @@ -59,48 +55,50 @@ const fetchVolume = (chain: Chain) => { } } `; - const response: IResponse = (await request(endpoint[chain], query)); - const data: IAsset[] = response.today.map((asset) => { + let response: IResponse + try { + response = await request(endpoint[chain], query) + } catch (error) { + console.error('Error fetching contango data', wrapGraphError(error).message); + return { timestamp }; + } + + const dailyOpenInterest = createBalances(); + const dailyFees = createBalances(); + const dailyVolume = createBalances(); + const totalFees = createBalances(); + const totalVolume = createBalances(); + + const tokens = response.today.map((asset) => asset.id); + const decimals = await api.multiCall({ abi: 'erc20:decimals', calls: tokens}) + + const data: IAsset[] = response.today.map((asset, index: number) => { const yesterday = response.yesterday.find((e: IAssetTotals) => e.id === asset.id); const totalVolume = Number(asset.totalVolume) - Number(yesterday?.totalVolume || 0); const totalFees = Number(asset.totalFees) - Number(yesterday?.totalFees || 0); const openInterest = Math.abs(Number(asset.openInterest)); + const multipliedBy = 10 ** Number(decimals[index]); return { id: asset.id, - openInterest: openInterest ? `${openInterest}` : 0, - fees: totalFees ? `${totalFees}` : 0, - volume: totalVolume ? `${totalVolume}` : 0, + openInterest: openInterest * multipliedBy, + fees: totalFees * multipliedBy, + volume: totalVolume * multipliedBy, } as IAsset }) - const coins = data.map((e: IAsset) => `${chain}:${e.id}`); - const prices = await getPrices(coins, timestamp); - const dailyVolume = data.reduce((acc, { volume, id }) => { - const price = prices[`${chain}:${id}`]?.price || 0; - return acc + Number(volume) * price; - }, 0); - const dailyFees = data.reduce((acc, { fees, id }) => { - const price = prices[`${chain}:${id}`]?.price || 0; - return acc + Number(fees) * price; - },0); - const dailyOpenInterest = data.reduce((acc, { openInterest, id }) => { - const price = prices[`${chain}:${id}`]?.price || 0; - return acc + Number(openInterest) * price; - },0); - const totalFees = response.today.reduce((acc , { totalFees, id }) => { - const price = prices[`${chain}:${id}`]?.price || 0; - return acc + Number(totalFees) * price; - }, 0); - const totalVolume = response.today.reduce((acc , { totalVolume, id }) => { - const price = prices[`${chain}:${id}`]?.price || 0; - return acc + Number(totalVolume) * price; - }, 0); + data.map(({ volume, id, openInterest, fees }) => { + dailyVolume.add(id, +volume) + dailyOpenInterest.add(id, +openInterest) + dailyFees.add(id, +fees) + }); + response.today.map(({ totalFees: tf, id, totalVolume: tv, }, index) => { + const multipliedBy = 10 ** Number(decimals[index]); + totalFees.add(id, +tf * multipliedBy) + totalVolume.add(id, +tv * multipliedBy) + }); return { - dailyOpenInterest: dailyOpenInterest ? `${dailyOpenInterest}` : undefined, - dailyFees: `${dailyFees}`, - dailyVolume: `${dailyVolume}`, - totalFees: totalFees ? `${totalFees}` : undefined, - totalVolume: totalVolume ? `${totalVolume}` : undefined, + dailyOpenInterest, dailyFees, dailyVolume, + // totalFees, totalVolume, timestamp }; } diff --git a/dexs/danogo/index.ts b/dexs/danogo/index.ts index b4ee003d98..8be996255b 100644 --- a/dexs/danogo/index.ts +++ b/dexs/danogo/index.ts @@ -1,12 +1,9 @@ -import { SimpleAdapter } from "../../adapters/types"; -import { getPrices } from "../../utils/prices"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import fetchURL from "../../utils/fetchURL"; -import { DanogoDimensions, DanogoVolumes } from "./types"; +import { DanogoDimensions, } from "./types"; const DANOGO_GATEWAY_ENDPOINT = 'https://danogo-gateway.tekoapis.com/api/v1/defillama-dimensions'; const DANOGO_START_TIMESTAMP = 1685404800 // 30/05/2023 -const CARDANO_COIN_ID = "coingecko:cardano"; -const ADA_DECIMAL = 6; const fetchDanogoGatewayData = async (timestamp: number): Promise => { const response = await fetchURL(`${DANOGO_GATEWAY_ENDPOINT}?timestamp=${timestamp}`); @@ -14,35 +11,17 @@ const fetchDanogoGatewayData = async (timestamp: number): Promise { - const price = await getPrices([CARDANO_COIN_ID], timestamp); - - return price[CARDANO_COIN_ID].price; -} - -const lovelaceToUSD = (lovelace: string, price: number) => { - const ada = Number(BigInt(lovelace) * BigInt(100) / BigInt(10 ** ADA_DECIMAL)) / 100 - - return (ada * price).toString(); -} - -const convertDataToUSD = (data: DanogoDimensions, price: number) => { - const convertedData: DanogoVolumes = { - dailyVolume: lovelaceToUSD(data.dailyVolumeAdaValue, price), - totalVolume: lovelaceToUSD(data.totalVolumeAdaValue, price), - }; - - return convertedData; -} - -const fetchData = async (timestamp: number) => { - const dataPromise = fetchDanogoGatewayData(timestamp); - const adaPricePromise = fetchADAprice(timestamp); - const [data, adaPrice] = await Promise.all([dataPromise, adaPricePromise]); +const fetchData = async (timestamp: number, _:ChainBlocks, { createBalances, }: FetchOptions) => { + const { dailyVolumeAdaValue, totalFeesAdaValue }= await fetchDanogoGatewayData(timestamp); + const dailyVolume = createBalances(); + const totalVolume = createBalances(); + dailyVolume.addGasToken(dailyVolumeAdaValue) + totalVolume.addGasToken(totalFeesAdaValue) return { - timestamp: timestamp, - ...convertDataToUSD(data, adaPrice) + timestamp, + dailyVolume, + // totalVolume, }; } diff --git a/dexs/kava-swap/index.ts b/dexs/kava-swap/index.ts index a20af24a88..a3bb31ec0e 100644 --- a/dexs/kava-swap/index.ts +++ b/dexs/kava-swap/index.ts @@ -1,86 +1,27 @@ -import { FetchResult, SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, FetchResult, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import fetchURL from "../../utils/fetchURL"; -import { getPrices } from "../../utils/prices"; - -interface ITokenInfo { - id: string; - decimals: number | 0; - symbol: string; -} interface ICallPoolData { denom: string; amount: string; } -interface IVolumeall { - id: string; - decimals: number | 0; - symbol: string; - amount: number; -} - const URL = "https://swap-data.kava.io/v1/pools/internal"; -const convertSymbol = (symbol: string): ITokenInfo => { - switch (symbol) { - case 'bnb': - return { id: 'binancecoin', decimals: 8, symbol: 'WBNB' }; - case 'btcb': - return { id: 'bitcoin', decimals: 8, symbol: 'WBTC' }; - case 'busd': - return { id: 'binance-usd', decimals: 8, symbol: 'BUSD' }; - case 'hard': - return { id: 'kava-lend', decimals: 6, symbol: 'HARD' }; - case 'hbtc': - return { id: 'bitcoin', decimals: 8, symbol: 'WBTC' }; - case 'swp': - return { id: 'kava-swap', decimals: 6, symbol: 'SWP' }; - case 'ukava': - return { id: 'kava', decimals: 6, symbol: 'KAVA' }; - case 'xrpb': - return { id: 'ripple', decimals: 8, symbol: 'XRP' }; - case 'ibc/B448C0CA358B958301D328CCDC5D5AD642FC30A6D3AE106FF721DB315F3DDE5C': - return { id: 'terrausd', decimals: 6, symbol: 'UST' }; - case 'ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2': - return { id: 'cosmos', decimals: 6, symbol: 'ATOM' }; - case 'usdx': - return { id: 'usdx', decimals: 6, symbol: 'USDX' }; - case 'ibc/799FDD409719A1122586A629AE8FCA17380351A51C1F47A80A1B8E7F2A491098': - return { id: 'akash-network', decimals: 6, symbol: 'AKT' }; - case 'ibc/0471F1C4E7AFD3F07702BEF6DC365268D64570F7C1FDC98EA6098DD6DE59817B': - return { id: 'osmosis', decimals: 6, symbol: 'OSMO' }; - case 'ibc/B8AF5D92165F35AB31F3FC7C7B444B9D240760FA5D406C49D24862BD0284E395': - return { id: 'lunatics', decimals: 6, symbol: 'LUNA' }; - default: - return { id: '', decimals: 0, symbol: ''} - } -} - -const fetch = async (timestamp: number): Promise => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); +const fetch = async (timestamp: number, _: ChainBlocks, { startOfDay, createBalances,}: FetchOptions): Promise => { + const dailyVolume = createBalances(); const poolCall = (await fetchURL(URL)); const poolDetail = poolCall .map((pool: any) => pool.volume .map((p: ICallPoolData) => p)).flat(); - const pools: IVolumeall[] = poolDetail.map((e:ICallPoolData) => { - const info = convertSymbol(e.denom); - return { - amount: Number(e.amount) / 10 ** info.decimals, - ...info - } + poolDetail.map((e:ICallPoolData) => { + dailyVolume.add(e.denom, e.amount) }); - const id = pools.map((p: IVolumeall) => `coingecko:${p.id}`); - const prices = await getPrices(id, dayTimestamp); - const dailyVolume = pools - .map((p: IVolumeall) => p.amount * prices[`coingecko:${p.id}`.toLowerCase()].price) - .reduce((a: number, b: number) => a + b, 0); return { - dailyVolume: dailyVolume.toString(), - timestamp: dayTimestamp + dailyVolume, + timestamp: startOfDay } } diff --git a/dexs/maverick/maverick.ts b/dexs/maverick/maverick.ts index cc402f4674..e22f756ed8 100644 --- a/dexs/maverick/maverick.ts +++ b/dexs/maverick/maverick.ts @@ -1,9 +1,6 @@ -// Maverick v1 data -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import { Chain } from "@defillama/sdk/build/general"; import { CHAIN } from "../../helpers/chains"; -import { getPrices } from "../../utils/prices"; -const { request, gql } = require("graphql-request"); +import { ChainBlocks, FetchOptions } from "../../adapters/types"; +const { request, } = require("graphql-request"); const info: { [key: string]: any } = { [CHAIN.ETHEREUM]: { @@ -23,16 +20,15 @@ const info: { [key: string]: any } = { }, }; -const getData = async (chain: string, timestamp: number) => { - const totdayTimestamp = getUniqStartOfTodayTimestamp( - new Date(timestamp * 1000) - ); +const getData = async ({ chain, createBalances, startOfDay }: FetchOptions) => { + const dailyFees = createBalances(); + const totalFees = createBalances(); + const dailyUserFees = createBalances(); + const totalUserFees = createBalances(); + const dailyVolume = createBalances(); + const totalVolume = createBalances(); let returnCount = 1000; - let daySum = 0; - let totalSum = 0; - let dayFee = 0; - let totalFee = 0; let step = 0; while (returnCount == 1000) { const graphQL = `{ @@ -41,16 +37,18 @@ const getData = async (chain: string, timestamp: number) => { orderDirection: desc first: 1000 skip: ${step * 1000} - where: {timestamp: "${totdayTimestamp}"} + where: {timestamp: "${startOfDay}"} ) { id pool { id tokenB { id + decimals } tokenA { id + decimals } tokenAVolume tokenBVolume @@ -66,66 +64,56 @@ const getData = async (chain: string, timestamp: number) => { returnCount = data.poolDayStats.length; step++; - let tokenArray = [] as string[]; for (const dailyData of data.poolDayStats) { - tokenArray.push(chain + ":" + dailyData.pool.tokenA.id); - tokenArray.push(chain + ":" + dailyData.pool.tokenB.id); - } - let unique = [...new Set(tokenArray)] as string[]; - const prices = await getPrices(unique, totdayTimestamp); - - for (const dailyData of data.poolDayStats) { - const tokenAId = chain + ":" + dailyData.pool.tokenA.id; - const tokenBId = chain + ":" + dailyData.pool.tokenB.id; - let aPrice = prices[tokenAId] === undefined ? 0 : prices[tokenAId].price; - let bPrice = prices[tokenBId] === undefined ? 0 : prices[tokenBId].price; - daySum += Number(dailyData.tokenAVolume) * aPrice; - daySum += Number(dailyData.tokenBVolume) * bPrice; - dayFee += Number(dailyData.tokenAVolume) * aPrice * dailyData.pool.fee; - dayFee += Number(dailyData.tokenBVolume) * bPrice * dailyData.pool.fee; + const tokenAId = dailyData.pool.tokenA.id; + const tokenBId = dailyData.pool.tokenB.id; + const multiplierA = 10 ** dailyData.pool.tokenA.decimals; + const multiplierB = 10 ** dailyData.pool.tokenB.decimals; + dailyVolume.add(tokenAId, dailyData.tokenAVolume * multiplierA); + dailyVolume.add(tokenBId, dailyData.tokenBVolume * multiplierB); + dailyFees.add(tokenAId, dailyData.tokenAVolume * multiplierA * dailyData.pool.fee); + dailyFees.add(tokenBId, dailyData.tokenBVolume * multiplierB * dailyData.pool.fee); - totalSum += Number(dailyData.pool.tokenAVolume) * aPrice; - totalSum += Number(dailyData.pool.tokenBVolume) * bPrice; - totalFee += - Number(dailyData.pool.tokenAVolume) * aPrice * dailyData.pool.fee; - totalFee += - Number(dailyData.pool.tokenBVolume) * bPrice * dailyData.pool.fee; + totalVolume.add(tokenAId, dailyData.pool.tokenAVolume * multiplierA); + totalVolume.add(tokenBId, dailyData.pool.tokenBVolume * multiplierB); + totalFees.add(tokenAId, dailyData.pool.tokenAVolume * multiplierA * dailyData.pool.fee); + totalFees.add(tokenBId, dailyData.pool.tokenBVolume * multiplierB * dailyData.pool.fee); } } return { - dailyFees: `${dayFee}`, - totalFees: `${totalFee}`, - dailyUserFees: `${dayFee}`, - totalUserFees: `${totalFee}`, - totalVolume: `${totalSum}`, - dailyVolume: `${daySum}`, - timestamp: totdayTimestamp, + dailyFees, + totalFees, + dailyUserFees: dailyFees, + totalUserFees: totalFees, + totalVolume, + dailyVolume, + timestamp: startOfDay, }; }; -export const fetchVolume = (chain: string) => { - return async (timestamp: number) => { - const data = await getData(chain, timestamp); +export const fetchVolume = (_chain: string) => { + return async (_timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const data = await getData(options); return { - totalVolume: data.totalVolume, + // totalVolume: data.totalVolume, dailyVolume: data.dailyVolume, timestamp: data.timestamp, }; }; }; -export const fetchFee = (chain: string) => { - return async (timestamp: number) => { - const data = await getData(chain, timestamp); +export const fetchFee = (_chain: string) => { + return async (timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const data = await getData(options); return { timestamp, dailyFees: data.dailyFees, - totalFees: data.totalFees, + // totalFees: data.totalFees, dailyUserFees: data.dailyUserFees, - totalUserFees: data.totalUserFees, + // totalUserFees: data.totalUserFees, }; }; }; diff --git a/dexs/minswap/index.ts b/dexs/minswap/index.ts index a3487205a1..d0ed7b3ea6 100644 --- a/dexs/minswap/index.ts +++ b/dexs/minswap/index.ts @@ -1,7 +1,5 @@ -import type { SimpleAdapter } from "../../adapters/types"; +import type { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import { getPrices } from "../../utils/prices"; import { httpGet } from "../../utils/fetchURL"; interface IVolumeall { @@ -12,24 +10,19 @@ interface IVolumeall { const historicalVolumeEndpoint = "https://api-mainnet-prod.minswap.org/defillama/v2/volume-series"; -const fetch = async (timestamp: number) => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) +const fetch = async (timestamp: number, _: ChainBlocks, { startOfDay, createBalances, }: FetchOptions) => { + const dailyVolume = createBalances(); + const totalVolume = createBalances(); const vols: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)); - - const dailyVolume = vols - .find(dayItem => new Date(Number(dayItem.time)).getTime() / 1000 === dayTimestamp)?.volume - - const totalVolume = vols - .find(dayItem => new Date(Number(dayItem.time)).getTime() / 1000 === dayTimestamp)?.totalVolume - - - const coinId = "coingecko:cardano"; - const prices = await getPrices([coinId], dayTimestamp) + const volData = vols + .find(dayItem => new Date(Number(dayItem.time)).getTime() / 1000 === startOfDay) + dailyVolume.addGasToken(volData?.volume) + totalVolume.addGasToken(volData?.totalVolume) return { - timestamp: dayTimestamp, - totalVolume: totalVolume ? String(Number(totalVolume)/1e6 * prices[coinId].price) : "0", - dailyVolume: dailyVolume ? String(Number(dailyVolume)/1e6 * prices[coinId].price) : "0" + timestamp: startOfDay, + // totalVolume, + dailyVolume, } } @@ -41,7 +34,7 @@ const getStartTimestamp = async () => { const adapter: SimpleAdapter = { adapter: { [CHAIN.CARDANO]: { - start: getStartTimestamp, + start: 1648080000, fetch: fetch, } } diff --git a/dexs/muesliswap/index.ts b/dexs/muesliswap/index.ts index f580b8b38c..77f0de4336 100644 --- a/dexs/muesliswap/index.ts +++ b/dexs/muesliswap/index.ts @@ -1,7 +1,7 @@ import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp, univ2Adapter } from "../../helpers/getUniSubgraphVolume"; -import { getPrices } from "../../utils/prices"; +import { univ2Adapter } from "../../helpers/getUniSubgraphVolume"; import { httpGet } from "../../utils/fetchURL"; +import { ChainBlocks, FetchOptions } from "../../adapters/types"; interface IVolumeall { time: number; @@ -10,21 +10,19 @@ interface IVolumeall { const historicalVolumeEndpoint = "https://analyticsv3.muesliswap.com/historical-volume"; -const fetch = async (timestamp: number) => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp*1000)) +const fetch = async (timestamp: number, _: ChainBlocks, { startOfDay, createBalances, }: FetchOptions) => { + const dailyVolume = createBalances(); + const totalVolume = createBalances(); const vols: IVolumeall[] = (await httpGet(historicalVolumeEndpoint)); - const totalVolume = vols - .filter((volItem: IVolumeall) => Number(volItem.time) <= dayTimestamp) - .reduce((acc, { volume }) => acc + Number(volume), 0); - const dailyVolume = vols - .find((dayItem: IVolumeall) => Number(dayItem.time) === dayTimestamp)?.volume - - const coinId = "coingecko:cardano"; - const prices = await getPrices([coinId], dayTimestamp) + vols + .filter((volItem: IVolumeall) => Number(volItem.time) <= startOfDay) + .map(({ volume }) => totalVolume.addGasToken(volume)); + dailyVolume.addGasToken(vols.find(dayItem => dayItem.time === startOfDay)?.volume) + return { - timestamp: dayTimestamp, - totalVolume: totalVolume ? String(totalVolume/1e6 * prices[coinId].price) : "0", - dailyVolume: dailyVolume ? String(dailyVolume/1e6 * prices[coinId].price) : "0" + timestamp: startOfDay, + dailyVolume, + // totalVolume, } } diff --git a/dexs/paint-swap/index.ts b/dexs/paint-swap/index.ts index 2a5214e4af..e833bf4d67 100644 --- a/dexs/paint-swap/index.ts +++ b/dexs/paint-swap/index.ts @@ -1,8 +1,6 @@ import fetchURL from "../../utils/fetchURL" -import { SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import { getPrices } from "../../utils/prices"; const dateFrom = 1630584906; const historicalVolumeEndpoint = (dateTo: number) => `https://api.paintswap.finance/v2/marketplaceDayDatas?numToSkip=0&numToFetch=1000&orderDirection=asc&dateFrom=${dateFrom}&dateTo=${dateTo}`; @@ -12,23 +10,22 @@ interface IVolumeall { date: number; } -const fetch = async (timestamp: number) => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) - const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint(dayTimestamp))).marketPlaceDayDatas; - const totalVolume = historicalVolume - .filter(volItem => (new Date(volItem.date).getTime()) <= dayTimestamp) - .reduce((acc, { dailyVolume }) => acc + Number(dailyVolume), 0) +const fetch = async (timestamp: number, _: ChainBlocks, { startOfDay, createBalances, }: FetchOptions) => { + const dailyVolume = createBalances(); + const totalVolume = createBalances(); + const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint(startOfDay))).marketPlaceDayDatas; + historicalVolume + .filter(volItem => (new Date(volItem.date).getTime()) <= startOfDay) + .map(({ dailyVolume }) => totalVolume.addGasToken(dailyVolume)) - const dailyVolume = historicalVolume - .find(dayItem => (new Date(dayItem.date).getTime()) === dayTimestamp)?.dailyVolume + dailyVolume.addGasToken(historicalVolume + .find(dayItem => (new Date(dayItem.date).getTime()) === startOfDay)?.dailyVolume) - const coinId = "coingecko:fantom"; - const prices = await getPrices([coinId], dayTimestamp) return { - timestamp: dayTimestamp, - totalVolume: totalVolume ? String(totalVolume/1e18 * prices[coinId].price) : "0", - dailyVolume: dailyVolume ? String(Number(dailyVolume)/1e18 * prices[coinId].price) : "0" + timestamp: startOfDay, + // totalVolume, + dailyVolume, }; };