From 27a38505abea72f9281dca654b65d857eca829e1 Mon Sep 17 00:00:00 2001 From: dunglv-smartosc Date: Thu, 2 Jan 2025 14:05:02 -0500 Subject: [PATCH] Update: D2Finance --- fees/d2finance/index.ts | 63 +++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/fees/d2finance/index.ts b/fees/d2finance/index.ts index c2aafe08c2..31db7936ee 100644 --- a/fees/d2finance/index.ts +++ b/fees/d2finance/index.ts @@ -1,6 +1,6 @@ import BigNumber from "bignumber.js"; import { FetchV2 } from "../../adapters/types"; -import { ARBITRUM } from "../../helpers/chains"; +import { CHAIN } from "../../helpers/chains"; import { getPrices } from "../../utils/prices"; import { gql, GraphQLClient } from "graphql-request"; import { @@ -39,8 +39,8 @@ const tokens = [ }, ]; -const fetchFeeData = async (timestamp: number) => { - const client = new GraphQLClient("https://d2.finance/subgraphs/name/d2"); +const fetchFeeData = async (url: string, timestamp: number) => { + const client = new GraphQLClient(url); const req = gql` query Query { feesWithdrawns(where: { blockTimestamp_lte: ${timestamp} }) { @@ -65,11 +65,53 @@ const fetchTokenPrices = async (timestamp: number) => { return prices; }; -const fetch: FetchV2 = async ({ startTimestamp }) => { +const fetchOnArbitrum: FetchV2 = async ({ startTimestamp }) => { const monthStartTimeStamp = getTimestampAtStartOfMonth(startTimestamp); const monthEndTimestamp = getTimestampAtStartOfNextMonth(startTimestamp); - const result = await fetchFeeData(startTimestamp); + const graphQlUrl = "https://d2.finance/subgraphs/name/smartosc/d2"; + const result = await fetchFeeData(graphQlUrl, startTimestamp); + const tokenPrices = await fetchTokenPrices(startTimestamp); + + let totalAmount = 0; + let monthlyAmount = 0; + for (let data of result) { + const token = tokens.find((token) => token.ticker === data.token); + if (token) { + const price = tokenPrices[token.geckoId].price; + const amountInDollar = Number( + BigNumber(data.amount) + .times(price) + .dividedBy(BigNumber(10).pow(token.decimals)) + ); + totalAmount += amountInDollar; + if ( + data.blockTimestamp >= monthStartTimeStamp && + data.blockTimestamp < monthEndTimestamp + ) { + monthlyAmount += amountInDollar; + } + } + } + + const monthFee = monthlyAmount / 30; + + return { + dailyFees: monthFee, + dailyRevenue: monthFee, + dailyProtocolRevenue: monthFee, + totalFees: totalAmount, + totalRevenue: totalAmount, + totalProtocolRevenue: totalAmount, + }; +}; + +const fetchOnBase: FetchV2 = async ({ startTimestamp }) => { + const monthStartTimeStamp = getTimestampAtStartOfMonth(startTimestamp); + const monthEndTimestamp = getTimestampAtStartOfNextMonth(startTimestamp); + + const graphQlUrl = "https://d2.finance/subgraphs/name/smartosc/base-d2"; + const result = await fetchFeeData(graphQlUrl, startTimestamp); const tokenPrices = await fetchTokenPrices(startTimestamp); let totalAmount = 0; @@ -108,9 +150,14 @@ const fetch: FetchV2 = async ({ startTimestamp }) => { export default { version: 2, adapter: { - [ARBITRUM]: { - fetch, - start: '2024-01-20', + [CHAIN.ARBITRUM]: { + fetch: fetchOnArbitrum, + start: "2024-01-20", + runAtCurrTime: true, + }, + [CHAIN.BASE]: { + fetch: fetchOnBase, + start: "2024-10-31", runAtCurrTime: true, }, },