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

chore: Base Names testnet compatibility #966

Merged
merged 5 commits into from
Aug 5, 2024
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
5 changes: 5 additions & 0 deletions .changeset/beige-bikes-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase/onchainkit": patch
---

-**chore**: Base Names testnet compatibility. By @cpcramer #966
18 changes: 18 additions & 0 deletions src/wallet/components/WalletDropdownBaseName.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { UseQueryResult } from '@tanstack/react-query';
import { render, screen } from '@testing-library/react';
import type { GetAccountReturnType } from '@wagmi/core';
import { base } from 'viem/chains';
import { describe, expect, it, vi } from 'vitest';
import { useAccount } from 'wagmi';
import { useName } from '../../identity/hooks/useName';
import { WalletDropdownBaseName } from './WalletDropdownBaseName';
import { useWalletContext } from './WalletProvider';

vi.mock('wagmi', () => ({
useAccount: vi.fn(),
Expand All @@ -14,12 +16,19 @@ vi.mock('../../identity/hooks/useName', () => ({
useName: vi.fn(),
}));

vi.mock('./WalletProvider', () => ({
useWalletContext: vi.fn(),
}));

describe('WalletDropdownBaseName', () => {
it('should render "Claim a Basename" when no basename', () => {
(useAccount as vi.Mock<[], Partial<GetAccountReturnType>>).mockReturnValue({
address: '0x1234' as `0x${string}`,
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
).mockReturnValue({
Expand All @@ -39,6 +48,9 @@ describe('WalletDropdownBaseName', () => {
address: '0x1234' as `0x${string}`,
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
).mockReturnValue({
Expand All @@ -58,6 +70,9 @@ describe('WalletDropdownBaseName', () => {
address: '0x1234' as `0x${string}`,
isConnected: true,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
});
(
useName as vi.Mock<[], Partial<UseQueryResult<string | null, Error>>>
).mockReturnValue({
Expand All @@ -79,6 +94,9 @@ describe('WalletDropdownBaseName', () => {
address: undefined,
isConnected: false,
});
(useWalletContext as vi.Mock).mockReturnValue({
chain: base,
});

const { container } = render(<WalletDropdownBaseName />);
expect(container.firstChild).toBeNull();
Expand Down
7 changes: 4 additions & 3 deletions src/wallet/components/WalletDropdownBaseName.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { base } from 'viem/chains';
import { useAccount } from 'wagmi';
import { useName } from '../../identity/hooks/useName';
import { Spinner } from '../../internal/components/Spinner';
import { baseNameSvg } from '../../internal/svg/baseNameSvg';
import { cn, pressable, text } from '../../styles/theme';
import type { WalletDropdownBaseNameReact } from '../types';
import { useWalletContext } from './WalletProvider';

export function WalletDropdownBaseName({
className,
}: WalletDropdownBaseNameReact) {
const { address } = useAccount();
const { chain } = useWalletContext();

if (!address) {
if (!address || !chain) {
return null;
}

const { data: baseName, isLoading } = useName({
address,
chain: base,
chain,
});

const hasBaseUserName = !!baseName;
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/components/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext, useContext, useState } from 'react';
import type { ReactNode } from 'react';
import { useValue } from '../../internal/hooks/useValue';
import { useOnchainKit } from '../../useOnchainKit';
import type { WalletContextType } from '../types';

const emptyContext = {} as WalletContextType;
Expand All @@ -12,9 +13,11 @@ type WalletProviderReact = {
};

export function WalletProvider({ children }: WalletProviderReact) {
const { chain } = useOnchainKit();
const [isOpen, setIsOpen] = useState(false);
const value = useValue({
isOpen,
chain,
setIsOpen,
});
return (
Expand Down
3 changes: 2 additions & 1 deletion src/wallet/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserOperation } from 'permissionless';
import type { Dispatch, ReactNode, SetStateAction } from 'react';
import type { Address, PublicClient } from 'viem';
import type { Address, Chain, PublicClient } from 'viem';
import type { UseBalanceReturnType, UseReadContractReturnType } from 'wagmi';
import type { SwapError } from '../swap';

Expand Down Expand Up @@ -61,6 +61,7 @@ export type UseGetTokenBalanceResponse = {
*/
export type WalletContextType = {
address?: Address | null; // The Ethereum address to fetch the avatar and name for.
chain?: Chain; // Optional chain for domain resolution
Copy link
Contributor

Choose a reason for hiding this comment

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

So, I think we probably want to start move away from using chain as type. Mostly because we are seeing occasional missmatch with Viem types.

Let's do instead chainId for those things going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thoughts on merging this and then updating to chainId in the next PR? Because the chainId change will require getName, Name, and useName updates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll push up the chainId changes and we can split it into two PRs if it's too large

isOpen: boolean;
setIsOpen: Dispatch<SetStateAction<boolean>>;
};
Expand Down