Skip to content

Commit

Permalink
removed duplicate query
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Sep 18, 2024
1 parent fc0fd45 commit e54b440
Showing 1 changed file with 0 additions and 171 deletions.
171 changes: 0 additions & 171 deletions packages/state/query/queries/contracts/DaoRewardsDistributor.extra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,177 +328,6 @@ export const fetchPendingDaoRewards = async (
}
}

/**
* List all pending rewards.
*/
export const listAllDaoRewardDistributorPendingRewards = async (
queryClient: QueryClient,
{
chainId,
address,
recipient,
}: {
chainId: string
address: string
recipient: string
}
): Promise<PendingRewardsResponse> => {
const rewards: DistributionPendingRewards[] = []

const limit = 30
while (true) {
const page = (
await queryClient.fetchQuery(
daoRewardsDistributorQueries.pendingRewards({
chainId,
contractAddress: address,
args: {
address: recipient,
limit,
startAfter: rewards[rewards.length - 1]?.id,
},
})
)
)?.pending_rewards

if (!page?.length) {
break
}

rewards.push(...page)

// If we have less than the limit of items, we've exhausted them.
if (page.length < limit) {
break
}
}

return {
pending_rewards: rewards,
}
}

/**
* Fetch all DAO reward distributions and pending rewards for an account.
*/
export const fetchPendingDaoRewards = async (
queryClient: QueryClient,
{
chainId,
daoAddress,
recipient,
}: {
chainId: string
daoAddress: string
recipient: string
}
): Promise<PendingDaoRewards> => {
// Active distributors for a DAO.
const distributors = (
await queryClient.fetchQuery(
daoDaoCoreQueries.listAllItems(queryClient, {
chainId,
contractAddress: daoAddress,
args: {
prefix: getRewardDistributorStorageItemKey(''),
},
})
)
).map(([, value]) => value)

// Fetch all distributions with pending rewards from all distributors.
const distributions = (
await Promise.all(
distributors.map(
async (address): Promise<PendingDaoRewards['distributions']> => {
const [distributions, { pending_rewards }] = await Promise.all([
queryClient.fetchQuery(
daoRewardsDistributorExtraQueries.distributions(queryClient, {
chainId,
address,
})
),
queryClient.fetchQuery(
daoRewardsDistributorExtraQueries.listAllPendingRewards(
queryClient,
{
chainId,
address,
recipient,
}
)
),
])

return distributions.map((distribution) => ({
distribution,
rewards: Number(
pending_rewards.find((pending) => pending.id === distribution.id)
?.pending_rewards || '0'
),
}))
}
)
)
).flat()

const uniqueTokenSources = uniq(
distributions.map(({ distribution }) =>
serializeTokenSource(distribution.token)
)
)

const rewards = await Promise.all(
uniqueTokenSources.map(
async (source): Promise<GenericTokenBalanceAndValue> => {
// These should already be cached from the distributions query.
const {
token,
usdPrice = 0,
timestamp = new Date(),
} = await queryClient
.fetchQuery(
tokenQueries.usdPrice(queryClient, deserializeTokenSource(source))
)
// If failed to load price, just load token info with no price.
.catch(
async (): Promise<GenericTokenWithUsdPrice> => ({
token: await queryClient.fetchQuery(
tokenQueries.info(queryClient, deserializeTokenSource(source))
),
})
)

// Sum all pending rewards for this token.
const allPendingRewards = distributions.reduce(
(acc, { distribution, rewards }) =>
acc + (tokensEqual(token, distribution.token) ? rewards : 0),
0
)

const balance = convertMicroDenomToDenomWithDecimals(
allPendingRewards,
token.decimals
)

const usdValue = balance * usdPrice

return {
token,
balance,
usdValue,
timestamp,
}
}
)
)

return {
distributions,
rewards,
}
}

export const daoRewardsDistributorExtraQueries = {
/**
* Fetch a reward distribution.
Expand Down

0 comments on commit e54b440

Please sign in to comment.