Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: D2 Finance #2259

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading