Skip to content

Commit

Permalink
feat: use chain specific transports
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskares committed Aug 3, 2024
1 parent cf71148 commit d23286a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 32 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ 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({
page,
account: privateKeyToAccount(
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80",
),
transport: http(),
defaultChain: sepolia,
transports: { [sepolia.id]: http() },
});
});

Expand All @@ -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);
}
},
});
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 4 additions & 4 deletions playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
// {
Expand Down
20 changes: 14 additions & 6 deletions src/createWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ import {
Transport,
createWalletClient,
fromHex,
type Chain,
http,
} from "viem";
import * as chains from "viem/chains";

export type Wallet = ReturnType<typeof createWallet>;

export function createWallet(account: LocalAccount, transport: Transport) {
let chainId: string | undefined;
export function createWallet(
account: LocalAccount,
transports?: Record<number, Transport>,
defaultChain?: Chain,
) {
let chain: Chain = defaultChain ?? getChain();

return {
request: async ({
Expand All @@ -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") {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down
10 changes: 6 additions & 4 deletions src/installMockWallet.ts
Original file line number Diff line number Diff line change
@@ -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<string, Wallet> = new Map();

export async function installMockWallet({
account,
transport,
transports,
defaultChain,
...params
}: {
account: LocalAccount;
transport: Transport;
transports: Record<number, Transport>;
defaultChain?: Chain;
} & ({ page: Page } | { browserContext: BrowserContext })) {
const browserOrPage =
"browserContext" in params ? params.browserContext : params.page;
Expand All @@ -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 }) => {
Expand Down
25 changes: 18 additions & 7 deletions tests/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ 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({
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);
},
},
});
});
Expand All @@ -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();
});

0 comments on commit d23286a

Please sign in to comment.