From e2f27b03a35f216075acebb223be218259221cb4 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Sat, 10 Feb 2024 17:29:03 +0100 Subject: [PATCH] Update fee adapters for various chains --- dexs/shell-protocol/index.ts | 86 +++--------------- dexs/slingshot/index.ts | 52 ++--------- dexs/sundaeswap/index.ts | 20 ++--- dexs/synfutures/index.ts | 26 ++---- dexs/wingriders/index.ts | 21 ++--- fees/aerodrome/bribes.ts | 53 +++-------- fees/aerodrome/index.ts | 14 +-- fees/aurora.ts | 2 +- fees/avalanche.ts | 42 +++------ fees/canto.ts | 2 +- fees/caviar-tangible.ts | 41 ++------- fees/celo.ts | 2 +- fees/cronos.ts | 2 +- fees/extra/index.ts | 38 ++++---- fees/foundation.ts | 118 ++++++------------------ fees/gnd-protocol.ts | 82 ++++------------- fees/hono.ts | 21 ++--- fees/justlend.ts | 143 ++++++------------------------ fees/klaytn.ts | 23 ++--- fees/liquis.ts | 66 ++++---------- fees/mixin.ts | 2 +- fees/moonbeam.ts | 2 +- fees/moonriver.ts | 2 +- fees/pepe-swaves/index.ts | 32 +++---- fees/ramses-exchange-v2/bribes.ts | 41 +++------ fees/ramses-exchange-v2/index.ts | 125 +++++++++++++------------- fees/synthetix.ts | 56 +++--------- fees/waves/index.ts | 26 ++---- fees/woofi.ts | 100 +++++---------------- fees/xdai.ts | 2 +- fees/y2k/y2k-finance-v2.ts | 21 ++--- fees/y2k/y2k-finance.ts | 24 ++--- helpers/blockscoutFees.ts | 45 +++++----- helpers/dexVolumeLogs.ts | 6 +- helpers/etherscanFees.ts | 50 ++++++----- helpers/getBlock.ts | 2 + helpers/token.ts | 18 ++-- 37 files changed, 405 insertions(+), 1003 deletions(-) diff --git a/dexs/shell-protocol/index.ts b/dexs/shell-protocol/index.ts index 16dc483aa9..8016e9f6a8 100644 --- a/dexs/shell-protocol/index.ts +++ b/dexs/shell-protocol/index.ts @@ -1,78 +1,18 @@ import ADDRESSES from '../../helpers/coreAssets.json' -import { SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../../helpers/getBlock"; -import BigNumber from "bignumber.js"; -import { getPrices } from "../../utils/prices"; - -const DAI_CONTRACT = ADDRESSES.optimism.DAI; -const USDC_CONTRACT = ADDRESSES.arbitrum.USDC; -const USDT_CONTRACT = ADDRESSES.arbitrum.USDT; -const WBTC_CONTRACT = ADDRESSES.arbitrum.WBTC; - -const topic = 'Transfer (index_topic_1 address from, index_topic_2 address to, uint256 value)'; -const topic0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'; -const topic1 = '0x000000000000000000000000c32eb36f886f638fffd836df44c124074cfe3584'; - -interface ITokenList { - address: string; -} - -const tokenList: ITokenList[] = [ - { - address: DAI_CONTRACT, - }, - { - address: USDC_CONTRACT, - }, - { - address: USDT_CONTRACT, - }, - { - address: WBTC_CONTRACT, - } -]; - -interface ILog { - data: string; - transactionHash: string; -} - -const fetch = async (timestamp: number) => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, 'arbitrum', {})); - const toBlock = (await getBlock(toTimestamp, 'arbitrum', {})); - const logs: ILog[][] = (await Promise.all(tokenList.map(({ address }) => sdk.getEventLogs({ - target: address, - topic: topic, - toBlock: toBlock, - fromBlock: fromBlock, - chain: 'arbitrum', - topics: [topic0, topic1] - })))) as any; - const coins = tokenList.map(({ address }) => `arbitrum:${address}`); - const prices = await getPrices(coins, timestamp); - const untrackVolumes = tokenList.map((token: ITokenList, index: number) => { - const log = logs[index] - .map((e: ILog) => { return { ...e, data: e.data } }) - .map((p: ILog) => { - const { decimals, price } = prices[`arbitrum:${token.address}`]; - const amountUSD = new BigNumber(p.data) - .div(new BigNumber(10).pow(decimals)) - .multipliedBy(price); - return amountUSD.toNumber() - }) - return log.reduce((a: number, b: number) => a + b, 0); - }); - - const dailyVolume = untrackVolumes.reduce((a: number, b: number) => a + b, 0); - return { - timestamp, - dailyVolume: dailyVolume.toString() - } +import { addTokensReceived } from "../../helpers/token"; + + +const fetch = async (timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const tokens = [ + ADDRESSES.arbitrum.DAI, + ADDRESSES.arbitrum.USDC, + ADDRESSES.arbitrum.USDT, + ADDRESSES.arbitrum.WBTC, + ] + const dailyVolume = await addTokensReceived({ tokens, options, fromAddressFilter: '0xC32eB36f886F638fffD836DF44C124074cFe3584' }) + return { timestamp, dailyVolume } } diff --git a/dexs/slingshot/index.ts b/dexs/slingshot/index.ts index 4fa20051f9..b5562e8cc9 100644 --- a/dexs/slingshot/index.ts +++ b/dexs/slingshot/index.ts @@ -1,9 +1,6 @@ import { Chain } from "@defillama/sdk/build/general"; -import { FetchResultVolume, SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getBlock } from "../../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; -import { getPrices } from "../../utils/prices"; type TContract = { [s: string | Chain]: string[]; @@ -38,26 +35,12 @@ const contract_address: TContract = { '0x8a1d036be71c9c4a6c3d951cc2a3ee028d12d3fa' ] } -interface ISwap { - token0Address: string; - token1Address: string; - token0Amount: number; - token1Amount: number; -} + const fetchVolume = (chain: Chain) => { - return async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - const fromBlock = (await getBlock(fromTimestamp, chain, {})); - const toBlock = (await getBlock(toTimestamp, chain, {})); - const logs: ILog[] = (await Promise.all(contract_address[chain].map((address: string) => sdk.getEventLogs({ - target: address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: chain, - topics: [topic0] - })))).flat(); - const rawData: ISwap[] = logs.map((log: ILog) => { + return async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, }: FetchOptions): Promise => { + const dailyVolume = createBalances() + const logs: ILog[] = await getLogs({ targets: contract_address[chain], topics: [topic0] }) + logs.map((log: ILog) => { const data = log.data.replace('0x', ''); const token0 = data.slice(0, 64); const token1 = data.slice(64, 128); @@ -65,29 +48,10 @@ const fetchVolume = (chain: Chain) => { const token1Amount = Number('0x' + data.slice(192, 256)); const token0Address = `0x${token0.slice(24, 64)}`; const token1Address = `0x${token1.slice(24, 64)}`; - return { - token0Address, - token1Address, - token0Amount, - token1Amount - } - }) - const coins = [...new Set(rawData.map((e: ISwap) => `${chain}:${e.token0Address}`).concat(rawData.map((e: ISwap) => `${chain}:${e.token1Address}`)))]; - const prices = await getPrices(coins, timestamp); - const volume: number[] = rawData.map((e: ISwap) => { - const token0Price = prices[`${chain}:${e.token0Address}`]?.price || 0; - const token1Price = prices[`${chain}:${e.token1Address}`]?.price || 0; - const token0Decimals = prices[`${chain}:${e.token0Address}`]?.decimals || 0; - const token1Decimals = prices[`${chain}:${e.token1Address}`]?.decimals || 0; - const token0Value = token0Price * (Number(e.token0Amount) / 10 ** token0Decimals); - const token1Value = token1Price * (Number(e.token1Amount) / 10 ** token1Decimals); - const untrackAmountUSD = token0Price !== 0 ? token0Value : token1Price !== 0 ? token1Value : 0; - return untrackAmountUSD; + dailyVolume.add(token0Address, token0Amount); }) - const dailyVolume = volume.reduce((a: number, b: number) => a + b, 0); return { - dailyVolume: `${dailyVolume}`, - timestamp + timestamp, dailyVolume, } } } diff --git a/dexs/sundaeswap/index.ts b/dexs/sundaeswap/index.ts index b209cf74e9..28bf483299 100644 --- a/dexs/sundaeswap/index.ts +++ b/dexs/sundaeswap/index.ts @@ -1,9 +1,8 @@ import fetchURL from "../../utils/fetchURL" -import { DISABLED_ADAPTER_KEY, FetchResultVolume, SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, DISABLED_ADAPTER_KEY, FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import disabledAdapter from "../../helpers/disabledAdapter"; -import { getPrices } from "../../utils/prices"; const historicalVolumeEndpoint = "https://stats.sundaeswap.finance/api/defillama/v0/global-stats/2100" @@ -12,20 +11,15 @@ interface IVolumeall { day: string; } -const fetch = async (timestamp: number): Promise => { - const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)) +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, startOfDay }: FetchOptions): Promise => { + const dailyVolume = createBalances() const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint)).response; - const dailyVolume = historicalVolume - .find(dayItem => getUniqStartOfTodayTimestamp(new Date(dayItem.day)) === dayTimestamp)?.volumeLovelace - - const coinId = "coingecko:cardano"; - const prices = await getPrices([coinId], timestamp) - - + dailyVolume.addGasToken(historicalVolume + .find(dayItem => getUniqStartOfTodayTimestamp(new Date(dayItem.day)) === startOfDay)?.volumeLovelace as any) return { - dailyVolume: dailyVolume ? String(Number(dailyVolume) / 1e6 * prices[coinId].price) : "0", - timestamp: dayTimestamp, + dailyVolume, + timestamp: startOfDay, }; }; diff --git a/dexs/synfutures/index.ts b/dexs/synfutures/index.ts index dee6022b3c..f2673132d7 100644 --- a/dexs/synfutures/index.ts +++ b/dexs/synfutures/index.ts @@ -1,9 +1,7 @@ // SynFutures v1 volume -import { SimpleAdapter } from "../../adapters/types"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; import { Chain } from '@defillama/sdk/build/general'; -import { getPrices } from "../../utils/prices"; const { request, } = require("graphql-request"); const info: {[key: string]: any} = { @@ -21,20 +19,14 @@ const info: {[key: string]: any} = { }, } -interface DailyVolume { - timestamp: number; - quoteAddr: string; - volume: number; -} - export function dayIdFromTimestamp(timestamp: number): number { return Math.floor(timestamp / 86400); } const fetch = (chain: Chain) => { - return async (timestamp: number) => { - const totdayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); - const endDayId = dayIdFromTimestamp(totdayTimestamp); + return async (timestamp: number , _: ChainBlocks, { createBalances, startOfDay }: FetchOptions) => { + const dailyVolume = createBalances() + const endDayId = dayIdFromTimestamp(startOfDay); const graphQL = `{ quoteDataDailySnapshots(first: 1000, where: {dayId: ${endDayId}}) { @@ -50,17 +42,13 @@ const fetch = (chain: Chain) => { const data = await request(info[chain].subgraph, graphQL); - let sum = 0; for (const dailyData of data.quoteDataDailySnapshots) { - const tokenId = chain+':'+dailyData.quote.id; - const prices = await getPrices([tokenId], totdayTimestamp); - sum += Number(dailyData.dayTradeVolume) / 10 ** 18 * prices[tokenId].price; + dailyVolume.add(dailyData.quote.id, Number(dailyData.dayTradeVolume)); } return { - totalVolume: undefined, - dailyVolume: `${sum}`, - timestamp: totdayTimestamp, + dailyVolume, + timestamp: startOfDay, }; } }; diff --git a/dexs/wingriders/index.ts b/dexs/wingriders/index.ts index d677dfe252..13cce6283d 100644 --- a/dexs/wingriders/index.ts +++ b/dexs/wingriders/index.ts @@ -1,26 +1,17 @@ -import BigNumber from "bignumber.js"; -import { Adapter } from "../../adapters/types" +import { Adapter, ChainBlocks, FetchOptions } from "../../adapters/types" import { CHAIN } from "../../helpers/chains"; -import { getPrices } from "../../utils/prices"; import { httpPost } from "../../utils/fetchURL"; const volUrl = 'https://aggregator.mainnet.wingriders.com/volumeInAda'; -async function fetchVolume(timestamp: number) { +async function fetchVolume(timestamp: number , _: ChainBlocks, { createBalances }: FetchOptions) { + const dailyVolume = createBalances() const last24hVolInAda = await httpPost(volUrl, { "lastNHours": 24 }); - const totalVolumeInAda = await httpPost(volUrl, {}); - const coinId = "coingecko:cardano"; - const prices = await getPrices([coinId], timestamp) - - const adaPrice = prices[coinId].price; - - const dailyVolume = (new BigNumber(last24hVolInAda).multipliedBy(adaPrice)).toString(); - const totalVolume = (new BigNumber(totalVolumeInAda).multipliedBy(adaPrice)).toString(); - + // const totalVolumeInAda = await httpPost(volUrl, {}); + dailyVolume.addGasToken(last24hVolInAda * 1e6); return { dailyVolume, - totalVolume, - timestamp: Date.now() / 1e3 + timestamp } } diff --git a/fees/aerodrome/bribes.ts b/fees/aerodrome/bribes.ts index d32553bf95..0af579ccb8 100644 --- a/fees/aerodrome/bribes.ts +++ b/fees/aerodrome/bribes.ts @@ -1,8 +1,7 @@ import ADDRESSES from '../../helpers/coreAssets.json' -import * as sdk from "@defillama/sdk"; -import { getPrices } from "../../utils/prices"; import { ethers } from "ethers"; import { CHAIN } from "../../helpers/chains"; +import { FetchOptions } from '../../adapters/types'; const event_notify_reward = 'event NotifyReward(address indexed from,address indexed reward,uint256 indexed epoch,uint256 amount)'; const event_geuge_created = 'event GaugeCreated(address indexed poolFactory,address indexed votingRewardsFactory,address indexed gaugeFactory,address pool,address bribeVotingReward,address feeVotingReward,address gauge,address creator)' @@ -13,12 +12,6 @@ const contract_interface = new ethers.Interface([ event_geuge_created ]); -type TPrice = { - [s: string]: { - price: number; - decimals: number - }; -} interface ILog { data: string; transactionHash: string; @@ -37,44 +30,22 @@ const abis: any = { "all": "function all(uint256 _limit, uint256 _offset, address _account) view returns ((address lp, string symbol, uint8 decimals, bool stable, uint256 total_supply, address token0, uint256 reserve0, uint256 claimable0, address token1, uint256 reserve1, uint256 claimable1, address gauge, uint256 gauge_total_supply, bool gauge_alive, address fee, address bribe, address factory, uint256 emissions, address emissions_token, uint256 account_balance, uint256 account_earned, uint256 account_staked, uint256 pool_fee, uint256 token0_fees, uint256 token1_fees)[])" } -export const fees_bribes = async (fromBlock: number, toBlock: number, timestamp: number): Promise => { - const ZERO_ADDRESS = ADDRESSES.null; - const bribeVotingReward: string[] = (await sdk.api2.abi.call({ +export const fees_bribes = async ({ getLogs, api, createBalances }: FetchOptions)=> { + const dailyFees = createBalances() + const bribeVotingReward: string[] = (await api.call({ target: gurar, params: [1000, 0, ADDRESSES.null], abi: abis.all, - chain: CHAIN.BASE, })).map((e: any) => { return e.bribe; - }).filter((e: string) => e !== ZERO_ADDRESS); + }).filter((e: string) => e !== ADDRESSES.null); const bribe_contracct = [...new Set(bribeVotingReward)]; - const logs: ILog[] = (await Promise.all(bribe_contracct.map((address: string) => sdk.getEventLogs({ - target: address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.BASE, - topics: ['0x52977ea98a2220a03ee9ba5cb003ada08d394ea10155483c95dc2dc77a7eb24b'] - })))).flat() as ILog[]; - - const logs_bribes = logs.map((e: ILog) => { - const value = contract_interface.parseLog(e) - return { - token: value!.args.reward, - amount: Number(value!.args.amount) - } as IBribes + const logs = await getLogs({ + targets: bribe_contracct, + eventAbi: event_notify_reward, + }) + logs.map((e: any) => { + dailyFees.add(e.reward, e.amount) }) - const coins = [...new Set(logs_bribes.map((e: IBribes) => `${CHAIN.BASE}:${e.token.toLowerCase()}`))] - const coins_split: string[][] = []; - for (let i = 0; i < coins.length; i += 100) { - coins_split.push(coins.slice(i, i + 100)) - } - const prices_result: any = (await Promise.all(coins_split.map((a: string[]) => getPrices(a, timestamp)))).flat().flat().flat(); - const prices: TPrice = Object.assign({}, {}); - prices_result.map((a: any) => Object.assign(prices, a)) - const fees_bribes_usd = logs_bribes.map((e: IBribes) => { - const price = prices[`${CHAIN.BASE}:${e.token.toLowerCase()}`]?.price || 0; - const decimals = prices[`${CHAIN.BASE}:${e.token.toLowerCase()}`]?.decimals || 0; - return (Number(e.amount) / 10 ** decimals) * price; - }).reduce((a: number, b: number) => a + b, 0); - return fees_bribes_usd; + return dailyFees; } diff --git a/fees/aerodrome/index.ts b/fees/aerodrome/index.ts index 8e0885154b..c07ffe976d 100644 --- a/fees/aerodrome/index.ts +++ b/fees/aerodrome/index.ts @@ -1,7 +1,6 @@ -import { FetchResultFees, SimpleAdapter } from "../../adapters/types" +import { ChainBlocks, FetchOptions, FetchResultFees, SimpleAdapter } from "../../adapters/types" import { CHAIN } from "../../helpers/chains" import * as sdk from "@defillama/sdk"; -import { getBlock } from "../../helpers/getBlock"; import { fees_bribes } from "./bribes"; import { getDexFees } from "../../helpers/dexVolumeLogs"; @@ -10,17 +9,12 @@ const abis: any = { "forSwaps": "function forSwaps() view returns ((address lp, bool stable, address token0, address token1, address factory)[])" } -const fetch = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp +const fetch = async (timestamp: number, _: ChainBlocks, fetchOptions: FetchOptions): Promise => { const forSwaps = await sdk.api2.abi.call({ target: gurar, abi: abis.forSwaps, chain: CHAIN.BASE, }) const pools = forSwaps.map((e: any) => e.lp) - const res: any = await getDexFees({ chain: CHAIN.BASE, fromTimestamp, toTimestamp, pools, timestamp, }) - const fromBlock = (await getBlock(fromTimestamp, CHAIN.BASE, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.BASE, {})); - const dailyBribesRevenue = await fees_bribes(fromBlock, toBlock, timestamp); - res.dailyBribesRevenue = dailyBribesRevenue.toString(); + const res: any = await getDexFees({ chain: CHAIN.BASE, fromTimestamp: fetchOptions.fromTimestamp, toTimestamp: fetchOptions.toTimestamp, pools, timestamp, fetchOptions }) + res.dailyBribesRevenue = await fees_bribes(fetchOptions); return res } diff --git a/fees/aurora.ts b/fees/aurora.ts index bbc90b18a8..f3744ca640 100644 --- a/fees/aurora.ts +++ b/fees/aurora.ts @@ -1,4 +1,4 @@ import { blockscoutFeeAdapter } from "../helpers/blockscoutFees"; import { CHAIN } from "../helpers/chains"; -export default blockscoutFeeAdapter(CHAIN.AURORA, "https://aurorascan.dev/api?module=stats&action=totalfees", "coingecko:ethereum") +export default blockscoutFeeAdapter(CHAIN.AURORA, "https://aurorascan.dev/api?module=stats&action=totalfees") diff --git a/fees/avalanche.ts b/fees/avalanche.ts index 709f2b8d03..3f3070a647 100644 --- a/fees/avalanche.ts +++ b/fees/avalanche.ts @@ -1,39 +1,23 @@ -import { Adapter, ProtocolType } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date"; -import { getPrices } from "../utils/prices"; -import { getBlock } from "../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; - - -const getBalance = async (block: string): Promise => { - const data = await sdk.api.eth.getBalance({ target: '0x0100000000000000000000000000000000000000', block: +block, chain: 'avax' }); - return +data.output / 1e18; -}; - +import { getTokenDiff } from "../helpers/token"; const adapter: Adapter = { adapter: { [CHAIN.AVAX]: { - fetch: async (timestamp: number) => { - const ts = getTimestampAtStartOfDayUTC(timestamp) - const endDatets = getTimestampAtStartOfNextDayUTC(timestamp) - const [chainBlockToday, chainBlockNextday] = await Promise.all([getBlock(ts, "avax", {}), getBlock(endDatets, "avax", {})]); - const [balanceToday, balanceNextday] = await Promise.all([getBalance(`0x${chainBlockToday.toString(16)}`), getBalance(`0x${chainBlockNextday.toString(16)}`)]); - const txFees = balanceNextday - balanceToday; - const pricesObj = await getPrices(["coingecko:avalanche-2"], ts); - const dailyFee = ((txFees) * pricesObj["coingecko:avalanche-2"].price); + fetch: async (timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const dailyFees = await getTokenDiff({ target: '0x0100000000000000000000000000000000000000', includeGasToken: true, options}) - return { - timestamp, - dailyFees: dailyFee.toString(), - dailyRevenue: dailyFee.toString(), - dailyHoldersRevenue: dailyFee.toString(), - }; - }, - start: 1609459200 + return { + timestamp, + dailyFees: dailyFees, + dailyRevenue: dailyFees, + dailyHoldersRevenue: dailyFees, + }; + }, + start: 1609459200 }, -}, + }, protocolType: ProtocolType.CHAIN } diff --git a/fees/canto.ts b/fees/canto.ts index 5a3ea5d67f..8062aa9054 100644 --- a/fees/canto.ts +++ b/fees/canto.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { blockscoutFeeAdapter } from "../helpers/blockscoutFees"; -export default blockscoutFeeAdapter(CHAIN.CANTO, "https://evm.explorer.canto.io/api?module=stats&action=totalfees", "coingecko:canto") \ No newline at end of file +export default blockscoutFeeAdapter(CHAIN.CANTO, "https://evm.explorer.canto.io/api?module=stats&action=totalfees", "canto") \ No newline at end of file diff --git a/fees/caviar-tangible.ts b/fees/caviar-tangible.ts index b59410fade..ebf5c269dc 100644 --- a/fees/caviar-tangible.ts +++ b/fees/caviar-tangible.ts @@ -1,42 +1,15 @@ import ADDRESSES from '../helpers/coreAssets.json' -import { FetchResultFees, SimpleAdapter } from "../adapters/types" +import { ChainBlocks, FetchOptions, FetchResultFees, SimpleAdapter } from "../adapters/types" import { CHAIN } from "../helpers/chains" -import { getBlock } from "../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; +import { addTokensReceived } from '../helpers/token'; -const topic0_evt_transfer = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'; -const topic1_evt_transfer = '0x000000000000000000000000bbc843dcb1009bc7dc988bceb5bb1b50299d9a6d'; -const topic2_evt_transfer = '0x0000000000000000000000006ced48efbb581a141667d7487222e42a3fa17cf7'; -const usdc = ADDRESSES.polygon.USDC; +const fetchFees = async (timestamp: number , _: ChainBlocks, options: FetchOptions): Promise => { + const dailyFees = await addTokensReceived({ options, tokens: [ADDRESSES.polygon.USDC], fromAddressFilter: '0xbbc843dcb1009bc7dc988bceb5bb1b50299d9a6d' , target: '0x6ced48efbb581a141667d7487222e42a3fa17cf7' }) -interface ILog { - data: string; - transactionHash: string; - topics: string[]; - address: string; -} - -const fetchFees = async (timestamp: number): Promise => { - const toTimestamp = timestamp; - const fromTimestamp = timestamp - 60 * 60 * 24; - const fromBlock = (await getBlock(fromTimestamp, CHAIN.POLYGON, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.POLYGON, {})); - - const logs: ILog[] = (await sdk.getEventLogs({ - target: usdc, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.POLYGON, - topics: [topic0_evt_transfer, topic1_evt_transfer, topic2_evt_transfer] - })) as ILog[]; - const dailyFees = logs.reduce((acc: number, log: ILog) => { - const amount = Number(log.data) / 10 ** 6; - return acc + amount; - }, 0); return { - dailyFees: `${dailyFees}`, - dailyHoldersRevenue: `${dailyFees}`, - dailyRevenue: `${dailyFees}`, + dailyFees: dailyFees, + dailyHoldersRevenue: dailyFees, + dailyRevenue: dailyFees, timestamp } } diff --git a/fees/celo.ts b/fees/celo.ts index 9809b3b07a..ea81c99c38 100644 --- a/fees/celo.ts +++ b/fees/celo.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { etherscanFeeAdapter } from "../helpers/etherscanFees"; -export default etherscanFeeAdapter(CHAIN.CELO, "https://celoscan.io/chart/transactionfee?output=csv", "coingecko:celo") +export default etherscanFeeAdapter(CHAIN.CELO, "https://celoscan.io/chart/transactionfee?output=csv") diff --git a/fees/cronos.ts b/fees/cronos.ts index f585164da2..52ec1e5812 100644 --- a/fees/cronos.ts +++ b/fees/cronos.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { blockscoutFeeAdapter } from "../helpers/blockscoutFees"; -export default blockscoutFeeAdapter(CHAIN.CRONOS, "https://cronos.org/explorer/api?module=stats&action=totalfees", "coingecko:crypto-com-chain") \ No newline at end of file +export default blockscoutFeeAdapter(CHAIN.CRONOS, "https://cronos.org/explorer/api?module=stats&action=totalfees") \ No newline at end of file diff --git a/fees/extra/index.ts b/fees/extra/index.ts index d4964415bd..8031344e3d 100644 --- a/fees/extra/index.ts +++ b/fees/extra/index.ts @@ -1,8 +1,6 @@ -import { Adapter, FetchResultFees } from "../../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getPrices } from "../../utils/prices"; -import request, { gql } from "graphql-request"; -import { getTimestampAtStartOfDayUTC } from "../../utils/date"; +import request from "graphql-request"; import BigNumber from "bignumber.js"; type TEndpoint = { @@ -30,12 +28,13 @@ interface ILendingPool { } const graphs = (chain: CHAIN) => { - return async (timestamp: number): Promise => { - const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp) - const fromTimestamp = todaysTimestamp - 60 * 60 * 24 - const toTimestamp = todaysTimestamp + return async (timestamp: number, _: ChainBlocks, { createBalances, startOfDay }: FetchOptions): Promise => { + const dailyFees = createBalances() - const farmingQuery = gql`{ + const fromTimestamp = startOfDay - 60 * 60 * 24 + const toTimestamp = startOfDay + + const farmingQuery = `{ feePaids( where: { blockTimestamp_lte: ${toTimestamp}, blockTimestamp_gte: ${fromTimestamp} }, first: 1000 @@ -46,7 +45,7 @@ const graphs = (chain: CHAIN) => { }` const graphRes: IFeePaid[] = (await request(endpoints[chain], farmingQuery)).feePaids; - const lendingQuery = gql`{ + const lendingQuery = `{ mintToTreasuries( where: { blockTimestamp_lte: ${toTimestamp}, blockTimestamp_gte: ${fromTimestamp} }, first: 1000 @@ -57,7 +56,7 @@ const graphs = (chain: CHAIN) => { }` const lendingGraphRes: ILendingPaid[] = (await request(endpoints[chain], lendingQuery)).mintToTreasuries; - const lendingPoolsQuery = gql`{ + const lendingPoolsQuery = `{ lendingReservePools(first: 1000) { eTokenAddress exchangeRate @@ -86,19 +85,16 @@ const graphs = (chain: CHAIN) => { const allFeesList = [...graphRes, ...lendingFeeList] - const coins = [...new Set(allFeesList.map((e: IFeePaid) => `${chain}:${e.asset.toLowerCase()}`))] - const prices = await getPrices(coins, todaysTimestamp); - const dailyFees = allFeesList.map((e: IFeePaid) => { - const decimals = prices[`${chain}:${e.asset.toLowerCase()}`]?.decimals; - const price = prices[`${chain}:${e.asset.toLowerCase()}`]?.price; - return new BigNumber(e.amount).dividedBy(new BigNumber(`1e+${decimals || 0}`)).toNumber() * (price || 0) - }).reduce((a: number, b: number) => a + b, 0) + allFeesList.map((e: IFeePaid) => { + dailyFees.add(e.asset, e.amount) + }) + const dailyRevenue = dailyFees.clone() + dailyRevenue.resizeBy(0.5) return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyFees * 0.5}`, - timestamp, + timestamp: startOfDay, + dailyFees, dailyRevenue, }; }; } diff --git a/fees/foundation.ts b/fees/foundation.ts index 2668637e34..38d99eb7c2 100644 --- a/fees/foundation.ts +++ b/fees/foundation.ts @@ -1,128 +1,68 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, FetchResultFees } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; -import { getPrices } from "../utils/prices"; - const market_address = '0xcda72070e455bb31c7690a170224ce43623d0b6f'; const nft_drop_market_address = '0x53f451165ba6fdbe39a134673d13948261b2334a'; const topic_0_reserveAuction_finalized = '0x2edb0e99c6ac35be6731dab554c1d1fa1b7beb675090dbb09fb14e615aca1c4a'; - const topic_0_private_sale_finalized = '0x6c623fa5e13aaaf28288f807e5b4f9ec6fb7ef812568e00317c552663bea918f'; - const topic_0_buyPrice_accepted = '0xd28c0a7dd63bc853a4e36306655da9f8c0b29ff9d0605bb976ae420e46a99930'; - const topic_0_offer_accepted = '0x1cb8adb37d6d35e94cd0695ca39895b84371864713f5ca7eada52af9ff23744b' - const topic_0_mint_from_fixed_price_drop = '0x05ebbb6b0ce7d564230ba625dd7a0e5108786b0852d6060de6099e1778203e34' - const topic_0_withdraw_creator_revenue_from_dutch_auction = '0x5e16e96b4ba4fe46f3be73d54d1fa0da481494ab74c2d6e33328366d6437693c' -interface IFee { - totalFees: number; -} -const fetch = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - const fromBlock = (await getBlock(fromTimestamp, CHAIN.ETHEREUM, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.ETHEREUM, {})); - const logs_reserveAuction_finalized: IFee[] = (await sdk.getEventLogs({ +// todo: track new events +const fetch = async (timestamp: number , _: ChainBlocks, { createBalances, getLogs, }: FetchOptions): Promise => { + const dailyFees = createBalances(); + (await getLogs({ target: market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_reserveAuction_finalized] + topics: [topic_0_reserveAuction_finalized], + eventAbi: "event ReserveAuctionFinalized(uint256 indexed auctionId, address indexed seller, address indexed bidder, uint256 totalFees, uint256 creatorRev, uint256 sellerRev)", })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(0, 64)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.totalFees) }); - const logs_private_sale_finalized: IFee[] = (await sdk.getEventLogs({ + (await getLogs({ target: market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_private_sale_finalized] + topics: [topic_0_private_sale_finalized], + eventAbi: "event PrivateSaleFinalized(address indexed nftContract, uint256 indexed tokenId, address indexed seller, address buyer, uint256 f8nFee, uint256 creatorFee, uint256 ownerRev, uint256 deadline)" })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(64, 128)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.f8nFee) }); - const logs_buyPrice_accepted: IFee[] = (await sdk.getEventLogs({ + (await getLogs({ target: market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_buyPrice_accepted] + topics: [topic_0_buyPrice_accepted], + eventAbi: "event BuyPriceAccepted(address indexed nftContract, uint256 indexed tokenId, address indexed seller, address buyer, uint256 totalFees, uint256 creatorRev, uint256 sellerRev)" })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(64, 128)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.totalFees) }); - const logs_offer_accepted: IFee[] = (await sdk.getEventLogs({ + (await getLogs({ target: market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_offer_accepted] + topics: [topic_0_offer_accepted], + eventAbi: "event OfferAccepted(address indexed nftContract, uint256 indexed tokenId, address indexed buyer, address seller, uint256 totalFees, uint256 creatorRev, uint256 sellerRev)" })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(64, 128)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.totalFees) }); - const logs_mint_from_fixed_price_drop: IFee[] = (await sdk.getEventLogs({ + (await getLogs({ target: nft_drop_market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_mint_from_fixed_price_drop] + topics: [topic_0_mint_from_fixed_price_drop], + eventAbi: "event MintFromFixedPriceDrop (address indexed nftContract, address indexed buyer, uint256 indexed firstTokenId, uint256 count, uint256 totalFees, uint256 creatorRev)" })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(64, 128)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.totalFees) }); - const logs_withdraw_creator_revenue_from_dutch_auction: IFee[] = (await sdk.getEventLogs({ + (await getLogs({ target: nft_drop_market_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic_0_withdraw_creator_revenue_from_dutch_auction] + topics: [topic_0_withdraw_creator_revenue_from_dutch_auction], + eventAbi: "event WithdrawCreatorRevenueFromDutchAuction (address indexed nftContract, uint256 clearingPrice, uint256 totalMintedCount, uint256 totalFees, uint256 creatorRev)" })).map((e: any) => { - const amount = Number('0x' + e.data.replace('0x', '').slice(128, 192)) / 10 ** 18; - return { - totalFees: amount - } + dailyFees.addGasToken(e.totalFees) }); - const total_logs = [ - ...logs_reserveAuction_finalized, - ...logs_private_sale_finalized, - ...logs_buyPrice_accepted, - ...logs_offer_accepted, - ...logs_mint_from_fixed_price_drop, - ...logs_withdraw_creator_revenue_from_dutch_auction, - ] - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const totalFees = total_logs.reduce((a: number, b: IFee) => a + b.totalFees, 0) - const dailyFees = totalFees * ethPrice - const dailyRevenue = dailyFees; return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyRevenue}`, - timestamp + dailyFees, dailyRevenue: dailyFees, timestamp } } diff --git a/fees/gnd-protocol.ts b/fees/gnd-protocol.ts index 2270314393..20be25da44 100644 --- a/fees/gnd-protocol.ts +++ b/fees/gnd-protocol.ts @@ -1,88 +1,36 @@ import ADDRESSES from '../helpers/coreAssets.json' import { Chain } from "@defillama/sdk/build/general"; -import { Adapter, FetchResultFees } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { getBlock } from "../helpers/getBlock"; -import { ethers } from "ethers"; -import * as sdk from "@defillama/sdk"; -import { getPrices } from "../utils/prices"; +import { addTokensReceived } from '../helpers/token'; - -const topic0_fund_supply = '0xb1fa5064e2075b991c022c25e7b05c0a1b56a9462985b12fe2e89e51b46c6b8b' const event_funds_supply = 'event SupplyFund(uint256 amount)'; -const contract_interface = new ethers.Interface([ - event_funds_supply, -]); -interface IData { - amount: number; -} type TAddress = { [s: string]: string; } const address_buyback: TAddress = { [CHAIN.ARBITRUM]: '0x5f0feef4dafea7fb4d6ca89c047767885226b5f9' } -type TTopics = { - [s: string]: string[]; -} -const weth_address_tranfer_topic:TTopics = { - [CHAIN.ARBITRUM]: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x000000000000000000000000d70811f1e4992aa051d54e29a04c8925b32fba7d', - '0x000000000000000000000000535ec56479892d9c02fe2bb86cebf7ed62e81131' - ] -} - -const weth_address: TAddress = { - [CHAIN.ARBITRUM]: ADDRESSES.arbitrum.WETH -} const fetch = (chain: Chain) => { - return async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, chain, {})); - const toBlock = (await getBlock(toTimestamp, chain, {})); + return async (timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const dividends = await addTokensReceived({ tokens: [ADDRESSES.arbitrum.WETH], options, fromAddressFilter: '0xd70811f1e4992aa051d54e29a04c8925b32fba7d', target: '0x535ec56479892d9c02fe2bb86cebf7ed62e81131' }) - const logs_fund_disposit: IData[] = (await sdk.getEventLogs({ + const logs_fund_disposit = (await options.getLogs({ target: address_buyback[chain], - toBlock: toBlock, - fromBlock: fromBlock, - chain: chain, - topics: [topic0_fund_supply] - })).map((a: any) => contract_interface.parseLog(a)) - .map((a: any) => { - return { - amount: Number(a!.args.amount) / 10 ** 18, - } as IData - }); + eventAbi: event_funds_supply, + })) - const logs_dividends: any[] = (await sdk.getEventLogs({ - target: weth_address[chain], - toBlock: toBlock, - fromBlock: fromBlock, - chain: chain, - topics: weth_address_tranfer_topic[chain] - })) - .map((a: any) => { - return { - amount: Number(a.data) / 10 ** 18, - } as IData - }); + const dailyRevenue = options.createBalances() + logs_fund_disposit.forEach((log) => dailyRevenue.addGasToken(log.amount)) + const dailyFees = dividends.clone() + dailyFees.addBalances(dailyRevenue) - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const buybackAmount = logs_fund_disposit.reduce((sum: number, a: IData) => sum + a.amount, 0); - const dividends = logs_dividends.reduce((a: number, b: IData) => a + b.amount, 0); - const buybackAmountUSD = buybackAmount * ethPrice; - const dividendsUSD = dividends * ethPrice; - const dailyFees = buybackAmountUSD + dividendsUSD; return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${buybackAmountUSD}`, - dailyHoldersRevenue: `${buybackAmountUSD}`, - dailySupplySideRevenue: `${dividendsUSD}`, + dailyFees: dailyFees, + dailyRevenue: dailyRevenue, + dailyHoldersRevenue: dailyRevenue, + dailySupplySideRevenue: dividends, timestamp } } diff --git a/fees/hono.ts b/fees/hono.ts index bd6fc656c6..390f39b632 100644 --- a/fees/hono.ts +++ b/fees/hono.ts @@ -1,11 +1,8 @@ -import ADDRESSES from '../helpers/coreAssets.json' import { Adapter, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { request, gql } from "graphql-request"; -import type { ChainEndpoints } from "../adapters/types" +import type { ChainBlocks, ChainEndpoints, FetchOptions } from "../adapters/types" import { Chain } from '@defillama/sdk/build/general'; -import { getPrices } from "../utils/prices"; - interface IData { id: string; @@ -27,22 +24,18 @@ const graph = (graphUrls: ChainEndpoints) => { }`; return (chain: Chain) => { - return async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp + return async (timestamp: number, _: ChainBlocks, { createBalances, fromTimestamp, toTimestamp, }: FetchOptions): Promise => { + const dailyFees = createBalances() const graphRes: IData[] = (await request(graphUrls[chain], graphQuery, { timestampFrom: fromTimestamp, timestampTo: toTimestamp })).dailyRevenueAggregators; - const ethcoinID = "ethereum:" + ADDRESSES.null; - const prices = await getPrices([ethcoinID], timestamp); - const value = graphRes.reduce((acc, cur) => acc + Number(cur.todayETHRevenue) / 10 ** 18, 0); - const dailyRevenue = (value) * prices[ethcoinID].price; - const dailyFees = dailyRevenue; + const value = graphRes.reduce((acc, cur) => acc + Number(cur.todayETHRevenue), 0); + dailyFees.addGasToken(value) return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyRevenue}`, + dailyFees, + dailyRevenue: dailyFees, timestamp } } diff --git a/fees/justlend.ts b/fees/justlend.ts index 8857c72f3b..353f2740a0 100644 --- a/fees/justlend.ts +++ b/fees/justlend.ts @@ -1,21 +1,11 @@ import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, ChainBlocks, FetchResultFees } from "../adapters/types" +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../adapters/types" import { CHAIN } from "../helpers/chains"; import * as sdk from "@defillama/sdk"; import { BigNumberish } from "ethers"; -import { getPrices } from "../utils/prices"; import { fromHex, toHex } from "tron-format-address"; import { httpGet } from "../utils/fetchURL"; -interface IPrices { - [address: string]: { - decimals: number; - price: number; - symbol: string; - timestamp: number; - }; -} - interface IContext { currentTimestamp: number; startTimestamp: number; @@ -25,7 +15,6 @@ interface IContext { markets: string[]; underlyings: string[]; reserveFactors: string[]; - prices: IPrices; } interface IAccrueInterestLog { market: string; @@ -35,53 +24,23 @@ interface IAccrueInterestLog { totalBorrowsNew: BigNumberish; } -interface ITx { - address: string; - data: string; - topics: string[]; - transactionHash: string; -} - -const comptrollerABI = { - getAllMarkets: "function getAllMarkets() external view returns (address[])", -}; - - -const tokenABI = { - underlying: "function underlying() external view returns (address)", - accrueInterest:"event AccrueInterest(uint256 cashPrior,uint256 interestAccumulated,uint256 borrowIndex,uint256 totalBorrows)", - reserveFactorMantissa: "function reserveFactorMantissa() external view returns (uint256)", -}; - -const fetch = async (timestamp: number): Promise => { - const context = await getContext(timestamp, {}); - const { dailyProtocolFees, dailyProtocolRevenue } = await getDailyProtocolFees(context); - const dailySupplySideRevenue = (dailyProtocolFees - dailyProtocolRevenue); +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, fromTimestamp, toTimestamp, }: FetchOptions): Promise => { + const context = await getContext(timestamp, {}, { fromTimestamp, toTimestamp }); + const dailyProtocolFees = createBalances(); + const dailyProtocolRevenue = createBalances(); + await getDailyProtocolFees(context, { dailyProtocolFees, dailyProtocolRevenue, }); + const dailySupplySideRevenue = dailyProtocolFees.clone(); + dailySupplySideRevenue.subtract(dailyProtocolRevenue); return { timestamp, - dailyFees: dailyProtocolFees.toString(), - dailyRevenue: dailyProtocolRevenue.toString(), - dailyHoldersRevenue: dailyProtocolRevenue.toString(), - dailySupplySideRevenue: `${dailySupplySideRevenue}` + dailyFees: dailyProtocolFees, + dailyRevenue: dailyProtocolRevenue, + dailyHoldersRevenue: dailyProtocolRevenue, + dailySupplySideRevenue: dailySupplySideRevenue } } -const getAllMarkets = async ( - unitroller: string, - chain: CHAIN -): Promise => { - return ( - await sdk.api2.abi.call({ - target: unitroller, - abi: comptrollerABI.getAllMarkets, - chain: chain, - }) - ); -}; - -const getContext = async (timestamp: number, _: ChainBlocks): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp +const getContext = async (timestamp: number, _: ChainBlocks, { fromTimestamp, toTimestamp }: { fromTimestamp: number, toTimestamp: number}): Promise => { const min_block_timestamp = fromTimestamp * 1000; const max_block_timestamp = toTimestamp * 1000; @@ -107,7 +66,7 @@ const getContext = async (timestamp: number, _: ChainBlocks): Promise 'TGkxzkDKyMeq2T7edKnyjZoFypyzjkkssq' ]; - const allMarketAddressess:string[] =[ + const allMarketAddressess: string[] = [ '0x2C7c9963111905d29eB8Da37d28b0F53A7bB5c28', '0xea09611b57e89d67FBB33A516eB90508Ca95a3e5', '0x6eF7C4870977C6a2543b0E8cF4F659AF883C96Dc', @@ -130,23 +89,17 @@ const getContext = async (timestamp: number, _: ChainBlocks): Promise ]; const reserveFactors: string[] = [ - '100000000000000000', '50000000000000000', - '50000000000000000', '1000000000000000000', - '200000000000000000', '100000000000000000', - '200000000000000000', '200000000000000000', + '100000000000000000', '50000000000000000', + '50000000000000000', '1000000000000000000', + '200000000000000000', '100000000000000000', + '200000000000000000', '200000000000000000', '1000000000000000000', '50000000000000000', - '200000000000000000', '300000000000000000', - '50000000000000000', '200000000000000000', - '50000000000000000', '1000000000000000000', - '100000000000000000', '100000000000000000', + '200000000000000000', '300000000000000000', + '50000000000000000', '200000000000000000', + '50000000000000000', '1000000000000000000', + '100000000000000000', '100000000000000000', '50000000000000000' ] - const prices = await getPrices( - [ - ...underlyings.filter((e: string) => e).map((x: string) => `${CHAIN.TRON}:${x.toLowerCase()}`), - ], - timestamp - ); return { currentTimestamp: timestamp, @@ -157,37 +110,11 @@ const getContext = async (timestamp: number, _: ChainBlocks): Promise markets: allMarketAddressess, underlyings, reserveFactors, - prices, - }; -}; - -const getMarketDetails = async (markets: string[], chain: CHAIN): Promise<{underlyings: string[], reserveFactors:string[]}> => { - const underlyings = await sdk.api2.abi.multiCall({ - calls: markets.map((market: string) => ({ - target: market, - })), - abi: tokenABI.underlying, - chain: chain, - permitFailure: true, - }); - - const reserveFactors = await sdk.api2.abi.multiCall({ - calls: markets.map((market: string) => ({ - target: market, - })), - abi: tokenABI.reserveFactorMantissa, - chain: chain, - permitFailure: true, - }); - const _underlyings = underlyings; - _underlyings[0] = ADDRESSES.tron.WTRX; - return { - underlyings: _underlyings, - reserveFactors: reserveFactors, }; }; const endpoint = `https://api.trongrid.io` +// TODO: check and replace code to fetch logs more than 200 const getLogs = async (address: string, min_block_timestamp: number, max_block_timestamp: number) => { const url = `${endpoint}/v1/contracts/${fromHex(address)}/events?event_name=AccrueInterest&min_block_timestamp=${min_block_timestamp}&max_block_timestamp=${max_block_timestamp}&limit=200`; const res = await httpGet(url); @@ -199,14 +126,12 @@ const getDailyProtocolFees = async ({ markets, underlyings, reserveFactors, - prices, startBlock, endBlock, -}: IContext) => { - let dailyProtocolFees = 0; - let dailyProtocolRevenue = 0; +}: IContext, { dailyProtocolFees, dailyProtocolRevenue }: { dailyProtocolFees: sdk.Balances, dailyProtocolRevenue: sdk.Balances}) => { + let logs: any[] = []; - for(let i = 0; i < markets.length; i++) { + for (let i = 0; i < markets.length; i++) { const address = markets[i]; await delay(2500) const _logs = await getLogs(address, startBlock, endBlock); @@ -226,24 +151,12 @@ const getDailyProtocolFees = async ({ } }); - raw_data.forEach((log: IAccrueInterestLog) => { const marketIndex = markets.findIndex((e: string) => e.toLowerCase() === log.market.toLowerCase()); const underlying = underlyings[marketIndex].toLowerCase(); - const price = prices[`${CHAIN.TRON}:${underlying?.toLowerCase()}`]; - - const interestTokens = Math.abs((Number(log.interestAccumulated) / (10 ** price?.decimals || 0))); - const reserveFactor = Math.abs(Number(reserveFactors[marketIndex]) / 1e18); - const interestUSD = interestTokens * price?.price || 0 - - dailyProtocolFees += interestUSD; - dailyProtocolRevenue += interestUSD * reserveFactor; + dailyProtocolFees.add(underlying, Number(log.interestAccumulated)); + dailyProtocolRevenue.add(underlying, Number(log.interestAccumulated) * Number(reserveFactors[marketIndex]) / 1e18); }); - - return { - dailyProtocolFees, - dailyProtocolRevenue, - }; }; diff --git a/fees/klaytn.ts b/fees/klaytn.ts index 790ea31816..38dc451051 100644 --- a/fees/klaytn.ts +++ b/fees/klaytn.ts @@ -1,28 +1,23 @@ -import { Adapter, FetchResult, ProtocolType } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResult, ProtocolType } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { getPrices } from "../utils/prices"; import { adapterBitqueryFeesEthereumNetwork, ITx } from "../helpers/bitqueryFees"; import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date"; const startTime = 1577836800; -const fetch = async (timestamp: number): Promise => { - const dayTimestamp = getTimestampAtStartOfDayUTC(timestamp); +const fetch = async (_timestamp: number , _: ChainBlocks, { createBalances, startOfDay }: FetchOptions): Promise => { + const dailyFees = createBalances() const startTimestamp = getTimestampAtStartOfDayUTC(startTime); - const tillTimestamp = getTimestampAtStartOfNextDayUTC(timestamp); + const tillTimestamp = getTimestampAtStartOfNextDayUTC(startOfDay); const form = new Date(startTimestamp * 1000).toISOString().split('T')[0]; const till = new Date((tillTimestamp - 1) * 1000).toISOString(); const result: ITx[] = await adapterBitqueryFeesEthereumNetwork(form, till, "klaytn"); - const totalFees = result.filter((a: ITx) => new Date(a.date.date).getTime() <= new Date(till).getTime()).reduce((a: number, b: ITx)=> a + b.gasValue, 0); - const dailyFees = result.find((a: ITx) => (getTimestampAtStartOfDayUTC(new Date(a.date.date).getTime()) /1000) === getTimestampAtStartOfDayUTC(new Date(dayTimestamp).getTime()))?.gasValue - const price_id = 'coingecko:klay-token' - const price = (await getPrices([price_id], dayTimestamp))[price_id].price; - const dailyFeesUsd = (dailyFees || 0) * price; - const totalFeesUsd = (totalFees * price) + // const totalFees = result.filter((a: ITx) => new Date(a.date.date).getTime() <= new Date(till).getTime()).reduce((a: number, b: ITx)=> a + b.gasValue, 0); + const _dailyFees = result.find((a: ITx) => (getTimestampAtStartOfDayUTC(new Date(a.date.date).getTime()) /1000) === getTimestampAtStartOfDayUTC(new Date(startOfDay).getTime()))?.gasValue + if (!_dailyFees) return { timestamp: startOfDay, }; + dailyFees.addGasToken(_dailyFees * 1e18); return { - timestamp, - totalFees: totalFeesUsd.toString(), - dailyFees: dailyFeesUsd.toString() + timestamp: startOfDay, dailyFees, }; }; diff --git a/fees/liquis.ts b/fees/liquis.ts index 336e6b8851..966a5d07b5 100644 --- a/fees/liquis.ts +++ b/fees/liquis.ts @@ -1,59 +1,27 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { getBlock } from "../helpers/getBlock"; -import { Adapter, FetchResultFees } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import { getTimestampAtStartOfDayUTC } from "../utils/date"; -import { getPrices } from "../utils/prices"; -import * as sdk from "@defillama/sdk"; +import { addTokensReceived } from '../helpers/token'; const LIT = '0xfd0205066521550d7d7ab19da8f72bb004b4c341'; const OLIT_TOKEN = '0x627fee87d0D9D2c55098A06ac805Db8F98B158Aa'; -const topic = 'event Transfer (address indexed from, address indexed to, uint256 amount)'; -const topic0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'; -const topic1 = '0x0000000000000000000000000000000000000000000000000000000000000000'; -const topic2 = '0x00000000000000000000000037aeB332D6E57112f1BFE36923a7ee670Ee9278b'; - -interface ILog { - topics: string[]; - data: string; - transactionHash: string; -} const fetch = () => { - return async (timestamp: number): Promise => { - const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp); - const dayAgo = todaysTimestamp - 60 * 60 * 24; - const fromBlock = await getBlock(dayAgo, CHAIN.ETHEREUM, {}); - const toBlock = await getBlock(todaysTimestamp, CHAIN.ETHEREUM, {}); - - const logs: ILog[] = (await sdk.getEventLogs({ - target: OLIT_TOKEN, - topic: topic, - toBlock, - fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic0, topic1, topic2] - })) as ILog[]; - - const olit_transfer_amounts: number[] = logs.map((e: any) => { - return Number(e.data) / 10 ** 18; - }); - - const litAddress = `ethereum:${LIT.toLowerCase()}`; - const litPrice = (await getPrices([litAddress], todaysTimestamp))[litAddress].price; - - const olit_transfer_amount = olit_transfer_amounts.reduce((a: number, b: number) => a + b, 0); - const dailyFee = olit_transfer_amount * (litPrice / 2); - const dailySupplySideRevenue = dailyFee * .75 - const dailyRevenue = dailyFee * .25; - const dailyHoldersRevenue = dailyFee * .03; - + return async (timestamp: number, _: ChainBlocks, options: FetchOptions): Promise => { + + const dailyFees = await addTokensReceived({ options, tokens: [OLIT_TOKEN], target: '0x37aeB332D6E57112f1BFE36923a7ee670Ee9278b', tokenTransform: () => LIT }) + dailyFees.resizeBy(0.5) + const dailyRevenue = dailyFees.clone() + dailyRevenue.resizeBy(0.25) + const dailyHoldersRevenue = dailyFees.clone() + dailyHoldersRevenue.resizeBy(0.03) + const dailySupplySideRevenue = dailyFees.clone() + dailySupplySideRevenue.resizeBy(0.75) return { - timestamp: todaysTimestamp, - dailyFees: dailyFee.toString(), - dailyRevenue: dailyRevenue.toString(), - dailySupplySideRevenue: dailySupplySideRevenue.toString(), - dailyHoldersRevenue: dailyHoldersRevenue.toString(), + timestamp, + dailyFees, + dailyRevenue: dailyRevenue, + dailySupplySideRevenue: dailySupplySideRevenue, + dailyHoldersRevenue: dailyHoldersRevenue, } as FetchResultFees } } diff --git a/fees/mixin.ts b/fees/mixin.ts index 1a04b6b6c7..4db6008c8c 100644 --- a/fees/mixin.ts +++ b/fees/mixin.ts @@ -1,4 +1,4 @@ import { blockscoutFeeAdapter } from "../helpers/blockscoutFees"; // DO NOT LIST, unsure if correct -export default blockscoutFeeAdapter("mixin", "https://scan.mvm.dev/api?module=stats&action=totalfees", "coingecko:mixin") \ No newline at end of file +export default blockscoutFeeAdapter("mixin", "https://scan.mvm.dev/api?module=stats&action=totalfees", "mixin") \ No newline at end of file diff --git a/fees/moonbeam.ts b/fees/moonbeam.ts index 3098754eed..1745ff8b38 100644 --- a/fees/moonbeam.ts +++ b/fees/moonbeam.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { etherscanFeeAdapter } from "../helpers/etherscanFees"; -export default etherscanFeeAdapter(CHAIN.MOONBEAN, "https://moonscan.io/chart/transactionfee?output=csv", "coingecko:moonbeam") +export default etherscanFeeAdapter(CHAIN.MOONBEAN, "https://moonscan.io/chart/transactionfee?output=csv") diff --git a/fees/moonriver.ts b/fees/moonriver.ts index 35ad7af931..2c2fccfc72 100644 --- a/fees/moonriver.ts +++ b/fees/moonriver.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { etherscanFeeAdapter } from "../helpers/etherscanFees"; -export default etherscanFeeAdapter(CHAIN.MOONRIVER, "https://moonriver.moonscan.io/chart/transactionfee?output=csv", "coingecko:moonriver") \ No newline at end of file +export default etherscanFeeAdapter(CHAIN.MOONRIVER, "https://moonriver.moonscan.io/chart/transactionfee?output=csv") \ No newline at end of file diff --git a/fees/pepe-swaves/index.ts b/fees/pepe-swaves/index.ts index 8d94674c3b..7ae167f801 100644 --- a/fees/pepe-swaves/index.ts +++ b/fees/pepe-swaves/index.ts @@ -1,15 +1,10 @@ -import { Adapter, ProtocolType } from "../../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getBlock } from "../../helpers/getBlock"; -import { secondsInDay } from "../../utils/date"; import fetchURL from "../../utils/fetchURL"; -import { getPrices } from "../../utils/prices"; const ADAPTER = "3PHTxmSNQsrZocZRAWidNbdcxqRpzHiK5Mt"; const WAVES_NODE = "https://nodes.wavesnodes.com"; -const MILLISECONDS_IN_SECOND = 1_000; const LIMIT_PER_REQUEST = 99; -const WAVES_DIVIDER = 1e8; const FEE_DIVIDER = 1e4; interface IData { @@ -40,17 +35,14 @@ const extractShareReward = (rewardShares: RewardShares, miner: string): number = }, 0) } -const fetch = async (timestamp: number) => { +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getFromBlock, getToBlock, }: FetchOptions) => { let miner = (await getData(ADAPTER, "ADAPTEE")).value; let feeRate = +(await getData(ADAPTER, "FEE_RATE")).value / FEE_DIVIDER; - const fromTimestamp = (timestamp - secondsInDay) * MILLISECONDS_IN_SECOND; - const toTimestamp = timestamp * MILLISECONDS_IN_SECOND; - - let startBlock = (await getBlock(fromTimestamp, CHAIN.WAVES, {})); - const endBlock = (await getBlock(toTimestamp, CHAIN.WAVES, {})); - const wavesToken = "waves:WAVES"; - const price = (await getPrices([wavesToken], timestamp))[wavesToken]?.price; + const dailyFees = createBalances() + let startBlock = await getFromBlock(); + const endBlock = await getToBlock() + const wavesToken = "WAVES"; let blockHeaders: IBlockHeader[] = []; while (startBlock < endBlock) { @@ -63,17 +55,15 @@ const fetch = async (timestamp: number) => { } } let mainerBlocHeaders = blockHeaders.filter(header => header.generator === miner); - let stakingRewardsInUSD = mainerBlocHeaders.reduce((acc, header) => { + dailyFees.add(wavesToken, mainerBlocHeaders.reduce((acc, header) => { let txReward = header.totalFee; return acc + txReward + extractShareReward(header.rewardShares, miner.toString()); - }, 0) / WAVES_DIVIDER * price; - let protocolRevenue = stakingRewardsInUSD * feeRate; + }, 0)) + const dailyRevenue = dailyFees.clone() + dailyRevenue.resizeBy(feeRate) return { - timestamp, - dailyFees: stakingRewardsInUSD.toString(), - dailyRevenue: protocolRevenue.toString(), - dailyHoldersRevenue: '0' + timestamp, dailyFees, dailyRevenue, }; }; diff --git a/fees/ramses-exchange-v2/bribes.ts b/fees/ramses-exchange-v2/bribes.ts index 56718fcb1c..29e01279c5 100644 --- a/fees/ramses-exchange-v2/bribes.ts +++ b/fees/ramses-exchange-v2/bribes.ts @@ -1,24 +1,17 @@ -import request, { gql } from "graphql-request"; -import { getPrices } from "../../utils/prices"; -import { CHAIN } from "../../helpers/chains"; - -type TPrice = { - [s: string]: { - price: number; - decimals: number; - }; -}; +import request from "graphql-request"; +import { Balances } from "@defillama/sdk"; interface IBribes { amount: number; token: { id: string; + decimals: number; }; } -export const fees_bribes = async (fromBlock: number, timestamp: number): Promise => { +export const fees_bribes = async (fromBlock: number, timestamp: number, balances: Balances) => { const endpoint = 'https://api.thegraph.com/subgraphs/name/ramsesexchange/concentrated-liquidity-graph'; - const graphQuery = gql` + const graphQuery = ` query GetBribes($fromBlock: Int!) { bribes( where: { timestamp_gte: ${timestamp} } @@ -26,31 +19,17 @@ export const fees_bribes = async (fromBlock: number, timestamp: number): Promise amount token { id + decimals } } } `; - const graphRes: { bribes: IBribes[] } = await request(endpoint, graphQuery, { - fromBlock, - }); + const graphRes: { bribes: IBribes[] } = await request(endpoint, graphQuery, { fromBlock, }); const logs_bribes = graphRes.bribes; - const coins = [...new Set(logs_bribes.map((e: IBribes) => `${CHAIN.ARBITRUM}:${e.token.id.toLowerCase()}`))]; - const coins_split: string[][] = []; - - for (let i = 0; i < coins.length; i += 100) { - coins_split.push(coins.slice(i, i + 100)); - } - - const prices_result: TPrice[] = await Promise.all(coins_split.map((a: string[]) => getPrices(a, timestamp))); - const prices: TPrice = Object.assign({}, ...prices_result); - - const fees_bribes_usd = logs_bribes.map((e: IBribes) => { - const price = prices[`${CHAIN.ARBITRUM}:${e.token.id.toLowerCase()}`]?.price || 0; - const decimals = prices[`${CHAIN.ARBITRUM}:${e.token.id.toLowerCase()}`]?.decimals || 0; - return (Number(e.amount)) * price; - }).reduce((a: number, b: number) => a + b, 0); - return fees_bribes_usd; + logs_bribes.map((e: IBribes) => { + balances.add(e.token.id, e.amount * Math.pow(10, e.token.decimals)); + }) }; diff --git a/fees/ramses-exchange-v2/index.ts b/fees/ramses-exchange-v2/index.ts index ea350246b3..361876e250 100644 --- a/fees/ramses-exchange-v2/index.ts +++ b/fees/ramses-exchange-v2/index.ts @@ -1,89 +1,84 @@ -import { Adapter, SimpleAdapter, FetchResultFees, BaseAdapter } from "../../adapters/types"; +import { Adapter, FetchOptions } from "../../adapters/types"; import { ARBITRUM, CHAIN } from "../../helpers/chains"; import { fees_bribes } from './bribes'; -import { getBlock } from '../../helpers/getBlock'; import { - getGraphDimensions, - DEFAULT_DAILY_VOLUME_FACTORY, - DEFAULT_TOTAL_VOLUME_FIELD, - } from "../../helpers/getUniSubgraph" + getGraphDimensions, + DEFAULT_DAILY_VOLUME_FACTORY, + DEFAULT_TOTAL_VOLUME_FIELD, +} from "../../helpers/getUniSubgraph" type TStartTime = { [key: string]: number; } -const startTimeV2:TStartTime = { +const startTimeV2: TStartTime = { [CHAIN.ARBITRUM]: 1685574000, } -const getBribes = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24; - const fromBlock = (await getBlock(timestamp, CHAIN.ARBITRUM, {})); - const bribes = await fees_bribes(fromBlock, timestamp); - const fromBlock_delta = (await getBlock(fromTimestamp, CHAIN.ARBITRUM, {})); - const bribes_delta = await fees_bribes(fromBlock_delta, fromTimestamp); +const getBribes = async ({ fromTimestamp, toTimestamp, createBalances, getFromBlock, }: FetchOptions): Promise => { + const fromBlock = await getFromBlock() + const bribes = createBalances(); + const bribes_delta = createBalances(); + await fees_bribes(fromBlock, toTimestamp, bribes_delta); + await fees_bribes(fromBlock, fromTimestamp, bribes); + bribes.subtract(bribes_delta); return { - dailyBribesRevenue: `${ bribes_delta - bribes}`, - timestamp: timestamp, + timestamp: toTimestamp, + dailyBribesRevenue: bribes, }; }; const v2Endpoints = { - [CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/ramsesexchange/concentrated-liquidity-graph", - }; + [CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/ramsesexchange/concentrated-liquidity-graph", +}; const VOLUME_USD = "volumeUSD"; const v2Graphs = getGraphDimensions({ - graphUrls: v2Endpoints, - totalVolume: { - factory: "factories", - field: DEFAULT_TOTAL_VOLUME_FIELD, - }, - dailyVolume: { - factory: DEFAULT_DAILY_VOLUME_FACTORY, - field: VOLUME_USD, - }, - feesPercent: { - type: "fees", - HoldersRevenue: 75, - ProtocolRevenue: 5, - SupplySideRevenue: 20, - UserFees: 100, // User fees are 100% of collected fees - Revenue: 80 // Revenue is 100% of collected fees - } - }); - // https://docs.ramses.exchange/ramses-cl-v2/concentrated-liquidity/fee-distribution - const methodology = { - UserFees: "User pays 0.3% fees on each swap.", - ProtocolRevenue: "Revenue going to the protocol. 5% of collected fees. (is probably right because the distribution is dynamic.)", - HoldersRevenue: "User fees are distributed among holders. 75% of collected fees. (is probably right because the distribution is dynamic.)", - SupplySideRevenue: "20% of collected fees are distributed among LPs. (is probably right because the distribution is dynamic.)" - } + graphUrls: v2Endpoints, + totalVolume: { + factory: "factories", + field: DEFAULT_TOTAL_VOLUME_FIELD, + }, + dailyVolume: { + factory: DEFAULT_DAILY_VOLUME_FACTORY, + field: VOLUME_USD, + }, + feesPercent: { + type: "fees", + HoldersRevenue: 75, + ProtocolRevenue: 5, + SupplySideRevenue: 20, + UserFees: 100, // User fees are 100% of collected fees + Revenue: 80 // Revenue is 100% of collected fees + } +}); +// https://docs.ramses.exchange/ramses-cl-v2/concentrated-liquidity/fee-distribution +const methodology = { + UserFees: "User pays 0.3% fees on each swap.", + ProtocolRevenue: "Revenue going to the protocol. 5% of collected fees. (is probably right because the distribution is dynamic.)", + HoldersRevenue: "User fees are distributed among holders. 75% of collected fees. (is probably right because the distribution is dynamic.)", + SupplySideRevenue: "20% of collected fees are distributed among LPs. (is probably right because the distribution is dynamic.)" +} - const adapter: Adapter = { - adapter: { - [CHAIN.ARBITRUM]: { - fetch: async (timestamp: number, chainBlocks: any) => { - const v2Result = await v2Graphs(ARBITRUM)(timestamp, chainBlocks); // Pass chainBlocks as the second argument - const bribesResult = await getBribes(timestamp); - - // Combine or process the results as needed - const combinedResult = { - ...v2Result, - ...bribesResult, - }; - - return combinedResult; - }, - start: startTimeV2[CHAIN.ARBITRUM], - meta: { - methodology: { - ...methodology, - UserFees: "User pays 0.05%, 0.30%, or 1% on each swap.", - }, - }, +const adapter: Adapter = { + adapter: { + [CHAIN.ARBITRUM]: { + fetch: async (timestamp: number, chainBlocks: any, options: FetchOptions) => { + const v2Result = await v2Graphs(ARBITRUM)(timestamp, chainBlocks); // Pass chainBlocks as the second argument + const bribesResult = await getBribes(options); + v2Result.dailyBribesRevenue = bribesResult.dailyBribesRevenue; + + return v2Result; + }, + start: startTimeV2[CHAIN.ARBITRUM], + meta: { + methodology: { + ...methodology, + UserFees: "User pays 0.05%, 0.30%, or 1% on each swap.", }, }, - }; + }, + }, +}; export default adapter; diff --git a/fees/synthetix.ts b/fees/synthetix.ts index 2c2dd9e0e5..8939ccf08f 100644 --- a/fees/synthetix.ts +++ b/fees/synthetix.ts @@ -1,11 +1,8 @@ import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; import { Chain } from '@defillama/sdk/build/general'; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; -import { getTimestampAtStartOfDayUTC, getTimestampAtStartOfNextDayUTC } from "../utils/date"; -import { getPrices } from "../utils/prices"; +import { addTokensReceived } from '../helpers/token'; const methodology = { @@ -15,10 +12,6 @@ const methodology = { Fees: "Fees generated on each synthetic asset exchange, between 0.1% and 1% (usually 0.3%)", } -const topic0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'; -const topic1 = '0x0000000000000000000000000000000000000000000000000000000000000000'; -const topic2 = '0x000000000000000000000000feefeefeefeefeefeefeefeefeefeefeefeefeef'; - type IContract = { [l: string | Chain]: string; } @@ -26,47 +19,18 @@ const contract_address: IContract = { [CHAIN.ETHEREUM]: ADDRESSES.ethereum.sUSD, [CHAIN.OPTIMISM]: ADDRESSES.optimism.sUSD } -interface ITx { - data: string; - transactionHash: string; -} -interface IFee { - amount: number; -} - const graphs = () => { return (chain: Chain) => { - return async (timestamp: number) => { - const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp) - const yesterdaysTimestamp = getTimestampAtStartOfNextDayUTC(timestamp) - - const fromBlock = (await getBlock(todaysTimestamp, chain, {})); - const toBlock = (await getBlock(yesterdaysTimestamp, chain, {})); - const logs: ITx[] = (await sdk.getEventLogs({ - target: contract_address[chain], - fromBlock: fromBlock, - toBlock: toBlock, - topics: [topic0, topic1, topic2], - chain: chain - })).map((e: any) => { return { data: e.data, transactionHash: e.transactionHash } as ITx}); - - const sUSD = `${chain}:${contract_address[chain].toLowerCase()}`; - const sUSDPrice = (await getPrices([sUSD], timestamp))[sUSD].price; - const fees = logs.map((e: ITx) => { - const amount = Number(e.data) / 10 ** 18; - return { - amount: amount - } as IFee; - }); - - const dailyFee = fees.reduce((a: number, b: IFee) => a+b.amount, 0) * sUSDPrice; // sUSD - + return async (timestamp: number , _: ChainBlocks, options : FetchOptions) => { + const token = contract_address[chain] + const dailyFee = await addTokensReceived({ tokens: [token], options, target: '0xfeefeefeefeefeefeefeefeefeefeefeefeefeef'}) + return { timestamp, - dailyUserFees: dailyFee.toString(), - dailyFees: dailyFee.toString(), - dailyRevenue: dailyFee.toString(), - dailyHoldersRevenue: dailyFee.toString() + dailyUserFees: dailyFee, + dailyFees: dailyFee, + dailyRevenue: dailyFee, + dailyHoldersRevenue: dailyFee }; }; }; diff --git a/fees/waves/index.ts b/fees/waves/index.ts index 848a5ec720..f1536217c2 100644 --- a/fees/waves/index.ts +++ b/fees/waves/index.ts @@ -1,27 +1,20 @@ -import { Adapter, ProtocolType } from "../../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, ProtocolType } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { getBlock } from "../../helpers/getBlock"; -import { secondsInDay } from "../../utils/date"; import fetchURL from "../../utils/fetchURL"; -import { getPrices } from "../../utils/prices"; -const wavesDivider = 1e8; const limitPerRequest = 99; -const millisecondsInSecond = 1000; const blockHeadersEndpoint = "https://nodes.wavesnodes.com/blocks/headers/seq"; interface IBlockHeader { totalFee: number, } -const fetch = async (timestamp: number) => { - const fromTimestamp = (timestamp - secondsInDay) * millisecondsInSecond; - const toTimestamp = timestamp * millisecondsInSecond; +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getFromBlock, getToBlock }: FetchOptions) => { + const dailyFees = createBalances() - let startBlock = (await getBlock(fromTimestamp, CHAIN.WAVES, {})); - const endBlock = (await getBlock(toTimestamp, CHAIN.WAVES, {})); - const wavesToken = "waves:WAVES"; - const price = (await getPrices([wavesToken], timestamp))[wavesToken]?.price; + let startBlock = await getFromBlock(); + let endBlock = await getToBlock(); + const wavesToken = "WAVES"; let blockHeaders: IBlockHeader[] = []; while (startBlock < endBlock) { @@ -34,12 +27,9 @@ const fetch = async (timestamp: number) => { } } - const dailyFees = blockHeaders.reduce((acc, header) => acc + header.totalFee, 0) / wavesDivider * price; + dailyFees.add(wavesToken, blockHeaders.reduce((acc, header) => acc + header.totalFee, 0)) - return { - timestamp, - dailyFees: dailyFees.toString(), - }; + return { timestamp, dailyFees, }; }; const adapter: Adapter = { diff --git a/fees/woofi.ts b/fees/woofi.ts index 62f8ed8270..f8c44b2008 100644 --- a/fees/woofi.ts +++ b/fees/woofi.ts @@ -1,16 +1,10 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, FetchResultFees } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, FetchResultFees } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; import { Chain } from "@defillama/sdk/build/general"; -import { getTimestampAtStartOfNextDayUTC } from "../utils/date"; - +import { addTokensReceived } from '../helpers/token'; type TFee = { - target: string; - targetDecimal: number - topics: string[]; + from: string; } type TFeeDetail = { @@ -18,86 +12,40 @@ type TFeeDetail = { } const fee_detail: TFeeDetail = { [CHAIN.AVAX]: { - target: ADDRESSES.avax.USDC, - targetDecimal: 6, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x0000000000000000000000006cb1bc6c8aabdae822a2bf8d83b36291cb70f169', - ] + from: '0x6cb1bc6c8aabdae822a2bf8d83b36291cb70f169', }, [CHAIN.BSC]: { - target: ADDRESSES.bsc.USDT, - targetDecimal: 18, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x000000000000000000000000da5e1d3aaa93e8716f87b5ee39e5f514cc934d5e', - ] + from: '0xda5e1d3aaa93e8716f87b5ee39e5f514cc934d5e', }, [CHAIN.FANTOM]: { - target: ADDRESSES.fantom.USDC, - targetDecimal: 6, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x0000000000000000000000000b5025d8d409a51615cb624b8ede132bb11a2550', - ] + from: '0x0b5025d8d409a51615cb624b8ede132bb11a2550', }, [CHAIN.POLYGON]: { - target: ADDRESSES.polygon.USDC, - targetDecimal: 6, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x000000000000000000000000938021351425dbfa606ed2b81fc66952283e0dd5', - ] + from: '0x938021351425dbfa606ed2b81fc66952283e0dd5', }, [CHAIN.ARBITRUM]: { - target: ADDRESSES.arbitrum.USDC, - targetDecimal: 6, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x0000000000000000000000000ba6c34af9713d15141dcc91d2788c3f370ecb9e', - ] + from: '0x0ba6c34af9713d15141dcc91d2788c3f370ecb9e', }, [CHAIN.OPTIMISM]: { - target: ADDRESSES.optimism.USDC, - targetDecimal: 6, - topics: [ - '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', - '0x000000000000000000000000a058798cd293f5acb4e7757b08c960a79f527699', - ] + from: '0xa058798cd293f5acb4e7757b08c960a79f527699', } } -interface ITx { - data: string; - transactionHash: string; -} const fetch = (chain: Chain) => { - return async (timestamp: number): Promise => { - const nextDayTimestamp = getTimestampAtStartOfNextDayUTC(timestamp) - if (new Date((nextDayTimestamp + 8400) * 1000).getTime() > new Date().getTime()) { - return { - timestamp - } - } - const fromBlock = (await getBlock(nextDayTimestamp, chain, {})); - const toBlock = (await getBlock(nextDayTimestamp + 8400, chain, {})); - - const logs: ITx[] = (await sdk.getEventLogs({ - target: fee_detail[chain].target, - chain: chain, - topics: fee_detail[chain].topics, - toBlock: toBlock, - fromBlock: fromBlock, - }))as ITx[]; - - const [first, second, third] = logs; - const dailyFees = (Number(first?.data || 0) + Number(second?.data || 0) + Number(third?.data || 0)) / 10 ** fee_detail[chain].targetDecimal; - const dailyRevenue = (Number(first?.data || 0) + Number(third?.data || 0)) / 10 ** fee_detail[chain].targetDecimal; - const dailyHolderRevenue = Number(first?.data || 0) / 10 ** fee_detail[chain].targetDecimal; + return async (timestamp: number, _: ChainBlocks, options: FetchOptions): Promise => { + const { api } = options; + const { from, } = fee_detail[chain]; + const token = await api.call({ abi: 'address:quoteToken', target: from }) + const rebateManager = await api.call({ abi: 'address:rebateManager', target: from }) + const treasury = await api.call({ abi: 'address:treasury', target: from }) + const vaultManager = await api.call({ abi: 'address:vaultManager', target: from }) + const dailyFees = await addTokensReceived({ fromAddressFilter: from, options, tokens: [token], targets: [vaultManager, rebateManager, treasury] }) + const dailyRevenue = await addTokensReceived({ fromAddressFilter: from, options, tokens: [token], targets: [vaultManager, treasury] }) + const dailyHoldersRevenue = await addTokensReceived({ fromAddressFilter: from, options, tokens: [token], targets: [vaultManager] }) return { - dailyFees: dailyFees.toString(), - dailyRevenue: dailyRevenue.toString(), - dailyHoldersRevenue: dailyHolderRevenue.toString(), + dailyFees, + dailyRevenue, + dailyHoldersRevenue, timestamp } } @@ -106,8 +54,8 @@ const fetch = (chain: Chain) => { const adapter: Adapter = { adapter: { [CHAIN.AVAX]: { - fetch: fetch(CHAIN.AVAX), - start: 1673222400, + fetch: fetch(CHAIN.AVAX), + start: 1673222400, }, [CHAIN.BSC]: { fetch: fetch(CHAIN.BSC), diff --git a/fees/xdai.ts b/fees/xdai.ts index 259a0e8aab..cd3a427a6b 100644 --- a/fees/xdai.ts +++ b/fees/xdai.ts @@ -1,4 +1,4 @@ import { CHAIN } from "../helpers/chains"; import { blockscoutFeeAdapter } from "../helpers/blockscoutFees"; -export default blockscoutFeeAdapter(CHAIN.XDAI, "https://blockscout.com/xdai/mainnet/api?module=stats&action=totalfees", "coingecko:dai") \ No newline at end of file +export default blockscoutFeeAdapter(CHAIN.XDAI, "https://blockscout.com/xdai/mainnet/api?module=stats&action=totalfees") \ No newline at end of file diff --git a/fees/y2k/y2k-finance-v2.ts b/fees/y2k/y2k-finance-v2.ts index 27de2f3a5c..925bd8064c 100644 --- a/fees/y2k/y2k-finance-v2.ts +++ b/fees/y2k/y2k-finance-v2.ts @@ -1,6 +1,6 @@ import ADDRESSES from '../../helpers/coreAssets.json' import { FetchResultFees } from "../../adapters/types"; -import { ethers } from "ethers"; +import { addTokensReceived } from '../../helpers/token'; const factory = "0xC3179AC01b7D68aeD4f27a19510ffe2bfb78Ab3e"; const event_market_create = @@ -11,11 +11,9 @@ const tokens = [ ADDRESSES.arbitrum.WETH, // WETH ]; const treasury = "0x5c84cf4d91dc0acde638363ec804792bb2108258"; -const topic0_transfer = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"; -const event_transfer = "event Transfer (address indexed from, address indexed to, uint256 amount)"; - -const fetch = async (timestamp: number, _, { getLogs, api, createBalances, }): Promise => { +const fetch = async (timestamp: number, _, options): Promise => { + const { createBalances, getLogs, } = options const market_create = await getLogs({ target: factory, @@ -29,16 +27,9 @@ const fetch = async (timestamp: number, _, { getLogs, api, createBalances, }): P const vaults = [...new Set([...premium, ...collateral])]; const dailyFees = createBalances() - for (const token of tokens) { - for (const vault of vaults) { - const transfer_treasury = await getLogs({ - target: token, - eventAbi: event_transfer, - topics: [topic0_transfer, ethers.zeroPadValue(vault, 32), ethers.zeroPadValue(treasury, 32)], - }) - transfer_treasury.forEach((i: any) => api.add(token, i.amount)) - } - } + for (const vault of vaults) + await addTokensReceived({ options, tokens, fromAddressFilter: vault, target: treasury, balances: dailyFees }) + return { dailyFees, dailyRevenue: dailyFees, timestamp, }; }; diff --git a/fees/y2k/y2k-finance.ts b/fees/y2k/y2k-finance.ts index 12f7165c42..64a13957fd 100644 --- a/fees/y2k/y2k-finance.ts +++ b/fees/y2k/y2k-finance.ts @@ -1,6 +1,6 @@ import ADDRESSES from '../../helpers/coreAssets.json' import { FetchResultFees } from "../../adapters/types"; -import { ethers } from "ethers"; +import { addTokensReceived } from '../../helpers/token'; const vault_factory = "0x984e0eb8fb687afa53fc8b33e12e04967560e092"; @@ -14,28 +14,18 @@ const tokens = [ ADDRESSES.arbitrum.WETH, // WETH ]; const treasury = "0x5c84cf4d91dc0acde638363ec804792bb2108258"; -const topic0_transfer = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"; -const event_transfer = "event Transfer (address indexed from, address indexed to, uint256 amount)"; -const fetch = async (timestamp: number, _, { api, createBalances, getLogs, }): Promise => { +const fetch = async (timestamp: number, _, options): Promise => { + const { api, createBalances, getLogs, } = options const vaultRes = await api.fetchList({ lengthAbi: abis.marketIndex, itemAbi: abis.getVaults, target: vault_factory }) - const vaults = vaultRes - .flat() - .map((e: string) => e.toLowerCase()); + const vaults = vaultRes.flat() const dailyFees = createBalances() - for (const token of tokens) { - for (const vault of vaults) { - const transfer_treasury = await getLogs({ - target: token, - eventAbi: event_transfer, - topics: [topic0_transfer, ethers.zeroPadValue(vault, 32), ethers.zeroPadValue(treasury, 32)], - }) - transfer_treasury.forEach((i: any) => api.add(token, i.amount)) - } - } + for (const vault of vaults) + await addTokensReceived({ options, tokens, fromAddressFilter: vault, target: treasury, balances: dailyFees }) + return { dailyFees, dailyRevenue: dailyFees, timestamp, }; }; diff --git a/helpers/blockscoutFees.ts b/helpers/blockscoutFees.ts index 5622a7f5d7..e5f57da018 100644 --- a/helpers/blockscoutFees.ts +++ b/helpers/blockscoutFees.ts @@ -1,29 +1,26 @@ -import { getTimestampAtStartOfDayUTC } from "../utils/date"; -import { getPrices } from "../utils/prices"; -import { Adapter, ProtocolType } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, ProtocolType } from "../adapters/types"; import { httpGet } from '../utils/fetchURL'; -export function blockscoutFeeAdapter(chain:string, url:string, coin:string){ - const adapter: Adapter = { - adapter: { - [chain]: { - fetch: async (timestamp: number) => { - const ts = getTimestampAtStartOfDayUTC(timestamp) - const date = new Date(ts*1000).toISOString().slice(0, "2011-10-05".length) - const fees = await httpGet(`${url}&date=${date}`) - const prices = await getPrices([coin], timestamp); - const usdFees = Number(fees.result)/1e18*prices[coin].price +export function blockscoutFeeAdapter(chain: string, url: string, CGToken?: string) { + const adapter: Adapter = { + adapter: { + [chain]: { + fetch: async (_timestamp: number, _: ChainBlocks, { createBalances, startOfDay, }: FetchOptions) => { + const dailyFees = createBalances() + const date = new Date(startOfDay * 1000).toISOString().slice(0, "2011-10-05".length) + const fees = await httpGet(`${url}&date=${date}`) + if (CGToken) dailyFees.addCGToken(CGToken, fees.result/1e18) + else dailyFees.addGasToken(fees.result) - return { - timestamp, - dailyFees: usdFees.toString(), - }; - }, - start: 1575158400 - }, + return { + timestamp: startOfDay, dailyFees, + }; + }, + start: 1575158400 }, - protocolType: ProtocolType.CHAIN - } - - return adapter + }, + protocolType: ProtocolType.CHAIN + } + + return adapter } diff --git a/helpers/dexVolumeLogs.ts b/helpers/dexVolumeLogs.ts index ec522a933d..d12c2f2d2b 100644 --- a/helpers/dexVolumeLogs.ts +++ b/helpers/dexVolumeLogs.ts @@ -56,8 +56,8 @@ export async function getDexFees({ factory, timestamp, pools, lengthAbi = 'allPa const { api } = fetchOptions if (!pools) pools = await api.fetchList({ lengthAbi, itemAbi, target: factory! }) - const token0s = await api.multiCall({ abi: 'address:token0', calls: pools! }) - const token1s = await api.multiCall({ abi: 'address:token1', calls: pools! }) + const token0s = await api.multiCall({ abi: 'address:token0', calls: pools!, permitFailure: true, }) + const token1s = await api.multiCall({ abi: 'address:token1', calls: pools!, permitFailure: true, }) const logs = await fetchOptions.getLogs({ targets: pools, @@ -67,7 +67,7 @@ export async function getDexFees({ factory, timestamp, pools, lengthAbi = 'allPa logs.forEach((log: any[], index: number) => { const token0 = token0s[index] const token1 = token1s[index] - if (!log.length) return + if (!log.length || !token0 || !token1) return log.forEach((i: any) => { api.add(token0, i.amount0) api.add(token1, i.amount1) diff --git a/helpers/etherscanFees.ts b/helpers/etherscanFees.ts index 3b870d92e8..6c6678601d 100644 --- a/helpers/etherscanFees.ts +++ b/helpers/etherscanFees.ts @@ -1,39 +1,41 @@ -import { getTimestampAtStartOfDayUTC } from "../utils/date"; -import { getPrices } from "../utils/prices"; -import { Adapter, ProtocolType } from "../adapters/types"; +import { Adapter, ChainBlocks, FetchOptions, ProtocolType } from "../adapters/types"; import { httpPost } from '../utils/fetchURL'; -export async function getEtherscanFees(timestamp: number, url:string, coin:string) { - const ts = getTimestampAtStartOfDayUTC(timestamp) - const dailyFees = await httpPost(url, { responseType: 'blob', headers: { +export async function getEtherscanFees({ startOfDay, }: FetchOptions, url: string) { + const dailyFees = await httpPost(url, { + responseType: 'blob', headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36", "Content-Type": "text/csv; charset=utf-8", - "Accept" : "text/csv; charset=utf-8", + "Accept": "text/csv; charset=utf-8", "origin": url, - }}); - const feesToday = dailyFees.split("\n").find((d: any) => d?.split(",")?.[1]?.slice(1, -1) == ts) - const pricesObj = await getPrices([coin], ts); - return Number(feesToday?.split(",")[2].slice(1, -2)) / 1e18 * pricesObj[coin].price + } + }); + const feesToday = dailyFees.split("\n").find((d: any) => d?.split(",")?.[1]?.slice(1, -1) == startOfDay) + return Number(feesToday?.split(",")[2].slice(1, -2)) } -export function etherscanFeeAdapter(chain:string, url:string, coin:string){ +export function etherscanFeeAdapter(chain: string, url: string, cgToken?: string) { const adapter: Adapter = { adapter: { - [chain]: { - fetch: async (timestamp: number) => { - const usdFees = await getEtherscanFees(timestamp, url, coin) + [chain]: { + fetch: async (_timestamp: number, _: ChainBlocks, options: FetchOptions) => { + const amount = await getEtherscanFees(options, url) + const dailyFees = options.createBalances() + if (cgToken) + dailyFees.addCGToken(cgToken, amount/1e18) + else + dailyFees.addGasToken(amount) - return { - timestamp, - dailyFees: usdFees.toString(), - }; - }, - start: 1690761600 - }, - }, + return { + timestamp: options.startOfDay, dailyFees, + }; + }, + start: 1690761600 + }, + }, protocolType: ProtocolType.CHAIN - } + } return adapter } diff --git a/helpers/getBlock.ts b/helpers/getBlock.ts index 65af3ba2dd..b05c09375f 100644 --- a/helpers/getBlock.ts +++ b/helpers/getBlock.ts @@ -63,6 +63,8 @@ async function getBlock(timestamp: number, chain: Chain, chainBlocks = {} as Cha let block: number | undefined try { + if (chain === CHAIN.WAVES) + timestamp = Math.floor(timestamp * 1000) block = await sdk.blocks.getBlockNumber(chain, timestamp) } catch (e) { console.log('error fetching block', e) } diff --git a/helpers/token.ts b/helpers/token.ts index 012f1214fb..d4018e914d 100644 --- a/helpers/token.ts +++ b/helpers/token.ts @@ -43,8 +43,10 @@ export async function addTokensReceived(params: { options: FetchOptions; balances?: sdk.Balances; tokens?: string[]; + toAddressFilter?: string | null; + tokenTransform?: (token: string) => string; }) { - let { target, targets, options, balances, tokens, fromAddressFilter = null } = params; + let { target, targets, options, balances, tokens, fromAddressFilter = null, tokenTransform = (i: string) => i } = params; const { chain, createBalances, getLogs, } = options if (!balances) balances = createBalances() @@ -55,12 +57,12 @@ export async function addTokensReceived(params: { clonedOptions.balances = balances await Promise.all(targets.map(target => addTokensReceived({ ...clonedOptions, target }))) return balances - } else if (!target) { - throw new Error('target or targets required') + } else if (!target && !fromAddressFilter) { + throw new Error('target/fromAddressFilter or targets required') } - if (!tokens) { + if (!tokens && target) { if (!ankrChainMapping[chain]) throw new Error('Chain Not supported: ' + chain) const ankrTokens = await ankrGetTokens(target, { onlyWhitelisted: true }) tokens = ankrTokens[ankrChainMapping[chain]] ?? [] @@ -68,18 +70,18 @@ export async function addTokensReceived(params: { if (!tokens!.length) return balances - const toAddressFilter = ethers.zeroPadValue(target, 32) + const toAddressFilter = target ? ethers.zeroPadValue(target, 32) : null if (fromAddressFilter) fromAddressFilter = ethers.zeroPadValue(fromAddressFilter, 32) const logs = await getLogs({ targets: tokens, flatten: false, eventAbi: 'event Transfer (address indexed from, address indexed to, uint256 value)', - topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', fromAddressFilter as string, toAddressFilter], + topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', fromAddressFilter as string, toAddressFilter as any], }) logs.forEach((logs, index) => { const token = tokens![index] - logs.forEach((i: any) => balances!.add(token, i.value)) + logs.forEach((i: any) => balances!.add(tokenTransform(token), i.value)) }) return balances } @@ -212,4 +214,4 @@ export async function getTokenDiff(params: { return balances -} \ No newline at end of file +}