From 7b76394171456ac0ccd09d58760e6f0e164b9a22 Mon Sep 17 00:00:00 2001 From: 0xward Date: Thu, 29 Jan 2026 08:43:45 +0700 Subject: [PATCH 1/4] Create connectBaseWallet.ts --- src/helpers/connectBaseWallet.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/helpers/connectBaseWallet.ts diff --git a/src/helpers/connectBaseWallet.ts b/src/helpers/connectBaseWallet.ts new file mode 100644 index 000000000..ea43c3bb1 --- /dev/null +++ b/src/helpers/connectBaseWallet.ts @@ -0,0 +1,21 @@ +import { ethers } from "ethers"; + +/** + * Connects MetaMask to the Base network and returns an ethers.js signer. + */ +export async function connectBaseWallet() { + if (!window.ethereum) throw new Error("MetaMask not detected"); + + const provider = new ethers.BrowserProvider(window.ethereum); + const accounts = await provider.send("eth_requestAccounts", []); + const network = await provider.getNetwork(); + + // Base mainnet chain ID = 8453 + if (network.chainId !== 8453) { + throw new Error(`Wrong network: ${network.chainId}. Please switch to Base Mainnet.`); + } + + const signer = await provider.getSigner(); + console.log("Connected to Base as:", accounts[0]); + return { provider, signer, address: accounts[0] }; +} From b0f59339cb525d789e60936df45cbdfabc506af2 Mon Sep 17 00:00:00 2001 From: 0xward Date: Thu, 29 Jan 2026 08:44:24 +0700 Subject: [PATCH 2/4] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 5c37a5bbb..0a637ba7a 100644 --- a/README.md +++ b/README.md @@ -218,3 +218,13 @@ yarn add @base-org/account 1. Fork this repo and clone it 1. From the root dir run `yarn install` 1. From the root dir run `yarn dev` + + +### 🧩 connectBaseWallet Helper +A lightweight function to connect MetaMask to the Base network and return an ethers.js signer. + +**Example usage:** +```ts +import { connectBaseWallet } from "./helpers/connectBaseWallet"; +const { signer } = await connectBaseWallet(); + From 74e048969cf677cdbf259329d577476653304483 Mon Sep 17 00:00:00 2001 From: 0xward Date: Thu, 29 Jan 2026 08:45:04 +0700 Subject: [PATCH 3/4] Create switchToBase.ts --- src/utils/switchToBase.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/utils/switchToBase.ts diff --git a/src/utils/switchToBase.ts b/src/utils/switchToBase.ts new file mode 100644 index 000000000..f7d94e47d --- /dev/null +++ b/src/utils/switchToBase.ts @@ -0,0 +1,19 @@ +/** + * Requests MetaMask to switch network to Base Mainnet. + */ +export async function switchToBaseNetwork() { + if (!window.ethereum) throw new Error("MetaMask not found"); + + await window.ethereum.request({ + method: "wallet_addEthereumChain", + params: [{ + chainId: "0x2105", // 8453 + chainName: "Base Mainnet", + nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 }, + rpcUrls: ["https://mainnet.base.org"], + blockExplorerUrls: ["https://basescan.org"] + }] + }); + + console.log("Switched to Base network ✅"); +} From 4d6979f4fd25980d860bab205dda283e45a610d8 Mon Sep 17 00:00:00 2001 From: 0xward Date: Thu, 29 Jan 2026 08:45:33 +0700 Subject: [PATCH 4/4] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 0a637ba7a..8e9c4cdf0 100644 --- a/README.md +++ b/README.md @@ -228,3 +228,6 @@ A lightweight function to connect MetaMask to the Base network and return an eth import { connectBaseWallet } from "./helpers/connectBaseWallet"; const { signer } = await connectBaseWallet(); +### 🧠 switchToBaseNetwork +Quick helper to prompt users to switch their MetaMask network to Base Mainnet. +