Skip to content

Commit

Permalink
fix version
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Feb 22, 2025
1 parent 37c6e6e commit b37e3d2
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions dexs/magma-finance/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fetchURL from "../../utils/fetchURL";
import { Chain } from "@defillama/sdk/build/general";
import { FetchOptions, SimpleAdapter } from "../../adapters/types";
import { FetchOptions, SimpleAdapter, FetchResult } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

Expand All @@ -17,38 +17,50 @@ const url: IUrl = {
},
};

interface IVolumeall {
interface IVolumeData {
num: string;
date: string;
}


async function fetchHistoricalVolume(chain: Chain): Promise<IVolumeData[]> {
const response = await fetchURL(url[chain].histogramUrl);
return response.data.list;
}

function calculateDailyVolume(historicalVolume: IVolumeData[], dateStr: string): string | undefined {
return historicalVolume.find(
(dayItem) => dayItem.date.split('T')[0] === dateStr
)?.num;
}

function calculateTotalVolume(historicalVolume: IVolumeData[]): string {
return historicalVolume.reduce(
(acc, item) => acc + Number(item.num),
0
).toString();
}

const fetch = (chain: Chain) => {
return async (options: FetchOptions) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(
new Date(options.endTimestamp * 1000),
);
const historicalVolume: IVolumeall[] = (
await fetchURL(url[chain].histogramUrl)
).data.list;
const dailyVolume = historicalVolume.find(
(dayItem) =>
new Date(dayItem.date.split("T")[0]).getTime() / 1000 === dayTimestamp,
)?.num;
const totalVolume = historicalVolume.reduce(
(acc, item) => acc + Number(item.num),
0,
);
return async (_tt: any,_t: any, options: FetchOptions): Promise<FetchResult> => {
const date = new Date(options.startOfDay * 1000);
const dateStr = date.toISOString().split('T')[0]; // Format: YYYY-MM-DD
const dayTimestamp = getUniqStartOfTodayTimestamp(date);

const historicalVolume = await fetchHistoricalVolume(chain);
const dailyVolume = calculateDailyVolume(historicalVolume, dateStr);
const totalVolume = calculateTotalVolume(historicalVolume);

return {
totalVolume: `${totalVolume}`,
dailyVolume: dailyVolume ? `${dailyVolume}` : undefined,
timestamp: dayTimestamp,
dailyVolume,
totalVolume,
};
};
};

const adapter: SimpleAdapter = {
version: 2,
version: 1,
adapter: {
[CHAIN.SUI]: {
fetch: fetch(CHAIN.SUI),
Expand Down

0 comments on commit b37e3d2

Please sign in to comment.