From 5db6408787abd55c62584c3237edd7444c8cd8c1 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Thu, 28 Mar 2024 16:28:26 +0000 Subject: [PATCH] fix --- dexs/keller/index.ts | 177 +-------------------------------------- fees/keller/bribes.ts | 25 +++--- fees/keller/index.ts | 36 ++++---- fees/keller/keller-v2.ts | 159 ----------------------------------- 4 files changed, 31 insertions(+), 366 deletions(-) delete mode 100644 fees/keller/keller-v2.ts diff --git a/dexs/keller/index.ts b/dexs/keller/index.ts index 4910ff5c40..a24743f812 100644 --- a/dexs/keller/index.ts +++ b/dexs/keller/index.ts @@ -1,183 +1,14 @@ import { SimpleAdapter } 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 BigNumber from "bignumber.js"; -import { getTimestamp } from "@defillama/sdk/build/util"; +import { getDexVolumeExports } from "../../helpers/dexVolumeLogs"; -interface ILog { - data: string; - transactionHash: string; -} -interface IAmount { - amount0In: string; - amount1In: string; - amount0Out: string; - amount1Out: string; -} - -const topic0 = '0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822'; const FACTORY_ADDRESS = '0xbc83f7dF70aE8A3e4192e1916d9D0F5C2ee86367'; -type TABI = { - [k: string]: object; -} -const ABIs: TABI = { - allPairsLength: { - "type": "function", - "stateMutability": "view", - "outputs": [ - { - "type": "uint256", - "name": "", - "internalType": "uint256" - } - ], - "name": "allPairsLength", - "inputs": [] - }, - allPairs: { - "type": "function", - "stateMutability": "view", - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "inputs": [ - { - "type": "uint256", - "name": "", - "internalType": "uint256" - } - ], - "name": "allPairs", - } -}; - -const PAIR_TOKEN_ABI = (token: string): object => { - return { - "constant": true, - "inputs": [], - "name": token, - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - } -}; - - -const fetch = async (timestamp: number) => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - try { - const poolLength = (await sdk.api.abi.call({ - target: FACTORY_ADDRESS, - chain: CHAIN.SCROLL, - abi: ABIs.allPairsLength, - }).catch(e =>{ - console.log('error', e); - throw e; - })).output; - console.log('poolLength', poolLength); - const poolsRes = await sdk.api.abi.multiCall({ - abi: ABIs.allPairs, - calls: Array.from(Array(Number(poolLength)).keys()).map((i) => ({ - target: FACTORY_ADDRESS, - params: i, - })), - chain: CHAIN.SCROLL - }); - - const lpTokens = poolsRes.output - .map(({ output }: any) => output); - - const [underlyingToken0, underlyingToken1] = await Promise.all( - ['token0', 'token1'].map((method) => - sdk.api.abi.multiCall({ - abi: PAIR_TOKEN_ABI(method), - calls: lpTokens.map((address: string) => ({ - target: address, - })), - chain: 'scroll' - }) - ) - ); - - const tokens0 = underlyingToken0.output.map((res: any) => res.output); - const tokens1 = underlyingToken1.output.map((res: any) => res.output); - const fromBlock = await getBlock(fromTimestamp, CHAIN.SCROLL, {}); - const toBlock = await getBlock(toTimestamp, CHAIN.SCROLL, {}); - - const logs: ILog[][] = (await Promise.all(lpTokens.map((address: string) => sdk.api.util.getLogs({ - target: address, - topic: '', - toBlock: toBlock, - fromBlock: fromBlock, - keys: [], - chain: CHAIN.SCROLL, - topics: [topic0] - })))) - .map((p: any) => p) - .map((a: any) => a.output); - const rawCoins = [...tokens0, ...tokens1].map((e: string) => `scroll:${e}`); - const coins = [...new Set(rawCoins)] - const prices = await getPrices(coins, timestamp); - const untrackVolumes: number[] = lpTokens.map((_: string, index: number) => { - const log: IAmount[] = logs[index] - .map((e: ILog) => { return { ...e, data: e.data.replace('0x', '') } }) - .map((p: ILog) => { - BigNumber.config({ POW_PRECISION: 100 }); - const amount0In = new BigNumber('0x' + p.data.slice(0, 64)).toString(); - const amount1In = new BigNumber('0x' + p.data.slice(64, 128)).toString(); - const amount0Out = new BigNumber('0x' + p.data.slice(128, 192)).toString(); - const amount1Out = new BigNumber('0x' + p.data.slice(192, 256)).toString(); - return { - amount0In, - amount1In, - amount0Out, - amount1Out, - } as IAmount - }) as IAmount[]; - const token0Price = (prices[`scroll:${tokens0[index]}`]?.price || 0); - const token1Price = (prices[`scroll:${tokens1[index]}`]?.price || 0); - const token0Decimals = (prices[`scroll:${tokens0[index]}`]?.decimals || 0) - const token1Decimals = (prices[`scroll:${tokens1[index]}`]?.decimals || 0) - const totalAmount0 = log - .reduce((a: number, b: IAmount) => Number(b.amount0In) + Number(b.amount0Out) + a, 0) / 10 ** token0Decimals * token0Price; - const totalAmount1 = log - .reduce((a: number, b: IAmount) => Number(b.amount1In) + Number(b.amount1Out) + a, 0) / 10 ** token1Decimals * token1Price; - - const untrackAmountUSD = token0Price !== 0 ? totalAmount0 : token1Price !== 0 ? totalAmount1 : 0; // counted only we have price data - return untrackAmountUSD; - }); - - const dailyVolume = untrackVolumes.reduce((a: number, b: number) => a + b, 0); - return { - dailyVolume: `${dailyVolume}`, - timestamp, - }; - } catch(error) { - console.error(error); - throw error; - } -} - const adapter: SimpleAdapter = { adapter: { - ["scroll"]: { - fetch, - start: async () => getTimestamp(4265908, "scroll") , + [CHAIN.SCROLL]: { + fetch: getDexVolumeExports({ chain: CHAIN.SCROLL, factory: FACTORY_ADDRESS }), + start: 1710806400, }, } }; diff --git a/fees/keller/bribes.ts b/fees/keller/bribes.ts index 542ba5164c..9c218efb79 100644 --- a/fees/keller/bribes.ts +++ b/fees/keller/bribes.ts @@ -1,7 +1,7 @@ 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)' @@ -24,12 +24,15 @@ interface IBribes { } -export const fees_bribes = async (fromBlock: number, toBlock: number, timestamp: number): Promise => { +export const getBribes = async (options: FetchOptions): Promise => { try { + const dailyBribesRevenue = options.createBalances(); + const fromBlock = await options.getFromBlock(); + const toBlock = await options.getToBlock(); const voter = '0x30f827DECe6F25c74F37d0dD45bC245d893266e6'; const logs_geuge_created: ethers.EventLog[] = (await sdk.api.util.getLogs({ target: voter, - fromBlock: 4265908, //Block number of the contract's creation + fromBlock: 4265093, //Block number of the contract's creation toBlock: toBlock, topic: '', topics: [topic0_geuge_created], @@ -37,7 +40,7 @@ export const fees_bribes = async (fromBlock: number, toBlock: number, timestamp: keys: [] })).output; const bribes_contract: string[] = logs_geuge_created.map((e: ethers.EventLog) => { - const value = contract_interface.parseLog(e); + const value = contract_interface.parseLog(e as any); return value?.args.bribeVotingReward; }) @@ -60,14 +63,12 @@ export const fees_bribes = async (fromBlock: number, toBlock: number, timestamp: amount: Number(value?.args.amount._hex) } as IBribes }) - const coins = [...new Set(logs_bribes.map((e: IBribes) => `${"scroll"}:${e.token.toLowerCase()}`))] - const prices = await getPrices(coins, timestamp); - const fees_bribes_usd = logs_bribes.map((e: IBribes) => { - const price = prices[`${"scroll"}:${e.token.toLowerCase()}`]?.price || 0; - const decimals = prices[`${"scroll"}:${e.token.toLowerCase()}`]?.decimals || 0; - return (Number(e.amount) / 10 ** decimals) * price; - }).reduce((a: number, b: number) => a+b, 0); - return fees_bribes_usd; + logs_bribes.forEach((e: IBribes) => { + dailyBribesRevenue.add(e.token, e.amount) + }); + return { + dailyBribesRevenue + }; } catch (error) { console.error(error); throw error; diff --git a/fees/keller/index.ts b/fees/keller/index.ts index 2f295f20af..21a2a38d1e 100644 --- a/fees/keller/index.ts +++ b/fees/keller/index.ts @@ -1,35 +1,27 @@ -import { Adapter, FetchResultFees } from '../../adapters/types'; +import { Adapter, FetchOptions, FetchResultV2 } from '../../adapters/types'; import { CHAIN } from '../../helpers/chains'; -import { fetchV2 } from './keller-v2'; -import { fees_bribes } from './bribes'; -import { getBlock } from '../../helpers/getBlock'; -import { getTimestamp } from '@defillama/sdk/build/util'; +import { getBribes } from './bribes'; +import { exportDexVolumeAndFees } from '../../helpers/dexVolumeLogs'; - -const getFees = async (timestamp: number): Promise => { - const fromTimestamp = timestamp - 60 * 60 * 24 - const toTimestamp = timestamp - const fromBlock = (await getBlock(fromTimestamp, CHAIN.SCROLL, {})); - const toBlock = (await getBlock(toTimestamp, CHAIN.SCROLL, {})); - - const [feeV2, bribes] = await Promise.all([fetchV2(fromBlock, toBlock, timestamp), fees_bribes(fromBlock, toBlock, timestamp)]); - const dailyFees = Number(feeV2.dailyFees); - const dailyRevenue = Number(feeV2.dailyRevenue) + bribes; - const dailyHoldersRevenue = Number(feeV2.dailyHoldersRevenue) + bribes; +const FACTORY_ADDRESS = '0xbc83f7dF70aE8A3e4192e1916d9D0F5C2ee86367'; +const getFees = async (options: FetchOptions): Promise => { + const v1Results = await exportDexVolumeAndFees({ chain: CHAIN.SCROLL, factory: FACTORY_ADDRESS })(options.endTimestamp, {}, options) + const bribesResult = await getBribes(options); + v1Results.dailyBribesRevenue = bribesResult.dailyBribesRevenue; return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyRevenue}`, - dailyHoldersRevenue: `${dailyHoldersRevenue}`, - dailyBribesRevenue: `${bribes}`, - timestamp + dailyFees: v1Results.dailyFees, + dailyRevenue: v1Results.dailyRevenue, + dailyHoldersRevenue: v1Results.dailyFees, + dailyBribesRevenue: v1Results.dailyBribesRevenue, } } const adapter: Adapter = { + version: 2, adapter: { [CHAIN.SCROLL]: { fetch: getFees, - start: async () => getTimestamp(4265908, "scroll"), // TODO: Add accurate timestamp + start: 1710806400 }, }, }; diff --git a/fees/keller/keller-v2.ts b/fees/keller/keller-v2.ts deleted file mode 100644 index 9ae7fbe6e1..0000000000 --- a/fees/keller/keller-v2.ts +++ /dev/null @@ -1,159 +0,0 @@ -import { FetchResultFees } from "../../adapters/types"; -import * as sdk from "@defillama/sdk"; -import { getPrices } from "../../utils/prices"; -import BigNumber from "bignumber.js"; -import { CHAIN } from "../../helpers/chains"; - -interface ILog { - data: string; - transactionHash: string; -} -interface IAmount { - amount0: string; - amount1: string; -} -const topic_name = 'Swap(index_topic_1 address sender, index_topic_2 address to, uint256 amount0In, uint256 amount1In, uint256 amount0Out, uint256 amount1Out)'; -const topic0 = '0x112c256902bf554b6ed882d2936687aaeb4225e8cd5b51303c90ca6cf43a8602'; -const FACTORY_ADDRESS = '0xbc83f7dF70aE8A3e4192e1916d9D0F5C2ee86367'; - -type TABI = { - [k: string]: object; -} -const ABIs: TABI = { - allPairsLength: { - "inputs": [], - "name": "allPairsLength", - "outputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "stateMutability": "view", - "type": "function" - }, - allPairs: { - "inputs": [ - { - "internalType": "uint256", - "name": "", - "type": "uint256" - } - ], - "name": "allPairs", - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "stateMutability": "view", - "type": "function" - } -}; - -const PAIR_TOKEN_ABI = (token: string): object => { - return { - "constant": true, - "inputs": [], - "name": token, - "outputs": [ - { - "internalType": "address", - "name": "", - "type": "address" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - } -}; - - -export const fetchV2 = async (fromBlock: number, toBlock: number, timestamp: number): Promise => { - try { - const poolLength = (await sdk.api.abi.call({ - target: FACTORY_ADDRESS, - chain: CHAIN.SCROLL, - abi: ABIs.allPairsLength, - })).output; - - const poolsRes = await sdk.api.abi.multiCall({ - abi: ABIs.allPairs, - calls: Array.from(Array(Number(poolLength)).keys()).map((i) => ({ - target: FACTORY_ADDRESS, - params: i, - })), - chain: CHAIN.SCROLL - }); - - const lpTokens = poolsRes.output - .map(({ output }: any) => output); - - const [underlyingToken0, underlyingToken1] = await Promise.all( - ['token0', 'token1'].map((method) => - sdk.api.abi.multiCall({ - abi: PAIR_TOKEN_ABI(method), - calls: lpTokens.map((address: string) => ({ - target: address, - })), - chain: CHAIN.SCROLL, - //permitFailure: true, - }) - ) - ); - - const tokens0 = underlyingToken0.output.map((res: any) => res.output); - const tokens1 = underlyingToken1.output.map((res: any) => res.output); - const logs: ILog[][] = (await Promise.all(lpTokens.map((address: string) => sdk.api.util.getLogs({ - target: address, - topic: topic_name, - toBlock: toBlock, - fromBlock: fromBlock, - keys: [], - chain: CHAIN.SCROLL, - topics: [topic0] - })))) - .map((p: any) => p) - .map((a: any) => a.output); - const rawCoins = [...tokens0, ...tokens1].map((e: string) => `${"scroll"}:${e}`); - const coins = [...new Set(rawCoins)] - const prices = await getPrices(coins, timestamp); - const untrackVolumes: number[] = lpTokens.map((_: string, index: number) => { - const log: IAmount[] = logs[index] - .map((e: ILog) => { return { ...e, data: e.data.replace('0x', '') } }) - .map((p: ILog) => { - BigNumber.config({ POW_PRECISION: 100 }); - const amount0 = new BigNumber('0x' + p.data.slice(0, 64)).toString(); - const amount1 = new BigNumber('0x' + p.data.slice(64, 128)).toString(); - return { - amount0, - amount1, - } as IAmount - }) as IAmount[]; - const token0Price = (prices[`${"scroll"}:${tokens0[index]}`]?.price || 0); - const token1Price = (prices[`${"scroll"}:${tokens1[index]}`]?.price || 0); - const token0Decimals = (prices[`${"scroll"}:${tokens0[index]}`]?.decimals || 0) - const token1Decimals = (prices[`${"scroll"}:${tokens1[index]}`]?.decimals || 0) - const totalAmount0 = log - .reduce((a: number, b: IAmount) => Number(b.amount0) + a, 0) / 10 ** token0Decimals * token0Price; - const totalAmount1 = log - .reduce((a: number, b: IAmount) => Number(b.amount1) + Number(b.amount1) + a, 0) / 10 ** token1Decimals * token1Price; - return (totalAmount0 + totalAmount1); - }); - - const dailyFees = untrackVolumes.reduce((a: number, b: number) => a + b, 0); - return { - dailyFees: `${dailyFees}`, - dailyRevenue: `${dailyFees}`, - dailyHoldersRevenue: `${dailyFees}`, - timestamp, - }; - } catch(error) { - console.error(error); - throw error; - } -}