Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ENS names #159

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { goerli, optimismGoerli } from 'wagmi/chains';
import { goerli, optimismGoerli, mainnet } from 'wagmi/chains';

export const chains = [goerli, optimismGoerli];
export const chains = [goerli, optimismGoerli, mainnet];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-12-01 at 4 38 35 PM

this adds mainnet to the chains dropdown; i played around locally to see if we can avoid this, and i came to the same conclusion that you did: seems unavoidable!


export enum DaoId {
PoolTogether = 'pool-together',
Expand Down
5 changes: 4 additions & 1 deletion hooks/useL2Delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { parseAbi } from 'viem';

type Props = {
delegatorAddress?: `0x${string}`;
enabled?: boolean;
};

export const useL2Delegate = ({ delegatorAddress }: Props) => {
export const useL2Delegate = ({ delegatorAddress, enabled }: Props) => {
let { address } = useAccount();
if (delegatorAddress !== undefined) {
address = delegatorAddress;
Expand All @@ -23,6 +24,8 @@ export const useL2Delegate = ({ delegatorAddress }: Props) => {
functionName: 'delegates',
args: [address],
chainId: l2.chain.id,
watch: true,
enabled: enabled,
});
return { data, error, isLoading };
};
30 changes: 24 additions & 6 deletions pages/[id]/delegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { InformationCircleIcon } from '@heroicons/react/24/outline';
import type { NextPage } from 'next';
import Head from 'next/head';
import Image from 'next/image';
import { useAccount, useNetwork, useWalletClient } from 'wagmi';
import { useAccount, useEnsAvatar, useEnsName, useNetwork, useWalletClient } from 'wagmi';
import { isAddress, formatUnits } from 'viem';
import { useForm } from 'react-hook-form';
import { useBalances } from '@/hooks/useBalances';
Expand All @@ -18,6 +18,7 @@ import { ZERO_ADDRESS } from '@/util/constants';
import CardWithHeader from '@/components/CardWithHeader';
import Spinner from '@/components/Spinner';
import { switchChain } from '@/util';
import { getEnsAddress } from '@/util/ens';
import { Tooltip } from 'react-tooltip';
import { useTokenInfo } from '@/hooks/useTokenInfo';
import { ConnectButton } from '@rainbow-me/rainbowkit';
Expand All @@ -35,7 +36,6 @@ const Delegate: NextPage = () => {
const { data: walletClient, isLoading: walletIsLoading } = useWalletClient();
const { chain } = useNetwork();
const [delegateAddress, setDelegateAddress] = useState(address);
const { data: delegatee } = useL2Delegate({});
const { data: l2VotingWeight } = useL2CurrentVotingWeight({
voterAddress: address || ZERO_ADDRESS,
});
Expand All @@ -47,8 +47,11 @@ const Delegate: NextPage = () => {
const {
register,
handleSubmit,
formState: { errors },
formState: { errors, isValid },
} = useForm<FormData>({ mode: 'onChange' });
const { data: delegatee } = useL2Delegate({ enabled: isValid });
const { data: delegateEnsName } = useEnsName({ address: delegatee, chainId: 1 });
const { data: delegateEnsAvatar } = useEnsAvatar({ name: delegateEnsName, chainId: 1 });

const l2VotingWeightFormatted = formatUnits(
l2VotingWeight || BigInt(0),
Expand Down Expand Up @@ -147,7 +150,14 @@ const Delegate: NextPage = () => {
<div className="self-center font-mono">
{mounted && delegatee !== ZERO_ADDRESS ? (
<div className="flex items-start gap-1">
{delegatee}{' '}
{delegateEnsAvatar && (
<img
className="inline-block h-6 w-6 rounded-full"
src={delegateEnsAvatar}
alt=""
/>
)}
{delegateEnsName || delegatee}{' '}
<InformationCircleIcon
id="delegate-address-information"
className="h-4"
Expand Down Expand Up @@ -179,10 +189,18 @@ const Delegate: NextPage = () => {
aria-invalid="true"
aria-describedby="address-error"
{...register('delegateAddress', {
onChange: (e) => {
setDelegateAddress(e.target.value);
onChange: async (e) => {
const val = await getEnsAddress(e.target.value);
setDelegateAddress(val || e.target.value);
},
validate: async (value) => {
if (value?.includes('.eth')) {
const val = await getEnsAddress(value);
if (val !== null) {
value = val;
}
}

const validAddress = isAddress(value);
const isBalanceNonzero = (l2.token?.value || BigInt(0)) > BigInt(0);
if (validAddress && isBalanceNonzero) return true;
Expand Down
20 changes: 20 additions & 0 deletions util/ens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { normalize } from 'viem/ens';
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';

export const getEnsAddress = async (name: string) => {
const client = createPublicClient({
chain: mainnet,
transport: http(),
});
try {
const ensAddress = await client.getEnsAddress({
name: normalize(name),
});
return ensAddress;
} catch (e) {
const err = e as Error;
console.error(err.message);
}
return null;
};