Skip to content

Commit

Permalink
Merge pull request #2275 from dunglv-smartosc/master
Browse files Browse the repository at this point in the history
Update: D2Finance
  • Loading branch information
dtmkeng authored Jan 4, 2025
2 parents f51d7e1 + 27a3850 commit c27f35f
Showing 1 changed file with 55 additions and 8 deletions.
63 changes: 55 additions & 8 deletions fees/d2finance/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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} }) {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
},
},
Expand Down

0 comments on commit c27f35f

Please sign in to comment.