diff --git a/docs/onchainkit/guides/use-basename-in-onchain-app.mdx b/docs/onchainkit/guides/use-basename-in-onchain-app.mdx index 04ed805d2..9167f6823 100644 --- a/docs/onchainkit/guides/use-basename-in-onchain-app.mdx +++ b/docs/onchainkit/guides/use-basename-in-onchain-app.mdx @@ -1,97 +1,1334 @@ +# Integrating Basenames with OnchainKit + +## Table of Contents +- [Overview](#overview) +- [What are Basenames?](#what-are-basenames) +- [Prerequisites](#prerequisites) +- [Installation](#installation) +- [Integration Methods](#integration-methods) + - [React Components](#react-components) + - [React Hooks](#react-hooks) + - [TypeScript Utilities](#typescript-utilities) +- [Advanced Usage](#advanced-usage) +- [Best Practices](#best-practices) +- [Troubleshooting](#troubleshooting) +- [Additional Resources](#additional-resources) + --- -title: Use Basename · OnchainKit -sidebarTitle: Use Basename -description: Integrate Basenames into your onchain app, in just a few steps. + +## Overview + +This guide provides a comprehensive walkthrough for integrating Basenames into your onchain application using OnchainKit. Basenames are human-readable identifiers for wallet addresses on Base, making blockchain interactions more user-friendly and accessible. + +### Key Benefits +- **User-Friendly**: Replace complex hexadecimal addresses with readable names +- **ENS Compatible**: Built on the same technology as Ethereum Name Service (ENS) +- **Native to Base**: Fully onchain and deployed on the Base network +- **Flexible Integration**: Multiple integration options (components, hooks, utilities) + --- +## What are Basenames? + Basenames are an essential onchain building block that empowers builders to establish their identity on Base by registering human-readable names for their wallet addresses. -They operate entirely onchain, utilizing the same technology as ENS names, and are deployed on Base. +### Technical Details +- **Protocol**: Based on ENS (Ethereum Name Service) technology +- **Network**: Deployed on Base L2 +- **Format**: `username.base.eth` +- **Resolution**: Bidirectional (address ↔ name) + +### Use Cases +- Display user identities in your dApp +- Simplify wallet address input +- Create social features with readable identifiers +- Build reputation systems +- Enhance UX in NFT marketplaces and DeFi applications + +--- + +## Prerequisites + +### System Requirements +- Node.js 16.x or higher +- npm, yarn, or pnpm package manager +- React 18.x or higher (for component/hook usage) +- TypeScript 4.x or higher (recommended) + +### Knowledge Prerequisites +- Basic understanding of React (for component/hook approaches) +- Familiarity with Web3/blockchain concepts +- Understanding of wallet addresses and ENS + +--- + +## Installation + +### For New Projects + +If you're new to OnchainKit, follow these steps: + +1. **Install OnchainKit** + ```bash + npm install @coinbase/onchainkit + # or + yarn add @coinbase/onchainkit + # or + pnpm add @coinbase/onchainkit + ``` + +2. **Install Required Dependencies** + ```bash + npm install viem wagmi + ``` + +3. **Configure Your Project** + + Refer to the [OnchainKit Getting Started Guide](https://onchainkit.xyz/getting-started) for detailed setup instructions, including: + - Setting up providers + - Configuring wallet connections + - Initializing the OnchainKit client + +### For Existing Projects + +If you're already using OnchainKit: + +1. **Update to Latest Version** + ```bash + npm update @coinbase/onchainkit + ``` + +2. **Verify Installation** + ```bash + npm list @coinbase/onchainkit + ``` + +--- + +## Integration Methods + +OnchainKit provides three flexible approaches to integrate Basenames, catering to different architectural needs and preferences. + +### React Components + +**Best for**: Quick integration, UI-focused applications, developers who prefer declarative code + +#### Available Components + +##### `` Component +Displays the avatar/profile picture associated with a Basename. + +**Props:** +- `address` (string, required): Ethereum address +- `chain` (Chain, optional): Chain configuration (use `base` from `viem/chains`) +- `className` (string, optional): CSS class for styling +- `defaultComponent` (ReactNode, optional): Fallback component -You can integrate [Basenames](https://www.base.org/names) into your app with these few steps. +**Example:** +```tsx +import { Avatar } from '@coinbase/onchainkit/identity'; +import { base } from 'viem/chains'; - - - Follow the [Getting Started](/onchainkit/getting-started) guide to install the package. - - - Update to the latest version and choose from the following steps: a React component approach, a React hook, or a pure TypeScript utility function. - - +function UserProfile() { + const address = '0x02feeb0AdE57b6adEEdE5A4EEea6Cf8c21BeB6B1'; + + return ( +
+ +
+ ); +} +``` +##### `` Component +Displays the Basename associated with an address. -## React components with `` and `` +**Props:** +- `address` (string, required): Ethereum address +- `chain` (Chain, optional): Chain configuration +- `className` (string, optional): CSS class for styling +- `sliced` (boolean, optional): Show abbreviated address if no name found -Use the [``](/onchainkit/identity/avatar) and [``](/onchainkit/identity/name) components to display Basenames associated with Ethereum addresses. +**Example:** +```tsx +import { Name } from '@coinbase/onchainkit/identity'; +import { base } from 'viem/chains'; -The `chain` prop is optional and setting to Base, it's what makes the components switch from ENS to Basenames. +function UserDisplay() { + const address = '0x02feeb0AdE57b6adEEdE5A4EEea6Cf8c21BeB6B1'; + + return ( +
+ +
+ ); +} +``` + +#### Combined Component Example ```tsx -// @noErrors: 2657 - JSX expressions must have one parent element import { Avatar, Name } from '@coinbase/onchainkit/identity'; import { base } from 'viem/chains'; -const address = '0x02feeb0AdE57b6adEEdE5A4EEea6Cf8c21BeB6B1'; +export function UserCard({ address }) { + return ( +
+ +
+ + {address.slice(0, 6)}...{address.slice(-4)} +
+
+ ); +} +``` + +#### Styling Components + +```tsx +// With Tailwind CSS +
+ + +
+ +// With custom CSS +
+ + +
+``` + +--- + +### React Hooks + +**Best for**: Custom UI implementations, advanced state management, building reusable components + +#### Available Hooks + +##### `useAvatar` Hook +Fetches avatar data for a given Basename or address. + +**Parameters:** +```typescript +{ + ensName?: string; // Basename (e.g., 'zizzamia.base.eth') + address?: string; // Ethereum address + chain?: Chain; // Chain configuration +} +``` + +**Returns:** +```typescript +{ + data: string | null; // Avatar URL or base64 data + isLoading: boolean; // Loading state + error: Error | null; // Error object if request fails + refetch: () => void; // Function to refetch data +} +``` + +**Example:** +```tsx +import { useAvatar } from '@coinbase/onchainkit/identity'; +import { base } from 'viem/chains'; + +function CustomAvatar({ ensName }) { + const { data: avatar, isLoading, error } = useAvatar({ + ensName, + chain: base + }); + + if (isLoading) return
; + if (error) return
Failed to load
; + + return ( + {`${ensName} + ); +} +``` + +##### `useName` Hook +Fetches the Basename for a given address. + +**Parameters:** +```typescript +{ + address: string; // Ethereum address + chain?: Chain; // Chain configuration +} +``` + +**Returns:** +```typescript +{ + data: string | null; // Basename or null + isLoading: boolean; // Loading state + error: Error | null; // Error object if request fails + refetch: () => void; // Function to refetch data +} +``` + +**Example:** +```tsx +import { useName } from '@coinbase/onchainkit/identity'; +import { base } from 'viem/chains'; + +function UserNameDisplay({ address }) { + const { data: name, isLoading, error } = useName({ + address, + chain: base + }); -// omitted component code for brevity - - + if (isLoading) return Loading...; + if (error) return Error loading name; + + return ( + + {name || `${address.slice(0, 6)}...${address.slice(-4)}`} + + ); +} ``` -