diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d0b389100..aada3eba7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -263,10 +263,10 @@ importers: dependencies: '@depay/web3-client': specifier: 10.18.6 - version: 10.18.6(@depay/solana-web3.js@1.26.0)(@depay/web3-blockchains@9.4.4)(ethers@5.7.2) + version: 10.18.6(@depay/solana-web3.js@1.26.0)(@depay/web3-blockchains@9.7.5)(ethers@5.7.2) '@depay/web3-mock': - specifier: 14.17.0 - version: 14.17.0 + specifier: 14.19.1 + version: 14.19.1 '@depay/web3-mock-evm': specifier: ^14.17.0 version: 14.18.0 @@ -581,6 +581,10 @@ packages: resolution: {integrity: sha512-z/L7QjtGCavkNOt+gzZbg44aILazzjQKK+ZAniNCy+DeT1HRNbBC3HXAfprb6GNiBaZBk6rmsLf8o1GsYQMa8g==} engines: {node: '>=10'} + '@depay/web3-blockchains@9.7.5': + resolution: {integrity: sha512-Nhte6p1mYolqe58JS3Gt3ebn1/xsMMzytNhHk6BK/rpCeqTwX2WyeaYeTAHXNn0sm99hlZM4Qf2Vo4BeZUKOLw==} + engines: {node: '>=18'} + '@depay/web3-client@10.18.6': resolution: {integrity: sha512-JeUAZ04/dsIra1ao3mvqAdVhRf4U1YxSoH0mE+XxhcXPgLC7KQTzH6oCLp07tAmoxholEL2cf5Oo20n6q1fZ/w==} engines: {node: '>=16'} @@ -593,8 +597,8 @@ packages: resolution: {integrity: sha512-St3Pzhw2zi5io+u+R48iKTTryZg9Dzsy+zGxBMMCd28rc/sSJGla5e/FjieMYjPtJaRGXX/jVJoEhOdB2BHH9Q==} engines: {node: '>=16'} - '@depay/web3-mock@14.17.0': - resolution: {integrity: sha512-0WCIpHqGUTPmOb5l3iN+4wCY+P3nHnGWd3uyWB+Wrt5DygS6MWI2b50gwtSCgYUCfgmEv9KlRuCnHDC4TDKCeA==} + '@depay/web3-mock@14.19.1': + resolution: {integrity: sha512-bBM1J0EWDWXJKVPtzo8YrX7fbGwUATYWN8kaJniQU2z5V+UK3kVhjQi+en0JMF9cCjinkERK7MqoZLaYR+cb+Q==} engines: {node: '>=16'} '@docsearch/css@3.6.1': @@ -5426,10 +5430,12 @@ snapshots: '@depay/web3-blockchains@9.4.4': {} - '@depay/web3-client@10.18.6(@depay/solana-web3.js@1.26.0)(@depay/web3-blockchains@9.4.4)(ethers@5.7.2)': + '@depay/web3-blockchains@9.7.5': {} + + '@depay/web3-client@10.18.6(@depay/solana-web3.js@1.26.0)(@depay/web3-blockchains@9.7.5)(ethers@5.7.2)': dependencies: '@depay/solana-web3.js': 1.26.0 - '@depay/web3-blockchains': 9.4.4 + '@depay/web3-blockchains': 9.7.5 ethers: 5.7.2 '@depay/web3-mock-evm@14.18.0': @@ -5440,10 +5446,10 @@ snapshots: - bufferutil - utf-8-validate - '@depay/web3-mock@14.17.0': + '@depay/web3-mock@14.19.1': dependencies: '@depay/solana-web3.js': 1.26.0 - '@depay/web3-blockchains': 9.4.4 + '@depay/web3-blockchains': 9.7.5 ethers: 5.7.2 transitivePeerDependencies: - bufferutil diff --git a/wallets/ethereum-wallet-mock/package.json b/wallets/ethereum-wallet-mock/package.json index 15fadaf7d..c53611897 100644 --- a/wallets/ethereum-wallet-mock/package.json +++ b/wallets/ethereum-wallet-mock/package.json @@ -35,13 +35,13 @@ }, "dependencies": { "@depay/web3-client": "10.18.6", - "@depay/web3-mock": "14.17.0", + "@depay/web3-mock": "14.19.1", "@depay/web3-mock-evm": "^14.17.0", "@synthetixio/synpress-core": "0.0.4", "viem": "2.9.9" }, "devDependencies": { - "@depay/web3-mock": "14.17.0", + "@depay/web3-mock": "14.19.1", "@synthetixio/synpress-tsconfig": "0.0.4", "@types/node": "20.11.17", "@vitest/coverage-v8": "1.2.2", diff --git a/wallets/ethereum-wallet-mock/src/playwright/EthereumWalletMock.ts b/wallets/ethereum-wallet-mock/src/playwright/EthereumWalletMock.ts index 8f058b42e..9a7be2d9f 100644 --- a/wallets/ethereum-wallet-mock/src/playwright/EthereumWalletMock.ts +++ b/wallets/ethereum-wallet-mock/src/playwright/EthereumWalletMock.ts @@ -5,6 +5,7 @@ import { EthereumWalletMockAbstract } from '../type/EthereumWalletMockAbstract' import type { Network } from '../type/Network' import type { WalletMock } from '../type/WalletMock' import { DEFAULT_NETWORK_ID } from './utils' +import { mockTransaction, sendTransaction } from './utils' /** * Mock implementation of an Ethereum wallet for testing purposes. @@ -230,4 +231,26 @@ export default class EthereumWalletMock extends EthereumWalletMockAbstract { [BLOCKCHAIN, [ACCOUNT_MOCK], wallet] ) } + + /** + * Sends a transaction. + * @param to - Recipient address. + * @param value - Transaction value in wei. + * @returns Promise that resolves to the transaction hash. + */ + async sendTransaction(to: string, value: string): Promise { + // Mock the transaction + await mockTransaction(this.page, [ + to, + ACCOUNT_MOCK, // from address + value + ]) + + // Send the transaction + return this.page.evaluate(sendTransaction, { + to, + from: ACCOUNT_MOCK, + value + }) + } } diff --git a/wallets/ethereum-wallet-mock/src/playwright/utils/index.ts b/wallets/ethereum-wallet-mock/src/playwright/utils/index.ts index 77d5358b2..f58b0b85d 100644 --- a/wallets/ethereum-wallet-mock/src/playwright/utils/index.ts +++ b/wallets/ethereum-wallet-mock/src/playwright/utils/index.ts @@ -1,2 +1,4 @@ export { default as mockEthereum } from './mockEthereum' +export * from './mockTransaction' +export * from './sendTransaction' export * from '../constants' diff --git a/wallets/ethereum-wallet-mock/src/playwright/utils/mockTransaction.ts b/wallets/ethereum-wallet-mock/src/playwright/utils/mockTransaction.ts new file mode 100644 index 000000000..c81c6ade4 --- /dev/null +++ b/wallets/ethereum-wallet-mock/src/playwright/utils/mockTransaction.ts @@ -0,0 +1,23 @@ +import type { Page } from '@playwright/test' + +/** + * Mocks an Ethereum transaction using Web3Mock + * @param page - Playwright page instance + * @param params - Array of transaction parameters [to, from, value] + * @returns Promise that resolves to the Web3Mock mock result + */ +export const mockTransaction = async (page: Page, params: [string, string, string]) => { + return page.evaluate( + ([params]) => { + return Web3Mock.mock({ + blockchain: 'ethereum', + transaction: { + to: params?.[0], + from: params?.[1], + value: params?.[2] + } + }) + }, + [params] + ) +} diff --git a/wallets/ethereum-wallet-mock/src/playwright/utils/sendTransaction.ts b/wallets/ethereum-wallet-mock/src/playwright/utils/sendTransaction.ts new file mode 100644 index 000000000..8f74dfa44 --- /dev/null +++ b/wallets/ethereum-wallet-mock/src/playwright/utils/sendTransaction.ts @@ -0,0 +1,17 @@ +interface SendTransactionParams { + to: string + from: string + value: string +} + +/** + * Sends an Ethereum transaction using Web3Mock + * @param params - Transaction parameters {to: string, from: string, value: string} + * @returns Promise that resolves to the transaction hash + */ +export const sendTransaction = (params: SendTransactionParams): Promise => { + return window.ethereum.request({ + method: 'eth_sendTransaction', + params: [params] + }) +} diff --git a/wallets/ethereum-wallet-mock/test/playwright/mock/sendTransaction.spec.ts b/wallets/ethereum-wallet-mock/test/playwright/mock/sendTransaction.spec.ts new file mode 100644 index 000000000..32a9001b2 --- /dev/null +++ b/wallets/ethereum-wallet-mock/test/playwright/mock/sendTransaction.spec.ts @@ -0,0 +1,57 @@ +import { ACCOUNT_MOCK } from '../../../src/constants' +import test from '../../../src/playwright/synpress' + +const { expect } = test + +const TO_ADDRESS = '0x5Af489c8786A018EC4814194dC8048be1007e390' +const VALUE = '2000000000000000000' // 2 ETH + +test('should mock and send transaction', async ({ ethereumWalletMock }) => { + // Connect wallet first + await ethereumWalletMock.connectToDapp() + + // Send the transaction + const txHash = await ethereumWalletMock.sendTransaction(TO_ADDRESS, VALUE) + + // Verify transaction hash format + expect(txHash).toMatch(/^0x[a-fA-F0-9]{64}$/) +}) + +test('should mock and send transaction with zero value', async ({ ethereumWalletMock }) => { + await ethereumWalletMock.connectToDapp() + + const txHash = await ethereumWalletMock.sendTransaction(TO_ADDRESS, '0') + + expect(txHash).toMatch(/^0x[a-fA-F0-9]{64}$/) +}) + +test('should mock and send transaction to same address', async ({ ethereumWalletMock }) => { + await ethereumWalletMock.connectToDapp() + + const txHash = await ethereumWalletMock.sendTransaction(ACCOUNT_MOCK, VALUE) + + expect(txHash).toMatch(/^0x[a-fA-F0-9]{64}$/) +}) + +test('should mock and send multiple transactions', async ({ ethereumWalletMock }) => { + await ethereumWalletMock.connectToDapp() + + const transactions = [ + { to: TO_ADDRESS, value: '1000000000000000000' }, // 1 ETH + { to: TO_ADDRESS, value: '3000000000000000000' }, // 3 ETH + { to: TO_ADDRESS, value: '500000000000000000' } // 0.5 ETH + ] + + for (const tx of transactions) { + const txHash = await ethereumWalletMock.sendTransaction(tx.to, tx.value) + expect(txHash).toMatch(/^0x[a-fA-F0-9]{64}$/) + } +}) + +test('should fail with invalid value', async ({ ethereumWalletMock }) => { + await ethereumWalletMock.connectToDapp() + + const invalidValue = 'invalid' + + await expect(ethereumWalletMock.sendTransaction(TO_ADDRESS, invalidValue)).rejects.toThrow() +})