|
| 1 | +import { cache } from 'react' |
| 2 | +import { formatEther, getAddress } from 'ethers' |
| 3 | +import { getTokenContract } from './rpc' |
| 4 | + |
| 5 | +const zeroAddress = '0x0000000000000000000000000000000000000000' |
| 6 | + |
| 7 | +type DelegateEvent = { |
| 8 | + args?: Record<string, unknown> & unknown[] |
| 9 | +} |
| 10 | + |
| 11 | +export type DelegateRecord = { |
| 12 | + address: string |
| 13 | + delegatorCount: number |
| 14 | + tokenBalance: bigint |
| 15 | + votingPower: bigint |
| 16 | +} |
| 17 | + |
| 18 | +export type DelegateOverview = { |
| 19 | + delegates: DelegateRecord[] |
| 20 | + totalSupply: bigint |
| 21 | +} |
| 22 | + |
| 23 | +function getEventArg<T>(event: DelegateEvent, key: string) { |
| 24 | + return event.args?.[key] as T |
| 25 | +} |
| 26 | + |
| 27 | +export const getDelegateOverview = cache( |
| 28 | + async (): Promise<DelegateOverview> => { |
| 29 | + const token = getTokenContract() |
| 30 | + const delegateChangedEvents = await token.queryFilter('DelegateChanged') |
| 31 | + const currentDelegates = new Map<string, string>() |
| 32 | + |
| 33 | + for (const event of delegateChangedEvents as DelegateEvent[]) { |
| 34 | + const delegator = getAddress(getEventArg<string>(event, 'delegator')) |
| 35 | + const delegate = getAddress(getEventArg<string>(event, 'toDelegate')) |
| 36 | + currentDelegates.set(delegator, delegate) |
| 37 | + } |
| 38 | + |
| 39 | + const delegateToDelegators = new Map<string, Set<string>>() |
| 40 | + |
| 41 | + for (const [delegator, delegate] of currentDelegates.entries()) { |
| 42 | + if (delegate === zeroAddress) { |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + const delegators = delegateToDelegators.get(delegate) || new Set<string>() |
| 47 | + delegators.add(delegator) |
| 48 | + delegateToDelegators.set(delegate, delegators) |
| 49 | + } |
| 50 | + |
| 51 | + const [totalSupply, delegates] = await Promise.all([ |
| 52 | + token.totalSupply() as Promise<bigint>, |
| 53 | + Promise.all( |
| 54 | + Array.from(delegateToDelegators.entries()).map( |
| 55 | + async ([address, delegators]) => { |
| 56 | + const [votingPower, tokenBalance] = await Promise.all([ |
| 57 | + token.getVotes(address) as Promise<bigint>, |
| 58 | + token.balanceOf(address) as Promise<bigint>, |
| 59 | + ]) |
| 60 | + |
| 61 | + return { |
| 62 | + address, |
| 63 | + delegatorCount: delegators.size, |
| 64 | + tokenBalance, |
| 65 | + votingPower, |
| 66 | + } satisfies DelegateRecord |
| 67 | + } |
| 68 | + ) |
| 69 | + ), |
| 70 | + ]) |
| 71 | + |
| 72 | + delegates.sort((left, right) => { |
| 73 | + const votingPowerDelta = Number(right.votingPower - left.votingPower) |
| 74 | + |
| 75 | + if (votingPowerDelta !== 0) { |
| 76 | + return votingPowerDelta |
| 77 | + } |
| 78 | + |
| 79 | + return right.delegatorCount - left.delegatorCount |
| 80 | + }) |
| 81 | + |
| 82 | + return { |
| 83 | + delegates: delegates.filter( |
| 84 | + (delegate) => delegate.votingPower > 0n || delegate.delegatorCount > 0 |
| 85 | + ), |
| 86 | + totalSupply, |
| 87 | + } |
| 88 | + } |
| 89 | +) |
| 90 | + |
| 91 | +export function formatDelegatedShare(votingPower: bigint, totalSupply: bigint) { |
| 92 | + if (totalSupply === 0n) { |
| 93 | + return '0.0%' |
| 94 | + } |
| 95 | + |
| 96 | + return `${((Number(formatEther(votingPower)) / Number(formatEther(totalSupply))) * 100).toFixed(1)}%` |
| 97 | +} |
0 commit comments