From 2fb51c57e219a8cb54337431f48768607a3f7820 Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 20:10:18 -0500 Subject: [PATCH 1/8] added url for indexer --- pages/api/get/indexedData/index.ts | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 pages/api/get/indexedData/index.ts diff --git a/pages/api/get/indexedData/index.ts b/pages/api/get/indexedData/index.ts new file mode 100644 index 0000000..8f2fdaf --- /dev/null +++ b/pages/api/get/indexedData/index.ts @@ -0,0 +1,54 @@ +import { Get, Query, createHandler } from "next-api-decorators" + +const getIndexedData = async (timestamp: number) => { + const graphQLQuery = { + query: `query($timestamp: BigInt!) { + rewardsDeposits(first: 1000, orderBy: "timestamp", orderDirection: "asc", where: {timestamp_gt: $timestamp}) { + id + mintReferral + mintReferralReward + timestamp + zora + zoraReward + } + }`, + variables: { + timestamp, + }, + } + const options = { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(graphQLQuery), + } + const results = [] + const response = await fetch(process.env.INDEXER_URL, options) + + const data = await response.json() + const rewards: Array = data?.data?.rewardsDeposits + results.push(...rewards) + return results +} + +class IndexedData { + @Get() + async index(@Query("days") days: number) { + const now = new Date() + const millisecondsInADay = 24 * 60 * 60 * 1000 + const initialTimestamp = now.getTime() - millisecondsInADay * days + const results = await getIndexedData(Math.floor(initialTimestamp / 1000)) + let hasMoreResults = results.length > 0 + while (hasMoreResults) { + const lastTimestamp = results[results.length - 1].timestamp + // eslint-disable-next-line no-await-in-loop + const newResults = await getIndexedData(lastTimestamp) + hasMoreResults = newResults.length > 0 + results.push(...newResults) + } + return results + } +} + +export default createHandler(IndexedData) From 1aad27593784a2f06b5fa0662e4f0d0d8f9b233a Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 20:17:55 -0500 Subject: [PATCH 2/8] added url for indexer --- pages/api/get/indexedData/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/api/get/indexedData/index.ts b/pages/api/get/indexedData/index.ts index 8f2fdaf..6abc513 100644 --- a/pages/api/get/indexedData/index.ts +++ b/pages/api/get/indexedData/index.ts @@ -24,14 +24,13 @@ const getIndexedData = async (timestamp: number) => { body: JSON.stringify(graphQLQuery), } const results = [] - const response = await fetch(process.env.INDEXER_URL, options) + const response = await fetch("https://ponderer.quickindexer.xyz", options) const data = await response.json() const rewards: Array = data?.data?.rewardsDeposits results.push(...rewards) return results } - class IndexedData { @Get() async index(@Query("days") days: number) { @@ -47,6 +46,7 @@ class IndexedData { hasMoreResults = newResults.length > 0 results.push(...newResults) } + return results } } From fd5ff229f72b3c6791670e9df58cca41c38caeca Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 22:07:39 -0500 Subject: [PATCH 3/8] modified leaderboard code --- hooks/useLeaderboard.tsx | 2 +- lib/getProtocolRewardsLeaderboard.tsx | 20 ++++++++++---------- pages/api/get/indexedData/index.ts | 11 +++++++++-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/hooks/useLeaderboard.tsx b/hooks/useLeaderboard.tsx index 43fa4f6..1d209bd 100644 --- a/hooks/useLeaderboard.tsx +++ b/hooks/useLeaderboard.tsx @@ -26,7 +26,7 @@ const useLeaderboard = () => { nftsOwned: formatEther(item.totalCreatorReward), twitterHandle: "", zoraReward: formatEther(item.zoraReward), - ethereumReward: formatEther(item.ethereumReward), + ethereumReward: formatEther(item.mainnetReward), baseReward: formatEther(item.baseReward), optimismReward: formatEther(item.optimismReward), })) diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index a157aee..0bb3780 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -1,21 +1,21 @@ import { base, mainnet, optimism, zora } from "@wagmi/core/chains" -import { getRewardsDepositEvents } from "./getRewardsDepositEvents" +import axios from "axios" const getProtocolRewardsLeaderboard = async (numberOfDays) => { const chains = [ - { id: mainnet.id, key: "ethereumReward" }, + { id: mainnet.id, key: "mainnetReward" }, { id: optimism.id, key: "optimismReward" }, { id: base.id, key: "baseReward" }, { id: zora.id, key: "zoraReward" }, ] - const fetchPromises = chains.map((chain) => - getRewardsDepositEvents(chain.id, numberOfDays).then((data) => - data.map((item) => ({ ...item, rewardType: chain.key })), - ), - ) - - const allData = (await Promise.all(fetchPromises)).flat() + const response = await axios.get("http://localhost:3000/api/get/indexedData", { + params: { days: numberOfDays }, + }) + const allData = response.data.map((item) => ({ + ...item, + rewardType: `${item.chain}Reward`, + })) const defaultRewards = chains.reduce( (acc, chain) => { @@ -24,6 +24,7 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { }, { totalCreatorReward: BigInt(0) }, ) + let totalZoraFees = BigInt(0) let totalCreatorFees = BigInt(0) @@ -47,7 +48,6 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { return acc }, {}) - const leaderboardData = Object.entries(groupedData) .map(([creator, rewards]: any) => ({ creator, diff --git a/pages/api/get/indexedData/index.ts b/pages/api/get/indexedData/index.ts index 6abc513..e63e3c2 100644 --- a/pages/api/get/indexedData/index.ts +++ b/pages/api/get/indexedData/index.ts @@ -5,11 +5,18 @@ const getIndexedData = async (timestamp: number) => { query: `query($timestamp: BigInt!) { rewardsDeposits(first: 1000, orderBy: "timestamp", orderDirection: "asc", where: {timestamp_gt: $timestamp}) { id + creator + createReferral mintReferral - mintReferralReward - timestamp + firstMinter zora + creatorReward + createReferralReward + firstMinterReward + mintReferralReward zoraReward + timestamp + chain } }`, variables: { From e7c8d3942740d88a89c1ef60beca88338e536ecc Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 22:08:44 -0500 Subject: [PATCH 4/8] changing axios api url --- lib/getProtocolRewardsLeaderboard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index 0bb3780..f0a1029 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -9,7 +9,7 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { { id: zora.id, key: "zoraReward" }, ] - const response = await axios.get("http://localhost:3000/api/get/indexedData", { + const response = await axios.get("/api/get/indexedData", { params: { days: numberOfDays }, }) const allData = response.data.map((item) => ({ From 39abaab77c98bea03e836669adac07292f265e37 Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 22:14:55 -0500 Subject: [PATCH 5/8] trying a specific named endpoint --- lib/getProtocolRewardsLeaderboard.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index f0a1029..61e3853 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -9,9 +9,12 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { { id: zora.id, key: "zoraReward" }, ] - const response = await axios.get("/api/get/indexedData", { - params: { days: numberOfDays }, - }) + const response = await axios.get( + "https://zorarewards-git-sameer-useindexer-sweetmantech.vercel.app//api/get/indexedData", + { + params: { days: numberOfDays }, + }, + ) const allData = response.data.map((item) => ({ ...item, rewardType: `${item.chain}Reward`, From b40577eda2969aa77b00a5ebd953d6b8f88490fa Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 22:15:18 -0500 Subject: [PATCH 6/8] trying a specific named endpoint --- lib/getProtocolRewardsLeaderboard.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index 61e3853..86acefb 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -10,7 +10,7 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { ] const response = await axios.get( - "https://zorarewards-git-sameer-useindexer-sweetmantech.vercel.app//api/get/indexedData", + "https://zorarewards-git-sameer-useindexer-sweetmantech.vercel.app/api/get/indexedData", { params: { days: numberOfDays }, }, From bcf34c487b0ec2f1b902e527bfa5ea060e88f4d1 Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Tue, 16 Jan 2024 22:24:01 -0500 Subject: [PATCH 7/8] trying a specific named endpoint --- lib/getProtocolRewardsLeaderboard.tsx | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index 86acefb..f0a1029 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -9,12 +9,9 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { { id: zora.id, key: "zoraReward" }, ] - const response = await axios.get( - "https://zorarewards-git-sameer-useindexer-sweetmantech.vercel.app/api/get/indexedData", - { - params: { days: numberOfDays }, - }, - ) + const response = await axios.get("/api/get/indexedData", { + params: { days: numberOfDays }, + }) const allData = response.data.map((item) => ({ ...item, rewardType: `${item.chain}Reward`, From 52b191569f82597b6dab71f549d40494d9d53ad1 Mon Sep 17 00:00:00 2001 From: sameer rasa Date: Wed, 17 Jan 2024 08:32:01 -0500 Subject: [PATCH 8/8] refactoring code --- lib/getIndexedData.ts | 57 ++++++++++++++++++++++++++ lib/getProtocolRewardsLeaderboard.tsx | 8 ++-- pages/api/get/indexedData/index.ts | 59 ++++----------------------- 3 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 lib/getIndexedData.ts diff --git a/lib/getIndexedData.ts b/lib/getIndexedData.ts new file mode 100644 index 0000000..b31b0a8 --- /dev/null +++ b/lib/getIndexedData.ts @@ -0,0 +1,57 @@ +const getIndexedData = async (timestamp: number) => { + const graphQLQuery = { + query: `query($timestamp: BigInt!) { + rewardsDeposits(first: 1000, orderBy: "timestamp", orderDirection: "asc", where: {timestamp_gt: $timestamp}) { + id + creator + createReferral + mintReferral + firstMinter + zora + creatorReward + createReferralReward + firstMinterReward + mintReferralReward + zoraReward + timestamp + chain + } + }`, + variables: { + timestamp, + }, + } + const options = { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(graphQLQuery), + } + const results = [] + const response = await fetch("https://ponderer.quickindexer.xyz", options) + + const data = await response.json() + const rewards: Array = data?.data?.rewardsDeposits + results.push(...rewards) + return results +} + +const getAllIndexedData = async (days: number) => { + const now = new Date() + const millisecondsInADay = 24 * 60 * 60 * 1000 + const initialTimestamp = now.getTime() - millisecondsInADay * days + const results = await getIndexedData(Math.floor(initialTimestamp / 1000)) + let hasMoreResults = results.length > 0 + while (hasMoreResults) { + const lastTimestamp = results[results.length - 1].timestamp + // eslint-disable-next-line no-await-in-loop + const newResults = await getIndexedData(lastTimestamp) + hasMoreResults = newResults.length > 0 + results.push(...newResults) + } + + return results +} + +export default getAllIndexedData diff --git a/lib/getProtocolRewardsLeaderboard.tsx b/lib/getProtocolRewardsLeaderboard.tsx index f0a1029..69ce47a 100644 --- a/lib/getProtocolRewardsLeaderboard.tsx +++ b/lib/getProtocolRewardsLeaderboard.tsx @@ -1,5 +1,5 @@ import { base, mainnet, optimism, zora } from "@wagmi/core/chains" -import axios from "axios" +import getAllIndexedData from "./getIndexedData" const getProtocolRewardsLeaderboard = async (numberOfDays) => { const chains = [ @@ -9,10 +9,8 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => { { id: zora.id, key: "zoraReward" }, ] - const response = await axios.get("/api/get/indexedData", { - params: { days: numberOfDays }, - }) - const allData = response.data.map((item) => ({ + const response = await getAllIndexedData(numberOfDays) + const allData = response.map((item) => ({ ...item, rewardType: `${item.chain}Reward`, })) diff --git a/pages/api/get/indexedData/index.ts b/pages/api/get/indexedData/index.ts index e63e3c2..c65678d 100644 --- a/pages/api/get/indexedData/index.ts +++ b/pages/api/get/indexedData/index.ts @@ -1,60 +1,17 @@ +import { error } from "console" import { Get, Query, createHandler } from "next-api-decorators" +import getAllIndexedData from "../../../../lib/getIndexedData" -const getIndexedData = async (timestamp: number) => { - const graphQLQuery = { - query: `query($timestamp: BigInt!) { - rewardsDeposits(first: 1000, orderBy: "timestamp", orderDirection: "asc", where: {timestamp_gt: $timestamp}) { - id - creator - createReferral - mintReferral - firstMinter - zora - creatorReward - createReferralReward - firstMinterReward - mintReferralReward - zoraReward - timestamp - chain - } - }`, - variables: { - timestamp, - }, - } - const options = { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(graphQLQuery), - } - const results = [] - const response = await fetch("https://ponderer.quickindexer.xyz", options) - - const data = await response.json() - const rewards: Array = data?.data?.rewardsDeposits - results.push(...rewards) - return results -} class IndexedData { @Get() async index(@Query("days") days: number) { - const now = new Date() - const millisecondsInADay = 24 * 60 * 60 * 1000 - const initialTimestamp = now.getTime() - millisecondsInADay * days - const results = await getIndexedData(Math.floor(initialTimestamp / 1000)) - let hasMoreResults = results.length > 0 - while (hasMoreResults) { - const lastTimestamp = results[results.length - 1].timestamp - // eslint-disable-next-line no-await-in-loop - const newResults = await getIndexedData(lastTimestamp) - hasMoreResults = newResults.length > 0 - results.push(...newResults) + try { + const results = await getAllIndexedData(days) + return results + } catch (e) { + error(e) + return e } - - return results } }