diff --git a/README.md b/README.md index 79af9c0..3f95aea 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ import { test } from "@playwright/test"; import { installMockWallet } from "@johanneskares/wallet-mock"; import { privateKeyToAccount } from "viem/accounts"; import { http } from "viem"; +import { sepolia } from "viem/chains"; test.beforeEach(async ({ page }) => { await installMockWallet({ @@ -25,7 +26,8 @@ test.beforeEach(async ({ page }) => { account: privateKeyToAccount( "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", ), - transport: http(), + defaultChain: sepolia, + transports: { [sepolia.id]: http() }, }); }); @@ -51,17 +53,20 @@ await installMockWallet({ account: privateKeyToAccount( "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", ), - transport: (config) => { - return custom({ - request: async ({ method, params }) => { - // Mock only this RPC call - if (method === "eth_sendTransaction") { - return "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; - } + defaultChain: sepolia, + transports: { + [sepolia.id]: (config) => { + return custom({ + request: async ({ method, params }) => { + // Mock only this RPC call + if (method === "eth_sendTransaction") { + return "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"; + } - return await http()(config).request({ method, params }); - }, - })(config); + return await http()(config).request({ method, params }); + }, + })(config); + } }, }); ``` diff --git a/package.json b/package.json index 8c1b106..3bf34e4 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "dev": "tsc -W", "test": "playwright test", "test-ui": "playwright test --ui", + "test-debug": "npx playwright test --project='Google Chrome' --debug", "install-conventional-commit": "curl -o- https://raw.githubusercontent.com/tapsellorg/conventional-commits-git-hook/master/scripts/install.sh | sh" } } diff --git a/playwright.config.ts b/playwright.config.ts index 6e13d92..ebf4984 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -40,10 +40,10 @@ export default defineConfig({ // use: { ...devices['Desktop Firefox'] }, // }, - // { - // name: 'webkit', - // use: { ...devices['Desktop Safari'] }, - // }, + { + name: "webkit", + use: { ...devices["Desktop Safari"] }, + }, /* Test against mobile viewports. */ // { diff --git a/src/createWallet.ts b/src/createWallet.ts index e0f1886..a7ff90e 100644 --- a/src/createWallet.ts +++ b/src/createWallet.ts @@ -4,13 +4,19 @@ import { Transport, createWalletClient, fromHex, + type Chain, + http, } from "viem"; import * as chains from "viem/chains"; export type Wallet = ReturnType; -export function createWallet(account: LocalAccount, transport: Transport) { - let chainId: string | undefined; +export function createWallet( + account: LocalAccount, + transports?: Record, + defaultChain?: Chain, +) { + let chain: Chain = defaultChain ?? getChain(); return { request: async ({ @@ -23,8 +29,8 @@ export function createWallet(account: LocalAccount, transport: Transport) { try { const client = createWalletClient({ account, - chain: getChain(chainId), - transport, + chain, + transport: transports?.[chain.id] ?? http(), }); if (method === "eth_accounts" || method === "eth_requestAccounts") { @@ -38,8 +44,10 @@ export function createWallet(account: LocalAccount, transport: Transport) { return [{ parentCapability: "eth_accounts" }]; } + if (method === "wallet_getPermissions") return []; + if (method === "wallet_switchEthereumChain") { - chainId = (params?.[0] as any).chainId; + chain = getChain((params?.[0] as any).chainId); return null; } @@ -78,7 +86,7 @@ export function createWallet(account: LocalAccount, transport: Transport) { }; } -function getChain(chainIdHex: string | undefined) { +function getChain(chainIdHex?: string) { if (!chainIdHex) return chains.mainnet; const chainId = fromHex(chainIdHex as Hex, "number"); diff --git a/src/installMockWallet.ts b/src/installMockWallet.ts index 2f9ab15..e30e6ef 100644 --- a/src/installMockWallet.ts +++ b/src/installMockWallet.ts @@ -1,17 +1,19 @@ import type { BrowserContext, Page } from "@playwright/test"; import { Wallet, createWallet } from "./createWallet"; -import { LocalAccount, Transport } from "viem"; +import { Chain, LocalAccount, Transport } from "viem"; import { randomUUID } from "crypto"; let wallets: Map = new Map(); export async function installMockWallet({ account, - transport, + transports, + defaultChain, ...params }: { account: LocalAccount; - transport: Transport; + transports: Record; + defaultChain?: Chain; } & ({ page: Page } | { browserContext: BrowserContext })) { const browserOrPage = "browserContext" in params ? params.browserContext : params.page; @@ -21,7 +23,7 @@ export async function installMockWallet({ // Everytime we call installMockWallet, we create a new uuid to identify the wallet. const uuid = randomUUID(); - wallets.set(uuid, createWallet(account, transport)); + wallets.set(uuid, createWallet(account, transports, defaultChain)); await browserOrPage.addInitScript( ({ uuid }) => { diff --git a/tests/transaction.spec.ts b/tests/transaction.spec.ts index dc25483..604e3ab 100644 --- a/tests/transaction.spec.ts +++ b/tests/transaction.spec.ts @@ -2,6 +2,7 @@ import { expect, test } from "@playwright/test"; import { installMockWallet } from "./../src/installMockWallet"; import { privateKeyToAccount } from "viem/accounts"; import { custom, http, isHex } from "viem"; +import { sepolia } from "viem/chains"; test.beforeEach(async ({ page }) => { await installMockWallet({ @@ -9,13 +10,21 @@ test.beforeEach(async ({ page }) => { account: privateKeyToAccount( isHex(process.env.PRIVATE_KEY) ? process.env.PRIVATE_KEY : "0x", ), - transport: (config) => { - return custom({ - request: async ({ method, params }) => { - console.log("LOG", method, params); - return await http()(config).request({ method, params }); - }, - })(config); + defaultChain: sepolia, + transports: { + [sepolia.id]: (config) => { + return custom({ + request: async ({ method, params }) => { + let result: unknown; + try { + result = await http()(config).request({ method, params }); + } finally { + console.log("METHOD", method, "PARAMS", params, "RESULT", result); + } + return result; + }, + })(config); + }, }, }); }); @@ -29,4 +38,6 @@ test("Metamask Wallet Test Dapp", async ({ page }) => { page.getByRole("heading", { name: "Active Provider" }), ).toBeVisible(); await expect(page.getByText("Name: Mock Wallet")).toBeVisible(); + + await page.pause(); });