Skip to content

Commit

Permalink
Prepare for voting (#437)
Browse files Browse the repository at this point in the history
* feat: clean up vote power query

* feat: fetch voting power

* fix: format number
  • Loading branch information
MrX-SNX authored Sep 5, 2024
1 parent 1d24809 commit db784b7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 55 deletions.
25 changes: 17 additions & 8 deletions governance/ui/src/pages/MyVotes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useState } from 'react';
import { CouncilImage } from '../components/CouncilImage';
import { ProfilePicture } from '../components/UserProfileCard/ProfilePicture';
import { getVoteSelectionState } from '../utils/localstorage';
import { utils } from 'ethers';

export default function MyVotes() {
const [showConfirmation, setShowConfirmation] = useState(false);
Expand All @@ -48,6 +49,14 @@ export default function MyVotes() {
.map(([council]) => council) as CouncilSlugs[];
const { mutateAsync, isPending } = useCastVotes(councilToCastVote, stateFromCouncils);
const navigate = useNavigate();
const formattedVotePower = formatNumber(
votingPowerSpartan?.power && votingPowerAmbassador?.power && votingPowerTreassury?.power
? utils.formatEther(
votingPowerSpartan.power.add(votingPowerAmbassador.power).add(votingPowerTreassury.power)
)
: 0
);

return (
<>
<CouncilTabs activeCouncil="spartan" />
Expand Down Expand Up @@ -178,14 +187,14 @@ export default function MyVotes() {
Total Voting Power
</Text>
<Text fontSize="sm" color="white" fontWeight="bold" data-cy="my-votes-voting-power">
{formatNumber(
votingPowerSpartan?.power && votingPowerAmbassador && votingPowerTreassury
? votingPowerSpartan.power
.add(votingPowerAmbassador.power)
.add(votingPowerTreassury.power)
.toString()
: 0
)}
{formattedVotePower === '0.00'
? formatNumber(
votingPowerSpartan?.power
.add(votingPowerAmbassador?.power || 0)
.add(votingPowerTreassury?.power || 0)
.toString() || 0
)
: formattedVotePower}
</Text>
</Flex>
<Button
Expand Down
2 changes: 1 addition & 1 deletion governance/ui/src/queries/useGetEpochIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function useGetEpochIndex(council: CouncilSlugs) {
const { network } = useNetwork();

return useQuery({
queryKey: ['epochId', council],
queryKey: ['epochId', council, network?.id],
queryFn: async () => {
return await getCouncilContract(council)
.connect(motherShipProvider(network?.id || 13001))
Expand Down
50 changes: 24 additions & 26 deletions governance/ui/src/queries/useGetUserVotingPower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,41 @@ export function useGetUserVotingPower(council: CouncilSlugs) {
const electionModule = getCouncilContract(council).connect(motherShipProvider(network.id));
const isMC = isMotherchain(network.id);

const snapshotContract = SnapshotRecordContract(network.id, council);
const electionId = await electionModule.getEpochIndex();
const ballot = isMC
? await electionModule.getBallot(activeWallet.address, network.id, electionId)
: await electionModule.connect(provider).getPreparedBallot(activeWallet.address);
const votingPowerFromMotherchain: BigNumber = isMC
? await electionModule
.connect(motherShipProvider(network.id))
.getVotePower(activeWallet.address, network.id, electionId)
: BigNumber.from(0);
const ballot: { votingPower: BigNumber } | undefined = await electionModule.getBallot(
activeWallet.address,
network.id,
electionId
);

if (ballot) {
if (ballot?.votingPower?.gt(0)) {
return {
power: isCI ? (ballot.votingPower as BigNumber) : votingPowerFromMotherchain,
isDeclared: true,
};
} else if ('gt' in ballot && ballot.gt(0)) {
return {
power: isCI ? (ballot as BigNumber) : votingPowerFromMotherchain,
isDeclared: true,
};
}
const periodId = await snapshotContract?.connect(provider).currentPeriodId();

const votingPowerFromMotherchain: BigNumber = isCI
? BigNumber.from(0)
: await electionModule
.connect(provider)
.getVotingPowerForUser(snapshotContract?.address, activeWallet.address, periodId);

if (ballot?.votingPower?.gt(0)) {
return {
power: isCI ? (ballot.votingPower as BigNumber) : votingPowerFromMotherchain,
isDeclared: true,
};
}
const votingPower: BigNumber = isCI
? isMC
? await electionModule
.connect(provider)
.callStatic.prepareBallotWithSnapshot(
SnapshotRecordContract(network.id, council)?.address,
snapshotContract?.address,
activeWallet?.address
)
: await SnapshotRecordContract(network.id, council)
?.connect(provider)
.balanceOfOnPeriod(activeWallet.address, 1)
: BigNumber.from(0);
: BigNumber.from(0)
: votingPowerFromMotherchain;

return {
power: process.env.CI === 'true' ? votingPower : votingPowerFromMotherchain,
power: votingPower,
isDeclared: false,
};
} catch (error) {
Expand Down
69 changes: 49 additions & 20 deletions governance/ui/src/utils/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,25 +912,6 @@ export const multicallABI = [
];

export const electionModuleABI = [
{
inputs: [
{
internalType: 'address',
name: 'voter',
type: 'address',
},
],
name: 'getPreparedBallot',
outputs: [
{
internalType: 'uint256',
name: 'power',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
Expand Down Expand Up @@ -1435,7 +1416,13 @@ export const electionModuleABI = [
type: 'error',
},
{
inputs: [],
inputs: [
{
internalType: 'enum Epoch.ElectionPeriod',
name: 'currentPeriod',
type: 'uint8',
},
],
name: 'NotCallableInCurrentPeriod',
type: 'error',
},
Expand Down Expand Up @@ -2432,6 +2419,19 @@ export const electionModuleABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getNumOfBallots',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'getRegisteredEmitters',
Expand Down Expand Up @@ -3060,6 +3060,35 @@ export const electionModuleABI = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
internalType: 'address',
name: 'snapshotContract',
type: 'address',
},
{
internalType: 'address',
name: 'voter',
type: 'address',
},
{
internalType: 'uint256',
name: 'periodId',
type: 'uint256',
},
],
name: 'getVotingPowerForUser',
outputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
stateMutability: 'view',
type: 'function',
},
{
inputs: [
{
Expand Down
1 change: 1 addition & 0 deletions governance/ui/src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export const SnapshotRecordContract = (chainId: number, council: CouncilSlugs) =
const abiForSnapshotMock = [
'function balanceOfOnPeriod(address, uint256) view returns (uint256)',
'function setBalanceOfOnPeriod(address, uint256, uint256) external',
'function currentPeriodId() external view returns (uint128)',
];

export const profileContract = new Contract(
Expand Down

0 comments on commit db784b7

Please sign in to comment.