|
1 | | -# Connecting to MetaMask with Checkout SDK in Next.js |
| 1 | +# Connect with MetaMask using the Checkout SDK |
2 | 2 |
|
3 | 3 | ## Introduction |
4 | | -This example app demonstrates how to connect your Next.js application to MetaMask using the Immutable Checkout SDK. It showcases how to initialize the SDK, create a provider for MetaMask, and establish a wallet connection with or without requesting permissions. |
| 4 | +This example app demonstrates how to connect a MetaMask wallet to your application using Immutable's Checkout SDK. It shows two different connection approaches: connecting with permissions and connecting without permissions. This app serves as a practical guide for implementing wallet connections, a fundamental step in blockchain application development. |
5 | 5 |
|
6 | 6 | [View app on Github](https://github.com/immutable/ts-immutable-sdk/tree/main/examples/checkout/sdk-connect-with-nextjs) |
7 | 7 |
|
8 | 8 | ## Features Overview |
9 | | -- Get list of supported wallets |
10 | | -- Create a MetaMask provider using Checkout SDK |
11 | | -- Connect to MetaMask with permission requests |
12 | | -- Connect to MetaMask without permission requests |
13 | | -- Validate provider and connection status |
14 | | -- Get connection status |
| 9 | +- Connect to MetaMask wallet with wallet permissions |
| 10 | +- Connect to MetaMask wallet without wallet permissions |
| 11 | +- Retrieve the list of supported wallets |
| 12 | +- Check if a provider is valid |
| 13 | +- Verify wallet connection status |
15 | 14 |
|
16 | 15 | ## SDK Integration Details |
17 | 16 |
|
18 | | -### **Get Wallet Allow List**: [Retrieve supported wallets](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L28-L31) |
| 17 | +### Connect with MetaMask |
| 18 | +**Feature Name**: Connect a wallet to the application using MetaMask. |
| 19 | + |
| 20 | +**Source Code**: [source code file](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx) |
| 21 | + |
| 22 | +**Implementation**: |
| 23 | + |
| 24 | +1. Getting the list of supported wallets: |
19 | 25 | ```typescript |
20 | 26 | // Get the list of default supported providers |
21 | 27 | const type = checkout.WalletFilterTypes.ALL; |
22 | 28 | const allowListRes = await checkoutSDK.getWalletAllowList({ type }); |
23 | 29 | ``` |
24 | | -**Explanation**: This code retrieves a list of all wallets supported by the Checkout SDK. The `WalletFilterTypes.ALL` parameter ensures all available wallet providers are returned in the response. |
25 | 30 |
|
26 | | -### **Create MetaMask Provider**: [Initialize wallet provider](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L35-L38) |
| 31 | +2. Creating a MetaMask provider: |
27 | 32 | ```typescript |
28 | 33 | // Create a provider given one of the default wallet provider names |
29 | 34 | const walletProviderName = checkout.WalletProviderName.METAMASK; |
30 | 35 | const providerRes = await checkoutSDK.createProvider({ walletProviderName }); |
31 | 36 | ``` |
32 | | -**Explanation**: This code creates a provider specifically for MetaMask. The SDK handles the interaction with the browser extension and returns a provider object that can be used for subsequent operations. |
33 | 37 |
|
34 | | -### **Validate Provider**: [Check provider validity](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L43-L45) |
| 38 | +3. Validating the provider: |
35 | 39 | ```typescript |
36 | | -// Check if the provider if a BrowserProvider |
| 40 | +// Check if the provider is a BrowserProvider |
37 | 41 | const isProviderRes = checkout.Checkout.isWrappedBrowserProvider(providerRes.provider); |
38 | 42 | ``` |
39 | | -**Explanation**: This function validates that the provider returned by the SDK is a proper BrowserProvider that can be used for blockchain interactions. This validation is essential before attempting to connect to a network. |
40 | 43 |
|
41 | | -### **Connect with Permissions**: [Connect with wallet permissions](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L50-L55) |
| 44 | +4. Connecting to the wallet with permissions: |
42 | 45 | ```typescript |
43 | | -// Get the current network information |
44 | | -// Pass through requestWalletPermissions to request the user's wallet permissions |
| 46 | +// Connect with permissions |
45 | 47 | const connectRes = await checkoutSDK.connect({ |
46 | 48 | provider: providerRes.provider, |
47 | 49 | requestWalletPermissions: true, |
48 | 50 | }); |
49 | 51 | ``` |
50 | | -**Explanation**: This code establishes a connection to the wallet while explicitly requesting permissions from the user. This approach is useful when your application needs specific permissions to interact with the user's wallet. |
51 | 52 |
|
52 | | -### **Connect without Permissions**: [Connect without additional permissions](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L59-L63) |
| 53 | +5. Connecting to the wallet without permissions: |
53 | 54 | ```typescript |
54 | | -// Get the current network information |
| 55 | +// Connect without permissions |
55 | 56 | const connectRes = await checkoutSDK.connect({ |
56 | 57 | provider: providerRes.provider |
57 | 58 | }); |
58 | 59 | ``` |
59 | | -**Explanation**: This code connects to the wallet without explicitly requesting permissions. This is suitable for applications that only need basic wallet interactions. |
60 | 60 |
|
61 | | -### **Check Connection Status**: [Verify connection status](https://github.com/immutable/ts-immutable-sdk/blob/main/examples/checkout/sdk-connect-with-nextjs/src/app/connect-with-metamask/page.tsx#L67-L70) |
| 61 | +6. Checking connection status: |
62 | 62 | ```typescript |
63 | | -// Check if the provider if a BrowserProvider |
| 63 | +// Check if the provider is connected |
64 | 64 | const isConnectedRes = await checkoutSDK.checkIsWalletConnected({ |
65 | 65 | provider: providerRes.provider |
66 | 66 | }); |
67 | 67 | ``` |
68 | | -**Explanation**: This function verifies if the wallet is connected and returns the connection status along with the wallet address. This is useful for displaying the user's wallet status in the UI. |
| 68 | + |
| 69 | +**Explanation**: |
| 70 | +The app first retrieves a list of supported wallet providers using the `getWalletAllowList` method. It then creates a MetaMask provider using the `createProvider` method with the MetaMask wallet provider name. |
| 71 | + |
| 72 | +Before attempting to connect, it validates the provider using `isWrappedBrowserProvider`. The connection to the wallet is made through the `connect` method, which can be called with or without requesting permissions. The difference is that when requesting permissions, the wallet will explicitly prompt the user for permission to interact with their wallet. |
| 73 | + |
| 74 | +Finally, the app verifies if the wallet is connected using the `checkIsWalletConnected` method, which returns the connection status and wallet address. |
69 | 75 |
|
70 | 76 | ## Running the App |
71 | 77 |
|
72 | 78 | ### Prerequisites |
73 | | -- Node.js 18.x or higher |
74 | | -- PNPM package manager |
| 79 | +- Node.js and pnpm installed on your machine |
75 | 80 | - MetaMask browser extension installed |
76 | | -- [Immutable Hub](https://hub.immutable.com/) account for obtaining a publishable API key |
| 81 | +- [Set up your environment variables in Immutable Hub](https://hub.immutable.com) |
| 82 | + |
| 83 | +### Steps to Run Locally |
77 | 84 |
|
78 | | -### Setup Instructions |
79 | 85 | 1. Clone the repository: |
80 | | - ```bash |
81 | | - git clone https://github.com/immutable/ts-immutable-sdk.git |
82 | | - cd ts-immutable-sdk/examples/checkout/sdk-connect-with-nextjs |
83 | | - ``` |
| 86 | +```bash |
| 87 | +git clone https://github.com/immutable/ts-immutable-sdk.git |
| 88 | +cd ts-immutable-sdk |
| 89 | +``` |
84 | 90 |
|
85 | 91 | 2. Install dependencies: |
86 | | - ```bash |
87 | | - pnpm install |
88 | | - ``` |
| 92 | +```bash |
| 93 | +pnpm install |
| 94 | +``` |
| 95 | + |
| 96 | +3. Navigate to the example app: |
| 97 | +```bash |
| 98 | +cd examples/checkout/sdk-connect-with-nextjs |
| 99 | +``` |
89 | 100 |
|
90 | | -3. Create environment file: |
91 | | - ```bash |
92 | | - cp .env.example .env |
93 | | - ``` |
| 101 | +4. Create your environment file: |
| 102 | +```bash |
| 103 | +cp .env.example .env |
| 104 | +``` |
94 | 105 |
|
95 | | -4. Add your Immutable Hub publishable key to the `.env` file: |
96 | | - ``` |
97 | | - NEXT_PUBLIC_PUBLISHABLE_KEY=your_publishable_key_here |
98 | | - ``` |
| 106 | +5. Edit the `.env` file and add your publishable API key from Immutable Hub: |
| 107 | +``` |
| 108 | +NEXT_PUBLIC_PUBLISHABLE_KEY=your-publishable-key |
| 109 | +``` |
99 | 110 |
|
100 | | -5. Run the development server: |
101 | | - ```bash |
102 | | - pnpm dev |
103 | | - ``` |
| 111 | +6. Start the development server: |
| 112 | +```bash |
| 113 | +pnpm dev |
| 114 | +``` |
104 | 115 |
|
105 | | -6. Open [http://localhost:3000](http://localhost:3000) in your browser to view the app. |
| 116 | +7. Open your browser and navigate to http://localhost:3000 to see the application. |
106 | 117 |
|
107 | 118 | ## Summary |
108 | | -This example demonstrates how to integrate Immutable's Checkout SDK with a Next.js application to connect to MetaMask. The app showcases different connection methods (with and without permissions), provider validation, and retrieving wallet information. It provides a foundation for building more complex blockchain applications that require wallet connectivity. |
109 | | - |
110 | | -Key takeaways: |
111 | | -- The Checkout SDK simplifies wallet connection in Next.js applications |
112 | | -- You can choose different connection approaches based on your permission requirements |
113 | | -- The SDK provides helpful utilities for validating connections and retrieving wallet information |
114 | | -- The integration works seamlessly with React's state management for displaying connection status |
| 119 | +This example demonstrates how to implement wallet connection functionality using Immutable's Checkout SDK. By following this guide, developers can easily integrate MetaMask wallet connectivity into their applications, with options for requesting different permission levels. The app showcases a straightforward implementation that can be extended to build more complex blockchain applications. |
0 commit comments