Skip to content

Commit

Permalink
refactor: Drop FlowUtils
Browse files Browse the repository at this point in the history
This is a hangover from the UBA. Relocate the function to SpokeUtils
instead. This will make it easier to group functionality into evm/xvm.
  • Loading branch information
pxrl committed Feb 28, 2025
1 parent 8887296 commit 31d70cb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isValidEvmAddress,
isZeroAddress,
toAddress,
validateFillForDeposit,
} from "../utils";
import {
duplicateEvent,
Expand All @@ -29,7 +30,6 @@ import {
spreadEvent,
spreadEventWithBlockNumber,
} from "../utils/EventUtils";
import { validateFillForDeposit } from "../utils/FlowUtils";
import { ZERO_ADDRESS } from "../constants";
import {
Deposit,
Expand Down
3 changes: 1 addition & 2 deletions src/utils/DepositUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { CachingMechanismInterface, Deposit, DepositWithBlock, Fill, SlowFillReq
import { getNetworkName } from "./NetworkUtils";
import { bnZero } from "./BigNumberUtils";
import { getDepositInCache, getDepositKey, setDepositInCache } from "./CachingUtils";
import { validateFillForDeposit } from "./FlowUtils";
import { getMessageHash, isUnsafeDepositId } from "./SpokeUtils";
import { getMessageHash, isUnsafeDepositId, validateFillForDeposit } from "./SpokeUtils";
import { getCurrentTime } from "./TimeUtils";
import { isDefined } from "./TypeGuards";
import { isDepositFormedCorrectly } from "./ValidatorUtils";
Expand Down
44 changes: 0 additions & 44 deletions src/utils/FlowUtils.ts

This file was deleted.

43 changes: 42 additions & 1 deletion src/utils/SpokeUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { BytesLike, Contract, PopulatedTransaction, providers, utils as ethersUtils } from "ethers";
import { CHAIN_IDs, MAX_SAFE_DEPOSIT_ID, ZERO_ADDRESS, ZERO_BYTES } from "../constants";
import { CHAIN_IDs, MAX_SAFE_DEPOSIT_ID, UNDEFINED_MESSAGE_HASH, ZERO_ADDRESS, ZERO_BYTES } from "../constants";
import { Deposit, FillStatus, FillWithBlock, RelayData } from "../interfaces";
import { SpokePoolClient } from "../clients";
import { chunk } from "./ArrayUtils";
Expand Down Expand Up @@ -88,6 +88,47 @@ export function getRelayEventKey(
.join("-");
}

const RELAYDATA_KEYS = [
"depositId",
"originChainId",
"destinationChainId",
"depositor",
"recipient",
"inputToken",
"inputAmount",
"outputToken",
"outputAmount",
"fillDeadline",
"exclusivityDeadline",
"exclusiveRelayer",
"messageHash",
] as const;

// Ensure that each deposit element is included with the same value in the fill. This includes all elements defined
// by the depositor as well as destinationToken, which are pulled from other clients.
export function validateFillForDeposit(
relayData: Omit<RelayData, "message"> & { messageHash: string; destinationChainId: number },
deposit?: Omit<Deposit, "quoteTimestamp" | "fromLiteChain" | "toLiteChain">
): { valid: true } | { valid: false; reason: string } {
if (deposit === undefined) {
return { valid: false, reason: "Deposit is undefined" };
}

// Note: this short circuits when a key is found where the comparison doesn't match.
// TODO: if we turn on "strict" in the tsconfig, the elements of FILL_DEPOSIT_COMPARISON_KEYS will be automatically
// validated against the fields in Fill and Deposit, generating an error if there is a discrepency.
let invalidKey = RELAYDATA_KEYS.find((key) => relayData[key].toString() !== deposit[key].toString());

// There should be no paths for `messageHash` to be unset, but mask it off anyway.
if (!isDefined(invalidKey) && [relayData.messageHash, deposit.messageHash].includes(UNDEFINED_MESSAGE_HASH)) {
invalidKey = "messageHash";
}

return isDefined(invalidKey)
? { valid: false, reason: `${invalidKey} mismatch (${relayData[invalidKey]} != ${deposit[invalidKey]})` }
: { valid: true };
}

/**
* Find the block range that contains the deposit ID. This is a binary search that searches for the block range
* that contains the deposit ID.
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from "./TypeGuards";
export * from "./TypeUtils";
export * from "./TokenUtils";
export * from "./LogUtils";
export * from "./FlowUtils";
export * from "./BundleUtils";
export * from "./JSONUtils";
export * from "./IPFSUtils";
Expand Down

0 comments on commit 31d70cb

Please sign in to comment.