Skip to content

Commit 5672ad3

Browse files
committed
chore(SpokePoolClient): Refactor before v3 introduction
This change makes some non-functional refactoring to the SpokePoolClient to make it easier to introduce the support for v3. Doing it now just reduces the size and complexity of the subsequent diff.
1 parent 2aff1d7 commit 5672ad3

File tree

1 file changed

+56
-42
lines changed

1 file changed

+56
-42
lines changed

src/clients/SpokePoolClient.ts

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import assert from "assert";
12
import { BigNumber, Contract, Event, EventFilter, ethers, providers } from "ethers";
23
import { groupBy } from "lodash";
34
import winston from "winston";
@@ -9,7 +10,11 @@ import {
910
MAX_BIG_INT,
1011
MakeOptional,
1112
assign,
13+
getFillAmount,
14+
getTotalFilledAmount,
1215
isDefined,
16+
isV2Deposit,
17+
isV2SpeedUp,
1318
mapAsync,
1419
stringifyJSONWithNumericString,
1520
toBN,
@@ -43,6 +48,7 @@ import {
4348
SpeedUpStringified,
4449
TokensBridged,
4550
TokensBridgedStringified,
51+
v2SpeedUp,
4652
} from "../interfaces";
4753
import { SpokePool } from "../typechain";
4854
import { getNetworkName } from "../utils/NetworkUtils";
@@ -298,24 +304,29 @@ export class SpokePoolClient extends BaseAbstractClient {
298304
* @returns A new deposit instance with the speed up signature appended to the deposit.
299305
*/
300306
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+
);
304313

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+
}
310319

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.
319330
}
320331

321332
/**
@@ -404,17 +415,16 @@ export class SpokePoolClient extends BaseAbstractClient {
404415
}
405416

406417
// 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+
});
414424

415425
const lastFill = fillsOrderedByTotalFilledAmount[0];
416426
return {
417-
unfilledAmount: toBN(lastFill.amount.sub(lastFill.totalFilledAmount)),
427+
unfilledAmount: getFillAmount(lastFill).sub(getTotalFilledAmount(lastFill)),
418428
fillCount: validFills.length,
419429
invalidFills,
420430
};
@@ -664,25 +674,29 @@ export class SpokePoolClient extends BaseAbstractClient {
664674
// is heavy as there is a fair bit of block number lookups that need to happen. Note this call REQUIRES that the
665675
// hubPoolClient is updated on the first before this call as this needed the the L1 token mapping to each L2 token.
666676
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+
}
685694
this.earlyDeposits = earlyDeposits;
695+
696+
const depositEvents = [
697+
...v2DepositEvents,
698+
// ...v3DepositEvents, @todo
699+
];
686700
if (depositEvents.length > 0) {
687701
this.log("debug", `Using ${depositEvents.length} newly queried deposit events for chain ${this.chainId}`, {
688702
earliestEvent: depositEvents[0].blockNumber,

0 commit comments

Comments
 (0)