|
9 | 9 | isChainSelectorSupported, |
10 | 10 | LAST_FINALIZED_BLOCK_NUMBER, |
11 | 11 | LATEST_BLOCK_NUMBER, |
| 12 | + logTriggerConfig, |
12 | 13 | type ProtoBigInt, |
13 | 14 | prepareReportRequest, |
14 | 15 | protoBigIntToBigint, |
@@ -268,6 +269,144 @@ describe('blockchain-helpers', () => { |
268 | 269 | }) |
269 | 270 | }) |
270 | 271 |
|
| 272 | + describe('encodeCallMsg error context', () => { |
| 273 | + test('should include field name in error for invalid from address', () => { |
| 274 | + const payload: EncodeCallMsgPayload = { |
| 275 | + from: 'not-hex' as `0x${string}`, |
| 276 | + to: '0x0000000000000000000000000000000000000000', |
| 277 | + data: '0x', |
| 278 | + } |
| 279 | + expect(() => encodeCallMsg(payload)).toThrow("Invalid hex in 'from' field of CallMsg") |
| 280 | + }) |
| 281 | + |
| 282 | + test('should include field name in error for invalid to address', () => { |
| 283 | + const payload: EncodeCallMsgPayload = { |
| 284 | + from: '0x0000000000000000000000000000000000000000', |
| 285 | + to: 'bad-hex' as `0x${string}`, |
| 286 | + data: '0x', |
| 287 | + } |
| 288 | + expect(() => encodeCallMsg(payload)).toThrow("Invalid hex in 'to' field of CallMsg") |
| 289 | + }) |
| 290 | + |
| 291 | + test('should include field name in error for invalid data', () => { |
| 292 | + const payload: EncodeCallMsgPayload = { |
| 293 | + from: '0x0000000000000000000000000000000000000000', |
| 294 | + to: '0x0000000000000000000000000000000000000000', |
| 295 | + data: 'not-hex' as `0x${string}`, |
| 296 | + } |
| 297 | + expect(() => encodeCallMsg(payload)).toThrow("Invalid hex in 'data' field of CallMsg") |
| 298 | + }) |
| 299 | + }) |
| 300 | + |
| 301 | + describe('logTriggerConfig', () => { |
| 302 | + const VALID_ADDRESS = '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9' |
| 303 | + const VALID_TOPIC = |
| 304 | + '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef' |
| 305 | + |
| 306 | + test('should encode a single address', () => { |
| 307 | + const result = logTriggerConfig({ addresses: [VALID_ADDRESS] }) |
| 308 | + expect(result.addresses).toHaveLength(1) |
| 309 | + expect(typeof result.addresses[0]).toBe('string') // base64 |
| 310 | + }) |
| 311 | + |
| 312 | + test('should encode multiple addresses', () => { |
| 313 | + const result = logTriggerConfig({ |
| 314 | + addresses: [VALID_ADDRESS, '0x0000000000000000000000000000000000000000'], |
| 315 | + }) |
| 316 | + expect(result.addresses).toHaveLength(2) |
| 317 | + }) |
| 318 | + |
| 319 | + test('should encode topics with correct structure', () => { |
| 320 | + const result = logTriggerConfig({ |
| 321 | + addresses: [VALID_ADDRESS], |
| 322 | + topics: [[VALID_TOPIC]], |
| 323 | + }) |
| 324 | + expect(result.topics).toHaveLength(1) |
| 325 | + expect(result.topics![0].values).toHaveLength(1) |
| 326 | + expect(typeof result.topics![0].values[0]).toBe('string') // base64 |
| 327 | + }) |
| 328 | + |
| 329 | + test('should encode multiple topic slots', () => { |
| 330 | + const result = logTriggerConfig({ |
| 331 | + addresses: [VALID_ADDRESS], |
| 332 | + topics: [[VALID_TOPIC], [VALID_TOPIC, VALID_TOPIC]], |
| 333 | + }) |
| 334 | + expect(result.topics).toHaveLength(2) |
| 335 | + expect(result.topics![0].values).toHaveLength(1) |
| 336 | + expect(result.topics![1].values).toHaveLength(2) |
| 337 | + }) |
| 338 | + |
| 339 | + test('should omit topics when not provided', () => { |
| 340 | + const result = logTriggerConfig({ addresses: [VALID_ADDRESS] }) |
| 341 | + expect(result.topics).toBeUndefined() |
| 342 | + }) |
| 343 | + |
| 344 | + test('should set confidence level', () => { |
| 345 | + const result = logTriggerConfig({ |
| 346 | + addresses: [VALID_ADDRESS], |
| 347 | + confidence: 'LATEST', |
| 348 | + }) |
| 349 | + expect(result.confidence).toBe('CONFIDENCE_LEVEL_LATEST') |
| 350 | + }) |
| 351 | + |
| 352 | + test('should set FINALIZED confidence level', () => { |
| 353 | + const result = logTriggerConfig({ |
| 354 | + addresses: [VALID_ADDRESS], |
| 355 | + confidence: 'FINALIZED', |
| 356 | + }) |
| 357 | + expect(result.confidence).toBe('CONFIDENCE_LEVEL_FINALIZED') |
| 358 | + }) |
| 359 | + |
| 360 | + test('should omit confidence when not provided', () => { |
| 361 | + const result = logTriggerConfig({ addresses: [VALID_ADDRESS] }) |
| 362 | + expect(result.confidence).toBeUndefined() |
| 363 | + }) |
| 364 | + |
| 365 | + test('should throw for empty addresses array', () => { |
| 366 | + expect(() => logTriggerConfig({ addresses: [] })).toThrow( |
| 367 | + 'logTriggerConfig requires at least one address', |
| 368 | + ) |
| 369 | + }) |
| 370 | + |
| 371 | + test('should throw for invalid hex address', () => { |
| 372 | + expect(() => |
| 373 | + logTriggerConfig({ addresses: ['not-hex' as `0x${string}`] }), |
| 374 | + ).toThrow('Invalid address at index 0') |
| 375 | + }) |
| 376 | + |
| 377 | + test('should throw for address with wrong byte length', () => { |
| 378 | + expect(() => |
| 379 | + logTriggerConfig({ addresses: ['0x1234' as `0x${string}`] }), |
| 380 | + ).toThrow('expected 20 bytes') |
| 381 | + }) |
| 382 | + |
| 383 | + test('should throw for topic with wrong byte length', () => { |
| 384 | + expect(() => |
| 385 | + logTriggerConfig({ |
| 386 | + addresses: [VALID_ADDRESS], |
| 387 | + topics: [['0x1234' as `0x${string}`]], |
| 388 | + }), |
| 389 | + ).toThrow('expected 32 bytes') |
| 390 | + }) |
| 391 | + |
| 392 | + test('should include index in address error', () => { |
| 393 | + expect(() => |
| 394 | + logTriggerConfig({ |
| 395 | + addresses: [VALID_ADDRESS, 'bad' as `0x${string}`], |
| 396 | + }), |
| 397 | + ).toThrow('Invalid address at index 1') |
| 398 | + }) |
| 399 | + |
| 400 | + test('should include slot and value index in topic error', () => { |
| 401 | + expect(() => |
| 402 | + logTriggerConfig({ |
| 403 | + addresses: [VALID_ADDRESS], |
| 404 | + topics: [[VALID_TOPIC], ['bad' as `0x${string}`]], |
| 405 | + }), |
| 406 | + ).toThrow('Invalid topic at topics[1][0]') |
| 407 | + }) |
| 408 | + }) |
| 409 | + |
271 | 410 | describe('isChainSelectorSupported', () => { |
272 | 411 | test('should return true for supported chain selectors', () => { |
273 | 412 | // Get all supported chain selectors from EVMClient |
|
0 commit comments