Skip to content

Commit

Permalink
Merge pull request #61 from cypherpepe/patch-1
Browse files Browse the repository at this point in the history
refactor(honey): cleared some code redundancy
  • Loading branch information
bearpong authored Sep 23, 2024
2 parents 2137a3b + 8d72f4f commit 2d8e76a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 179 deletions.
83 changes: 23 additions & 60 deletions apps/honey/src/app/api/useHoneyEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,52 @@ import { getAbsoluteUrl } from "~/utils/vercel-utils";

const DEFAULT_SIZE = 10;

const fetchData = async (url: string) => {
try {
const res = await fetch(url, {
method: "GET",
headers: {
"x-vercel-protection-bypass": "4fphLjUJKQcW0kAbaFBGUNcAHb2WwnTX",
},
});
const jsonRes = await res.json();
return jsonRes;
} catch (e) {
console.error(e);
return null;
}
};

export const useHoneyEvents = () => {
const {
data: allData,
size: allDataSize,
setSize: setAllDataSize,
isLoading: isAllDataLoading,
isLoading: isAllDataLoading
} = useSWRInfinite(
(index) => ["allHoneyData", index],
async (key: any[]) => {
const page = key[1] ?? 0 + 1;
try {
const res = await fetch(
`${getAbsoluteUrl()}/api?page=${page}&perPage=${DEFAULT_SIZE}`,
{
method: "GET",
headers: {
"x-vercel-protection-bypass": "4fphLjUJKQcW0kAbaFBGUNcAHb2WwnTX",
},
},
);
const jsonRes = await res.json();
return jsonRes;
} catch (e) {
console.error(e);
}
},
{
refreshInterval: POLLING.SLOW,
},
(key) => fetchData(`${getAbsoluteUrl()}/api?page=${(key[1] ?? 0) + 1}&perPage=${DEFAULT_SIZE}`),
{ refreshInterval: POLLING.SLOW }
);

const {
data: mintData,
size: mintDataSize,
setSize: setMintDataSize,
isLoading: isMintDataLoading,
isLoading: isMintDataLoading
} = useSWRInfinite(
(index) => ["mintData", index],
async (key: any[]) => {
const page = key[1] ?? 0 + 1;
try {
const res = await fetch(
`${getAbsoluteUrl()}/api?page=${page}&perPage=${DEFAULT_SIZE}&mint`,
{
method: "GET",
headers: {
"x-vercel-protection-bypass": "4fphLjUJKQcW0kAbaFBGUNcAHb2WwnTX",
},
},
);
const jsonRes = await res.json();
return jsonRes;
} catch (e) {
console.error(e);
}
},
(key) => fetchData(`${getAbsoluteUrl()}/api?page=${(key[1] ?? 0) + 1}&perPage=${DEFAULT_SIZE}&mint`)
);

const {
data: burnData,
size: burnDataSize,
setSize: setBurnDataSize,
isLoading: isBurnDataLoading,
isLoading: isBurnDataLoading
} = useSWRInfinite(
(index) => ["burnData", index],
async (key: any[]) => {
const page = key[1] ?? 0 + 1;
try {
const res = await fetch(
`${getAbsoluteUrl()}/api?page=${page}&perPage=${DEFAULT_SIZE}&burn`,
{
method: "GET",
headers: {
"x-vercel-protection-bypass": "4fphLjUJKQcW0kAbaFBGUNcAHb2WwnTX",
},
},
);
const jsonRes = await res.json();
return jsonRes;
} catch (e) {
console.error(e);
}
},
(key) => fetchData(`${getAbsoluteUrl()}/api?page=${(key[1] ?? 0) + 1}&perPage=${DEFAULT_SIZE}&burn`)
);

const isAllDataLoadingMore =
Expand Down
59 changes: 21 additions & 38 deletions apps/honey/src/hooks/useHoneyEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import useSWRInfinite from "swr/infinite";

const DEFAULT_SIZE = 10;

const generateHoneyDataFetcher = (query: any, txnType?: string) => {
return async (key: any[]) => {
const page = key[1];
try {
const variables = { page: page * DEFAULT_SIZE, limit: DEFAULT_SIZE, txnType };

const res = await honeyClient.query({
query,
variables,
});
return res.data.honeyTxns || [];
} catch (error) {
console.error("Failed to fetch honey transactions:", error);
return [];
}
};
};

export const useHoneyEvents = () => {
const {
data: allData,
Expand All @@ -16,16 +34,7 @@ export const useHoneyEvents = () => {
isLoading: isAllDataLoadingMore,
} = useSWRInfinite(
(index) => ["allData", index],
(key: any[]) => {
const page = key[1];
return honeyClient
.query({
query: GetHoneyTxn,
variables: { page: page * DEFAULT_SIZE, limit: DEFAULT_SIZE },
})
.then((res: any) => res.data.honeyTxns)
.catch((e: any) => console.error(e));
},
generateHoneyDataFetcher(GetHoneyTxn),
{ refreshInterval: 1800000 },
);

Expand All @@ -36,20 +45,7 @@ export const useHoneyEvents = () => {
isLoading: isMintDataLoading,
} = useSWRInfinite(
(index) => ["mintData", index],
(key: any[]) => {
const page = key[1];
return honeyClient
.query({
query: GetHoneyTxnByType,
variables: {
page: page * DEFAULT_SIZE,
limit: DEFAULT_SIZE,
txnType: "Mint",
},
})
.then((res: any) => res.data.honeyTxns)
.catch((e: any) => console.error(e));
},
generateHoneyDataFetcher(GetHoneyTxnByType, "Mint"),
{ refreshInterval: 1800000 },
);

Expand All @@ -60,20 +56,7 @@ export const useHoneyEvents = () => {
isLoading: isRedemptionDataLoading,
} = useSWRInfinite(
(index) => ["redeemData", index],
(key: any[]) => {
const page = key[1];
return honeyClient
.query({
query: GetHoneyTxnByType,
variables: {
page: page * DEFAULT_SIZE,
limit: DEFAULT_SIZE,
txnType: "Redeem",
},
})
.then((res: any) => res.data.honeyTxns)
.catch((e: any) => console.error(e));
},
generateHoneyDataFetcher(GetHoneyTxnByType, "Redeem"),
{ refreshInterval: 1800000 },
);

Expand Down
107 changes: 26 additions & 81 deletions apps/honey/src/utils/graph-utils.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,56 @@
import { type HoneyEntry } from "~/app/type";

export function fullHourTimestamps(endUnix: number): number[] {
function generateTimestamps(endUnix: number, step: number): number[] {
const timestamps: number[] = [];
const currentTimeInseconds = Math.floor(Date.now() / 1000);
let current = currentTimeInseconds - (currentTimeInseconds % 3600);
const currentTimeInSeconds = Math.floor(Date.now() / 1000);
let current = currentTimeInSeconds - (currentTimeInSeconds % step);
while (current >= endUnix) {
timestamps.push(current);
current -= 3600;
current -= step;
}
return timestamps;
}

export function fullDayTimestamps(endUnix: number): number[] {
const timestamps: number[] = [];
const currentTimeInseconds = Math.floor(Date.now() / 1000);
let current = currentTimeInseconds - (currentTimeInseconds % 86400);
while (current >= endUnix) {
timestamps.push(current);
current -= 86400;
}
return timestamps;
}

export function fillVolumeDataByHour(list: HoneyEntry[], endUnix: number) {
function fillData(
list: HoneyEntry[],
endUnix: number,
step: number,
typename: "HoneyVolumeHourData" | "HoneyVolumeDayData" | "HoneySupplyHourData" | "HoneySupplyDayData"
) {
const data = [...list];
const timeList = fullHourTimestamps(endUnix + 3600);
const timeList = generateTimestamps(endUnix + step, step);
let lastAmount = list.length > 0 ? data[0]!.amount : "0";

timeList.forEach((time: number) => {
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time);
if (!entry) {
data.push({
//@ts-ignore
__typename: "HoneyVolumeHourData",
id: (time / 3600).toString(),
__typename: typename,
id: (time / step).toString(),
timestamp: time,
amount: "0",
amount: lastAmount,
});
} else {
lastAmount = entry.amount;
}
});

return data.sort((a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp);
}

export function fillVolumeDataByHour(list: HoneyEntry[], endUnix: number) {
return fillData(list, endUnix, 3600, "HoneyVolumeHourData");
}

export function fillVolumeDataByDay(list: HoneyEntry[], endUnix: number) {
const data = [...list];
const timeList = fullDayTimestamps(endUnix + 24 * 3600);
timeList.forEach((time: number) => {
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time);
if (!entry) {
data.push({
//@ts-ignore
__typename: "HoneyVolumeDayData",
id: (time / 86400).toString(),
timestamp: time,
amount: "0",
});
}
});
return data.sort((a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp);
return fillData(list, endUnix, 86400, "HoneyVolumeDayData");
}

export function fillSupplyDataByHour(list: HoneyEntry[], endUnix: number) {
if (list && list.length > 0) {
const data = [...list];
const timeList = fullHourTimestamps(endUnix + 3600);
let supply = data[0]!.amount;
timeList.forEach((time: number) => {
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time);
if (!entry) {
data.push({
//@ts-ignore
__typename: "HoneySupplyHourData",
id: (time / 3600).toString(),
timestamp: time,
amount: supply,
});
} else {
supply = entry.amount;
}
});
return data.sort(
(a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp,
);
}
return [];
return fillData(list, endUnix, 3600, "HoneySupplyHourData");
}

export function fillSupplyDataByDay(list: HoneyEntry[], endUnix: number) {
if (list && list.length > 0) {
const data = [...list];
const timeList = fullDayTimestamps(endUnix + 24 * 3600);
let supply = data[0]!.amount;
timeList.forEach((time: number) => {
const entry = data.find((entry: HoneyEntry) => entry.timestamp === time);
if (!entry) {
data.push({
//@ts-ignore
__typename: "HoneySupplyDayData",
id: (time / 86400).toString(),
timestamp: time,
amount: supply,
});
} else {
supply = entry.amount;
}
});
return data.sort(
(a: HoneyEntry, b: HoneyEntry) => a.timestamp - b.timestamp,
);
}
return [];
return fillData(list, endUnix, 86400, "HoneySupplyDayData");
}

0 comments on commit 2d8e76a

Please sign in to comment.