Skip to content

Commit

Permalink
add eth_sendTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskares committed Jun 14, 2024
1 parent a403477 commit fee4126
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
/blob-report/
/playwright/.cache/
/dist
.env
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand All @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 17 additions & 2 deletions src/createWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/installMockWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
47 changes: 0 additions & 47 deletions tests/example.spec.ts

This file was deleted.

45 changes: 45 additions & 0 deletions tests/transaction.spec.ts
Original file line number Diff line number Diff line change
@@ -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 });
});

0 comments on commit fee4126

Please sign in to comment.