Displaying Farcaster Wallet Connection Status in a Mini App

Joy DevJoy Dev
2 min read

Table of contents

Ssup guys, I've been thinking about how to identify if a user is connected with a wallet on Farcaster and display this in a Miniapp. I will guide how you can use the Farcaster Miniapps SDK and the Wagmi connector for wallet interactions. Below is a corrected step-by-step guide:

1. Set Up the Wagmi Connector:

- Install and configure the Wagmi connector to detect the user’s Ethereum wallet. The `farcasterMiniapp` connector checks for a connected wallet.

- configuration:

```javascript

import { http, createConfig } from 'wagmi'

import { base } from 'wagmi/chains'

import { farcasterMiniapp } from '@farcaster/miniapp-wagmi-connector'

export const config = createConfig({

chains: [base],

transports: { [base.id]: http() },

connectors: [farcasterMiniapp()]

})

```

- Use Wagmi’s `useAccount` hook to check wallet connection status:

```javascript

import { useAccount } from 'wagmi'

function MyMiniapp() {

const { isConnected, address } = useAccount()

return (

<div>

{isConnected ? (

<p>Connected Wallet: {address}</p>

) : (

<p>No wallet connected</p>

)}

</div>

)

}

```

2. Access User Context via Farcaster SDK:

- Use the Farcaster Miniapps SDK to access user context, including their Farcaster ID (FID) and wallet address.

-

```javascript

import { sdk } from '@farcaster/miniapp-sdk'

useEffect(() => {

const checkContext = async () => {

const context = await sdk.context

const userAddress = context.user?.address

const fid = context.user?.fid

if (userAddress) {

console.log(`User FID: ${fid}, Wallet: ${userAddress}`)

} else {

console.log('No wallet connected')

}

}

checkContext()

}, [])

```

- The `sdk.context` provides user details, confirming wallet connection via a Farcaster client like Warpcast.

3. Display Wallet Connection in the Miniapp:

- Update the Miniapp’s UI to show wallet connection status:

```javascript

function WalletStatus() {

const { isConnected, address } = useAccount()

return (

<div>

{isConnected ? (

<p>Connected to Farcaster with wallet: {address}</p>

) : (

<p>Please connect your wallet in Warpcast</p>

)}

</div>

)

}

- Optionally, display additional user info (e.g., FID or username) from `sdk.context.user`.

4. Handle Wallet Interactions:

- For connected users, enable onchain actions using Wagmi hooks like `useSendTransaction`. The `farcasterMiniapp` connector handles wallet connections automatically.

- Check supported chains via `sdk.context.client.supportedChainIds` to ensure compatibility (e.g., Base, Optimism).

5. Publish and Test the Miniapp:

- Host your Miniapp and publish its manifest at `/.well-known/farcaster.json` for integration with Farcaster clients like Warpcast.

- Test the wallet connection flow in Warpcast’s in-app browser to verify the UI displays the connected wallet address correctly.

Notes:

- Users must have a Farcaster account via a wallet app like Warpcast for wallet connection.

- Use the `@farcaster/create-miniapp` CLI for streamlined project setup with wallet integration.

- Validate wallet interactions to ensure user safety.

0
Subscribe to my newsletter

Read articles from Joy Dev directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Joy Dev
Joy Dev

Bullish Blockchain Developer