Skip to content

Commit

Permalink
Merge pull request #13 from SweetmanTech/sameer/useIndexer
Browse files Browse the repository at this point in the history
added url for indexer
  • Loading branch information
crazzywizard authored Jan 17, 2024
2 parents 7dabf7b + 52b1915 commit 0f789f0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hooks/useLeaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}))
Expand Down
57 changes: 57 additions & 0 deletions lib/getIndexedData.ts
Original file line number Diff line number Diff line change
@@ -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<any> = 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
18 changes: 8 additions & 10 deletions lib/getProtocolRewardsLeaderboard.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { base, mainnet, optimism, zora } from "@wagmi/core/chains"
import { getRewardsDepositEvents } from "./getRewardsDepositEvents"
import getAllIndexedData from "./getIndexedData"

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 getAllIndexedData(numberOfDays)
const allData = response.map((item) => ({
...item,
rewardType: `${item.chain}Reward`,
}))

const defaultRewards = chains.reduce(
(acc, chain) => {
Expand All @@ -24,6 +22,7 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => {
},
{ totalCreatorReward: BigInt(0) },
)

let totalZoraFees = BigInt(0)
let totalCreatorFees = BigInt(0)

Expand All @@ -47,7 +46,6 @@ const getProtocolRewardsLeaderboard = async (numberOfDays) => {

return acc
}, {})

const leaderboardData = Object.entries(groupedData)
.map(([creator, rewards]: any) => ({
creator,
Expand Down
18 changes: 18 additions & 0 deletions pages/api/get/indexedData/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { error } from "console"
import { Get, Query, createHandler } from "next-api-decorators"
import getAllIndexedData from "../../../../lib/getIndexedData"

class IndexedData {
@Get()
async index(@Query("days") days: number) {
try {
const results = await getAllIndexedData(days)
return results
} catch (e) {
error(e)
return e
}
}
}

export default createHandler(IndexedData)

0 comments on commit 0f789f0

Please sign in to comment.