diff --git a/.changeset/governance-spell-deployments.md b/.changeset/governance-spell-deployments.md new file mode 100644 index 0000000..51495a1 --- /dev/null +++ b/.changeset/governance-spell-deployments.md @@ -0,0 +1,5 @@ +--- +"@reserve-protocol/sdk": patch +--- + +Decode proposals to every `GovernanceSpell_09_18_2026` deployment (the 2% and 1% threshold redeployments on Ethereum, Base and BSC) and OPEN's `GovernanceSpell_10_21_2026`, so a preview names them without the explorer ABI fallback. diff --git a/.changeset/vote-token-decimals.md b/.changeset/vote-token-decimals.md new file mode 100644 index 0000000..0fd3a5b --- /dev/null +++ b/.changeset/vote-token-decimals.md @@ -0,0 +1,11 @@ +--- +"@reserve-protocol/sdk": patch +--- + +Index DTF governance amounts now use the decimals of the vote token. Before, the SDK formatted them with 18 decimals, so a DAO with a 6-decimal vote token (for example a vote-lock vault on USDC) showed a quorum of 0.00000000000003 instead of 0.03. Raw values did not change. + +- Proposals, proposal lists and voting snapshots get a `voteTokenDecimals` field from the subgraph. `getProposalState` uses it for the threshold amounts and uses 18 when it is missing. +- `getVoterState`, `getProposalVoterState`, `getOptimisticProposalVoterState`, `getOptimisticVotes`, `getPastOptimisticVotes` and `getOptimisticProposalContext` read `decimals()` in the same multicall as their other reads. +- `getDelegates` reads the vote token decimals from the subgraph. +- The vote-lock state formats `optimisticVotingPower` with the vault decimals. +- `getProposalVotes`, `getProposerState` and `getOptimisticProposalContext` accept an optional `voteTokenDecimals`. Without it, `getProposalVotes` and `getProposerState` with a `timepoint` make one more `decimals()` call. diff --git a/apps/docs/src/pages/sdk/governance.mdx b/apps/docs/src/pages/sdk/governance.mdx index d22232c..4214c87 100644 --- a/apps/docs/src/pages/sdk/governance.mdx +++ b/apps/docs/src/pages/sdk/governance.mdx @@ -27,6 +27,8 @@ const proposal = await sdk.index.getProposal({ Proposal IDs can be extremely long decimal strings. Keep them as strings in routes and UI state. +Vote amounts (votes, quorum, thresholds, voting power, delegated votes) use the decimals of the vote token, not a fixed 18. A vote-lock vault on USDC has 6 decimals. Proposal reads set `voteTokenDecimals` on the proposal. `getProposalVotes` and `getProposerState` with a `timepoint` read the token `decimals()` in one more call; pass `voteTokenDecimals` to skip it. + ## Voting And Account State | Method | Use | diff --git a/packages/sdk/src/index-dtf/governance/contract-map.ts b/packages/sdk/src/index-dtf/governance/contract-map.ts index c805f61..8d169fc 100644 --- a/packages/sdk/src/index-dtf/governance/contract-map.ts +++ b/packages/sdk/src/index-dtf/governance/contract-map.ts @@ -108,6 +108,33 @@ const EXTRA_PROPOSAL_CONTRACTS: readonly { abi: governanceSpell09182026Abi, addresses: INDEX_DTF_GOVERNANCE_SPELL_09_18_2026_ADDRESS, }, + // Redeployments of the same spell with a 2% and then a 1% proposal threshold; proposals can target any of them. + { + contract: "Governance Spell 09_18_2026", + abi: governanceSpell09182026Abi, + addresses: { + 1: "0xC72fD080F4CF5Ce6a3314192888419f45c562d87", + 8453: "0x24a4B37F9c40fB0E80ec436Df2e9989FBAFa8bB7", + 56: "0xE19ae8D1f3FFf987aaEaa65248BAB3A0d1FDC809", + }, + }, + { + contract: "Governance Spell 09_18_2026", + abi: governanceSpell09182026Abi, + addresses: { + 1: "0xeE6066Df6E27F142b921FD38e6766a9D6aF35c2f", + 8453: "0x971c890ACb9EeB084f292996Be667bB9A2889AE9", + 56: "0x281EDD83b9b3C7cD05E260EF7E17A9734731A52B", + }, + }, + // OPEN's migration spell: other governance parameters, the same functions. + { + contract: "Governance Spell 10_21_2026", + abi: governanceSpell09182026Abi, + addresses: { + 1: "0xaf029ad44Cc44fB2AefFF84029dd0c29D5Ea8D87", + }, + }, { contract: "Upgrade Spell 6.0.0", abi: upgradeSpell600Abi, diff --git a/packages/sdk/src/index-dtf/governance/delegates.test.ts b/packages/sdk/src/index-dtf/governance/delegates.test.ts index b8e9ed1..8f21313 100644 --- a/packages/sdk/src/index-dtf/governance/delegates.test.ts +++ b/packages/sdk/src/index-dtf/governance/delegates.test.ts @@ -15,6 +15,7 @@ describe("Index DTF governance delegates", () => { currentOptimisticDelegates: "2", token: { totalSupply: "10000000000000000000", + decimals: 18, }, delegates: [ { @@ -153,4 +154,50 @@ describe("Index DTF governance delegates", () => { }, }); }); + + it("formats delegated votes in the vote token decimals", async () => { + const client = { + subgraph: { + queryIndex: vi.fn(async () => ({ + stakingToken: { + id: "0x0000000000000000000000000000000000000001", + totalDelegates: "1", + currentDelegates: "1", + totalOptimisticDelegates: "1", + currentOptimisticDelegates: "1", + token: { + totalSupply: "1000000", + decimals: 6, + }, + delegates: [ + { + address: "0x0000000000000000000000000000000000000002", + delegatedVotesRaw: "300000", + optimisticDelegatedVotesRaw: "200000", + numberVotes: 1, + numberOptimisticVotes: 1, + hasBeenStandardDelegate: true, + hasBeenOptimisticDelegate: true, + tokenHoldersRepresentedAmount: 1, + optimisticTokenHoldersRepresentedAmount: 1, + }, + ], + }, + })), + }, + } as unknown as DtfClient; + + const delegates = await getDelegates(client, { + chainId: 8453, + stToken: "0x0000000000000000000000000000000000000001", + }); + + expect(delegates.delegates[0]).toMatchObject({ + delegatedVotes: { raw: 300000n, formatted: "0.3" }, + optimisticDelegatedVotes: { raw: 200000n, formatted: "0.2" }, + weightedVotes: 30, + optimisticWeightedVotes: 20, + }); + expect(delegates.voteSupply).toEqual({ raw: 1000000n, formatted: "1" }); + }); }); diff --git a/packages/sdk/src/index-dtf/governance/delegates.ts b/packages/sdk/src/index-dtf/governance/delegates.ts index d58155f..51e6d3f 100644 --- a/packages/sdk/src/index-dtf/governance/delegates.ts +++ b/packages/sdk/src/index-dtf/governance/delegates.ts @@ -32,13 +32,14 @@ export async function getDelegates(client: DtfClient, params: GetIndexDtfDelegat } const voteSupply = BigInt(stakingToken.token.totalSupply); + const decimals = stakingToken.token.decimals; const delegates: IndexDtfDelegate[] = []; const normalDelegates: IndexDtfDelegate[] = []; const optimisticDelegates: IndexDtfDelegate[] = []; for (const delegate of stakingToken.delegates) { - const delegatedVotes = mapAmount(delegate.delegatedVotesRaw); - const optimisticDelegatedVotes = mapAmount(delegate.optimisticDelegatedVotesRaw); + const delegatedVotes = mapAmount(delegate.delegatedVotesRaw, decimals); + const optimisticDelegatedVotes = mapAmount(delegate.optimisticDelegatedVotesRaw, decimals); const mappedDelegate = { address: getAddress(delegate.address), delegatedVotes, @@ -74,7 +75,7 @@ export async function getDelegates(client: DtfClient, params: GetIndexDtfDelegat currentNormalDelegates: Number(stakingToken.currentDelegates), totalOptimisticDelegates: Number(stakingToken.totalOptimisticDelegates), currentOptimisticDelegates: Number(stakingToken.currentOptimisticDelegates), - voteSupply: mapAmount(voteSupply), + voteSupply: mapAmount(voteSupply, decimals), }; } diff --git a/packages/sdk/src/index-dtf/governance/mapper.ts b/packages/sdk/src/index-dtf/governance/mapper.ts index b78bd6e..d5ea809 100644 --- a/packages/sdk/src/index-dtf/governance/mapper.ts +++ b/packages/sdk/src/index-dtf/governance/mapper.ts @@ -101,6 +101,7 @@ export function mapIndexDtfProposalSummary( creationBlock: Number(proposal.creationBlock), voteStart: Number(proposal.voteStart), voteEnd: Number(proposal.voteEnd), + voteTokenDecimals: decimals, quorumVotes: mapAmount(proposal.quorumVotes, decimals), forWeightedVotes: mapAmount(proposal.forWeightedVotes, decimals), againstWeightedVotes: mapAmount(proposal.againstWeightedVotes, decimals), @@ -170,6 +171,7 @@ export function mapIndexDtfProposalVotingSnapshot( ...(optimistic === undefined ? {} : { optimistic }), voteStart: Number(proposal.voteStart), voteEnd: Number(proposal.voteEnd), + voteTokenDecimals: decimals, quorumVotes: mapAmount(proposal.quorumVotes, decimals), forWeightedVotes: mapAmount(proposal.forWeightedVotes, decimals), againstWeightedVotes: mapAmount(proposal.againstWeightedVotes, decimals), diff --git a/packages/sdk/src/index-dtf/governance/optimistic.test.ts b/packages/sdk/src/index-dtf/governance/optimistic.test.ts index dccbeaa..4d1c4c8 100644 --- a/packages/sdk/src/index-dtf/governance/optimistic.test.ts +++ b/packages/sdk/src/index-dtf/governance/optimistic.test.ts @@ -14,8 +14,11 @@ const MAX_UINT256 = (1n << 256n) - 1n; describe("Index DTF optimistic governance", () => { it("reads optimistic proposal veto context", async () => { - const multicall = vi.fn(async () => [100n, "0x0000000000000000000000000000000000000002"]); - const readContract = vi.fn().mockResolvedValueOnce(true).mockResolvedValueOnce(100000000000000000000n); + const multicall = vi + .fn() + .mockResolvedValueOnce([100n, "0x0000000000000000000000000000000000000002"]) + .mockResolvedValueOnce([100000000000000000000n, 18]); + const readContract = vi.fn().mockResolvedValueOnce(true); const client = { viem: { getPublicClient: vi.fn(() => ({ multicall })), @@ -60,6 +63,38 @@ describe("Index DTF optimistic governance", () => { ], }), ); + expect(multicall).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + contracts: [ + expect.objectContaining({ functionName: "getPastTotalSupply", args: [100n] }), + expect.objectContaining({ address: "0x0000000000000000000000000000000000000002", functionName: "decimals" }), + ], + }), + ); + }); + + it("formats optimistic proposal veto context in the vote token decimals", async () => { + const multicall = vi.fn().mockResolvedValueOnce([1000000n, 6]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + readContract: vi.fn(), + }, + } as unknown as DtfClient; + + const context = await getOptimisticProposalContext(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + proposalId: "42", + isOptimistic: true, + voteToken: "0x0000000000000000000000000000000000000002", + snapshot: 100n, + vetoThreshold: 50000000000000000n, + }); + + expect(context?.snapshotSupply).toEqual({ raw: 1000000n, formatted: "1" }); + expect(context?.vetoThresholdVotes).toEqual({ raw: 50000n, formatted: "0.05" }); }); it("does not infer proposal-specific veto threshold from current governor params", async () => { @@ -100,6 +135,7 @@ describe("Index DTF optimistic governance", () => { voteToken: "0x0000000000000000000000000000000000000002", snapshot: 100n, vetoThreshold: 200000000000000000n, + voteTokenDecimals: 18, }); expect(getPublicClient).not.toHaveBeenCalled(); @@ -157,6 +193,7 @@ describe("Index DTF optimistic governance", () => { voteToken: "0x0000000000000000000000000000000000000002", snapshot: 100n, vetoThreshold: 1n, + voteTokenDecimals: 18, }); expect(context?.vetoThresholdVotes).toEqual({ @@ -615,14 +652,13 @@ describe("Index DTF optimistic governance", () => { it("reads current and past optimistic voting power from the vote token", async () => { const getBlock = vi.fn(async () => ({ timestamp: 1_000n })); - const readContract = vi + const multicall = vi .fn() - .mockResolvedValueOnce(3000000000000000000n) - .mockResolvedValueOnce(2000000000000000000n); + .mockResolvedValueOnce([3000000000000000000n, 18]) + .mockResolvedValueOnce([2000000000000000000n, 18]); const client = { viem: { - getPublicClient: vi.fn(() => ({ getBlock })), - readContract, + getPublicClient: vi.fn(() => ({ getBlock, multicall })), }, } as unknown as DtfClient; const params = { @@ -639,12 +675,34 @@ describe("Index DTF optimistic governance", () => { raw: 2000000000000000000n, formatted: "2", }); - expect(readContract).toHaveBeenNthCalledWith( + expect(multicall).toHaveBeenNthCalledWith( 2, expect.objectContaining({ - functionName: "getPastOptimisticVotes", - args: ["0x0000000000000000000000000000000000000002", 999n], + contracts: [ + expect.objectContaining({ + functionName: "getPastOptimisticVotes", + args: ["0x0000000000000000000000000000000000000002", 999n], + }), + expect.objectContaining({ functionName: "decimals" }), + ], }), ); }); + + it("formats optimistic votes in the vote token decimals", async () => { + const multicall = vi.fn().mockResolvedValueOnce([300000n, 6]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + }, + } as unknown as DtfClient; + + await expect( + getOptimisticVotes(client, { + chainId: 8453, + voteToken: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + }), + ).resolves.toEqual({ raw: 300000n, formatted: "0.3" }); + }); }); diff --git a/packages/sdk/src/index-dtf/governance/optimistic.ts b/packages/sdk/src/index-dtf/governance/optimistic.ts index 06d7b2c..3ff9987 100644 --- a/packages/sdk/src/index-dtf/governance/optimistic.ts +++ b/packages/sdk/src/index-dtf/governance/optimistic.ts @@ -1,4 +1,4 @@ -import { getAddress, type Address, type Hex } from "viem"; +import { erc20Abi, getAddress, type Address, type Hex } from "viem"; import type { DtfClient } from "@/client"; import type { Amount } from "@/types/common"; @@ -91,14 +91,14 @@ export async function getOptimisticProposalContext( throw new Error("Missing optimistic proposal context values"); } - const snapshotSupply = await client.viem.readContract({ - chainId: params.chainId, - address: token, - abi: dtfIndexStakingVaultAbi, - functionName: "getPastTotalSupply", - args: [snapshot], - }); - const mappedSnapshotSupply = mapAmount(snapshotSupply); + const [snapshotSupply, decimals] = await readSnapshotSupply( + client, + params.chainId, + token, + snapshot, + params.voteTokenDecimals, + ); + const mappedSnapshotSupply = mapAmount(snapshotSupply, decimals); const vetoThresholdVotes = getOptimisticVetoThresholdVotes({ snapshotSupply: mappedSnapshotSupply, vetoThreshold, @@ -110,7 +110,7 @@ export async function getOptimisticProposalContext( snapshot, snapshotSupply: mappedSnapshotSupply, vetoThreshold, - vetoThresholdVotes: mapAmount(vetoThresholdVotes), + vetoThresholdVotes: mapAmount(vetoThresholdVotes, decimals), }; } @@ -244,15 +244,25 @@ export async function getProposalThrottleCharges( } export async function getOptimisticVotes(client: DtfClient, params: GetIndexDtfOptimisticVotesParams): Promise { - const votes = await client.viem.readContract({ - chainId: params.chainId, - address: getAddress(params.voteToken), - abi: dtfIndexStakingVaultOptimisticAbi, - functionName: "getOptimisticVotes", - args: [getAddress(params.account)], - }); + const voteToken = getAddress(params.voteToken); + const [votes, decimals] = (await client.viem.getPublicClient(params.chainId).multicall({ + allowFailure: false, + contracts: [ + { + address: voteToken, + abi: dtfIndexStakingVaultOptimisticAbi, + functionName: "getOptimisticVotes", + args: [getAddress(params.account)], + }, + { + address: voteToken, + abi: erc20Abi, + functionName: "decimals", + }, + ], + })) as readonly [bigint, number]; - return mapAmount(votes); + return mapAmount(votes, decimals); } export async function getPastOptimisticVotes( @@ -260,15 +270,49 @@ export async function getPastOptimisticVotes( params: GetIndexDtfPastOptimisticVotesParams, ): Promise { const timepoint = params.timepoint ?? (await getLatestBlockTimepoint(client.viem, params.chainId)); - const votes = await client.viem.readContract({ - chainId: params.chainId, - address: getAddress(params.voteToken), - abi: dtfIndexStakingVaultOptimisticAbi, - functionName: "getPastOptimisticVotes", - args: [getAddress(params.account), timepoint], - }); + const voteToken = getAddress(params.voteToken); + const [votes, decimals] = (await client.viem.getPublicClient(params.chainId).multicall({ + allowFailure: false, + contracts: [ + { + address: voteToken, + abi: dtfIndexStakingVaultOptimisticAbi, + functionName: "getPastOptimisticVotes", + args: [getAddress(params.account), timepoint], + }, + { + address: voteToken, + abi: erc20Abi, + functionName: "decimals", + }, + ], + })) as readonly [bigint, number]; + + return mapAmount(votes, decimals); +} - return mapAmount(votes); +async function readSnapshotSupply( + client: DtfClient, + chainId: GetIndexDtfOptimisticProposalContextParams["chainId"], + token: Address, + snapshot: bigint, + voteTokenDecimals: number | undefined, +): Promise { + const supplyCall = { + address: token, + abi: dtfIndexStakingVaultAbi, + functionName: "getPastTotalSupply", + args: [snapshot], + } as const; + + if (voteTokenDecimals !== undefined) { + return [await client.viem.readContract({ chainId, ...supplyCall }), voteTokenDecimals]; + } + + return (await client.viem.getPublicClient(chainId).multicall({ + allowFailure: false, + contracts: [supplyCall, { address: token, abi: erc20Abi, functionName: "decimals" }], + })) as readonly [bigint, number]; } function getRoleMemberCalls(timelock: Address, role: Hex, count: bigint) { diff --git a/packages/sdk/src/index-dtf/governance/proposals.test.ts b/packages/sdk/src/index-dtf/governance/proposals.test.ts index 35db211..1b3b947 100644 --- a/packages/sdk/src/index-dtf/governance/proposals.test.ts +++ b/packages/sdk/src/index-dtf/governance/proposals.test.ts @@ -129,6 +129,9 @@ describe("Index DTF governance proposals", () => { expect(proposal.votes).toEqual([ { voter: target, choice: "FOR", weight: { raw: 2500000n, formatted: decimals === 6 ? "2.5" : "0.025" } }, ]); + expect(proposal.voteTokenDecimals).toBe(decimals); + expect(proposal.votingState.threshold.targetVotes).toEqual(proposal.quorumVotes); + expect(proposal.votingState.threshold.currentVotes).toEqual(proposal.forWeightedVotes); expect(proposal.state).toBe("ACTIVE"); expect(proposal.decoded.unknownCalls).toEqual([]); expect(proposal.decoded.calls).toHaveLength(1); diff --git a/packages/sdk/src/index-dtf/governance/proposals.ts b/packages/sdk/src/index-dtf/governance/proposals.ts index da3dcf6..cd66c88 100644 --- a/packages/sdk/src/index-dtf/governance/proposals.ts +++ b/packages/sdk/src/index-dtf/governance/proposals.ts @@ -225,6 +225,7 @@ async function withOptimisticContext( voteToken: proposal.voteToken, snapshot: BigInt(proposal.voteStart), vetoThreshold: proposal.vetoThreshold, + ...(proposal.voteTokenDecimals === undefined ? {} : { voteTokenDecimals: proposal.voteTokenDecimals }), }); if (!optimistic) { diff --git a/packages/sdk/src/index-dtf/governance/ref.ts b/packages/sdk/src/index-dtf/governance/ref.ts index bed2d76..52a36e2 100644 --- a/packages/sdk/src/index-dtf/governance/ref.ts +++ b/packages/sdk/src/index-dtf/governance/ref.ts @@ -77,10 +77,12 @@ export function createIndexDtfGovernanceRef(client: DtfClient, params: DtfParams ) => getOptimisticProposalContext(client, { ...proposal, chainId }), getProposalThrottleCharges: (charges: Pick) => getProposalThrottleCharges(client, { ...charges, chainId }), - getProposerState: (proposer: Pick) => - getProposerState(client, { ...proposer, chainId }), - getProposalVotes: (votes: Pick) => - getProposalVotes(client, { ...votes, chainId }), + getProposerState: ( + proposer: Pick, + ) => getProposerState(client, { ...proposer, chainId }), + getProposalVotes: ( + votes: Pick, + ) => getProposalVotes(client, { ...votes, chainId }), getProposalVoterState: (voter: Pick) => getProposalVoterState(client, { ...voter, chainId }), getOptimisticProposalVoterState: ( diff --git a/packages/sdk/src/index-dtf/governance/spell-decoding.test.ts b/packages/sdk/src/index-dtf/governance/spell-decoding.test.ts index c7e2d32..a11b2f6 100644 --- a/packages/sdk/src/index-dtf/governance/spell-decoding.test.ts +++ b/packages/sdk/src/index-dtf/governance/spell-decoding.test.ts @@ -101,4 +101,39 @@ describe("spell proposal decoding", () => { ["Governance Spell 09_18_2026", "retireOldStakingVault"], ]); }); + + // Later deployments of the same spell (2% and 1% thresholds) and OPEN's 10_21_2026 spell share its ABI. + it.each([ + [8453, "0x24a4B37F9c40fB0E80ec436Df2e9989FBAFa8bB7", "Governance Spell 09_18_2026"], + [8453, "0x971c890ACb9EeB084f292996Be667bB9A2889AE9", "Governance Spell 09_18_2026"], + [1, "0xeE6066Df6E27F142b921FD38e6766a9D6aF35c2f", "Governance Spell 09_18_2026"], + [56, "0x281EDD83b9b3C7cD05E260EF7E17A9734731A52B", "Governance Spell 09_18_2026"], + [1, "0xaf029ad44Cc44fB2AefFF84029dd0c29D5Ea8D87", "Governance Spell 10_21_2026"], + ] as const)("names a retire call to the spell at %s %s", (chainId, spell, name) => { + const decoded = decodeIndexDtfProposalCalldatas({ + targets: [spell], + calldatas: [ + encodeFunctionData({ + abi: governanceSpell09182026Abi, + functionName: "retireOldStakingVault", + args: [OLD_VAULT], + }), + ], + contractMap: buildProposalContractMap({ + chainId, + dtf: { + address: DTF, + proxyAdmin: PROXY_ADMIN, + legacyAdminGovernance: [], + legacyTradingGovernance: [], + stakingToken: { address: ST_TOKEN, legacyGovernance: [] }, + }, + }), + }); + + expect(decoded.unknownCalls).toEqual([]); + expect(decoded.calls.map(({ contract, functionName }) => [contract, functionName])).toEqual([ + [name, "retireOldStakingVault"], + ]); + }); }); diff --git a/packages/sdk/src/index-dtf/governance/utils.test.ts b/packages/sdk/src/index-dtf/governance/utils.test.ts index 48b434e..0414fab 100644 --- a/packages/sdk/src/index-dtf/governance/utils.test.ts +++ b/packages/sdk/src/index-dtf/governance/utils.test.ts @@ -128,3 +128,58 @@ describe("getProposalState stale PENDING past the deadline", () => { ).toMatchObject({ state: "QUORUM_NOT_REACHED" }); }); }); + +describe("getProposalState vote token decimals", () => { + const SIX_DECIMAL_PROPOSAL = { + state: "ACTIVE", + voteStart: 100, + voteEnd: 200, + voteTokenDecimals: 6, + forWeightedVotes: { raw: 10000n, formatted: "0.01" }, + againstWeightedVotes: { raw: 0n, formatted: "0" }, + abstainWeightedVotes: { raw: 0n, formatted: "0" }, + quorumVotes: { raw: 30000n, formatted: "0.03" }, + } as const; + + it("formats the quorum threshold in the vote token decimals", () => { + expect(getProposalState(SIX_DECIMAL_PROPOSAL, 150).threshold).toEqual({ + currentVotes: { raw: 10000n, formatted: "0.01" }, + targetVotes: { raw: 30000n, formatted: "0.03" }, + progress: expect.any(Number), + reached: false, + hasTarget: true, + }); + }); + + it("formats the optimistic veto threshold in the vote token decimals", () => { + const state = getProposalState( + { + ...SIX_DECIMAL_PROPOSAL, + isOptimistic: true, + vetoThreshold: 50000000000000000n, + againstWeightedVotes: { raw: 20000n, formatted: "0.02" }, + optimistic: { + snapshotSupply: { raw: 1000000n, formatted: "1" }, + vetoThreshold: 50000000000000000n, + vetoThresholdVotes: { raw: 50000n, formatted: "0.05" }, + }, + }, + 150, + ); + + expect(state.threshold).toMatchObject({ + currentVotes: { raw: 20000n, formatted: "0.02" }, + targetVotes: { raw: 50000n, formatted: "0.05" }, + hasTarget: true, + }); + }); + + it("keeps 18 decimals when the proposal does not carry vote token decimals", () => { + const { voteTokenDecimals: _, ...proposal } = SIX_DECIMAL_PROPOSAL; + + expect(getProposalState(proposal, 150).threshold.targetVotes).toEqual({ + raw: 30000n, + formatted: "0.00000000000003", + }); + }); +}); diff --git a/packages/sdk/src/index-dtf/governance/utils.ts b/packages/sdk/src/index-dtf/governance/utils.ts index 40e3339..61d0eed 100644 --- a/packages/sdk/src/index-dtf/governance/utils.ts +++ b/packages/sdk/src/index-dtf/governance/utils.ts @@ -44,6 +44,7 @@ type ProposalVoteStateInput = Pick< | "vetoThreshold" | "voteEnd" | "voteStart" + | "voteTokenDecimals" > & { readonly optimistic?: Partial< Pick @@ -113,7 +114,8 @@ export function getZeroValues(length: number): bigint[] { } export function getProposalState(proposal: ProposalVoteStateInput, timestamp = getCurrentTime()): ProposalVotingState { - const state = createInitialVotingState(proposal.state); + const voteTokenDecimals = proposal.voteTokenDecimals ?? 18; + const state = createInitialVotingState(proposal.state, voteTokenDecimals); const isOptimistic = proposal.isOptimistic === true; const optimisticVetoThresholdVotes = isOptimistic ? getOptimisticVetoVotes(proposal) : undefined; @@ -183,6 +185,7 @@ export function getProposalState(proposal: ProposalVoteStateInput, timestamp = g : proposal.forWeightedVotes.raw + proposal.abstainWeightedVotes.raw, targetVotes: isOptimistic ? optimisticVetoThresholdVotes : quorumVotes, reached: isOptimistic ? state.vetoReached : state.participationQuorumReached, + decimals: voteTokenDecimals, }); if (totalVotes > 0n) { @@ -215,7 +218,7 @@ export function withVoteState( }; } -function createInitialVotingState(state: ProposalState): MutableProposalVotingState { +function createInitialVotingState(state: ProposalState, decimals: number): MutableProposalVotingState { return { state, deadline: null, @@ -224,7 +227,7 @@ function createInitialVotingState(state: ProposalState): MutableProposalVotingSt participationQuorumReached: false, vetoReached: false, threshold: { - currentVotes: mapAmount(0n), + currentVotes: mapAmount(0n, decimals), progress: 0, reached: false, hasTarget: false, @@ -239,14 +242,16 @@ function getVotingThresholdState({ currentVotes, targetVotes, reached, + decimals, }: { readonly currentVotes: bigint; readonly targetVotes: bigint | undefined; readonly reached: boolean; + readonly decimals: number; }): ProposalVotingState["threshold"] { if (targetVotes === undefined || targetVotes === MAX_UINT256) { return { - currentVotes: mapAmount(currentVotes), + currentVotes: mapAmount(currentVotes, decimals), progress: 0, reached: false, hasTarget: false, @@ -254,8 +259,8 @@ function getVotingThresholdState({ } return { - currentVotes: mapAmount(currentVotes), - targetVotes: mapAmount(targetVotes), + currentVotes: mapAmount(currentVotes, decimals), + targetVotes: mapAmount(targetVotes, decimals), progress: targetVotes > 0n ? getVotePercentage(currentVotes, targetVotes) : reached ? 100 : 0, reached, hasTarget: true, diff --git a/packages/sdk/src/index-dtf/governance/voting.test.ts b/packages/sdk/src/index-dtf/governance/voting.test.ts index 121f592..f4a57a1 100644 --- a/packages/sdk/src/index-dtf/governance/voting.test.ts +++ b/packages/sdk/src/index-dtf/governance/voting.test.ts @@ -5,6 +5,7 @@ import type { DtfClient } from "@/client"; import { getOptimisticProposalVoterState, getProposalVoterState, + getProposalVotes, getProposerState, getVoterState, } from "@/index-dtf/governance/index"; @@ -22,6 +23,7 @@ describe("Index DTF governance voting", () => { 5000000000000000000n, 3000000000000000000n, 10000000000000000000n, + 18, ]) .mockResolvedValueOnce([ { status: "success", result: "0x0000000000000000000000000000000000000003" }, @@ -49,6 +51,7 @@ describe("Index DTF governance voting", () => { expect.objectContaining({ functionName: "balanceOf" }), expect.objectContaining({ functionName: "getVotes" }), expect.objectContaining({ functionName: "totalSupply" }), + expect.objectContaining({ functionName: "decimals" }), ]), }), ); @@ -81,8 +84,8 @@ describe("Index DTF governance voting", () => { it("reads proposer state at the latest governor clock timepoint", async () => { const multicall = vi .fn() - .mockResolvedValueOnce([1_000n, 3000000000000000000n]) - .mockResolvedValueOnce([5000000000000000000n]); + .mockResolvedValueOnce([1_000n, 3000000000000000000n, "0x0000000000000000000000000000000000000003"]) + .mockResolvedValueOnce([5000000000000000000n, 18]); const client = { viem: { getPublicClient: vi.fn(() => ({ multicall })), @@ -130,6 +133,7 @@ describe("Index DTF governance voting", () => { async ({ contracts }: { contracts: readonly { functionName: string; args?: readonly unknown[] }[] }) => contracts.map(({ functionName, args }) => { if (functionName === "clock") return 1_000_000n; + if (functionName === "decimals") return 18; if (functionName === "getVotes") return args?.[1] === 999_900n ? 4000000000000000000n : 0n; throw new Error(`Unexpected read: ${functionName}`); }), @@ -190,7 +194,7 @@ describe("Index DTF governance voting", () => { it.each([1_000_000, 1_000_100])( "clamps the standard snapshot %i at or after the current clock", async (voteStart) => { - const multicall = vi.fn().mockResolvedValueOnce([1_000_000n]).mockResolvedValueOnce([4000000000000000000n]); + const multicall = vi.fn().mockResolvedValueOnce([1_000_000n, 18]).mockResolvedValueOnce([4000000000000000000n]); const client = { viem: { getPublicClient: vi.fn(() => ({ multicall })), @@ -226,7 +230,7 @@ describe("Index DTF governance voting", () => { it("reads optimistic proposal voter state from the vote token snapshot", async () => { const multicall = vi .fn() - .mockResolvedValueOnce([1_000_000n]) + .mockResolvedValueOnce([1_000_000n, 18]) .mockResolvedValueOnce([{ status: "success", result: 2000000000000000000n }]); const client = { viem: { @@ -275,7 +279,7 @@ describe("Index DTF governance voting", () => { it("reads unified proposal voter state for optimistic proposals", async () => { const multicall = vi .fn() - .mockResolvedValueOnce([1_000_000n]) + .mockResolvedValueOnce([1_000_000n, 18]) .mockResolvedValueOnce([{ status: "success", result: 2000000000000000000n }]); const client = { viem: { @@ -317,7 +321,7 @@ describe("Index DTF governance voting", () => { it("clamps optimistic proposal voter state reads before future snapshots", async () => { const multicall = vi .fn() - .mockResolvedValueOnce([1_000_000n]) + .mockResolvedValueOnce([1_000_000n, 18]) .mockResolvedValueOnce([{ status: "success", result: 2000000000000000000n }]); const client = { viem: { @@ -358,4 +362,183 @@ describe("Index DTF governance voting", () => { }), ); }); + + describe("with a 6-decimal vote token", () => { + it("formats voter state in the vote token decimals", async () => { + const multicall = vi + .fn() + .mockResolvedValueOnce(["0x0000000000000000000000000000000000000002", 300000n, 300000n, 1000000n, 6]) + .mockResolvedValueOnce([ + { status: "success", result: "0x0000000000000000000000000000000000000002" }, + { status: "success", result: 200000n }, + ]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + }, + } as unknown as DtfClient; + + const state = await getVoterState(client, { + chainId: 8453, + stToken: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + }); + + expect(state).toMatchObject({ + balance: { raw: 300000n, formatted: "0.3" }, + votingPower: { raw: 300000n, formatted: "0.3" }, + optimisticVotingPower: { raw: 200000n, formatted: "0.2" }, + voteSupply: { raw: 1000000n, formatted: "1" }, + votingWeight: 30, + }); + }); + + it("formats proposer state in the vote token decimals", async () => { + const multicall = vi + .fn() + .mockResolvedValueOnce([1_000n, 10000n, "0x0000000000000000000000000000000000000003"]) + .mockResolvedValueOnce([300000n, 6]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + }, + } as unknown as DtfClient; + + const state = await getProposerState(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + }); + + expect(multicall).toHaveBeenNthCalledWith( + 2, + expect.objectContaining({ + contracts: expect.arrayContaining([ + expect.objectContaining({ + address: "0x0000000000000000000000000000000000000003", + functionName: "decimals", + }), + ]), + }), + ); + expect(state).toMatchObject({ + votingPower: { raw: 300000n, formatted: "0.3" }, + proposalThreshold: { raw: 10000n, formatted: "0.01" }, + canPropose: true, + }); + }); + + it("uses known vote token decimals for proposer state at a timepoint without a decimals read", async () => { + const multicall = vi.fn().mockResolvedValueOnce([300000n, 10000n, "0x0000000000000000000000000000000000000003"]); + const readContract = vi.fn(); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + readContract, + }, + } as unknown as DtfClient; + + const state = await getProposerState(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + timepoint: 999n, + voteTokenDecimals: 6, + }); + + expect(readContract).not.toHaveBeenCalled(); + expect(state.votingPower).toEqual({ raw: 300000n, formatted: "0.3" }); + }); + + it("reads vote token decimals for proposal votes", async () => { + const multicall = vi + .fn() + .mockResolvedValueOnce([[10000n, 30000n, 0n], "0x0000000000000000000000000000000000000003"]); + const readContract = vi.fn().mockResolvedValueOnce(6); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + readContract, + }, + } as unknown as DtfClient; + + const votes = await getProposalVotes(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + proposalId: "1", + }); + + expect(readContract).toHaveBeenCalledWith( + expect.objectContaining({ + address: "0x0000000000000000000000000000000000000003", + functionName: "decimals", + }), + ); + expect(votes).toEqual({ + againstVotes: { raw: 10000n, formatted: "0.01" }, + forVotes: { raw: 30000n, formatted: "0.03" }, + abstainVotes: { raw: 0n, formatted: "0" }, + }); + }); + + it("formats proposal voter state in the vote token decimals", async () => { + const multicall = vi.fn().mockResolvedValueOnce([1_000_000n, 6]).mockResolvedValueOnce([300000n]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + }, + } as unknown as DtfClient; + + const state = await getProposalVoterState(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + proposal: { + id: "1", + voteStart: 999_900, + voteToken: "0x0000000000000000000000000000000000000003", + votes: [], + }, + }); + + expect(multicall).toHaveBeenNthCalledWith( + 1, + expect.objectContaining({ + contracts: expect.arrayContaining([ + expect.objectContaining({ + address: "0x0000000000000000000000000000000000000003", + functionName: "decimals", + }), + ]), + }), + ); + expect(state.votingPower).toEqual({ raw: 300000n, formatted: "0.3" }); + }); + + it("formats optimistic proposal voter state in the vote token decimals", async () => { + const multicall = vi + .fn() + .mockResolvedValueOnce([1_000_000n, 6]) + .mockResolvedValueOnce([{ status: "success", result: 200000n }]); + const client = { + viem: { + getPublicClient: vi.fn(() => ({ multicall })), + }, + } as unknown as DtfClient; + + const state = await getOptimisticProposalVoterState(client, { + chainId: 8453, + governance: "0x0000000000000000000000000000000000000001", + account: "0x0000000000000000000000000000000000000002", + proposal: { + id: "1", + voteStart: 999_900, + voteToken: "0x0000000000000000000000000000000000000003", + votes: [], + }, + }); + + expect(state.optimisticVotingPower).toEqual({ raw: 200000n, formatted: "0.2" }); + }); + }); }); diff --git a/packages/sdk/src/index-dtf/governance/voting.ts b/packages/sdk/src/index-dtf/governance/voting.ts index 8da364f..6d93ea4 100644 --- a/packages/sdk/src/index-dtf/governance/voting.ts +++ b/packages/sdk/src/index-dtf/governance/voting.ts @@ -1,4 +1,4 @@ -import { getAddress, type Address } from "viem"; +import { erc20Abi, getAddress, type Address } from "viem"; import type { DtfClient } from "@/client"; import type { @@ -38,7 +38,7 @@ export async function getVoterState( abi: dtfIndexStakingVaultAbi, } as const; const publicClient = client.viem.getPublicClient(params.chainId); - const [delegate, balance, votingPower, voteSupply] = (await publicClient.multicall({ + const [delegate, balance, votingPower, voteSupply, decimals] = (await publicClient.multicall({ allowFailure: false, contracts: [ { @@ -60,25 +60,31 @@ export async function getVoterState( ...baseCall, functionName: "totalSupply", }, + { + address: stToken, + abi: erc20Abi, + functionName: "decimals", + }, ], - })) as readonly [Address, bigint, bigint, bigint]; + })) as readonly [Address, bigint, bigint, bigint, number]; const [optimisticDelegate, optimisticVotingPower] = await readOptionalCurrentOptimisticVoterState( client, params.chainId, stToken, account, ); - const mappedOptimisticVotingPower = optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower); + const mappedOptimisticVotingPower = + optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower, decimals); return { account, delegate, optimisticDelegate, - balance: mapAmount(balance), - votingPower: mapAmount(votingPower), + balance: mapAmount(balance, decimals), + votingPower: mapAmount(votingPower, decimals), votingWeight: voteSupply === 0n ? 0 : (Number(votingPower) / Number(voteSupply)) * 100, optimisticVotingPower: mappedOptimisticVotingPower, - voteSupply: mapAmount(voteSupply), + voteSupply: mapAmount(voteSupply, decimals), isSelfDelegated: sameAddress(delegate, account), isOptimisticSelfDelegated: optimisticDelegate !== null && sameAddress(optimisticDelegate, account), hasVotingPower: votingPower > 0n, @@ -99,7 +105,7 @@ export async function getProposerState( } as const; if (params.timepoint !== undefined) { - const [votingPower, proposalThreshold] = (await publicClient.multicall({ + const [votingPower, proposalThreshold, voteToken] = (await publicClient.multicall({ allowFailure: false, contracts: [ { @@ -111,19 +117,24 @@ export async function getProposerState( ...baseCall, functionName: "proposalThreshold", }, + { + ...baseCall, + functionName: "token", + }, ], - })) as readonly [bigint, bigint]; + })) as readonly [bigint, bigint, Address]; + const decimals = params.voteTokenDecimals ?? (await readTokenDecimals(client, params.chainId, voteToken)); return { account, governance, - votingPower: mapAmount(votingPower), - proposalThreshold: mapAmount(proposalThreshold), + votingPower: mapAmount(votingPower, decimals), + proposalThreshold: mapAmount(proposalThreshold, decimals), canPropose: votingPower >= proposalThreshold, }; } - const [clock, proposalThreshold] = (await publicClient.multicall({ + const [clock, proposalThreshold, voteToken] = (await publicClient.multicall({ allowFailure: false, contracts: [ { @@ -134,11 +145,15 @@ export async function getProposerState( ...baseCall, functionName: "proposalThreshold", }, + { + ...baseCall, + functionName: "token", + }, ], - })) as readonly [number | bigint, bigint]; + })) as readonly [number | bigint, bigint, Address]; const clockTimepoint = typeof clock === "bigint" ? clock : BigInt(clock); const timepoint = clockTimepoint > 0n ? clockTimepoint - 1n : 0n; - const [votingPower] = (await publicClient.multicall({ + const [votingPower, voteTokenDecimals] = (await publicClient.multicall({ allowFailure: false, contracts: [ { @@ -146,14 +161,20 @@ export async function getProposerState( functionName: "getVotes", args: [account, timepoint], }, + { + address: voteToken, + abi: erc20Abi, + functionName: "decimals", + }, ], - })) as readonly [bigint]; + })) as readonly [bigint, number]; + const decimals = params.voteTokenDecimals ?? voteTokenDecimals; return { account, governance, - votingPower: mapAmount(votingPower), - proposalThreshold: mapAmount(proposalThreshold), + votingPower: mapAmount(votingPower, decimals), + proposalThreshold: mapAmount(proposalThreshold, decimals), canPropose: votingPower >= proposalThreshold, }; } @@ -162,22 +183,31 @@ export async function getProposalVotes( client: DtfClient, params: GetIndexDtfProposalVotesParams, ): Promise { - const [[againstVotes, forVotes, abstainVotes]] = (await client.viem.getPublicClient(params.chainId).multicall({ - allowFailure: false, - contracts: [ - { - address: getAddress(params.governance), - abi: dtfIndexGovernanceAbi, - functionName: "proposalVotes", - args: [BigInt(params.proposalId)], - }, - ], - })) as readonly [readonly [bigint, bigint, bigint]]; + const governance = getAddress(params.governance); + const [[againstVotes, forVotes, abstainVotes], voteToken] = (await client.viem + .getPublicClient(params.chainId) + .multicall({ + allowFailure: false, + contracts: [ + { + address: governance, + abi: dtfIndexGovernanceAbi, + functionName: "proposalVotes", + args: [BigInt(params.proposalId)], + }, + { + address: governance, + abi: dtfIndexGovernanceAbi, + functionName: "token", + }, + ], + })) as readonly [readonly [bigint, bigint, bigint], Address]; + const decimals = params.voteTokenDecimals ?? (await readTokenDecimals(client, params.chainId, voteToken)); return { - againstVotes: mapAmount(againstVotes), - forVotes: mapAmount(forVotes), - abstainVotes: mapAmount(abstainVotes), + againstVotes: mapAmount(againstVotes, decimals), + forVotes: mapAmount(forVotes, decimals), + abstainVotes: mapAmount(abstainVotes, decimals), }; } @@ -199,17 +229,18 @@ async function getStandardProposalVoterState( const account = getAddress(params.account); const governance = getAddress(params.governance); const vote = getAccountProposalVote(params.proposal.votes, account); - const votingPower = await readStandardProposalVotingPower( + const [votingPower, decimals] = await readStandardProposalVotingPower( client, params.chainId, governance, + getAddress(params.proposal.voteToken), account, params.proposal.voteStart, ); return { account, - votingPower: mapAmount(votingPower), + votingPower: mapAmount(votingPower, decimals), vote, hasVoted: vote !== null, hasVotingPower: votingPower > 0n, @@ -221,14 +252,14 @@ async function getUnifiedOptimisticProposalVoterState( params: GetIndexDtfProposalVoterStateParams, ): Promise { const account = getAddress(params.account); - const optimisticVotingPower = await readProposalOptimisticVotingPower( + const [optimisticVotingPower, decimals] = await readProposalOptimisticVotingPower( client, params.chainId, account, params.proposal, ); const vote = getAccountProposalVote(params.proposal.votes, account); - const votingPower = mapAmount(optimisticVotingPower ?? 0n); + const votingPower = mapAmount(optimisticVotingPower ?? 0n, decimals); return { account, @@ -244,7 +275,7 @@ export async function getOptimisticProposalVoterState( params: GetIndexDtfOptimisticProposalVoterStateParams, ): Promise { const account = getAddress(params.account); - const optimisticVotingPower = await readProposalOptimisticVotingPower( + const [optimisticVotingPower, decimals] = await readProposalOptimisticVotingPower( client, params.chainId, account, @@ -254,7 +285,7 @@ export async function getOptimisticProposalVoterState( return { account, - optimisticVotingPower: optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower), + optimisticVotingPower: optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower, decimals), vote, hasVoted: vote !== null, hasOptimisticVotingPower: (optimisticVotingPower ?? 0n) > 0n, @@ -322,21 +353,23 @@ async function readProposalOptimisticVotingPower( ) { const voteToken = getAddress(proposal.optimistic?.voteToken ?? proposal.voteToken); const snapshot = proposal.optimistic?.snapshot ?? BigInt(proposal.voteStart); - const timepoint = await getPastVoteTimepoint(client, chainId, voteToken, snapshot); + const [timepoint, decimals] = await getPastVoteTimepoint(client, chainId, voteToken, snapshot); + const votingPower = await readPastOptimisticVotes(client, chainId, voteToken, account, timepoint); - return readPastOptimisticVotes(client, chainId, voteToken, account, timepoint); + return [votingPower, decimals] as const; } async function readStandardProposalVotingPower( client: DtfClient, chainId: GetIndexDtfProposalVoterStateParams["chainId"], governance: Address, + voteToken: Address, account: GetIndexDtfProposalVoterStateParams["account"], voteStart: number, ) { const snapshotTimepoint = BigInt(voteStart); const publicClient = client.viem.getPublicClient(chainId); - const [clock] = (await publicClient.multicall({ + const [clock, decimals] = (await publicClient.multicall({ allowFailure: false, contracts: [ { @@ -344,8 +377,13 @@ async function readStandardProposalVotingPower( abi: dtfIndexGovernanceAbi, functionName: "clock", }, + { + address: voteToken, + abi: erc20Abi, + functionName: "decimals", + }, ], - })) as readonly [number | bigint]; + })) as readonly [number | bigint, number]; const clockTimepoint = typeof clock === "bigint" ? clock : BigInt(clock); const latestSafeTimepoint = clockTimepoint > 0n ? clockTimepoint - 1n : 0n; const timepoint = snapshotTimepoint > latestSafeTimepoint ? latestSafeTimepoint : snapshotTimepoint; @@ -361,7 +399,7 @@ async function readStandardProposalVotingPower( ], })) as readonly [bigint]; - return votingPower; + return [votingPower, decimals] as const; } async function getPastVoteTimepoint( @@ -370,7 +408,7 @@ async function getPastVoteTimepoint( voteToken: Address, snapshotTimepoint: bigint, ) { - const [clock] = (await client.viem.getPublicClient(chainId).multicall({ + const [clock, decimals] = (await client.viem.getPublicClient(chainId).multicall({ allowFailure: false, contracts: [ { @@ -378,12 +416,30 @@ async function getPastVoteTimepoint( abi: dtfIndexStakingVaultAbi, functionName: "clock", }, + { + address: voteToken, + abi: erc20Abi, + functionName: "decimals", + }, ], - })) as readonly [number | bigint]; + })) as readonly [number | bigint, number]; const clockTimepoint = typeof clock === "bigint" ? clock : BigInt(clock); const latestSafeTimepoint = clockTimepoint > 0n ? clockTimepoint - 1n : 0n; - return snapshotTimepoint > latestSafeTimepoint ? latestSafeTimepoint : snapshotTimepoint; + return [snapshotTimepoint > latestSafeTimepoint ? latestSafeTimepoint : snapshotTimepoint, decimals] as const; +} + +async function readTokenDecimals( + client: DtfClient, + chainId: GetIndexDtfProposalVotesParams["chainId"], + token: Address, +): Promise { + return client.viem.readContract({ + chainId, + address: token, + abi: erc20Abi, + functionName: "decimals", + }); } async function readPastOptimisticVotes( diff --git a/packages/sdk/src/index-dtf/subgraph/dtf.generated.ts b/packages/sdk/src/index-dtf/subgraph/dtf.generated.ts index 7ef645e..0b33875 100644 --- a/packages/sdk/src/index-dtf/subgraph/dtf.generated.ts +++ b/packages/sdk/src/index-dtf/subgraph/dtf.generated.ts @@ -7747,7 +7747,7 @@ export type GetIndexDtfDelegatesQueryVariables = Exact<{ }>; -export type GetIndexDtfDelegatesQuery = { stakingToken?: { id: string, totalDelegates: string, currentDelegates: string, totalOptimisticDelegates: string, currentOptimisticDelegates: string, token: { totalSupply: string }, delegates: Array<{ address: string, delegatedVotesRaw: string, optimisticDelegatedVotesRaw: string, numberVotes: number, numberOptimisticVotes: number, hasBeenStandardDelegate: boolean, hasBeenOptimisticDelegate: boolean, tokenHoldersRepresentedAmount: number, optimisticTokenHoldersRepresentedAmount: number }> } | null }; +export type GetIndexDtfDelegatesQuery = { stakingToken?: { id: string, totalDelegates: string, currentDelegates: string, totalOptimisticDelegates: string, currentOptimisticDelegates: string, token: { totalSupply: string, decimals: number }, delegates: Array<{ address: string, delegatedVotesRaw: string, optimisticDelegatedVotesRaw: string, numberVotes: number, numberOptimisticVotes: number, hasBeenStandardDelegate: boolean, hasBeenOptimisticDelegate: boolean, tokenHoldersRepresentedAmount: number, optimisticTokenHoldersRepresentedAmount: number }> } | null }; export type GetIndexDtfRebalancesQueryVariables = Exact<{ dtf: Scalars['String']['input']; @@ -8360,6 +8360,7 @@ export const GetIndexDtfDelegatesDocument = new TypedDocumentString(` currentOptimisticDelegates token { totalSupply + decimals } delegates( first: $limit diff --git a/packages/sdk/src/index-dtf/subgraph/dtf.graphql b/packages/sdk/src/index-dtf/subgraph/dtf.graphql index c41d288..48382a3 100644 --- a/packages/sdk/src/index-dtf/subgraph/dtf.graphql +++ b/packages/sdk/src/index-dtf/subgraph/dtf.graphql @@ -448,6 +448,7 @@ query GetIndexDtfDelegates($stToken: ID!, $limit: Int = 10) { currentOptimisticDelegates token { totalSupply + decimals } delegates( first: $limit diff --git a/packages/sdk/src/index-dtf/vote-lock/index.ts b/packages/sdk/src/index-dtf/vote-lock/index.ts index 666e2b1..4148674 100644 --- a/packages/sdk/src/index-dtf/vote-lock/index.ts +++ b/packages/sdk/src/index-dtf/vote-lock/index.ts @@ -276,7 +276,8 @@ async function readVoteLockState( maxWithdraw: mapAmount(maxWithdrawResult.result, underlying.decimals), shareBalance: mapAmount(shareBalanceResult.result, underlying.decimals), exchangeRate: mapAmount(exchangeRateResult.result, underlying.decimals), - optimisticVotingPower: optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower), + optimisticVotingPower: + optimisticVotingPower === null ? null : mapAmount(optimisticVotingPower, underlying.decimals), hasOptimisticVotingPower: (optimisticVotingPower ?? 0n) > 0n, unstakingDelay: unstakingDelayResult.result, unstakingManager: unstakingManagerResult.result, diff --git a/packages/sdk/src/types/governance.ts b/packages/sdk/src/types/governance.ts index bd40d11..cafde1f 100644 --- a/packages/sdk/src/types/governance.ts +++ b/packages/sdk/src/types/governance.ts @@ -82,6 +82,8 @@ export type GetIndexDtfOptimisticProposalContextParams = { readonly voteToken?: Address; readonly snapshot?: bigint; readonly vetoThreshold?: bigint; + /** Skips the vote token `decimals()` read when the caller already knows it. */ + readonly voteTokenDecimals?: number; }; export type GetIndexDtfLegacyVoteLocksParams = @@ -107,12 +109,16 @@ export type GetIndexDtfProposerStateParams = { readonly governance: Address; readonly account: Address; readonly timepoint?: bigint; + /** Skips the extra vote token `decimals()` read when the caller already knows it. */ + readonly voteTokenDecimals?: number; }; export type GetIndexDtfProposalVotesParams = { readonly chainId: SupportedChainId; readonly governance: Address; readonly proposalId: string | bigint; + /** Skips the extra vote token `decimals()` read when the caller already knows it. */ + readonly voteTokenDecimals?: number; }; export type GetIndexDtfProposalVoterStateParams = { @@ -214,6 +220,8 @@ export type IndexDtfProposalSummary = { readonly forWeightedVotes: Amount; readonly againstWeightedVotes: Amount; readonly abstainWeightedVotes: Amount; + /** Decimals of the vote token; vote weights, quorum and thresholds use them. Treated as 18 when missing. */ + readonly voteTokenDecimals?: number; readonly isOptimistic?: boolean; readonly wasChallenged?: boolean; readonly challengedProposalId?: string; @@ -246,6 +254,7 @@ export type IndexDtfProposalVotingSnapshot = { readonly forWeightedVotes: Amount; readonly againstWeightedVotes: Amount; readonly abstainWeightedVotes: Amount; + readonly voteTokenDecimals?: number; readonly isOptimistic?: boolean; readonly vetoThreshold?: bigint; readonly optimistic?: IndexDtfOptimisticProposalContext;