import { describe, it, expect, beforeEach, vi } from 'vitest' import { EthereumTransactionService } from './ethereum-transaction-service' import type { EVMNativeTransferRequest, EVMContractCallRequest, EVMDataTransferRequest, NetworkId } from '@meshconnect/uwc-types' import type SignClient from '@walletconnect/sign-client' import type { SessionTypes } from '@walletconnect/types' // Mock dependencies vi.mock('../../utils/account-utils', () => ({ getChainIdFromNetworkId: vi.fn((networkId: NetworkId) => { const chainMap: Record = { 'eip155:1': 1, 'eip155:137': 137, 'eip155:8453': 8453, 'eip155:42161': 42161, 'eip155:10': 10 } return chainMap[networkId] || 1 }) })) vi.mock('ethers', () => ({ ethers: { Interface: vi.fn().mockImplementation(() => ({ encodeFunctionData: vi.fn().mockReturnValue('0xencodeddata') })) } })) describe('WalletConnect EthereumTransactionService', () => { let service: EthereumTransactionService let mockClient: SignClient let mockSession: SessionTypes.Struct beforeEach(() => { vi.clearAllMocks() service = new EthereumTransactionService() mockClient = { request: vi.fn().mockResolvedValue('0xtxhash') } as any mockSession = { topic: 'test-topic', pairingTopic: 'pairing-topic', relay: { protocol: 'irn' }, expiry: Date.now() + 86400, acknowledged: true, controller: 'test-controller', namespaces: {}, requiredNamespaces: {}, optionalNamespaces: {}, self: { publicKey: 'test-public-key', metadata: { name: 'Test dApp', description: 'Test', url: 'https://test.com', icons: [] } }, peer: { publicKey: 'peer-public-key', metadata: { name: 'Test Wallet', description: 'Test', url: 'https://wallet.com', icons: [] } } } }) describe('sendTransaction routing', () => { it('should route native transfer to sendNativeTransaction', async () => { const request: EVMNativeTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', amount: BigInt('1000000000000000000'), gasConfig: { gasLimit: 21000 } } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ topic: mockSession.topic, chainId: 'eip155:1' }) ) }) it('should route contract call to sendContractCall', async () => { const request: EVMContractCallRequest = { from: '0x1234567890123456789012345678901234567890', contractAddress: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', abi: [ { inputs: [ { name: 'recipient', type: 'address' }, { name: 'amount', type: 'uint256' } ], name: 'transfer', outputs: [{ name: '', type: 'bool' }], stateMutability: 'nonpayable', type: 'function' } ], functionName: 'transfer', args: ['0x0987654321098765432109876543210987654321', BigInt(1000000)] } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') }) it('should route data transfer to sendDataTransfer', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '1' } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') }) it('should throw error for batch transactions', async () => { const request = { from: '0x1234567890123456789012345678901234567890', calls: [] } as any await expect( service.sendTransaction(mockClient, mockSession, request, 'eip155:1') ).rejects.toThrow('Batch transactions are not yet supported') }) it('should throw error for invalid request type', async () => { const invalidRequest = { from: '0x1234567890123456789012345678901234567890' } as any await expect( service.sendTransaction( mockClient, mockSession, invalidRequest, 'eip155:1' ) ).rejects.toThrow('Invalid transaction request type') }) }) describe('sendDataTransfer', () => { it('should send data transfer successfully', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb000000000000000000000000123456789012345678901234567890123456789000000000000000000000000000000000000000000000000000000000000f4240', chainId: '1' } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') expect(mockClient.request).toHaveBeenCalledWith({ topic: mockSession.topic, chainId: 'eip155:1', request: { method: 'eth_sendTransaction', params: [ expect.objectContaining({ from: request.from, to: request.to, data: request.data }) ] } }) }) it('should send data transfer with value', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '1', value: BigInt('500000000000000000') } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') }) it('should send data transfer with gas config', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0x00', chainId: '137', gasConfig: { gasLimit: 100000, maxFeePerGas: 50000000000, maxPriorityFeePerGas: 2000000000 } } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:137' ) expect(result).toBe('0xtxhash') expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ chainId: 'eip155:137' }) ) }) it('should send data transfer on different networks', async () => { const networks = [ 'eip155:1', 'eip155:137', 'eip155:8453', 'eip155:42161', 'eip155:10' ] for (const networkId of networks) { vi.mocked(mockClient.request).mockClear() const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: networkId.split(':')[1] } const result = await service.sendTransaction( mockClient, mockSession, request, networkId ) expect(result).toBe('0xtxhash') expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ chainId: networkId }) ) } }) it('should handle empty data field', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0x', chainId: '1' } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ request: { method: 'eth_sendTransaction', params: [ expect.objectContaining({ data: '0x' }) ] } }) ) }) it('should handle complex encoded data', async () => { const complexData = '0x' + 'a'.repeat(1000) const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: complexData, chainId: '1' } const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') }) }) describe('parseTransaction', () => { it('should convert BigInt values to hex format', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '1', value: BigInt('1000000000000000000'), gasConfig: { gasLimit: 21000, maxFeePerGas: 50000000000, maxPriorityFeePerGas: 2000000000 } } await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ request: { method: 'eth_sendTransaction', params: [ expect.objectContaining({ from: request.from, to: request.to, data: request.data, gasLimit: expect.stringMatching(/^0x/), maxFeePerGas: expect.stringMatching(/^0x/), maxPriorityFeePerGas: expect.stringMatching(/^0x/) }) ] } }) ) }) it('should handle missing value field', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '1' } await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) const callArgs = vi.mocked(mockClient.request).mock.calls[0][0] const params = (callArgs.request as any).params[0] // Value should either not exist or be undefined/deleted expect(params.value === undefined || !('value' in params)).toBe(true) }) }) describe('error handling', () => { it('should handle WalletConnect request errors', async () => { const mockError = new Error('User rejected') vi.mocked(mockClient.request).mockRejectedValueOnce(mockError) const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '1' } await expect( service.sendTransaction(mockClient, mockSession, request, 'eip155:1') ).rejects.toThrow() }) it('should handle network mismatch', async () => { const request: EVMDataTransferRequest = { from: '0x1234567890123456789012345678901234567890', to: '0x0987654321098765432109876543210987654321', data: '0xa9059cbb', chainId: '137' // Polygon } // Send on Ethereum network const result = await service.sendTransaction( mockClient, mockSession, request, 'eip155:1' ) expect(result).toBe('0xtxhash') // Should use the activeNetworkId for chainId in request expect(mockClient.request).toHaveBeenCalledWith( expect.objectContaining({ chainId: 'eip155:1' }) ) }) }) })