From 2420a8ec60b496ff8b1552fd36c43c9a4652b045 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Mon, 4 Dec 2023 10:53:34 -0800 Subject: [PATCH] Added query to dao-voting-cw721-staked to map staked NFT token ID to staker address. --- .../contract/voting/daoVotingCw721Staked.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/data/formulas/contract/voting/daoVotingCw721Staked.ts b/src/data/formulas/contract/voting/daoVotingCw721Staked.ts index 95dfb9c8..79831843 100644 --- a/src/data/formulas/contract/voting/daoVotingCw721Staked.ts +++ b/src/data/formulas/contract/voting/daoVotingCw721Staked.ts @@ -178,3 +178,49 @@ export const topStakers: ContractFormula< return stakers }, } + +// Map NFT token ID to staker. +export const ownersOfStakedNfts: ContractFormula> = { + filter: { + codeIdsKeys: CODE_IDS_KEYS, + }, + compute: async (env) => { + const { contractAddress, getTransformationMap } = env + + const stakedCountMap = + (await getTransformationMap( + contractAddress, + 'stakedCount' + )) ?? {} + const stakers = Object.entries(stakedCountMap) + // Remove zero counts. + .filter(([, stakedCount]) => Number(stakedCount) > 0) + .map(([address]) => address) + + const stakedNftsPerStaker = await Promise.all( + stakers.map(async (address) => ({ + address, + tokenIds: await stakedNfts.compute({ + ...env, + args: { + address, + }, + }), + })) + ) + + return stakedNftsPerStaker.reduce( + (acc, { address, tokenIds }) => ({ + ...acc, + ...tokenIds.reduce( + (acc, tokenId) => ({ + ...acc, + [tokenId]: address, + }), + {} as Record + ), + }), + {} as Record + ) + }, +}