diff --git a/dexs/synfutures-v2/index.ts b/dexs/synfutures-v2/index.ts index 27664785b1..b9d494cebe 100644 --- a/dexs/synfutures-v2/index.ts +++ b/dexs/synfutures-v2/index.ts @@ -1,48 +1,30 @@ import ADDRESSES from '../../helpers/coreAssets.json' import { Chain } from "@defillama/sdk/build/general"; -import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; -import { getBlock } from "../../helpers/getBlock"; import { CHAIN } from "../../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { SimpleAdapter } from "../../adapters/types"; -import { get } from "http"; -import { getPrices } from "../../utils/prices"; +import { ChainBlocks, FetchOptions, SimpleAdapter } from "../../adapters/types"; type TContracts = { [key in Chain]: string[][]; }; const contracts: TContracts = { [CHAIN.POLYGON]: [ - ['0x89cf9a71d45c58673a7f8b4c829df466da7a473a',ADDRESSES.polygon.WETH_1], - ['0xa40986ccd9dc00c533ba71c9362529c9694f9f7b',ADDRESSES.polygon.USDC], - ['0xf479405a2518b02a79c060f3d91fe0c98cfe6e25',ADDRESSES.polygon.USDC], - ['0x64b8b618cddc31c61305338c6ab7a2d85a7ab5ca',ADDRESSES.polygon.USDC], - ['0x19759660e5ee693df6105fc3629d5d91cb0a9447',ADDRESSES.polygon.WMATIC_2], - ['0xbda26f37656218b2b3983800bc5734998445cc92',ADDRESSES.polygon.USDC], - ['0x4b6e851015581e88824f1efd71cb65dd9826189b',ADDRESSES.polygon.USDC], - ['0x4baa9f1af8653a875d51324ca563d12d7925adf5',ADDRESSES.polygon.USDC], - ['0x8c7822f283137e1e73ea0bdc537ccfb2fa1fb030',ADDRESSES.polygon.USDC], - ['0x13f7f23db676431a2371d0f1884ae1526a0fe5c2',ADDRESSES.polygon.WMATIC_2], - ['0xb09d2ec9afff13589ddfab190e2d1aba7e02095b',ADDRESSES.polygon.WMATIC_2], - ['0x6909bfbe79f9c33589c637eed166a32afea76ef6',ADDRESSES.polygon.USDC], - ['0xf51dcd52bd075c91632aac42c3926b2a523b3da5',ADDRESSES.polygon.WMATIC_2], - ['0x9ceeb2a6f17d3c7bd086471095fcf80cfe780149',ADDRESSES.polygon.WETH_1], + ['0x89cf9a71d45c58673a7f8b4c829df466da7a473a', ADDRESSES.polygon.WETH_1], + ['0xa40986ccd9dc00c533ba71c9362529c9694f9f7b', ADDRESSES.polygon.USDC], + ['0xf479405a2518b02a79c060f3d91fe0c98cfe6e25', ADDRESSES.polygon.USDC], + ['0x64b8b618cddc31c61305338c6ab7a2d85a7ab5ca', ADDRESSES.polygon.USDC], + ['0x19759660e5ee693df6105fc3629d5d91cb0a9447', ADDRESSES.polygon.WMATIC_2], + ['0xbda26f37656218b2b3983800bc5734998445cc92', ADDRESSES.polygon.USDC], + ['0x4b6e851015581e88824f1efd71cb65dd9826189b', ADDRESSES.polygon.USDC], + ['0x4baa9f1af8653a875d51324ca563d12d7925adf5', ADDRESSES.polygon.USDC], + ['0x8c7822f283137e1e73ea0bdc537ccfb2fa1fb030', ADDRESSES.polygon.USDC], + ['0x13f7f23db676431a2371d0f1884ae1526a0fe5c2', ADDRESSES.polygon.WMATIC_2], + ['0xb09d2ec9afff13589ddfab190e2d1aba7e02095b', ADDRESSES.polygon.WMATIC_2], + ['0x6909bfbe79f9c33589c637eed166a32afea76ef6', ADDRESSES.polygon.USDC], + ['0xf51dcd52bd075c91632aac42c3926b2a523b3da5', ADDRESSES.polygon.WMATIC_2], + ['0x9ceeb2a6f17d3c7bd086471095fcf80cfe780149', ADDRESSES.polygon.WETH_1], ] } - -interface ILogs { - address: string; - blockHash: string; - blockNumber: string; - data: string; - logIndex: string; - removed: boolean; - topics: string[]; - transactionHash: string; - transactionIndex: string; -} - function twosComplementHexToDecimal(hexValue: string): number { // Check if the hex value starts with '0xf' if (!hexValue.startsWith('0xf')) { @@ -66,69 +48,32 @@ function twosComplementHexToDecimal(hexValue: string): number { } const topic0_trade = '0xeef2964c19d154a021c80f1901318bed137c1214368f991d6a118e9c64c5d9f6'; -interface IVolume { - contract: string; - amount_quote: number; - quoteAddr: string; -} const fetchVolume = (chain: Chain) => { - return async (timestamp: number) => { - const now = new Date().getTime() / 1000; - const fromTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000)); - const toTimestamp = timestamp > now ? now : timestamp; + return async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, api }: FetchOptions) => { + const dailyVolume = createBalances() + const tokens = contracts[chain].map(i => i[1]); + const decimals = await api.multiCall({ abi: 'erc20:decimals', calls: tokens}) - const toBlock = await getBlock(toTimestamp, chain, {}); - const fromBlock = await getBlock(fromTimestamp, chain, {}); - - const logs: ILogs[] = (await Promise.all(contracts[chain].map(contract => sdk.getEventLogs({ - target: contract[0], + const logs = await getLogs({ + targets: contracts[chain].map(i => i[0]), topics: [topic0_trade], - toBlock, - fromBlock, - chain: chain, - })))).flat(); - - const volumeRaw: IVolume[] = logs.map((log:ILogs) => { - const data = log.data.replace('0x', ''); - const price = Number('0x'+data.slice(0, 64)) / 10 ** 18; - const amount = twosComplementHexToDecimal('0x'+data.slice(64, 128)) / 10 ** 18; - const quoteAddr = contracts[chain].find((x: string[]) => x[0].toLowerCase() === log.address.toLowerCase())?.[1]; - return { - contract: log.address, - amount_quote: amount * price, - quoteAddr: quoteAddr || '' - } - }); - - const coins = [...new Set(volumeRaw.map(e => `${chain}:${e.quoteAddr.toLowerCase()}`))]; - const prices = await getPrices(coins, fromTimestamp); - - - // getPrices API currently has a bug that it doesn't return price for polygon:0x2791bca1f2de4661ed88a30c99a7a9449aa84174 (USDC) for some reason - // so we set it as 1 manually to temporarily fix this issue - if (!prices['polygon:' + ADDRESSES.polygon.USDC]) { - prices['polygon:' + ADDRESSES.polygon.USDC] = { - symbol: 'USDC', - timestamp: now, - price: 1, - decimals: 18 - } - } - - // check prices - coins.forEach(coin => { - if (!prices[coin]) { - throw new Error(`No price found for ${coin}`); - } - }); - - const volume = volumeRaw.map((e: IVolume) => e.amount_quote * prices[`${chain}:${e.quoteAddr.toLowerCase()}`]?.price || 0); - const dailyVolume = volume.reduce((acc: number, cur: number) => acc + cur, 0); - return { - dailyVolume: `${dailyVolume}`, - timestamp - } + flatten: false, + skipCacheRead: true, + }) + logs.forEach((_logs, index) => { + const token = tokens[index] + const powDivider = 18 - +decimals[index] + + _logs.forEach((log) => { + const data = log.data.replace('0x', ''); + const price = Number('0x' + data.slice(0, 64)) / 10 ** 18; + const amount = twosComplementHexToDecimal('0x' + data.slice(64, 128)) / (10 ** powDivider) + dailyVolume.add(token, amount * price); + }) + }) + + return { dailyVolume, timestamp } } } diff --git a/dexs/zkSwap_Finance/zkswapFinance.ts b/dexs/zkSwap_Finance/zkswapFinance.ts index ce3ba07b0f..7d5f77b7d4 100644 --- a/dexs/zkSwap_Finance/zkswapFinance.ts +++ b/dexs/zkSwap_Finance/zkswapFinance.ts @@ -1,7 +1,7 @@ import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume"; 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.ERA]: { @@ -11,44 +11,13 @@ const info: { [key: string]: any } = { }, }; -const getData = async (chain: string, timestamp: number) => { - const startDayTimestamp = getUniqStartOfTodayTimestamp( - new Date(timestamp * 1000) - ); +const getData = async ({ chain, startOfDay, createBalances}: FetchOptions) => { + const dailyVolume = createBalances() let dayMiliseconds = 24 * 60 * 60; let returnCount = 1000; - let daySum = 0; - let totalSum = 0; - let step = 0; - - const graphQLTotalVolume = `{ - pairs(first: 1000) { - volumeToken0 - token0 { - id - } - volumeUSD - } - }`; - - const data = await request(info[chain].subgraph_v1, graphQLTotalVolume); - let token0rray = [] as string[]; - for (const pair of data.pairs) { - token0rray.push(chain + ":" + pair.token0.id); - } - let unique = [...new Set(token0rray)] as string[]; - const prices = await getPrices(unique, startDayTimestamp); - - for (const pair of data.pairs) { - const token0Id = chain + ":" + pair.token0.id; - let price0 = prices[token0Id] === undefined ? 0 : prices[token0Id].price; - - totalSum += Number(pair.volumeToken0) * price0; - } - - let lasTimestampQuery = startDayTimestamp; + let lasTimestampQuery = startOfDay; while (returnCount == 1000) { const graphQL = `{ @@ -57,7 +26,7 @@ const getData = async (chain: string, timestamp: number) => { orderDirection: asc first: 1000 where: {timestamp_gte: ${lasTimestampQuery}, timestamp_lt: ${ - startDayTimestamp + dayMiliseconds + startOfDay + dayMiliseconds } } ) { amount0In @@ -66,6 +35,7 @@ const getData = async (chain: string, timestamp: number) => { pair { token0 { id + decimals } } } @@ -77,18 +47,16 @@ const getData = async (chain: string, timestamp: number) => { lasTimestampQuery = data?.swaps[returnCount - 1]?.timestamp; for (const swap of data.swaps) { - const token0Id = chain + ":" + swap.pair.token0.id; - let price0 = prices[token0Id] === undefined ? 0 : prices[token0Id].price; - - daySum += Number(swap.amount0In) * price0; - daySum += Number(swap.amount0Out) * price0; + const token0Id = swap.pair.token0.id; + const multiplier = Math.pow(10, swap.pair.token0.decimals); + dailyVolume.add(token0Id, Number(swap.amount0In) * multiplier); + dailyVolume.add(token0Id, Number(swap.amount0Out) * multiplier); } } return { - totalVolume: `${totalSum}`, - dailyVolume: `${daySum}`, - timestamp: startDayTimestamp, + dailyVolume, + timestamp: startOfDay, }; }; @@ -134,15 +102,9 @@ const getToDateVolume = async (chain: string, timestamp: number) => { return volumSum; }; -export const fetchVolume = (chain: string) => { - return async (timestamp: number) => { - const data = await getData(chain, timestamp); - const totalVolume = await getToDateVolume(chain, timestamp); - - return { - totalVolume: totalVolume.toString(), - dailyVolume: data.dailyVolume, - timestamp: data.timestamp, - }; +export const fetchVolume = (_chain: string) => { + return async (_timestamp: number, _: ChainBlocks, options: FetchOptions) => { + return getData(options); + // const totalVolume = await getToDateVolume(options); }; }; diff --git a/fees/friend-room.ts b/fees/friend-room.ts index 76ce37145b..beaea95c3e 100644 --- a/fees/friend-room.ts +++ b/fees/friend-room.ts @@ -1,72 +1,14 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, FetchResultFees, SimpleAdapter } from "../adapters/types"; +import { Adapter, } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; -import { getPrices } from "../utils/prices"; -import { Chain } from "@defillama/sdk/build/general"; -import { ethers } from "ethers"; +import { getFeesExport } from '../helpers/friend-tech'; const friendRoomSharesAddress = '0x9BD0474CC4F118efe56f9f781AA8f0F03D4e7A9c'; -const topic0_trade = '0x68e42cc58cdc03b82d6c135e12e98660f43a75ce1ccfb1a625965e253f50d7b0'; const event_trade = 'event Trade(uint256 index, uint256 serverId, address trader, uint256 tokenId, bool isBuy, uint256 shareAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 supply)' -const contract_interface = new ethers.Interface([ - event_trade -]); - -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} - -interface IFee { - fees: number; - rev: 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: ILog[] = (await sdk.getEventLogs({ - target: friendRoomSharesAddress, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ETHEREUM, - topics: [topic0_trade] - })) as ILog[]; - - const fees_details: IFee[] = logs.map((e: ILog) => { - const value = contract_interface.parseLog(e); - const protocolEthAmount = Number(value!.args.protocolEthAmount) / 10 ** 18; - const subjectEthAmount = Number(value!.args.subjectEthAmount) / 10 ** 18; - return { - fees: protocolEthAmount + subjectEthAmount, - rev: protocolEthAmount - } as IFee - }) - const dailyFees = fees_details.reduce((a: number, b: IFee) => a + b.fees, 0) - const dailyRev = fees_details.reduce((a: number, b: IFee) => a + b.rev, 0) - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const dailyFeesUSD = (dailyFees) * ethPrice; - const dailyRevUSD = (dailyRev) * ethPrice; - return { - dailyFees: `${dailyFeesUSD}`, - dailyRevenue: `${dailyRevUSD}`, - timestamp - } - -} - const adapter: Adapter = { adapter: { [CHAIN.ETHEREUM]: { - fetch: fetch, + fetch: getFeesExport(friendRoomSharesAddress, [event_trade]), start: 1693731179, }, } diff --git a/fees/friend3.ts b/fees/friend3.ts index f68c1bd6f0..63d374e28b 100644 --- a/fees/friend3.ts +++ b/fees/friend3.ts @@ -1,49 +1,21 @@ -import { Adapter, Fetch, FetchOptions, FetchResultFees } from "../adapters/types"; +import { Adapter, } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; +import { getFeesExport } from "../helpers/friend-tech"; const FriendV1Address = '0x1e70972ec6c8a3fae3ac34c9f3818ec46eb3bd5d'; const event_trade = 'event Trade(address trader, address subject, bool isBuy, uint256 ticketAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 supply)' const FriendV2Address = '0x2C5bF6f0953ffcDE678A35AB7d6CaEBC8B6b29F0'; -const event_trade_V2 = 'event Trade (address trader , bytes32 subjectId , bool isBuy , uint256 ticketAmount , uint256 tokenAmount , uint256 protocolAmount , uint256 subjectAmount , uint256 holderAmount , uint256 referralAmount , uint256 supply)' - - -const fetch = (async (timestamp: number, _cb: any, { getLogs, createBalances }: FetchOptions): Promise => { - let logs = await getLogs({ target: FriendV1Address, eventAbi: event_trade, }) - const dailyFees = createBalances() - const dailyRevenue = createBalances() - logs.forEach(i => { - dailyFees.addGasToken(i.protocolEthAmount) - dailyFees.addGasToken(i.subjectEthAmount) - dailyRevenue.addGasToken(i.protocolEthAmount) - }) - return { dailyFees, dailyRevenue, timestamp } -}) as Fetch - -const fetchOpbnb = (async (timestamp: number, _cb: any, { getLogs, createBalances }: FetchOptions): Promise => { - - let logs = await getLogs({ target: FriendV2Address, eventAbi: event_trade_V2, }) - const dailyFees = createBalances() - const dailyRevenue = createBalances() - logs.forEach(i => { - dailyFees.addGasToken(i.protocolAmount) - dailyFees.addGasToken(i.subjectAmount) - dailyFees.addGasToken(i.holderAmount) - dailyRevenue.addGasToken(i?.protocolEthAmount || BigInt(0)) - dailyRevenue.addGasToken(BigInt(i.referralAmount * BigInt(-1))) - }) - return { dailyFees, dailyRevenue, timestamp } -}) as Fetch - +const event_trade_V2 = 'event Trade (address trader , bytes32 subjectId , bool isBuy , uint256 ticketAmount , uint256 tokenAmount , uint256 protocolEthAmount , uint256 protocolEthAmount , uint256 holderEthAmount , uint256 referrerEthAmount , uint256 supply)' const adapter: Adapter = { adapter: { [CHAIN.BSC]: { - fetch: fetch, + fetch: getFeesExport(FriendV1Address, [event_trade]), start: 1692835200, }, [CHAIN.OP_BNB]: { - fetch: fetchOpbnb, + fetch: getFeesExport(FriendV2Address, [event_trade_V2]), start: 1698710400, }, } diff --git a/fees/post-tech.ts b/fees/post-tech.ts index c3e892db77..79f347b85c 100644 --- a/fees/post-tech.ts +++ b/fees/post-tech.ts @@ -1,72 +1,14 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { FetchResultFees, SimpleAdapter } from "../adapters/types" +import { SimpleAdapter } 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 { getFeesExport } from '../helpers/friend-tech' const contract_address = '0x87da6930626fe0c7db8bc15587ec0e410937e5dc' -const topic0_trade = '0xd5bfddbe72aa2c9b73b3fe3ad6d90e4dc2bb1b80d51272e831927c33f587a441' const event_trade = 'event Trade(address trader,address subject,bool isBuy,uint256 shareAmount,uint256 ethAmount,uint256 protocolEthAmount,uint256 subjectEthAmount,uint256 holderEthAmount,uint256 referralEthAmount,uint256 supply)'; -const contract_interface = new ethers.Interface([ - event_trade -]) -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} -interface IFee { - fees: number; - rev: number; -} - -const fetchFees = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - try { - const fromBlock = await getBlock(fromTimestamp, CHAIN.ARBITRUM, {}); - const toBlock = await getBlock(toTimestamp, CHAIN.ARBITRUM, {}); - - const logs = (await sdk.getEventLogs({ - target: contract_address, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.ARBITRUM, - topics: [topic0_trade] - })) as ILog[]; - - const fees_details = logs.map((e: ILog) => { - const value = contract_interface.parseLog(e); - const protocolEthAmount = Number(value!.args.protocolEthAmount) / 10 ** 18; - const subjectEthAmount = Number(value!.args.subjectEthAmount) / 10 ** 18; - return { - fees: protocolEthAmount + subjectEthAmount, - rev: protocolEthAmount - } as IFee - }) - const dailyFees = fees_details.reduce((a: number, b: IFee) => a+b.fees, 0) - const dailyRev = fees_details.reduce((a: number, b: IFee) => a+b.rev, 0) - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const dailyFeesUSD = (dailyFees) * ethPrice; - const dailyRevUSD = (dailyRev) * ethPrice; - return { - dailyFees: `${dailyFeesUSD}`, - dailyRevenue: `${dailyRevUSD}`, - timestamp - } - } catch (error) { - throw error; - } - -} const adapter: SimpleAdapter = { adapter: { [CHAIN.ARBITRUM]: { - fetch: fetchFees, + fetch: getFeesExport(contract_address, [event_trade]), start: 1693267200 } } diff --git a/fees/squa-defi.ts b/fees/squa-defi.ts index 3853e2884e..5cc1e7c543 100644 --- a/fees/squa-defi.ts +++ b/fees/squa-defi.ts @@ -1,77 +1,17 @@ -import { Adapter, FetchResultFees, SimpleAdapter } from "../adapters/types"; +import { Adapter, } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; -import { ethers } from "ethers"; +import ADDRESSES from "../helpers/coreAssets.json"; +import { getFeesExport } from "../helpers/friend-tech"; -const usdcDecimals = 6 const keyManagerQureFiAddr = '0xfad362E479AA318F2De7b2c8a1993Df9BB2B3b1f'; -const topic0_trade = '0xfc742fe2e3355b8dcced6d8103bd681a9c1e0e72a5f292d77eb3dbe7874c3557'; -const event_trade = 'event Trade(address indexed trader,address indexed influencer,uint8 indexed direction,uint256 keysAmount,uint256 price,uint256 platformFee,uint256 influencerFee,uint256 keysSupply)'; -const contract_interface = new ethers.Interface([ - event_trade -]); - -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} - -interface IFee { - fees: number; - rev: number; -} - -const fetch = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, CHAIN.BASE, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.BASE, {})); - try { - let _logs: ILog[] = []; - for(let i = fromBlock; i < toBlock; i += 5000) { - const logs: ILog[] = (await sdk.getEventLogs({ - target: keyManagerQureFiAddr, - toBlock: i + 5000, - fromBlock: i, - chain: CHAIN.BASE, - topics: [topic0_trade] - })) as ILog[]; - _logs = _logs.concat(logs); - } - - const fees_details: IFee[] = _logs.map((e: ILog) => { - const value = contract_interface.parseLog(e); - - const platformFee = Number(value!.args.platformFee) / 10 ** usdcDecimals; - const influencerFee = Number(value!.args.influencerFee) / 10 ** usdcDecimals; - - return { - fees: platformFee + influencerFee, - rev: platformFee - } as IFee - }) - - const dailyFeesUSDC = fees_details.reduce((a: number, b: IFee) => a+b.fees, 0) - const dailyRevUSDC = fees_details.reduce((a: number, b: IFee) => a+b.rev, 0) - return { - dailyFees: `${dailyFeesUSDC}`, - dailyRevenue: `${dailyRevUSDC}`, - timestamp - } - } catch (error) { - throw error; - } - -} - +const event_trade = 'event Trade(address indexed trader,address indexed influencer,uint8 indexed direction,uint256 keysAmount,uint256 price,uint256 protocolEthAmount,uint256 subjectEthAmount,uint256 keysSupply)'; const adapter: Adapter = { adapter: { [CHAIN.BASE]: { - fetch: fetch, + fetch: getFeesExport(keyManagerQureFiAddr, [event_trade], { + token: ADDRESSES.base.USDC, + }), start: 1703255087, }, } diff --git a/fees/synthetix.ts b/fees/synthetix.ts index 8939ccf08f..bbe51c4eb7 100644 --- a/fees/synthetix.ts +++ b/fees/synthetix.ts @@ -19,19 +19,17 @@ const contract_address: IContract = { [CHAIN.ETHEREUM]: ADDRESSES.ethereum.sUSD, [CHAIN.OPTIMISM]: ADDRESSES.optimism.sUSD } -const graphs = () => { - return (chain: Chain) => { - 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, - dailyFees: dailyFee, - dailyRevenue: dailyFee, - dailyHoldersRevenue: dailyFee - }; +const graphs = (chain: Chain) => { + 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, + dailyFees: dailyFee, + dailyRevenue: dailyFee, + dailyHoldersRevenue: dailyFee }; }; }; @@ -40,14 +38,14 @@ const graphs = () => { const adapter: Adapter = { adapter: { [CHAIN.ETHEREUM]: { - fetch: graphs()(CHAIN.ETHEREUM), + fetch: graphs(CHAIN.ETHEREUM), start: 1653523200, meta: { methodology } }, [CHAIN.OPTIMISM]: { - fetch: graphs()(CHAIN.OPTIMISM), + fetch: graphs(CHAIN.OPTIMISM), start: 1636606800, meta: { methodology diff --git a/fees/tangible-rwa.ts b/fees/tangible-rwa.ts index 795a034915..a0efff8ac3 100644 --- a/fees/tangible-rwa.ts +++ b/fees/tangible-rwa.ts @@ -1,46 +1,24 @@ 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 * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; +import { addTokensReceived } from '../helpers/token'; -const topic0 = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'; -const topic2 = '0x0000000000000000000000006ced48efbb581a141667d7487222e42a3fa17cf7' -const usdc = ADDRESSES.polygon.USDC -const list_from_addres_hex = [ - '0x00000000000000000000000043e656716cf49c008435a8196d8f825f66f37254', - '0x000000000000000000000000cb7daa45ed2a9253ad3c900583b33bed822e8283', - '0x00000000000000000000000049c7371daecb7f06fc7303a14ab80174453df4cf', -]; -interface ILog { - data: string; - transactionHash: string; - topics: 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 Promise.all(list_from_addres_hex.map((topic1: string) => sdk.getEventLogs({ - target: usdc, - toBlock: toBlock, - fromBlock: fromBlock, - chain: CHAIN.POLYGON, - topics: [topic0, topic1, topic2] - })))).flat(); - const dailyFees = logs.map((e: ILog) => { - const value = Number(e.data) / 10 ** 6; - return value; - }).reduce((a: number, b: number) => a + b, 0) +const fetchFees = async (timestamp: number, _: ChainBlocks, options: FetchOptions): Promise => { + const fromAdddesses = [ + '0x43e656716cf49c008435a8196d8f825f66f37254', + '0xcb7daa45ed2a9253ad3c900583b33bed822e8283', + '0x49c7371daecb7f06fc7303a14ab80174453df4cf', + ]; + const dailyFees = options.createBalances() + for (const fromAdddress of fromAdddesses) { + await addTokensReceived({ options, target: '0x6ceD48EfBb581A141667D7487222E42a3FA17cf7', fromAddressFilter: fromAdddress, balances: dailyFees, tokens: [ADDRESSES.polygon.USDC] }) + } return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyFees}`, + dailyFees: dailyFees, + dailyRevenue: dailyFees, timestamp } - } const adapter: SimpleAdapter = { diff --git a/fees/touch.fan.ts b/fees/touch.fan.ts index f67627efac..6aaf4cdd1d 100644 --- a/fees/touch.fan.ts +++ b/fees/touch.fan.ts @@ -1,107 +1,20 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, FetchResultFees, SimpleAdapter } from "../adapters/types"; +import { Adapter, 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"; -import { Chain } from "@defillama/sdk/build/general"; -import { ethers } from "ethers"; +import { getFeesExport } from '../helpers/friend-tech'; const fan_address = '0x9842114F1d9c5286A6b8e23cF0D8142DAb2B3E9b'; const touch_address = `0xC612eD7a1FC5ED084C967bD71F1e0F0a338Cf816` -const topic0_trade = '0xc9d4f93ded9b42fa24561e02b2a40f720f71601eb1b3f7b3fd4eff20877639ee'; -const topic1_trade = '0xc9eb3cd369a1da18b8489f028fd6a49d0aca6d6ad28c01fe1451126ce41a7fa4'; const event_trade_fan = 'event Trade(address trader, address subject, bool isBuy, uint256 shareAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 referrerEthAmount, uint256 supply, uint256 trader_balance, uint256 blockTime)' const event_trade_touch = `event Trade(address trader,uint256 CommunityID,bool isBuy,uint256 shareAmount,uint256 ethAmount,uint256 protocolEthAmount,uint256 referrerEthAmount,uint256 supply,uint256 trader_balance,uint256 blockTime)` -const contract_fan_interface = new ethers.Interface([ - event_trade_fan -]); - -const contract_touch_interface = new ethers.Interface([ - event_trade_touch -]); - -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} - -interface IFee { - fees: number; - rev: number; -} - -const fetch = async (timestamp: number): Promise => { - // - const fromTimestamp = timestamp - 60 * 60 * 24 - // - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, CHAIN.ERA, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.ERA, {})); - try { - let _logs: ILog[] = []; - let _logs1: ILog[] = []; - for (let i = fromBlock; i < toBlock; i += 5000) { - const logs: ILog[] = (await sdk.getEventLogs({ - target: fan_address, - toBlock: i + 5000, - fromBlock: i, - chain: CHAIN.ERA, - topics: [topic0_trade] - })) as ILog[]; - _logs = _logs.concat(logs); - } - - for (let i = fromBlock; i < toBlock; i += 5000) { - const logs: ILog[] = (await sdk.getEventLogs({ - target: touch_address, - toBlock: i + 5000, - fromBlock: i, - chain: CHAIN.ERA, - topics: [topic1_trade] - })) as ILog[]; - _logs1 = _logs1.concat(logs); - } - - const fan_fees_details: IFee[] = _logs.map((e: ILog) => { - const value = contract_fan_interface.parseLog(e); - const protocolEthAmount = Number(value!.args.protocolEthAmount) / 10 ** 18; - const subjectEthAmount = Number(value!.args.subjectEthAmount) / 10 ** 18; - const refferEthAmount = Number(value!.args.referrerEthAmount) / 10 ** 18; - return { - fees: protocolEthAmount + subjectEthAmount + refferEthAmount, - rev: protocolEthAmount - } as IFee - }) - const touch_fees_details: IFee[] = _logs1.map((e: ILog) => { - const value = contract_touch_interface.parseLog(e); - - const protocolEthAmount = Number(value!.args.protocolEthAmount) / 10 ** 18; - const referrerEthAmount = Number(value!.args.referrerEthAmount) / 10 ** 18; - - return { - fees: protocolEthAmount + referrerEthAmount, - rev: protocolEthAmount - } as IFee - }) - let fees = [...fan_fees_details, ...touch_fees_details] - const dailyFees = fees.reduce((a: number, b: IFee) => a + b.fees, 0) - const dailyRev = fees.reduce((a: number, b: IFee) => a + b.rev, 0) - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const dailyFeesUSD = (dailyFees) * ethPrice; - const dailyRevUSD = (dailyRev) * ethPrice; - return { - dailyFees: `${dailyFeesUSD}`, - dailyRevenue: `${dailyRevUSD}`, - timestamp - } - } catch (error) { - throw error; - } +const fetch: any = async (...args: any[]): Promise => { + // @ts-ignore + const res: any = await getFeesExport(fan_address, [event_trade_fan])(...args); + // @ts-ignore + const res1: any = await getFeesExport(touch_address, [event_trade_touch])(...args); + res.dailyFees.addBalances(res1.dailyFees); + res.dailyRevenue.addBalances(res1.dailyRevenue); + return res; } diff --git a/fees/yield-yak-staked-avax.ts b/fees/yield-yak-staked-avax.ts index c9e1d80cc6..aaf0aae878 100644 --- a/fees/yield-yak-staked-avax.ts +++ b/fees/yield-yak-staked-avax.ts @@ -1,103 +1,34 @@ -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 { ethers } from "ethers"; -import { getBlock } from "../helpers/getBlock"; -import * as sdk from "@defillama/sdk"; -import { getPrices } from "../utils/prices"; - - const address = '0x185214FD3696942FBf29Af2983AA7493112777Ae'; -const topic_0_distribution = '0xb6bcab815b7a952b8759f2f92fc9981dc1156f6c11bf4dc7e9cb3036495e653a'; -const topic_0_paid = '0x4f2d18324ee95128de091ed2adc501295479000ce4c2cec607aeb1b67e189e2f'; const event_distribution = 'event Distribution(uint256 indexed epoch,address indexed by,uint256 amount)'; const event_paid = 'event Paid(uint256 indexed epoch,address indexed payee,uint256 amount)'; -const AVAX = ADDRESSES.avax.WAVAX; const yield_yak_master = '0x0cf605484a512d3f3435fed77ab5ddc0525daf5f'; const yak_gov = '0x5925c5c6843a8f67f7ef2b55db1f5491573c85eb'; -const contract_interface = new ethers.Interface([ - event_distribution, - event_paid -]); - -interface IPaid { - payee: string; - amount: string; -} - -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} - -const fetch = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, CHAIN.AVAX, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.AVAX, {})); - - const logs_distribution: ILog[] = (await sdk.getEventLogs({ - target: address, - fromBlock: fromBlock, - toBlock: toBlock, - topics: [topic_0_distribution], - chain: CHAIN.AVAX - })) as ILog[]; - - const logs_paid: ILog[] = (await sdk.getEventLogs({ - target: address, - fromBlock: fromBlock, - toBlock: toBlock, - topics: [topic_0_paid], - chain: CHAIN.AVAX - })) as ILog[]; - - - const avaxAddress = `${CHAIN.AVAX}:${AVAX}`; - const prices = (await getPrices([avaxAddress], timestamp)) - const dailyFees = logs_distribution.map((e: ILog) => { - const price = prices[`${CHAIN.AVAX}:${AVAX}`].price; - const decimals = prices[`${CHAIN.AVAX}:${AVAX}`].decimals; - const value = contract_interface.parseLog(e); - const amount = Number(value!.args.amount) - return (amount / 10 ** decimals) * price; - }).reduce((a: number, b: number) => a + b, 0); - const revenue = logs_paid.map((e: ILog) => { - const value = contract_interface.parseLog(e); - if (value!.args.payee.toLowerCase() === yield_yak_master.toLowerCase()) { - const price = prices[`${CHAIN.AVAX}:${AVAX}`].price; - const decimals = prices[`${CHAIN.AVAX}:${AVAX}`].decimals; - const value = contract_interface.parseLog(e); - const amount = Number(value!.args.amount) - return (amount / 10 ** decimals) * price; - } - return 0; - }).reduce((a, b) => a + b,0) +const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, getLogs, }: FetchOptions): Promise => { + const dailyFees = createBalances() + const dailyHoldersRevenue = createBalances() + const dailyProtocolRevenue = createBalances() + const logs_distribution = await getLogs({ target: address, eventAbi: event_distribution, }) + const logs_paid = await getLogs({ target: address, eventAbi: event_paid, }) - const dailyProtocolRevenue = logs_paid.map((e: ILog) => { - const value = contract_interface.parseLog(e); - if (value!.args.payee.toLowerCase() === yak_gov.toLowerCase()) { - const price = prices[`${CHAIN.AVAX}:${AVAX}`].price; - const decimals = prices[`${CHAIN.AVAX}:${AVAX}`].decimals; - const value = contract_interface.parseLog(e); - const amount = Number(value!.args.amount) - return (amount / 10 ** decimals) * price; - } - return 0; - }).reduce((a, b) => a + b,0) + logs_distribution.map((e: any) => dailyFees.addGasToken(e.amount)) - const dailyRevenue = dailyFees; - const dailyHoldersRevenue = revenue; + logs_paid.map((e: any) => { + const payee = e.payee.toLowerCase() + if (payee === yield_yak_master) + dailyHoldersRevenue.addGasToken(e.amount) + else if (payee === yak_gov) + dailyProtocolRevenue.addGasToken(e.amount) + }) return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyRevenue}`, - dailyHoldersRevenue: `${dailyHoldersRevenue}`, - dailyProtocolRevenue: `${dailyProtocolRevenue}`, + dailyFees, + dailyRevenue: dailyFees, + dailyHoldersRevenue, + dailyProtocolRevenue, timestamp } } diff --git a/fees/zapper-channels.ts b/fees/zapper-channels.ts index 334082b96c..511a73456b 100644 --- a/fees/zapper-channels.ts +++ b/fees/zapper-channels.ts @@ -1,76 +1,14 @@ -import ADDRESSES from '../helpers/coreAssets.json' -import { Adapter, FetchResultFees, SimpleAdapter } from "../adapters/types"; +import { Adapter, } from "../adapters/types"; import { CHAIN } from "../helpers/chains"; -import * as sdk from "@defillama/sdk"; -import { getBlock } from "../helpers/getBlock"; -import { getPrices } from "../utils/prices"; -import { Chain } from "@defillama/sdk/build/general"; -import { ethers } from "ethers"; +import { getFeesExport } from '../helpers/friend-tech'; const FriendtechSharesAddress = '0xbc98176dc471cb67dc19fa4558104f034d8965fa'; -const topic0_trade = '0xc9eb3cd369a1da18b8489f028fd6a49d0aca6d6ad28c01fe1451126ce41a7fa4'; -const event_trade = 'event Trade(address trader,uint256 channelId,bool isBuy,uint256 shareAmount,uint256 totalShares,uint256 ethAmount,uint256 protocolEthAmount,uint256 channelEthAmount,uint256 totalSupply,uint256 channelFeePerShare)' -const contract_interface = new ethers.Interface([ - event_trade -]); - -interface ILog { - data: string; - transactionHash: string; - topics: string[]; -} - -interface IFee { - fees: number; - rev: number; -} - -const fetch = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - - const fromBlock = (await getBlock(fromTimestamp, CHAIN.BASE, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.BASE, {})); - let _logs: ILog[] = []; - for (let i = fromBlock; i < toBlock; i += 5000) { - const logs: ILog[] = (await sdk.getEventLogs({ - target: FriendtechSharesAddress, - toBlock: i + 5000, - fromBlock: i, - chain: CHAIN.BASE, - topics: [topic0_trade] - })) as ILog[]; - _logs = _logs.concat(logs); - } - - const fees_details: IFee[] = _logs.map((e: ILog) => { - const value = contract_interface.parseLog(e); - const protocolEthAmount = Number(value!.args.protocolEthAmount) / 10 ** 18; - const subjectEthAmount = Number(value!.args.channelEthAmount) / 10 ** 18; - return { - fees: protocolEthAmount + subjectEthAmount, - rev: protocolEthAmount - } as IFee - }) - const dailyFees = fees_details.reduce((a: number, b: IFee) => a + b.fees, 0) - const dailyRev = fees_details.reduce((a: number, b: IFee) => a + b.rev, 0) - const ethAddress = "ethereum:" + ADDRESSES.null; - const ethPrice = (await getPrices([ethAddress], timestamp))[ethAddress].price; - const dailyFeesUSD = (dailyFees) * ethPrice; - const dailyRevUSD = (dailyRev) * ethPrice; - return { - dailyFees: `${dailyFeesUSD}`, - dailyRevenue: `${dailyRevUSD}`, - timestamp - } - -} - +const event_trade = 'event Trade(address trader,uint256 channelId,bool isBuy,uint256 shareAmount,uint256 totalShares,uint256 ethAmount,uint256 protocolEthAmount,uint256 subjectEthAmount,uint256 totalSupply,uint256 channelFeePerShare)' const adapter: Adapter = { adapter: { [CHAIN.BASE]: { - fetch: fetch, + fetch: getFeesExport(FriendtechSharesAddress, [event_trade]), start: 1696204800, }, } diff --git a/fees/zerion-wallet.ts b/fees/zerion-wallet.ts index 102e75132a..6df1d94404 100644 --- a/fees/zerion-wallet.ts +++ b/fees/zerion-wallet.ts @@ -19,7 +19,7 @@ const multisigs: TMulitsig = { const fetch: any = async (timestamp: number, _: any, options: FetchOptions): Promise => { const dailyFees = await addGasTokensReceived({ multisigs: multisigs[options.chain], options }) - await addTokensReceived({ targets: multisigs[options.chain], options, balances: dailyFees }) + await addTokensReceived({ targets: multisigs[options.chain], options, balances: dailyFees, fetchTokenList: true, }) return { timestamp, dailyFees, dailyRevenue: dailyFees, dailyProtocolRevenue: dailyFees, } } diff --git a/helpers/friend-tech.ts b/helpers/friend-tech.ts index 6edbbfa2a9..677b4c8a60 100644 --- a/helpers/friend-tech.ts +++ b/helpers/friend-tech.ts @@ -1,21 +1,27 @@ import { ChainBlocks, Fetch, FetchOptions } from "../adapters/types"; +import ADDRESSES from "./coreAssets.json"; + const event_trade = 'event Trade(address trader, address subject, bool isBuy, uint256 shareAmount, uint256 ethAmount, uint256 protocolEthAmount, uint256 subjectEthAmount, uint256 supply)' -export function getFeesExport(FriendtechSharesAddress: string) { +export function getFeesExport(FriendtechSharesAddress: string, eventAbis = [event_trade], { + token = ADDRESSES.null, +}: { token?: string } = {}) { return (async (timestamp: number, _: ChainBlocks, { getLogs, createBalances, }: FetchOptions) => { const dailyFees = createBalances() const dailyRevenue = createBalances() - const logs = (await getLogs({ - target: FriendtechSharesAddress, - eventAbi: event_trade - })) - - logs.map((e) => { - dailyFees.addGasToken(e.protocolEthAmount) - dailyFees.addGasToken(e.subjectEthAmount) - dailyRevenue.addGasToken(e.protocolEthAmount) - }) - return { dailyFees, dailyRevenue, timestamp, } + for (const eventAbi of eventAbis) { + const logs = await getLogs({ target: FriendtechSharesAddress, eventAbi }) + logs.map((e: any) => { + if (e.protocolEthAmount) { + dailyFees.add(token, e.protocolEthAmount) + dailyRevenue.add(token, e.protocolEthAmount) + } + if (e.subjectEthAmount) dailyFees.add(token, e.subjectEthAmount) + if (e.referrerEthAmount) dailyFees.add(token, e.referrerEthAmount) + if (e.holderEthAmount) dailyFees.add(token, e.holderEthAmount) + }) + } + return { dailyFees, dailyRevenue, timestamp, } }) as Fetch } \ No newline at end of file diff --git a/helpers/token.ts b/helpers/token.ts index d4018e914d..72accd5466 100644 --- a/helpers/token.ts +++ b/helpers/token.ts @@ -45,9 +45,12 @@ export async function addTokensReceived(params: { tokens?: string[]; toAddressFilter?: string | null; tokenTransform?: (token: string) => string; + fetchTokenList?: boolean; + token?: string; }) { - let { target, targets, options, balances, tokens, fromAddressFilter = null, tokenTransform = (i: string) => i } = params; + let { target, targets, options, balances, tokens, fromAddressFilter = null, tokenTransform = (i: string) => i, fetchTokenList= false, token } = params; const { chain, createBalances, getLogs, } = options + if (!tokens && token) tokens = [token] if (!balances) balances = createBalances() @@ -62,13 +65,13 @@ export async function addTokensReceived(params: { } - if (!tokens && target) { + if (!tokens && target && fetchTokenList) { if (!ankrChainMapping[chain]) throw new Error('Chain Not supported: ' + chain) const ankrTokens = await ankrGetTokens(target, { onlyWhitelisted: true }) tokens = ankrTokens[ankrChainMapping[chain]] ?? [] } - if (!tokens!.length) return balances + if (!tokens?.length) return balances const toAddressFilter = target ? ethers.zeroPadValue(target, 32) : null if (fromAddressFilter) fromAddressFilter = ethers.zeroPadValue(fromAddressFilter, 32)