1
+ import assert from "assert" ;
1
2
import { BigNumber , Contract , Event , EventFilter , ethers , providers } from "ethers" ;
2
3
import { groupBy } from "lodash" ;
3
4
import winston from "winston" ;
@@ -9,7 +10,11 @@ import {
9
10
MAX_BIG_INT ,
10
11
MakeOptional ,
11
12
assign ,
13
+ getFillAmount ,
14
+ getTotalFilledAmount ,
12
15
isDefined ,
16
+ isV2Deposit ,
17
+ isV2SpeedUp ,
13
18
mapAsync ,
14
19
stringifyJSONWithNumericString ,
15
20
toBN ,
@@ -43,6 +48,7 @@ import {
43
48
SpeedUpStringified ,
44
49
TokensBridged ,
45
50
TokensBridgedStringified ,
51
+ v2SpeedUp ,
46
52
} from "../interfaces" ;
47
53
import { SpokePool } from "../typechain" ;
48
54
import { getNetworkName } from "../utils/NetworkUtils" ;
@@ -298,24 +304,29 @@ export class SpokePoolClient extends BaseAbstractClient {
298
304
* @returns A new deposit instance with the speed up signature appended to the deposit.
299
305
*/
300
306
public appendMaxSpeedUpSignatureToDeposit ( deposit : DepositWithBlock ) : DepositWithBlock {
301
- const maxSpeedUp = this . speedUps [ deposit . depositor ] ?. [ deposit . depositId ] ?. reduce ( ( prev , current ) =>
302
- prev . newRelayerFeePct . gt ( current . newRelayerFeePct ) ? prev : current
303
- ) ;
307
+ if ( isV2Deposit ( deposit ) ) {
308
+ const maxSpeedUps = this . speedUps [ deposit . depositor ] ?. [ deposit . depositId ] ?. filter ( ( ) => isV2SpeedUp ) ;
309
+ const maxSpeedUp = ( ( maxSpeedUps ?? [ ] ) as v2SpeedUp [ ] ) . reduce (
310
+ ( prev , current ) => ( prev . newRelayerFeePct . gt ( current . newRelayerFeePct ) ? prev : current ) ,
311
+ { newRelayerFeePct : deposit . relayerFeePct } as v2SpeedUp
312
+ ) ;
304
313
305
- // We assume that the depositor authorises SpeedUps in isolation of each other, which keeps the relayer
306
- // logic simple: find the SpeedUp with the highest relayerFeePct, and use all of its fields
307
- if ( ! maxSpeedUp || maxSpeedUp . newRelayerFeePct . lte ( deposit . relayerFeePct ) ) {
308
- return deposit ;
309
- }
314
+ // We assume that the depositor authorises SpeedUps in isolation of each other, which keeps the relayer
315
+ // logic simple: find the SpeedUp with the highest relayerFeePct, and use all of its fields
316
+ if ( ! maxSpeedUp || maxSpeedUp . newRelayerFeePct . lte ( deposit . relayerFeePct ) ) {
317
+ return deposit ;
318
+ }
310
319
311
- // Return deposit with updated params from the speedup with the highest updated relayer fee pct.
312
- return {
313
- ...deposit ,
314
- speedUpSignature : maxSpeedUp . depositorSignature ,
315
- newRelayerFeePct : maxSpeedUp . newRelayerFeePct ,
316
- updatedRecipient : maxSpeedUp . updatedRecipient ,
317
- updatedMessage : maxSpeedUp . updatedMessage ,
318
- } ;
320
+ // Return deposit with updated params from the speedup with the highest updated relayer fee pct.
321
+ return {
322
+ ...deposit ,
323
+ speedUpSignature : maxSpeedUp . depositorSignature ,
324
+ newRelayerFeePct : maxSpeedUp . newRelayerFeePct ,
325
+ updatedRecipient : maxSpeedUp . updatedRecipient ,
326
+ updatedMessage : maxSpeedUp . updatedMessage ,
327
+ } ;
328
+ }
329
+ assert ( false ) ; // v3 is coming.
319
330
}
320
331
321
332
/**
@@ -404,17 +415,16 @@ export class SpokePoolClient extends BaseAbstractClient {
404
415
}
405
416
406
417
// Order fills by totalFilledAmount and then return the first fill's full deposit amount minus total filled amount.
407
- const fillsOrderedByTotalFilledAmount = validFills . sort ( ( fillA , fillB ) =>
408
- fillB . totalFilledAmount . gt ( fillA . totalFilledAmount )
409
- ? 1
410
- : fillB . totalFilledAmount . lt ( fillA . totalFilledAmount )
411
- ? - 1
412
- : 0
413
- ) ;
418
+ const fillsOrderedByTotalFilledAmount = validFills . sort ( ( fillA , fillB ) => {
419
+ const totalFilledA = getTotalFilledAmount ( fillA ) ;
420
+ const totalFilledB = getTotalFilledAmount ( fillB ) ;
421
+
422
+ return totalFilledB . gt ( totalFilledA ) ? 1 : totalFilledB . lt ( totalFilledA ) ? - 1 : 0 ;
423
+ } ) ;
414
424
415
425
const lastFill = fillsOrderedByTotalFilledAmount [ 0 ] ;
416
426
return {
417
- unfilledAmount : toBN ( lastFill . amount . sub ( lastFill . totalFilledAmount ) ) ,
427
+ unfilledAmount : getFillAmount ( lastFill ) . sub ( getTotalFilledAmount ( lastFill ) ) ,
418
428
fillCount : validFills . length ,
419
429
invalidFills,
420
430
} ;
@@ -664,25 +674,29 @@ export class SpokePoolClient extends BaseAbstractClient {
664
674
// is heavy as there is a fair bit of block number lookups that need to happen. Note this call REQUIRES that the
665
675
// hubPoolClient is updated on the first before this call as this needed the the L1 token mapping to each L2 token.
666
676
if ( eventsToQuery . includes ( "FundsDeposited" ) ) {
667
- const allDeposits = [
668
- ...( queryResults [ eventsToQuery . indexOf ( "FundsDeposited" ) ] as FundsDepositedEvent [ ] ) ,
669
- ...this . earlyDeposits ,
670
- ] ;
671
- const { earlyDeposits = [ ] , depositEvents = [ ] } = groupBy ( allDeposits , ( depositEvent ) => {
672
- if ( this . _isEarlyDeposit ( depositEvent , currentTime ) ) {
673
- const { args, transactionHash } = depositEvent ;
674
- this . logger . debug ( {
675
- at : "SpokePoolClient#update" ,
676
- message : "Deferring early deposit event." ,
677
- currentTime,
678
- deposit : { args, transactionHash } ,
679
- } ) ;
680
- return "earlyDeposits" ;
681
- } else {
682
- return "depositEvents" ;
683
- }
684
- } ) ;
677
+ // Filter out any early v2 deposits (quoteTimestamp > HubPoolClient.currentTime). Early deposits are no longer a
678
+ // critical risk in v3, so don't worry about filtering those. This will reduce complexity in several places.
679
+ const { earlyDeposits = [ ] , v2DepositEvents = [ ] } = groupBy (
680
+ [
681
+ ...this . earlyDeposits ,
682
+ ...( ( queryResults [ eventsToQuery . indexOf ( "FundsDeposited" ) ] ?? [ ] ) as FundsDepositedEvent [ ] ) ,
683
+ ] ,
684
+ ( depositEvent ) => ( this . _isEarlyDeposit ( depositEvent , currentTime ) ? "earlyDeposits" : "v2DepositEvents" )
685
+ ) ;
686
+ if ( earlyDeposits . length > 0 ) {
687
+ this . logger . debug ( {
688
+ at : "SpokePoolClient#update" ,
689
+ message : `Deferring ${ earlyDeposits . length } early v2 deposit events.` ,
690
+ currentTime,
691
+ deposits : earlyDeposits . map ( ( { args, transactionHash } ) => ( { depositId : args . depositId , transactionHash } ) ) ,
692
+ } ) ;
693
+ }
685
694
this . earlyDeposits = earlyDeposits ;
695
+
696
+ const depositEvents = [
697
+ ...v2DepositEvents ,
698
+ // ...v3DepositEvents, @todo
699
+ ] ;
686
700
if ( depositEvents . length > 0 ) {
687
701
this . log ( "debug" , `Using ${ depositEvents . length } newly queried deposit events for chain ${ this . chainId } ` , {
688
702
earliestEvent : depositEvents [ 0 ] . blockNumber ,
0 commit comments