Skip to content

Commit

Permalink
New design for direct delegation (#46)
Browse files Browse the repository at this point in the history
* Get current direct delegatee

* fix: load voting power correctly and fetch delegatee

* fix: get delegatee

* loading + case with no delegatee

* refactor

* add comment

* add fetch direct delegation

* feat: new design for direct delegation

* fix: optimistic vote - build fix

* fix: refuse

* style fix
  • Loading branch information
Dom-Mac committed Jan 12, 2024
1 parent b9b1405 commit e56ae1d
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 120 deletions.
25 changes: 25 additions & 0 deletions src/app/api/delegations/getDelegations.ts
Expand Up @@ -213,3 +213,28 @@ async function getCurrentDelegatorsForAddress({
})),
] as Delegation[];
}

const getDirectDelegateeForAddress = async ({
address,
}: {
address: string;
}) => {
const [proxyAddress, delegatee] = await Promise.all([
getProxyAddress(address),
prisma.delegatees.findFirst({
where: { delegator: address.toLowerCase() },
}),
]);

if (delegatee?.delegatee === proxyAddress?.toLowerCase()) {
return null;
}

return delegatee;
};

export const getDirectDelegatee = ({
addressOrENSName,
}: {
addressOrENSName: string;
}) => addressOrEnsNameWrap(getDirectDelegateeForAddress, addressOrENSName);
8 changes: 8 additions & 0 deletions src/app/delegates/[addressOrENSName]/page.jsx
Expand Up @@ -14,6 +14,7 @@ import DelegateVotesProvider from "@/contexts/DelegateVotesContext";
import {
getCurrentDelegatees,
getCurrentDelegators,
getDirectDelegatee,
} from "@/app/api/delegations/getDelegations";
import DelegationsContainer from "@/components/Delegates/Delegations/DelegationsContainer";
import ResourceNotFound from "@/components/shared/ResourceNotFound/ResourceNotFound";
Expand Down Expand Up @@ -80,6 +81,12 @@ async function getProxyAddress(addressOrENSName) {
return getProxy({ addressOrENSName });
}

async function fetchDirectDelegatee(addressOrENSName) {
"use server";

return getDirectDelegatee({ addressOrENSName });
}

export default async function Page({ params: { addressOrENSName } }) {
let delegate;
let delegateVotes;
Expand Down Expand Up @@ -118,6 +125,7 @@ export default async function Page({ params: { addressOrENSName } }) {
fetchBalanceForDirectDelegation={fetchBalanceForDirectDelegation}
getProxyAddress={getProxyAddress}
fetchCurrentDelegatees={getDelegatees}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
</VStack>

Expand Down
12 changes: 11 additions & 1 deletion src/app/delegates/page.jsx
Expand Up @@ -15,7 +15,10 @@ import {
getVotingPowerAvailableForSubdelegation,
isDelegatingToProxy,
} from "../api/voting-power/getVotingPower";
import { getCurrentDelegatees } from "../api/delegations/getDelegations";
import {
getCurrentDelegatees,
getDirectDelegatee,
} from "../api/delegations/getDelegations";

async function fetchDelegates(sort, page = 1, seed) {
"use server";
Expand Down Expand Up @@ -64,6 +67,12 @@ async function fetchDaoMetrics() {
return getMetrics();
}

async function fetchDirectDelegatee(addressOrENSName) {
"use server";

return getDirectDelegatee({ addressOrENSName });
}

export default async function Page({ searchParams }) {
const sort =
delegatesFilterOptions[searchParams.orderBy]?.sort || "weighted_random";
Expand Down Expand Up @@ -95,6 +104,7 @@ export default async function Page({ searchParams }) {
checkIfDelegatingToProxy={checkIfDelegatingToProxy}
fetchCurrentDelegatees={fetchCurrentDelegatees}
getProxyAddress={getProxyAddress}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
</section>
);
Expand Down
2 changes: 1 addition & 1 deletion src/app/lib/hooks/useIsAdvancedUser.ts
Expand Up @@ -11,7 +11,7 @@ const useIsAdvancedUser = () => {
const { address } = useAccount();
const [isAdvancedUser, setIsAdvancedUser] = useState(false);
const allowList = [
"0x4D5d7d63989BBE6358a3352A2449d59Aa5A08267",
"0x4D5d7d63989BBE6358a3352A2449d59Aa5A08267", // Dom Test 1
"0xd0f23E5ea6c8088eD0FFf294F3fC29e719EE6B8b",
"0xde748c3dd4311A5d6b305E2eeFd6481BCDA1e84B", // Base tester
"0x85A0779CA390adaD02Aa63075373bF33e7C5a711", // Base tester
Expand Down
12 changes: 6 additions & 6 deletions src/components/Delegates/DelegateCard/DelegateActions.tsx
Expand Up @@ -4,12 +4,11 @@ import { HStack } from "@/components/Layout/Stack";
import { DelegateButton } from "./DelegateButton";
import { DelegateSocialLinks } from "./DelegateSocialLinks";
import { useAccount } from "wagmi";
import { useState } from "react";
import { AdvancedDelegateButton } from "./AdvancedDelegateButton";
import { Delegation } from "@/app/api/delegations/delegation";
import { useAgoraContext } from "@/contexts/AgoraContext";
import { DelegateChunk } from "../DelegateCardList/DelegateCardList";
import useIsAdvancedUser from "@/app/lib/hooks/useIsAdvancedUser";
import { Delegatees } from "@prisma/client";

export function DelegateActions({
delegate,
Expand All @@ -20,6 +19,7 @@ export function DelegateActions({
fetchCurrentDelegatees,
getProxyAddress,
isAdvancedUser,
fetchDirectDelegatee,
}: {
delegate: DelegateChunk;
className?: string;
Expand All @@ -33,6 +33,7 @@ export function DelegateActions({
fetchCurrentDelegatees: (addressOrENSName: string) => Promise<Delegation[]>;
getProxyAddress: (addressOrENSName: string) => Promise<string>;
isAdvancedUser: boolean;
fetchDirectDelegatee: (addressOrENSName: string) => Promise<Delegatees>;
}) {
const { isConnected } = useAgoraContext();
const { address } = useAccount();
Expand Down Expand Up @@ -61,10 +62,9 @@ export function DelegateActions({
) : (
<DelegateButton
full={!twitter && !discord}
delegate={delegate.address}
fetchBalanceForDirectDelegation={() =>
fetchBalanceForDirectDelegation(address)
}
delegate={delegate}
fetchBalanceForDirectDelegation={fetchBalanceForDirectDelegation}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
))}
</div>
Expand Down
37 changes: 0 additions & 37 deletions src/components/Delegates/DelegateCard/DelegateButton.jsx

This file was deleted.

40 changes: 40 additions & 0 deletions src/components/Delegates/DelegateCard/DelegateButton.tsx
@@ -0,0 +1,40 @@
import { Button } from "@/components/Button";
import { useOpenDialog } from "@/components/Dialogs/DialogProvider/DialogProvider";
import { DelegateChunk } from "../DelegateCardList/DelegateCardList";
import { Delegatees } from "@prisma/client";

type Props = {
full: boolean;
delegate: DelegateChunk;
fetchBalanceForDirectDelegation: (
addressOrENSName: string
) => Promise<string>;
fetchDirectDelegatee: (addressOrENSName: string) => Promise<Delegatees>;
};
export function DelegateButton({
full,
delegate,
fetchBalanceForDirectDelegation,
fetchDirectDelegatee,
}: Props) {
const openDialog = useOpenDialog();

return (
<Button
onClick={(e: any) => {
e.preventDefault();
openDialog({
type: "DELEGATE",
params: {
delegate,
fetchBalanceForDirectDelegation,
fetchDirectDelegatee,
},
});
}}
className={full ? "w-full" : undefined}
>
Delegate
</Button>
);
}
2 changes: 2 additions & 0 deletions src/components/Delegates/DelegateCard/DelegateCard.jsx
Expand Up @@ -12,6 +12,7 @@ export default async function DelegateCard({
checkIfDelegatingToProxy,
fetchCurrentDelegatees,
getProxyAddress,
fetchDirectDelegatee,
}) {
const delegate = await fetchDelegate(addressOrENSName);

Expand Down Expand Up @@ -85,6 +86,7 @@ export default async function DelegateCard({
checkIfDelegatingToProxy={checkIfDelegatingToProxy}
fetchCurrentDelegatees={fetchCurrentDelegatees}
getProxyAddress={getProxyAddress}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
</VStack>
</div>
Expand Down
@@ -1,7 +1,24 @@
"use client";

import { Delegation } from "@/app/api/delegations/delegation";
import { DelegateChunk } from "../DelegateCardList/DelegateCardList";
import { DelegateActions } from "./DelegateActions";
import useIsAdvancedUser from "@/app/lib/hooks/useIsAdvancedUser";
import { Delegatees } from "@prisma/client";

type Props = {
delegate: DelegateChunk;
fetchBalanceForDirectDelegation: (
addressOrENSName: string
) => Promise<string>;
fetchVotingPowerForSubdelegation: (
addressOrENSName: string
) => Promise<string>;
checkIfDelegatingToProxy: (addressOrENSName: string) => Promise<boolean>;
fetchCurrentDelegatees: (addressOrENSName: string) => Promise<Delegation[]>;
getProxyAddress: (addressOrENSName: string) => Promise<string>;
fetchDirectDelegatee: (addressOrENSName: string) => Promise<Delegatees>;
};

export default function DelegateCardClient({
delegate,
Expand All @@ -10,7 +27,8 @@ export default function DelegateCardClient({
checkIfDelegatingToProxy,
fetchCurrentDelegatees,
getProxyAddress,
}) {
fetchDirectDelegatee,
}: Props) {
const { isAdvancedUser } = useIsAdvancedUser();

return (
Expand All @@ -22,6 +40,7 @@ export default function DelegateCardClient({
fetchCurrentDelegatees={fetchCurrentDelegatees}
getProxyAddress={getProxyAddress}
isAdvancedUser={isAdvancedUser}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
);
}
Expand Up @@ -12,6 +12,7 @@ import { useRouter } from "next/navigation";
import { DialogProvider } from "@/components/Dialogs/DialogProvider/DialogProvider";
import { Delegate } from "@/app/api/delegates/delegate";
import useIsAdvancedUser from "@/app/lib/hooks/useIsAdvancedUser";
import { Delegatees } from "@prisma/client";

export type DelegateChunk = Pick<
Delegate,
Expand All @@ -35,7 +36,8 @@ interface Props {
checkIfDelegatingToProxy: (addressOrENSName: string) => Promise<boolean>;
fetchCurrentDelegatees: (addressOrENSName: string) => Promise<any>;
getProxyAddress: (addressOrENSName: string) => Promise<string>;
completeDelegation: (addressOrENSName: string) => void;
completeDelegation: () => void;
fetchDirectDelegatee: (addressOrENSName: string) => Promise<Delegatees>;
}

export default function DelegateCardList({
Expand All @@ -46,6 +48,7 @@ export default function DelegateCardList({
checkIfDelegatingToProxy,
fetchCurrentDelegatees,
getProxyAddress,
fetchDirectDelegatee,
}: Props) {
const router = useRouter();
const fetching = React.useRef(false);
Expand Down Expand Up @@ -145,6 +148,7 @@ export default function DelegateCardList({
fetchCurrentDelegatees={fetchCurrentDelegatees}
getProxyAddress={getProxyAddress}
isAdvancedUser={isAdvancedUser}
fetchDirectDelegatee={fetchDirectDelegatee}
/>
</VStack>
</VStack>
Expand Down

0 comments on commit e56ae1d

Please sign in to comment.