|
1 | 1 | import { |
2 | | - type Chain, |
3 | 2 | createWalletClient, |
4 | 3 | custom, |
5 | 4 | fallback, |
6 | | - type Hex, |
7 | 5 | http, |
8 | | - PrivateKeyAccount, |
9 | | - type RpcTransactionRequest, |
| 6 | + type Address, |
| 7 | + type Chain, |
| 8 | + type CustomTransport, |
| 9 | + type Hex, |
| 10 | + type PrivateKeyAccount, |
10 | 11 | } from 'viem' |
11 | 12 | import { privateKeyToAccount } from 'viem/accounts' |
12 | | -import type { TenderlyConfig } from '@cy/support/helpers/tenderly/account' |
13 | | -import { Address } from '@ui-kit/utils' |
14 | | -import { createConnector, type CreateConnectorFn } from '@wagmi/core' |
15 | | -import { sendVnetTransaction } from './tenderly/vnet-transaction' |
16 | | - |
17 | | -type Options = { |
18 | | - /** A hexadecimal private key used to generate a test account */ |
19 | | - privateKey: Hex |
20 | | - /** The testnet chain configuration */ |
21 | | - chain: Chain |
22 | | - /** Tenderly configuration for the Virtual Testnet */ |
23 | | - tenderly: TenderlyConfig |
24 | | -} |
| 13 | +import { type CreateConnectorFn, createConnector } from 'wagmi' |
25 | 14 |
|
26 | 15 | type ConnectParams<T> = { chainId?: number; isReconnecting?: boolean; withCapabilities: T } |
27 | 16 | type ConnectResult<T> = { accounts: readonly T[]; chainId: number } |
28 | 17 | type Account = { address: Address; capabilities: Record<string, unknown> } |
29 | 18 |
|
30 | | -/** |
31 | | - * Creates a custom transport that intercepts JSON-RPC requests to handle account retrieval and |
32 | | - * transaction sending via Tenderly Virtual Testnet Admin API. |
33 | | - * |
34 | | - * This is necessary because our code under test uses a BrowserProvider with http transport, |
35 | | - * which relies on RPC methods to retrieve accounts and send transactions. |
36 | | - */ |
37 | | -const customTransport = (account: PrivateKeyAccount, tenderly: TenderlyConfig) => |
| 19 | +/** Default custom transport for Cypress E2E tests, read-only */ |
| 20 | +const cypressTransport = (account: PrivateKeyAccount) => |
38 | 21 | custom({ |
39 | | - request: async ({ method, params: [param] }): Promise<any> => { |
| 22 | + request: async ({ method }): Promise<any> => { |
40 | 23 | if (method === 'eth_accounts') { |
41 | 24 | return [account.address] |
42 | 25 | } |
43 | | - if (method === 'eth_sendTransaction') { |
44 | | - return await sendVnetTransaction({ tenderly, tx: param as RpcTransactionRequest }).catch((err) => { |
45 | | - console.error(`Tenderly failed for ${method}(${JSON.stringify(param)}). Error: ${err}`) |
46 | | - throw err |
47 | | - }) |
48 | | - } |
49 | 26 | throw new Error(`Unsupported method: ${method}, http fallback is used`) |
50 | 27 | }, |
51 | 28 | }) |
52 | 29 |
|
| 30 | +export type CreateTestConnectorOptions = { |
| 31 | + /** A hexadecimal private key used to generate a test account */ |
| 32 | + privateKey: Hex |
| 33 | + /** The testnet chain configuration */ |
| 34 | + chain: Chain |
| 35 | + /** |
| 36 | + * Creates a custom transport that intercepts JSON-RPC requests to handle account retrieval and such. |
| 37 | + * |
| 38 | + * This is necessary because our code under test uses a BrowserProvider with http transport, |
| 39 | + * which relies on RPC methods not always available to retrieve accounts and send transactions. |
| 40 | + * |
| 41 | + * Defaults to a read-only custom transport for Cypress. |
| 42 | + */ |
| 43 | + transport?: (account: PrivateKeyAccount) => CustomTransport |
| 44 | +} |
| 45 | + |
53 | 46 | /** |
54 | | - * Cypress test connector for Wagmi. |
| 47 | + * Creates a wagmi test connector for Cypress. |
55 | 48 | * |
56 | | - * This connector is designed for use in test environments (e.g., Cypress) with a testnet chain. |
57 | | - * It creates a wallet using a custom seed (private key) to allow testing contract write calls |
| 49 | + * This connector is designed for use in test environments (e.g., Cypress) with optionally a testnet chain. |
| 50 | + * It creates a wallet using a custom seed (private key) to allow testing contract read and write calls |
58 | 51 | * without relying on third-party browser extensions like MetaMask. |
59 | 52 | */ |
60 | | -export function createTestConnector({ privateKey, chain, tenderly }: Options): CreateConnectorFn { |
| 53 | +export function createTestConnector({ privateKey, chain, transport }: CreateTestConnectorOptions): CreateConnectorFn { |
61 | 54 | const account = privateKeyToAccount(privateKey) |
62 | 55 |
|
63 | 56 | const client = createWalletClient({ |
64 | 57 | account, |
65 | 58 | chain, |
66 | | - transport: fallback([customTransport(account, tenderly), ...chain.rpcUrls.default.http.map((url) => http(url))]), |
| 59 | + transport: fallback([ |
| 60 | + (transport ?? cypressTransport)(account), |
| 61 | + ...chain.rpcUrls.default.http.map((url) => http(url)), |
| 62 | + ]), |
67 | 63 | }) |
68 | 64 |
|
69 | 65 | // A connect function with overloads to satisfy Wagmi's conditional return type |
|
0 commit comments