|
| 1 | +import { TransactionType } from '@metamask/transaction-controller'; |
| 2 | +import { Hex } from '@metamask/utils'; |
| 3 | +import Logger from '../../../../util/Logger'; |
| 4 | +import { validateDepositTransactions } from './validateTransactions'; |
| 5 | + |
| 6 | +jest.mock('../../../../util/Logger', () => ({ |
| 7 | + error: jest.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +const mockLoggerError = Logger.error as jest.Mock; |
| 11 | + |
| 12 | +const VALID_ADDRESS = '0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174' as Hex; |
| 13 | +const VALID_DATA = '0x095ea7b3000000000000000000000000' as Hex; |
| 14 | + |
| 15 | +function createValidTransaction(overrides = {}) { |
| 16 | + return { |
| 17 | + params: { |
| 18 | + to: VALID_ADDRESS, |
| 19 | + data: VALID_DATA, |
| 20 | + }, |
| 21 | + type: TransactionType.contractInteraction, |
| 22 | + ...overrides, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe('validateDepositTransactions', () => { |
| 27 | + const context = { providerId: 'polymarket' }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('passes validation for valid transactions', () => { |
| 34 | + const transactions = [createValidTransaction(), createValidTransaction()]; |
| 35 | + |
| 36 | + expect(() => |
| 37 | + validateDepositTransactions(transactions, context), |
| 38 | + ).not.toThrow(); |
| 39 | + expect(mockLoggerError).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('throws error when transaction is null', () => { |
| 43 | + const transactions = [null as never]; |
| 44 | + |
| 45 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 46 | + 'Invalid transaction: transaction at index 0 is null or undefined', |
| 47 | + ); |
| 48 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('throws error when transaction is undefined', () => { |
| 52 | + const transactions = [undefined as never]; |
| 53 | + |
| 54 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 55 | + 'Invalid transaction: transaction at index 0 is null or undefined', |
| 56 | + ); |
| 57 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('throws error when params object is missing', () => { |
| 61 | + const transactions = [ |
| 62 | + { type: TransactionType.contractInteraction } as never, |
| 63 | + ]; |
| 64 | + |
| 65 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 66 | + 'Invalid transaction: transaction at index 0 is missing params object', |
| 67 | + ); |
| 68 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('throws error when to address is missing', () => { |
| 72 | + const transactions = [ |
| 73 | + createValidTransaction({ params: { data: VALID_DATA } }), |
| 74 | + ]; |
| 75 | + |
| 76 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 77 | + "Invalid transaction: transaction at index 0 is missing 'to' address", |
| 78 | + ); |
| 79 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws error when to address is too short', () => { |
| 83 | + const transactions = [ |
| 84 | + createValidTransaction({ params: { to: '0xshort', data: VALID_DATA } }), |
| 85 | + ]; |
| 86 | + |
| 87 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 88 | + "Invalid transaction: transaction at index 0 has invalid 'to' address format", |
| 89 | + ); |
| 90 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws error when to address does not start with 0x', () => { |
| 94 | + const transactions = [ |
| 95 | + createValidTransaction({ |
| 96 | + params: { |
| 97 | + to: '1234567890123456789012345678901234567890ab', |
| 98 | + data: VALID_DATA, |
| 99 | + }, |
| 100 | + }), |
| 101 | + ]; |
| 102 | + |
| 103 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 104 | + "Invalid transaction: transaction at index 0 has invalid 'to' address format", |
| 105 | + ); |
| 106 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('throws error when data is missing', () => { |
| 110 | + const transactions = [ |
| 111 | + createValidTransaction({ params: { to: VALID_ADDRESS } }), |
| 112 | + ]; |
| 113 | + |
| 114 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 115 | + 'Invalid transaction: transaction at index 0 is missing data', |
| 116 | + ); |
| 117 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 118 | + }); |
| 119 | + |
| 120 | + it('throws error when data does not start with 0x', () => { |
| 121 | + const transactions = [ |
| 122 | + createValidTransaction({ |
| 123 | + params: { to: VALID_ADDRESS, data: 'not-hex-data' }, |
| 124 | + }), |
| 125 | + ]; |
| 126 | + |
| 127 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 128 | + 'Invalid transaction: transaction at index 0 has invalid data format (must be hex string starting with 0x)', |
| 129 | + ); |
| 130 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('throws error when data is too short', () => { |
| 134 | + const transactions = [ |
| 135 | + createValidTransaction({ params: { to: VALID_ADDRESS, data: '0x1234' } }), |
| 136 | + ]; |
| 137 | + |
| 138 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 139 | + 'Invalid transaction: transaction at index 0 has insufficient data (length: 6, minimum: 10)', |
| 140 | + ); |
| 141 | + expect(mockLoggerError).toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('validates all transactions in array', () => { |
| 145 | + const transactions = [ |
| 146 | + createValidTransaction(), |
| 147 | + createValidTransaction({ params: { to: VALID_ADDRESS, data: '0x1234' } }), |
| 148 | + ]; |
| 149 | + |
| 150 | + expect(() => validateDepositTransactions(transactions, context)).toThrow( |
| 151 | + 'Invalid transaction: transaction at index 1 has insufficient data (length: 6, minimum: 10)', |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('logs error with correct context', () => { |
| 156 | + const transactions = [ |
| 157 | + { type: TransactionType.contractInteraction } as never, |
| 158 | + ]; |
| 159 | + |
| 160 | + expect(() => validateDepositTransactions(transactions, context)).toThrow(); |
| 161 | + |
| 162 | + expect(mockLoggerError).toHaveBeenCalledWith( |
| 163 | + expect.any(Error), |
| 164 | + expect.objectContaining({ |
| 165 | + tags: expect.objectContaining({ |
| 166 | + feature: 'Predict', |
| 167 | + provider: 'polymarket', |
| 168 | + }), |
| 169 | + context: expect.objectContaining({ |
| 170 | + name: 'PredictController', |
| 171 | + data: expect.objectContaining({ |
| 172 | + method: 'depositWithConfirmation', |
| 173 | + providerId: 'polymarket', |
| 174 | + transactionIndex: 0, |
| 175 | + }), |
| 176 | + }), |
| 177 | + }), |
| 178 | + ); |
| 179 | + }); |
| 180 | +}); |
0 commit comments