Skip to content

Commit

Permalink
Improved delegate flow
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Aug 23, 2024
1 parent a69d798 commit fa3ec58
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
9 changes: 7 additions & 2 deletions plugins/members/hooks/useAnnounceDelegation.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { DelegateAnnouncerAbi } from "../artifacts/DelegationWall.sol";
import { PUB_DELEGATION_WALL_CONTRACT_ADDRESS } from "@/constants";
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { toHex } from "viem";
import { useAlerts } from "@/context/Alerts";
import { uploadToPinata } from "@/utils/ipfs";
import { useCallback, useEffect, useState } from "react";
import { toHex } from "viem";
import { useWaitForTransactionReceipt, useWriteContract } from "wagmi";
import { type IAnnouncementMetadata } from "../utils/types";
import { useDelegates } from "./useDelegates";

export function useAnnounceDelegation(onSuccess?: () => void) {
const { addAlert } = useAlerts();
const { writeContract, data: hash, error, status } = useWriteContract();
const { refetch } = useDelegates();
const [uploading, setUploading] = useState(false);
const { isLoading: isConfirming, isSuccess: isConfirmed } = useWaitForTransactionReceipt({ hash });

Expand Down Expand Up @@ -44,6 +46,9 @@ export function useAnnounceDelegation(onSuccess?: () => void) {
txHash: hash,
});

// Force a refresh of the delegates list
refetch();

onSuccess?.();
}, [status, hash, isConfirming, isConfirmed]);

Expand Down
30 changes: 12 additions & 18 deletions plugins/members/hooks/useDelegates.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { useConfig } from "wagmi";
import { useReadContract } from "wagmi";
import { DelegateAnnouncerAbi } from "@/plugins/members/artifacts/DelegationWall.sol";
import { PUB_DELEGATION_WALL_CONTRACT_ADDRESS } from "@/constants";
import { readContract } from "@wagmi/core";
import { useQuery } from "@tanstack/react-query";
import { useShuffled } from "@/hooks/useShuffled";

/** Returns the list of delegates who posted a candidacy */
export function useDelegates() {
const config = useConfig();
const { data, status, refetch } = useReadContract({
abi: DelegateAnnouncerAbi,
address: PUB_DELEGATION_WALL_CONTRACT_ADDRESS,
functionName: "getCandidateAddresses",

const { data, status, refetch } = useQuery({
queryKey: ["delegate-addresses-list", PUB_DELEGATION_WALL_CONTRACT_ADDRESS],
queryFn: () => {
return readContract(config, {
abi: DelegateAnnouncerAbi,
address: PUB_DELEGATION_WALL_CONTRACT_ADDRESS,
functionName: "getCandidateAddresses",
args: [],
});
query: {
retry: true,
refetchOnMount: false,
refetchOnReconnect: false,
retryOnMount: true,
staleTime: 1000 * 60 * 10,
},
retry: true,
refetchOnMount: false,
refetchOnReconnect: false,
retryOnMount: true,
staleTime: 1000 * 60 * 10,
});

const shuffledDelegates = useShuffled(data);

return { delegates: shuffledDelegates, status, refetch };
Expand Down

0 comments on commit fa3ec58

Please sign in to comment.