@@ -286,7 +286,7 @@ contract("Pyth", function () {
286
286
return replaced ;
287
287
}
288
288
289
- it . only ( "should parse batch price attestation correctly" , async function ( ) {
289
+ it ( "should parse batch price attestation correctly" , async function ( ) {
290
290
let attestationTime = 1647273460 ; // re-used for publishTime
291
291
let publishTime = 1647273465 ; // re-used for publishTime
292
292
let priceVal = 1337 ;
@@ -300,9 +300,9 @@ contract("Pyth", function () {
300
300
301
301
const receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch ] ) ;
302
302
303
- expectEvent ( receipt , 'PriceFeedUpdate' , {
303
+ expectEventMultipleTimes ( receipt , 'PriceFeedUpdate' , {
304
304
price : "1337" ,
305
- } ) ;
305
+ } , 10 ) ;
306
306
307
307
for ( var i = 1 ; i <= RAW_BATCH_ATTESTATION_COUNT ; i ++ ) {
308
308
const price_id =
@@ -352,9 +352,6 @@ contract("Pyth", function () {
352
352
const receipt = await updatePriceFeeds ( this . pythProxy , [ ] ) ;
353
353
expectEvent . notEmitted ( receipt , 'PriceFeedUpdate' ) ;
354
354
expectEvent . notEmitted ( receipt , 'BatchPriceFeedUpdate' ) ;
355
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
356
- batchCount : '0' ,
357
- } ) ;
358
355
} ) ;
359
356
360
357
it ( "should attest price updates with multiple batches of correct order" , async function ( ) {
@@ -363,15 +360,12 @@ contract("Pyth", function () {
363
360
let rawBatch2 = generateRawBatchAttestation ( ts + 5 , ts + 10 , 1338 ) ;
364
361
const receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch1 , rawBatch2 ] ) ;
365
362
expectEvent ( receipt , 'PriceFeedUpdate' , {
366
- fresh : true ,
367
- } ) ;
368
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
369
- batchSize : '10' ,
370
- freshPricesInBatch : '10' ,
363
+ publishTime : ( ts - 5 ) . toString ( ) ,
371
364
} ) ;
372
- expectEvent ( receipt , 'UpdatePriceFeeds ' , {
373
- batchCount : '2' ,
365
+ expectEvent ( receipt , 'PriceFeedUpdate ' , {
366
+ publishTime : ( ts + 5 ) . toString ( ) ,
374
367
} ) ;
368
+ expectEventMultipleTimes ( receipt , 'BatchPriceFeedUpdate' , { } , 2 ) ;
375
369
} ) ;
376
370
377
371
it ( "should attest price updates with multiple batches of wrong order" , async function ( ) {
@@ -380,21 +374,11 @@ contract("Pyth", function () {
380
374
let rawBatch2 = generateRawBatchAttestation ( ts + 5 , ts + 10 , 1338 ) ;
381
375
const receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch2 , rawBatch1 ] ) ;
382
376
expectEvent ( receipt , 'PriceFeedUpdate' , {
383
- fresh : true ,
384
- } ) ;
385
- expectEvent ( receipt , 'PriceFeedUpdate' , {
386
- fresh : false ,
387
- } ) ;
388
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
389
- batchSize : '10' ,
390
- freshPricesInBatch : '10' ,
377
+ publishTime : ( ts + 5 ) . toString ( ) ,
391
378
} ) ;
392
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
393
- batchSize : '10' ,
394
- freshPricesInBatch : '0' ,
395
- } ) ;
396
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
397
- batchCount : '2' ,
379
+ expectEventMultipleTimes ( receipt , 'BatchPriceFeedUpdate' , { } , 2 ) ;
380
+ expectEventNotEmittedWithArgs ( receipt , 'PriceFeedUpdate' , {
381
+ publishTime : ( ts - 5 ) . toString ( ) ,
398
382
} ) ;
399
383
} ) ;
400
384
@@ -449,10 +433,7 @@ contract("Pyth", function () {
449
433
let feeInWei = await this . pythProxy . methods [ "getUpdateFee(bytes[])" ] ( [ rawBatch1 , rawBatch2 ] ) ;
450
434
assert . equal ( feeInWei , 20 ) ;
451
435
452
- const receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch1 , rawBatch2 ] , feeInWei ) ;
453
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
454
- fee : feeInWei
455
- } ) ;
436
+ await updatePriceFeeds ( this . pythProxy , [ rawBatch1 , rawBatch2 ] , feeInWei ) ;
456
437
const pythBalance = await web3 . eth . getBalance ( this . pythProxy . address ) ;
457
438
assert . equal ( pythBalance , feeInWei ) ;
458
439
} ) ;
@@ -479,10 +460,7 @@ contract("Pyth", function () {
479
460
let feeInWei = await this . pythProxy . methods [ "getUpdateFee(bytes[])" ] ( [ rawBatch1 , rawBatch2 ] ) ;
480
461
assert . equal ( feeInWei , 20 ) ;
481
462
482
- const receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch1 , rawBatch2 ] , feeInWei + 10 ) ;
483
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
484
- fee : feeInWei
485
- } ) ;
463
+ await updatePriceFeeds ( this . pythProxy , [ rawBatch1 , rawBatch2 ] , feeInWei + 10 ) ;
486
464
const pythBalance = await web3 . eth . getBalance ( this . pythProxy . address ) ;
487
465
assert . equal ( pythBalance , feeInWei + 10 ) ;
488
466
} ) ;
@@ -497,15 +475,11 @@ contract("Pyth", function () {
497
475
) ;
498
476
let receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch ] ) ;
499
477
expectEvent ( receipt , 'PriceFeedUpdate' , {
500
- fresh : true ,
501
- } ) ;
502
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
503
- batchSize : '10' ,
504
- freshPricesInBatch : '10' ,
505
- } ) ;
506
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
507
- batchCount : '1' ,
478
+ price : priceVal . toString ( ) ,
479
+ publishTime : ( currentTimestamp - 5 ) . toString ( )
508
480
} ) ;
481
+ expectEvent ( receipt , 'BatchPriceFeedUpdate' ) ;
482
+
509
483
510
484
let first_prod_id = "0x" + "01" . repeat ( 32 ) ;
511
485
let first_price_id = "0x" + "fe" . repeat ( 32 ) ;
@@ -529,15 +503,10 @@ contract("Pyth", function () {
529
503
) ;
530
504
receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch2 ] ) ;
531
505
expectEvent ( receipt , 'PriceFeedUpdate' , {
532
- fresh : true ,
533
- } ) ;
534
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
535
- batchSize : '10' ,
536
- freshPricesInBatch : '10' ,
537
- } ) ;
538
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
539
- batchCount : '1' ,
506
+ price : ( priceVal + 5 ) . toString ( ) ,
507
+ publishTime : ( nextTimestamp - 5 ) . toString ( )
540
508
} ) ;
509
+ expectEvent ( receipt , 'BatchPriceFeedUpdate' ) ;
541
510
542
511
first = await this . pythProxy . queryPriceFeed ( first_price_id ) ;
543
512
assert . equal ( first . price . price , priceVal + 5 ) ;
@@ -552,16 +521,8 @@ contract("Pyth", function () {
552
521
priceVal + 10
553
522
) ;
554
523
receipt = await updatePriceFeeds ( this . pythProxy , [ rawBatch3 ] ) ;
555
- expectEvent ( receipt , 'PriceFeedUpdate' , {
556
- fresh : false ,
557
- } ) ;
558
- expectEvent ( receipt , 'BatchPriceFeedUpdate' , {
559
- batchSize : '10' ,
560
- freshPricesInBatch : '0' ,
561
- } ) ;
562
- expectEvent ( receipt , 'UpdatePriceFeeds' , {
563
- batchCount : '1' ,
564
- } ) ;
524
+ expectEvent . notEmitted ( receipt , 'PriceFeedUpdate' ) ;
525
+ expectEvent ( receipt , 'BatchPriceFeedUpdate' ) ;
565
526
566
527
first = await this . pythProxy . queryPriceFeed ( first_price_id ) ;
567
528
assert . equal ( first . price . price , priceVal + 5 ) ;
@@ -1170,10 +1131,7 @@ contract("Pyth", function () {
1170
1131
insufficientFeeError
1171
1132
) ;
1172
1133
1173
- const receiptUpdateFeeds = await updatePriceFeeds ( this . pythProxy , [ rawBatch ] , 5000 ) ;
1174
- expectEvent ( receiptUpdateFeeds , 'UpdatePriceFeeds' , {
1175
- fee : "5000"
1176
- } ) ;
1134
+ await updatePriceFeeds ( this . pythProxy , [ rawBatch ] , 5000 ) ;
1177
1135
} ) ;
1178
1136
1179
1137
it ( "Setting valid period should work" , async function ( ) {
@@ -1316,3 +1274,35 @@ async function createVAAFromUint8Array(
1316
1274
0
1317
1275
) ;
1318
1276
}
1277
+
1278
+ // There is no way to check event with given args has not emitted with expectEvent
1279
+ // or how many times an event was emitted. This function is implemented to count
1280
+ // the matching events and is used for the mentioned purposes.
1281
+ function getNumMatchingEvents ( receipt , eventName , args ) {
1282
+ let matchCnt = 0 ;
1283
+ for ( let log of receipt . logs ) {
1284
+ if ( log . event === eventName ) {
1285
+ let match = true ;
1286
+ for ( let argKey in args ) {
1287
+ if ( log . args [ argKey ] . toString ( ) !== args [ argKey ] . toString ( ) ) {
1288
+ match = false ;
1289
+ break ;
1290
+ }
1291
+ }
1292
+ if ( match ) {
1293
+ matchCnt ++ ;
1294
+ }
1295
+ }
1296
+ }
1297
+ return matchCnt ;
1298
+ }
1299
+
1300
+ function expectEventNotEmittedWithArgs ( receipt , eventName , args ) {
1301
+ const matches = getNumMatchingEvents ( receipt , eventName , args ) ;
1302
+ assert ( matches === 0 , `Expected no matching emitted event. But found ${ matches } .` ) ;
1303
+ }
1304
+
1305
+ function expectEventMultipleTimes ( receipt , eventName , args , cnt ) {
1306
+ const matches = getNumMatchingEvents ( receipt , eventName , args ) ;
1307
+ assert ( matches === cnt , `Expected ${ cnt } event matches, found ${ matches } .` ) ;
1308
+ }
0 commit comments