bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 75쪽
l-known/farcaster.json` endpoint.
```ts lines title="app/.well-known/farcaster.json/route.ts" theme={null}
...
const appUrl = process.env.NEXT_PUBLIC_URL;
const farcasterConfig = {
// accountAssociation details are required to associate the published app with it's author
// instructions on how to get these values are provided later in this guide.
accountAssociation: {
"header": "",
"payload": "",
"signature": ""
},
frame: {
version: "1",
name: "Monad Farcaster Mini App Template", // Name of your Mini App
iconUrl: `${appUrl}/images/icon.png`, // Icon of the app in the app store
homeUrl: `${appUrl}`, // Default launch URL
imageUrl: `${appUrl}/images/feed.png`, // Default image to show if shared in a feed.
screenshotUrls: [], // Visual previews of the app
tags: ["monad", "farcaster", "miniapp", "template"], // Descriptive tags for search
primaryCategory: "developer-tools",
buttonTitle: "Launch Template",
splashImageUrl: `${appUrl}/images/splash.png`, // URL of image to show on loading screen.
splashBackgroundColor: "#ffffff", // Hex color code to use on loading screen.
webhookUrl: `${appUrl}/api/webhook` // Webhook url for notifications
}
};
...
```
The above example is a partial representation of the `farcaster.json` file. The full reference for the [farcaster.json](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/.well-known/farcaster.json/route.ts) file can be found [here](https://miniapps.farcaster.xyz/docs/guides/publishing#define-your-application-configuration).
## Generating `accountAssociation` using the Farcaster Mobile App.
The `accountAssociation` object in the [farcaster.json](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/.well-known/farcaster.json/route.ts) file is used to associate the Mini App with the publisher's Farcaster account.
To generate the `accountAssociation` object you can use the Farcaster Mobile App.
Go to `Settings (under Developer) > Domains`, enter the domain where the Mini App is hosted and click `Generate Domain Manifest`.
<Accordion title="Can't see Developer options?">
For the `Developer` options to be visible you will need to have `Developer mode` enabled in Farcaster.
You can enable it by going to `Settings (under Profile and Account) > Advanced > scroll down and enable Developer mode`.
<iframe title="YouTube video player" />
</Accordion>
The `accountAssociation` object will be generated it will be available in your clipboard you can then paste the values in the [farcaster.json](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/.well-known/farcaster.json/route.ts) file.
```ts lines title="app/.well-known/farcaster.json/route.ts" theme={null}
...
const appUrl = process.env.NEXT_PUBLIC_URL;
const farcasterConfig = {
accountAssociation: {
header: "eyJmaWQiOjE3OTc5LCJ0eXBlIjoiY3VzdG9keSIsImtleSI6IjB4MGMxNWE5QkVmRTg3RjY0N0IwMDNhMjI0MTY4NDYwMzYyODQ0M2Y4YiJ9",
payload: "eyJkb21haW4iOiJtb25hZC1taW5pYXBwLXRlbXBsYXRlLXNldmVuLnZlcmNlbC5hcHAifQ",
signature: "MHgwYzY2NDdjZDhjOWJiY2JmYzg2NGIzZjVjYWVjY2ExMTdlOTY4ZGQwMWIzMmM0NGViMjU5ZDhlOGQyMzdhZTZiMDU1MmNmNWRiMDU1MDMwNTZmNTNhZmEwZDZlZTBlZmIyMmJmNDNmMDQ4NTdhMzk2NmY0YmMzODk2N2NlZDI5ZjFi"
},
...
};
...
```
<iframe title="YouTube video player" />
You can find the `farcaster.json` file for Monad Mini App Template app [here](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/.well-known/farcaster.json/route.ts).
Once the `farcaster.json` file is hosted, your Mini App is now discoverable in the Farcaster app, you can test it by searching for the Mini App in the Farcaster app!
# How to send notifications to users of the Farcaster Mini App
Source: https://docs.monad.xyz/templates/farcaster-miniapp/sending-notifications
This guide walks you through how you can send notifications to your Mini App users.
This guide uses [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template), which already has the code for sending notifications; all you have to do is modify a few lines of code.
If you aren't using the template, you can still follow the guide; feel free to copy code as needed from the template.
## Demo
If you want to try the Mini App notification experience, you can try the [Monad Mini App Template](https://farcaster.xyz/miniapps/ODHcbibktF7T/monad-farcaster-miniapp-template) app.
## Modifying the Mini App manifest
<Note>
Mini App account association process has to be completed before the Mini App can send notifications.
</Note>
Mini Apps can send notifications to the user when:
* The user has added the Mini App and not disabled notifications manually
* The user has explicitly subscribed to notifications from the Mini App
Farcaster app or clients send events like `miniapp_added`, `miniapp_removed`, `notifications_disabled` and `notifications_enabled` to the Mini App's webhook endpoint, this allows the Mini App to keep track of which users the Mini App can send notifications to.
The `webhookUrl` has to be specified in the Mini App manifest file:
```ts title="app/.well-known/farcaster.json/route.ts" theme={null}
...
export async function GET() {
const farcasterConfig = {
// TODO: Add your own account association
frame: {
version: "1",
name: "Monad Farcaster Mini App Template",
...
splashBackgroundColor: "#ffffff",
webhookUrl: `${APP_URL}/api/webhook`, // <--- Edit this
},
};
...
```
If you are using the [Monad Mini App Template](https://github.com/monad-developers/monad-miniapp-template), you don't need to edit the `webhookUrl`.
## Processing Farcaster client events
The code for processing webhook events can be found in the file `/app/api/webhook/route.ts`.
### Verifying webhook events
Events are signed by the app key of a user with a [JSON Farcaster Signature](https://github.com/farcasterxyz/protocol/discussions/208). This allows Mini Apps to verify the Farcaster client that generated the notification and the Farcaster user they generated it for.
It is important to verify events in order to make sure the Mini App is correctly tracking which users it can send notifications to.
If you are using the [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template), the code for verifying events is already available in [`/app/api/webhook/route.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/api/webhook/route.ts). If not, you can copy code from the same file.
<Info>
A Neynar API key is required to verify webhook events. You can sign up to the [Neynar](https://neynar.com/) service and get an API key for free.
Once done, add a environment variable named `NEYNAR_API_KEY` to your `.env` file.
</Info>
### Processing webhook payload
Once the `webhookUrl` is specified, Farcaster app and clients will send client events to the webhook endpoint.
The `miniapp_added` and `notifications_enabled` events are received by the Mini App along with `fid` and `notificationDetails`.
`notificationDetails` has a `url` and a `token` that can be used to send notifications to a specific Farcaster user.
Example of webhook payload:
```json theme={null}
{
"fid": 17979,
"event": {
"event": "notifications_enabled",
"notificationDetails": {
"url": "https://api.farcaster.xyz/v1/frame-notifications",
"token": "a05059ef2415c67b08ecceb539201cbc6"
}
}
}
```
You can find the code, handling the Farcaster client events in [`/app/api/webhook/route.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/api/webhook/route.ts).
The Mini App can use any database service of choice to store the `url` and `token` and use it when sending notifications.
The [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template) uses [Upstash's Redis](https://console.upstash.com/redis) service for storing the `notificationDetails`, if you wish to use a different database you can modify it in [`/lib/kv.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/lib/kv.ts).
<Note>
Since the [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template) uses Redis for storing notification url and token, environment variables `UPSTASH_REDIS_REST_URL` and `UPSTASH_REDIS_REST_TOKEN` are required in `.env`.
Once you sign up for the [Upstash](https://console.upstash.com/redis) service and create a Redis database you will be able to get these environment variables.
If you plan to use some other database, adjust environment variables accordingly.
</Note>
Example of storing notification details in Redis:
```ts title="/lib/kv.ts" theme={null}
...
const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
...
export async function setUserNotificationDetails(
fid: number,
notificationDetails: MiniAppNotificationDetails
): Promise<void> {
// Modify lines that use redis to use your own database implementation
await redis.set(getUserNotificationDetailsKey(fid), notificationDetails);
}
...
```
## Sending notifications
<img alt="Example of a notification sent from a Farcaster Mini App" />
Once you have a notification token for a user, you can send them a notification by sending a `POST` request to the url associated with that token.
If you are using the [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template) the code for sending notification is already available in the file [`/app/api/send-notification/route.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/api/send-notification/route.ts). If not, you can copy code from [`/lib/notifs.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/lib/notifs.ts) and [`/app/api/send-notification/route.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/api/send-notification/route.ts).
### Personalizing notification content
You can change a few lines of code in [`/app/api/send-notification/route.ts`](https://github.com/monad-developers/monad-miniapp-template/blob/main/app/api/send-notification/route.ts) file to personalize the notification:
```ts title="/app/api/send-notification/route.ts" theme={null}
...
// This function sends the notification
const sendResult = await sendFrameNotification({
fid: requestBody.data.fid,
// You can modify/personalize the below line to make the title of the notification dynamic based on the fid (user)
title: "Test notification",
// You can modify/personalize the below line to make the body of the notification dynamic based on the fid (user)
body: "Sent at " + new Date().toISOString(),
});
...
```
<Warning>
#### Notification Rate Limits
The standard rate limits, which are enforced by Farcaster, are:
* 1 notification per 30 seconds per token
* 100 notifications per day per token
</Warning>
If you are using the [Monad Mini App template](https://github.com/monad-developers/monad-miniapp-template), you can send notifications from the server or the Mini App by making a `POST` to `/api/send-notification` endpoint!
That's all you need to send notifications to your Mini App users!
# How to use the Next.js Serwist 0x Privy embedded wallet template
Source: https://docs.monad.xyz/templates/next-serwist-0x-privy-embedded-wallet
This guide walks you through using the [template](https://github.com/monad-developers/next-serwist-privy-0x) which uses [Next.js](https://nextjs.org/docs), [Serwist](https://serwist.pages.dev/) (offline capabilities), [0x](https://0x.org/docs/) (token trading), and [Privy embedded wallet](https://docs.privy.io/wallets/wallets/create/create-a-wallet) (authentication) to build a Progressive Web Application (PWA) on Monad.
## Prerequisites
* [Node.js](https://nodejs.org/) (v18 or higher)
* a [Privy account](https://www.privy.io/)
* a [0x account](https://www.0x.org/)
<Accordion title="Setting up Privy">
1. Create your Privy app:
<img alt="Create your app" />
Select "Web" as the platform. Then, click "Create app".
<img alt="Name your app" />
On the next screen, make sure to save your App ID.
2. Set up login methods:
<img alt="Set up login methods" />
3. Disable External Wallets:
<img alt="Disable external wallets" />
4. Scroll down and enable "Automatically create embedded wallets on login" and select "EVM Wallets":
<img alt="Create wallets automatically" />
<Tip>
You can enable "Test Accounts" for testing purposes.
<img alt="Test accounts" />
</Tip>
</Accordion>
<Accordion title="Setting up 0x">
1. Create your 0x account:
Go to [0x dashboard](https://dashboard.0x.org/create-account) and create your account.
On the next screen, make sure to save your API Key.
2. Get your API Key:
To get your API key, create an app and then go to API Keys.
Copy the API key and save it for later.
</Accordion>
## Setup
1. Clone the repository:
```bash theme={null}
git clone https://github.com/monad-developers/next-serwist-privy-0x.git
```
2. `cd` into the project directory:
```bash theme={null}
cd next-serwist-privy-0x
```
3. Install dependencies:
```bash theme={null}
npm install
```
4. Create a `.env.local` file in the root directory:
```bash theme={null}
cp .env.example .env.local
```
5. Start adding your environment variables to the `.env.local` file:
```bash theme={null}
# Privy
NEXT_PUBLIC_PRIVY_APP_ID=your_privy_app_id_here
NEXT_PUBLIC_PRIVY_CLIENT_ID= # optional, you can leave this empty
# VAPID Keys for push notifications
NEXT_PUBLIC_VAPID_PUBLIC_KEY=your_vapid_public_key_here
VAPID_PRIVATE_KEY=your_vapid_private_key_here
# 0x Configuration
ZEROX_API_KEY=your_0x_api_key_here
```
If you lost your Privy App ID, you can find it in the Privy dashboard.
6. Generate VAPID keys for web push notifications:
```bash theme={null}
npx web-push generate-vapid-keys --json
```
Copy the generated keys to your .env.local file (replace the placeholder values from step 5).
7. Running the Application:
**Development Mode**:
```bash theme={null}
npm run dev
```
The application will be available at [http://localhost:3000](http://localhost:3000).
**Production Mode**:
For full PWA functionality (including install prompts):
```bash theme={null}
npm run build && npm run start
```
## Folder structure of the template
```
next-serwist-privy-0x/
├── app/
│ ├── components/ # React components
│ │ ├── 0x/ # 0x Protocol integration
│ │ ├── InstallPWA.tsx # PWA install prompt
│ │ ├── SwapComponent.tsx # Token swap interface
│ │ └── ...
│ ├── api/ # API routes
│ │ ├── price/ # Token price endpoints
│ │ └── quote/ # Swap quote endpoints
│ ├── ~offline/ # Offline page
│ └── ...
├── public/ # Static assets
├── utils/ # Utility functions
└── ...
```
## Changing the app name
* Edit [`public/manifest.json`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/public/manifest.json):
* Change the `name` and `short_name` fields
* Run `npm run build` to update the app
## Notification Setup
<Warning title="Enable notifications for the best experience!">
To receive push notifications from this app, you need to enable notifications in your browser and/or system settings:
</Warning>
### Browser Settings
<Accordion title="Chrome/Edge">
1. Click the lock icon 🔒 in the address bar
2. Set "Notifications" to "Allow"
3. Or go to Settings → Privacy and security → Site Settings → Notifications
</Accordion>
<Accordion title="Firefox">
1. Click the shield icon 🛡️ in the address bar
2. Turn off "Enhanced Tracking Protection" for this site (if needed)
3. Allow notifications when prompted
4. Or go to Settings → Privacy & Security → Permissions → Notifications
</Accordion>
<Accordion title="Safari">
1. Go to Safari → Settings → Websites → Notifications
2. Find your site and set it to "Allow"
</Accordion>
### System Settings
<Accordion title="macOS">
1. System Preferences → Notifications & Focus
2. Find your browser and ensure notifications are enabled
3. Check "Allow notifications from websites" in browser settings
</Accordion>
<Accordion title="Windows">
1. Settings → System → Notifications & actions
2. Ensure your browser can send notifications
3. Check browser notification settings
</Accordion>
<Accordion title="iOS">
1. Settings → Notifications → \[Your Browser]
2. Enable "Allow Notifications"
3. Also enable in browser settings
</Accordion>
<Accordion title="Android">
1. Settings → Apps → \[Your Browser] → Notifications
2. Enable notifications
3. Check browser notification permissions
</Accordion>
### Backend Integration Required
<Info title="The [`SendNotification.tsx`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/app/components/SendNotification.tsx) component is sample code">
This requires backend implementation:
* **Save subscription data** when users subscribe (see TODO comments in code)
* **Delete subscription data** when users unsubscribe
* **Implement [`/notification` endpoint](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/app/notification/route.ts)** to send actual push notifications
* **Use `web-push` library** or similar for server-side notification delivery
</Info>
### Customizing Notification Content
To customize your push notification content, edit [`app/notification/route.ts`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/app/notification/route.ts) and modify the `title`, `message`, `icon`, and other properties in the `sendNotification` call.
## Modifying the App Icon & Splash Screen
### App Icons
Replace the icon files in the [`public/icons/`](https://github.com/monad-developers/next-serwist-privy-0x/tree/main/public/icons) directory with your custom icons:
* **`icon-512x512.png`** - Main app icon (512×512px)
* **`android-chrome-192x192.png`** - Android icon (192×192px)
* **`apple-touch-icon.png`** - iOS home screen icon (180×180px)
Also update the favicon:
* **[`public/favicon.ico`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/public/favicon.ico)** - Browser favicon
* **[`app/favicon.ico`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/app/favicon.ico)** - Next.js app favicon
### Splash Screen
Splash screens are automatically generated from your app icon and theme colors defined in [`manifest.json`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/public/manifest.json). To customize:
1. Update the `theme_color` and `background_color` in [`manifest.json`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/public/manifest.json)
2. Ensure your main icon (`icon-512x512.png`) represents your brand
3. Run `npm run build` to apply changes
<Tip>
Use tools like [PWA Asset Generator](https://www.pwabuilder.com/imageGenerator) to create all required icon sizes from a single source image.
</Tip>
## Adding More Tokens
The template currently supports WMON and USDT tokens. To add more tokens for trading, follow these steps:
### 1. Find Token Information
Before adding a token, you'll need the following information:
* **Contract Address**: The token's smart contract address
* **Symbol**: The token's symbol (e.g., "ETH", "USDC")
* **Name**: The full name of the token
* **Decimals**: Number of decimal places (usually 18 for most ERC-20 tokens)
* **Logo URI**: URL to the token's logo image
You can find this information on the DEXes that 0x Swap API supports.
To get the DEXes that 0x Swap API supports, you can query the [sources](https://api.0x.org/sources) endpoint. For reference, see [getSources](https://0x.org/docs/api#tag/Sources/operation/sources::getSources) page.
### 2. Update Token Constants
Edit [`utils/constants.ts`](https://github.com/monad-developers/next-serwist-privy-0x/blob/main/utils/constants.ts) and add your new token to three places:
#### A. Add to MONAD\_TESTNET\_TOKENS array
```typescript title="utils/constants.ts" theme={null}
export cons프로젝트로 돌아가기