-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve celoscan utilities
- Loading branch information
Showing
10 changed files
with
259 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useMemo } from 'react'; | ||
import { SpinnerWithLabel } from 'src/components/animation/Spinner'; | ||
import { Identicon } from 'src/components/icons/Identicon'; | ||
import { Collapse } from 'src/components/menus/Collapse'; | ||
import { MergedProposalData } from 'src/features/governance/useGovernanceProposals'; | ||
import { useProposalUpvoters } from 'src/features/governance/useProposalUpvoters'; | ||
import { useValidatorGroups } from 'src/features/validators/useValidatorGroups'; | ||
import { cleanGroupName } from 'src/features/validators/utils'; | ||
import { shortenAddress } from 'src/utils/addresses'; | ||
import { objKeys } from 'src/utils/objects'; | ||
|
||
export function ProposalUpvotersTable({ propData }: { propData: MergedProposalData }) { | ||
return ( | ||
<div className="hidden md:block"> | ||
<Collapse | ||
button={<h2 className="text-left font-serif text-2xl">Upvoters</h2>} | ||
buttonClasses="w-full" | ||
defaultOpen={true} | ||
> | ||
<UpvoterTableContent propData={propData} /> | ||
</Collapse> | ||
</div> | ||
); | ||
} | ||
|
||
function UpvoterTableContent({ propData }: { propData: MergedProposalData }) { | ||
const { isLoading, upvoters } = useProposalUpvoters(propData.id); | ||
const { addressToGroup } = useValidatorGroups(); | ||
|
||
const tableData = useMemo(() => { | ||
if (!upvoters) return []; | ||
return objKeys(upvoters).map((address) => { | ||
const groupName = cleanGroupName(addressToGroup?.[address]?.name || ''); | ||
const label = groupName || shortenAddress(address); | ||
return { label, address }; | ||
}); | ||
}, [upvoters, addressToGroup]); | ||
|
||
if (isLoading) { | ||
return ( | ||
<SpinnerWithLabel size="md" className="py-6"> | ||
Loading upvoters | ||
</SpinnerWithLabel> | ||
); | ||
} | ||
|
||
if (!tableData.length) { | ||
return <div className="py-6 text-center text-sm text-gray-600">No upvoters found</div>; | ||
} | ||
|
||
return ( | ||
<table> | ||
<tbody> | ||
{tableData.map((row) => ( | ||
<tr key={row.address}> | ||
<td className="py-2"> | ||
<Identicon address={row.address} size={20} /> | ||
</td> | ||
<td className="px-4 py-2 text-sm">{row.label}</td> | ||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { governanceABI } from '@celo/abis'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { useToastError } from 'src/components/notifications/useToastError'; | ||
import { Addresses } from 'src/config/contracts'; | ||
import { queryCeloscanLogs } from 'src/features/explorers/celoscan'; | ||
import { TransactionLog } from 'src/features/explorers/types'; | ||
import { isValidAddress } from 'src/utils/addresses'; | ||
import { logger } from 'src/utils/logger'; | ||
import { objFilter } from 'src/utils/objects'; | ||
import { decodeEventLog, encodeEventTopics } from 'viem'; | ||
|
||
export function useProposalUpvoters(id?: number) { | ||
const { isLoading, isError, error, data } = useQuery({ | ||
queryKey: ['useProposalUpvoters', id], | ||
queryFn: () => { | ||
if (!id) return null; | ||
logger.debug(`Fetching proposals upvoters for ${id}`); | ||
return fetchProposalUpvoters(id); | ||
}, | ||
gcTime: Infinity, | ||
staleTime: 60 * 60 * 1000, // 1 hour | ||
}); | ||
|
||
useToastError(error, 'Error fetching proposals upvoters'); | ||
|
||
return { | ||
isLoading, | ||
isError, | ||
upvoters: data || undefined, | ||
}; | ||
} | ||
|
||
async function fetchProposalUpvoters(id: number): Promise<AddressTo<bigint>> { | ||
// Get encoded topics | ||
const upvoteTopics = encodeEventTopics({ | ||
abi: governanceABI, | ||
eventName: 'ProposalUpvoted', | ||
args: { proposalId: BigInt(id) }, | ||
}); | ||
|
||
// Prep query URLs | ||
const upvoteParams = `topic0=${upvoteTopics[0]}&topic1=${upvoteTopics[1]}&topic0_1_opr=and`; | ||
const upvoteEvents = await queryCeloscanLogs(Addresses.Governance, upvoteParams); | ||
|
||
// Reduce logs to a map of voters to upvotes | ||
const voterToUpvotes: AddressTo<bigint> = {}; | ||
reduceLogs(voterToUpvotes, upvoteEvents); | ||
|
||
// Filter out stakers who have already revoked all votes | ||
return objFilter(voterToUpvotes, (_, votes): votes is bigint => votes > 0n); | ||
} | ||
|
||
function reduceLogs(voterToUpvotes: AddressTo<bigint>, logs: TransactionLog[]) { | ||
for (const log of logs) { | ||
try { | ||
if (!log.topics || log.topics.length < 3) continue; | ||
const { eventName, args } = decodeEventLog({ | ||
abi: governanceABI, | ||
data: log.data, | ||
// @ts-ignore https://github.com/wevm/viem/issues/381 | ||
topics: log.topics, | ||
strict: false, | ||
}); | ||
|
||
if (eventName !== 'ProposalUpvoted') continue; | ||
|
||
const { account, upvotes } = args; | ||
if (!account || !isValidAddress(account) || !upvotes) continue; | ||
|
||
voterToUpvotes[account] ||= 0n; | ||
voterToUpvotes[account] += upvotes; | ||
} catch (error) { | ||
logger.warn('Error decoding event log', error, log); | ||
} | ||
} | ||
} |
Oops, something went wrong.