diff --git a/.gitignore b/.gitignore index bbc06c5..00b1601 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ /blob-report/ /playwright/.cache/ /dist +.env diff --git a/package-lock.json b/package-lock.json index 4e6aa89..bf4374b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "@johanneskares/wallet-mock", - "version": "0.1.1", + "version": "0.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@johanneskares/wallet-mock", - "version": "0.1.1", + "version": "0.1.4", "license": "ISC", "devDependencies": { "@playwright/test": "^1.44.1", "@types/node": "^20.14.2", + "dotenv": "^16.4.5", "prettier": "^3.3.2", "typescript": "^5.4.5", "viem": "^2.13.8" @@ -131,6 +132,18 @@ } } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "dev": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/fsevents": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", diff --git a/package.json b/package.json index e3f841b..9132e72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@johanneskares/wallet-mock", - "version": "0.1.4", + "version": "0.1.5", "description": "Mock Web3 Browser wallets, like Metamask, in Playwright tests.", "main": "dist/index.js", "types": "dist/index.d.ts", @@ -19,6 +19,7 @@ "devDependencies": { "@playwright/test": "^1.44.1", "@types/node": "^20.14.2", + "dotenv": "^16.4.5", "prettier": "^3.3.2", "typescript": "^5.4.5", "viem": "^2.13.8" @@ -28,7 +29,6 @@ "url": "git+https://github.com/johanneskares/wallet-mock.git" }, "namespace": "@johanneskares", - "dependencies": {}, "peerDependencies": { "@playwright/test": "^1.44.1", "viem": "^2.13.8" diff --git a/playwright.config.ts b/playwright.config.ts index f9e103f..6e13d92 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,10 +1,8 @@ import { defineConfig, devices } from "@playwright/test"; +import dotenv from "dotenv"; -/** - * Read environment variables from file. - * https://github.com/motdotla/dotenv - */ -// require('dotenv').config(); +// Read enviornment variables from file +dotenv.config(); /** * See https://playwright.dev/docs/test-configuration. diff --git a/src/createWallet.ts b/src/createWallet.ts index 047aa22..c1093ba 100644 --- a/src/createWallet.ts +++ b/src/createWallet.ts @@ -28,7 +28,7 @@ export function createWallet(account: LocalAccount, transport: Transport) { }); if (method === "eth_accounts" || method === "eth_requestAccounts") { - return client.getAddresses(); + return await client.getAddresses(); } if ( @@ -44,13 +44,28 @@ export function createWallet(account: LocalAccount, transport: Transport) { } if (method === "personal_sign") { - return client.account.signMessage({ + return await client.account.signMessage({ message: { raw: params?.[0] as Hex, }, }); } + if (method === "eth_sendTransaction") { + const from = (params?.[0] as any).from; + if (from !== account.address) throw new Error("Invalid from address"); + + return await client.sendTransaction({ + to: (params?.[0] as any).to, + data: (params?.[0] as any).data, + gas: (params?.[0] as any).gas, + gasPrice: (params?.[0] as any).gasPrice, + value: (params?.[0] as any).value, + maxFeePerGas: (params?.[0] as any).maxFeePerGas, + maxPriorityFeePerGas: (params?.[0] as any).maxPriorityFeePerGas, + }); + } + return await client.request({ method: method as any, params: params as any, diff --git a/src/installMockWallet.ts b/src/installMockWallet.ts index 0d2a9db..4fc0c42 100644 --- a/src/installMockWallet.ts +++ b/src/installMockWallet.ts @@ -93,11 +93,13 @@ async function eip1193Request({ const wallet = wallets.get(uuid); if (wallet == null) throw new Error("Account or transport not found"); + // console.log("eip1193Request", method, params); + const result = await wallet.request({ method, params, }); - console.log("eip1193Request", method, params, result); + // console.log("eip1193Result", result); return result; } diff --git a/tests/example.spec.ts b/tests/example.spec.ts deleted file mode 100644 index 858cd83..0000000 --- a/tests/example.spec.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { expect, test } from "@playwright/test"; -import { installMockWallet } from "./../src/installMockWallet"; -import { privateKeyToAccount } from "viem/accounts"; -import { http } from "viem"; - -test.beforeEach(async ({ page }) => { - await installMockWallet({ - page, - account: privateKeyToAccount( - "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", - ), - transport: http(), - }); -}); - -test("talentir", async ({ page }) => { - await page.goto("https://dev.talentir.com"); - - await page.getByRole("button", { name: "Accept all" }).click(); - - await page.getByRole("button", { name: "Log In" }).click(); - - await page.getByRole("button", { name: "Choose Wallet" }).click(); - - await page.getByRole("menuitem", { name: "Mock Wallet" }).last().click(); - - await expect(page).toHaveURL("https://dev.talentir.com/signup/username"); - await page.goto("https://dev.talentir.com/dashboard/wallet"); - - // wait for 10 seconds - await page.waitForTimeout(2000); -}); - -test("jumper", async ({ page }) => { - await page.goto("https://jumper.exchange/exchange"); - - await page.getByRole("button", { name: "Connect" }).click(); - - await page.getByRole("button", { name: "Get Started" }).click(); - - // await page.getByRole("button", { name: "Choose Wallet" }).click(); - - // await page.getByRole("menuitem", { name: "Mock Wallet" }).click(); - - // wait for 10 seconds - await page.waitForTimeout(2000); -}); diff --git a/tests/transaction.spec.ts b/tests/transaction.spec.ts new file mode 100644 index 0000000..fa526c8 --- /dev/null +++ b/tests/transaction.spec.ts @@ -0,0 +1,45 @@ +import { expect, test } from "@playwright/test"; +import { installMockWallet } from "./../src/installMockWallet"; +import { privateKeyToAccount } from "viem/accounts"; +import { http, isHex } from "viem"; + +test.beforeEach(async ({ page }) => { + await installMockWallet({ + page, + account: privateKeyToAccount( + isHex(process.env.PRIVATE_KEY) ? process.env.PRIVATE_KEY : "0x", + ), + transport: http(), + }); +}); + +test("talentir", async ({ page }) => { + const baseUrl = "https://dev.talentir.com"; + // const baseUrl = "http://localhost:3000"; + await page.goto(baseUrl); + + await page.getByRole("button", { name: "Accept all" }).click(); + await page.getByRole("button", { name: "Log In" }).click(); + await page.getByRole("button", { name: "Choose Wallet" }).click(); + await page.getByRole("menuitem", { name: "Mock Wallet" }).last().click(); + + await expect(page).toHaveURL(baseUrl + "/dashboard/assets"); + await page.getByRole("link", { name: "Wallet" }).click(); + + await page.waitForTimeout(2000); + + await page.getByLabel("Transfer").click(); + await page + .getByPlaceholder("0xe0a942ff2e1724A2fe10627728bE327a43fE8C23") + .fill("0xe0a942ff2e1724A2fe10627728bE327a43fE8C26"); + + await page.getByPlaceholder("-").fill("- 0.01"); + + await page.getByRole("button", { name: "Send USDC" }).click(); + await page.getByLabel("I take full responsibility").click(); + await page.getByRole("button", { name: "Confirm" }).click(); + + await expect( + page.getByRole("heading", { name: "Transaction Successful" }), + ).toBeVisible({ timeout: 30000 }); +});