diff --git a/src/clients/BundleDataClient/BundleDataClient.ts b/src/clients/BundleDataClient/BundleDataClient.ts index 6e412ca73..4c8cf2998 100644 --- a/src/clients/BundleDataClient/BundleDataClient.ts +++ b/src/clients/BundleDataClient/BundleDataClient.ts @@ -1,3 +1,4 @@ +import assert from "assert"; import _ from "lodash"; import { ProposedRootBundle, @@ -24,7 +25,6 @@ import { bnZero, queryHistoricalDepositForFill, assign, - assert, fixedPointAdjustment, isDefined, toBN, @@ -392,7 +392,7 @@ export class BundleDataClient { const fill = await verifyFillRepayment( _fill, this.spokePoolClients[_fill.destinationChainId].spokePool.provider, - matchingDeposit!, + matchingDeposit, this.clients.hubPoolClient ); if (!isDefined(fill)) { @@ -403,7 +403,7 @@ export class BundleDataClient { this.clients.hubPoolClient, blockRanges, this.chainIdListForBundleEvaluationBlockNumbers, - matchingDeposit!.fromLiteChain // Use ! because we've already asserted that matchingDeposit is defined. + matchingDeposit.fromLiteChain ); // Assume that lp fees are 0 for the sake of speed. In the future we could batch compute // these or make hardcoded assumptions based on the origin-repayment chain direction. This might result @@ -850,16 +850,20 @@ export class BundleDataClient { slowFillRequest: undefined, }; } else { - v3RelayHashes[relayDataHash].deposits!.push(deposit); + const { deposits } = v3RelayHashes[relayDataHash]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); + deposits.push(deposit); } // Account for duplicate deposits by concatenating the relayDataHash with the count of the number of times // we have seen it so far. - const newBundleDepositHash = `${relayDataHash}@${v3RelayHashes[relayDataHash].deposits!.length - 1}`; + const { deposits } = v3RelayHashes[relayDataHash]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); + const newBundleDepositHash = `${relayDataHash}@${deposits.length - 1}`; const decodedBundleDepositHash = decodeBundleDepositHash(newBundleDepositHash); assert( decodedBundleDepositHash.relayDataHash === relayDataHash && - decodedBundleDepositHash.index === v3RelayHashes[relayDataHash].deposits!.length - 1, + decodedBundleDepositHash.index === deposits.length - 1, "Not using correct bundle deposit hash key" ); if (deposit.blockNumber >= originChainBlockRange[0]) { @@ -921,16 +925,14 @@ export class BundleDataClient { const relayDataHash = getRelayEventKey(fill); if (v3RelayHashes[relayDataHash]) { if (!v3RelayHashes[relayDataHash].fill) { - assert( - isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits!.length > 0, - "Deposit should exist in relay hash dictionary." - ); + const { deposits } = v3RelayHashes[relayDataHash]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); v3RelayHashes[relayDataHash].fill = fill; if (fill.blockNumber >= destinationChainBlockRange[0]) { const fillToRefund = await verifyFillRepayment( fill, destinationClient.spokePool.provider, - v3RelayHashes[relayDataHash].deposits![0], + deposits[0], this.clients.hubPoolClient ); if (!isDefined(fillToRefund)) { @@ -941,7 +943,7 @@ export class BundleDataClient { v3RelayHashes[relayDataHash].fill = fillToRefund; validatedBundleV3Fills.push({ ...fillToRefund, - quoteTimestamp: v3RelayHashes[relayDataHash].deposits![0].quoteTimestamp, + quoteTimestamp: deposits[0].quoteTimestamp, }); // Now that we know this deposit has been filled on-chain, identify any duplicate deposits @@ -952,7 +954,7 @@ export class BundleDataClient { // fill is from a prior bundle. Paying out the filler keeps the behavior consistent for how // we deal with duplicate deposits regardless if the deposit is matched with a pre-fill or // a current bundle fill. - const duplicateDeposits = v3RelayHashes[relayDataHash].deposits!.slice(1); + const duplicateDeposits = deposits.slice(1); duplicateDeposits.forEach((duplicateDeposit) => { if (isSlowFill(fill)) { updateExpiredDepositsV3(expiredDepositsToRefundV3, duplicateDeposit); @@ -970,7 +972,7 @@ export class BundleDataClient { // events. if ( fill.relayExecutionInfo.fillType === FillType.ReplacedSlowFill && - _canCreateSlowFillLeaf(v3RelayHashes[relayDataHash].deposits![0]) + _canCreateSlowFillLeaf(deposits[0]) ) { fastFillsReplacingSlowFills.push(relayDataHash); } @@ -1079,17 +1081,15 @@ export class BundleDataClient { if (v3RelayHashes[relayDataHash]) { if (!v3RelayHashes[relayDataHash].slowFillRequest) { v3RelayHashes[relayDataHash].slowFillRequest = slowFillRequest; - if (v3RelayHashes[relayDataHash].fill) { + const { deposits, fill } = v3RelayHashes[relayDataHash]; + if (fill) { // Exiting here assumes that slow fill requests must precede fills, so if there was a fill // following this slow fill request, then we would have already seen it. We don't need to check // for a fill older than this slow fill request. return; } - assert( - isDefined(v3RelayHashes[relayDataHash].deposits) && v3RelayHashes[relayDataHash].deposits!.length > 0, - "Deposit should exist in relay hash dictionary." - ); - const matchedDeposit = v3RelayHashes[relayDataHash].deposits![0]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); + const matchedDeposit = deposits[0]; if ( slowFillRequest.blockNumber >= destinationChainBlockRange[0] && @@ -1195,7 +1195,7 @@ export class BundleDataClient { const fillToRefund = await verifyFillRepayment( fill, destinationClient.spokePool.provider, - v3RelayHashes[relayDataHash].deposits![0], + deposits[0], this.clients.hubPoolClient ); if (!isDefined(fillToRefund)) { @@ -1243,18 +1243,18 @@ export class BundleDataClient { // then we wouldn't be in this branch of the code. const prefill = await this.findMatchingFillEvent(deposit, destinationClient); assert(isDefined(prefill), `findFillEvent# Cannot find prefill: ${relayDataHash}`); - assert(getRelayEventKey(prefill!) === relayDataHash, "Relay hashes should match."); + assert(getRelayEventKey(prefill) === relayDataHash, "Relay hashes should match."); const verifiedFill = await verifyFillRepayment( - prefill!, + prefill, destinationClient.spokePool.provider, deposit, this.clients.hubPoolClient ); if (!isDefined(verifiedFill)) { - bundleUnrepayableFillsV3.push(prefill!); + bundleUnrepayableFillsV3.push(prefill); } else if (!isSlowFill(verifiedFill)) { validatedBundleV3Fills.push({ - ...verifiedFill!, + ...verifiedFill, quoteTimestamp: deposit.quoteTimestamp, }); } else { @@ -1377,14 +1377,16 @@ export class BundleDataClient { validatedBundleV3Fills.length > 0 ? this.clients.hubPoolClient.batchComputeRealizedLpFeePct( validatedBundleV3Fills.map((fill) => { - const matchedDeposit = v3RelayHashes[getRelayEventKey(fill)].deposits![0]; + const { deposits } = v3RelayHashes[getRelayEventKey(fill)]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); + const matchedDeposit = deposits[0]; assert(isDefined(matchedDeposit), "Deposit should exist in relay hash dictionary."); const { chainToSendRefundTo: paymentChainId } = getRefundInformationFromFill( fill, this.clients.hubPoolClient, blockRangesForChains, chainIds, - matchedDeposit!.fromLiteChain + matchedDeposit.fromLiteChain ); return { ...fill, @@ -1421,14 +1423,16 @@ export class BundleDataClient { }); v3FillLpFees.forEach(({ realizedLpFeePct }, idx) => { const fill = validatedBundleV3Fills[idx]; - const associatedDeposit = v3RelayHashes[getRelayEventKey(fill)].deposits![0]; + const { deposits } = v3RelayHashes[getRelayEventKey(fill)]; + assert(isDefined(deposits) && deposits.length > 0, "Deposit should exist in relay hash dictionary."); + const associatedDeposit = deposits[0]; assert(isDefined(associatedDeposit), "Deposit should exist in relay hash dictionary."); const { chainToSendRefundTo, repaymentToken } = getRefundInformationFromFill( fill, this.clients.hubPoolClient, blockRangesForChains, chainIds, - associatedDeposit!.fromLiteChain + associatedDeposit.fromLiteChain ); updateBundleFillsV3(bundleFillsV3, fill, realizedLpFeePct, chainToSendRefundTo, repaymentToken, fill.relayer); }); diff --git a/src/relayFeeCalculator/chain-queries/baseQuery.ts b/src/relayFeeCalculator/chain-queries/baseQuery.ts index 8de1399e9..de144a6ad 100644 --- a/src/relayFeeCalculator/chain-queries/baseQuery.ts +++ b/src/relayFeeCalculator/chain-queries/baseQuery.ts @@ -13,10 +13,10 @@ import { BigNumber, toBNWei, bnZero, - assert, chainIsOPStack, fixedPointAdjustment, } from "../../utils"; +import assert from "assert"; import { Logger, QueryInterface } from "../relayFeeCalculator"; import { Transport } from "viem"; import { getGasPriceEstimate } from "../../gasPriceOracle/oracle"; diff --git a/src/utils/CachingUtils.ts b/src/utils/CachingUtils.ts index ee9142d7b..338ed9e7e 100644 --- a/src/utils/CachingUtils.ts +++ b/src/utils/CachingUtils.ts @@ -1,6 +1,6 @@ import { DEFAULT_CACHING_SAFE_LAG, DEFAULT_CACHING_TTL } from "../constants"; import { CachingMechanismInterface, Deposit, Fill, SlowFillRequest } from "../interfaces"; -import { assert } from "./LogUtils"; +import assert from "assert"; import { composeRevivers, objectWithBigNumberReviver } from "./ReviverUtils"; import { getRelayEventKey } from "./SpokeUtils"; import { getCurrentTime } from "./TimeUtils"; diff --git a/src/utils/LogUtils.ts b/src/utils/LogUtils.ts index c9d38e8db..f3c6b041e 100644 --- a/src/utils/LogUtils.ts +++ b/src/utils/LogUtils.ts @@ -24,15 +24,3 @@ export function formattedLog( }); } } - -/** - * Asserts the truth of a condition. If the condition is false, an error is thrown with the provided message. - * @param condition The condition to assert. - * @param message The message to throw if the condition is false. - * @throws Error if the condition is false. - */ -export function assert(condition: boolean, message: string): void { - if (!condition) { - throw new Error(message); - } -} diff --git a/test/SpokePoolClient.SpeedUp.ts b/test/SpokePoolClient.SpeedUp.ts index 29bcd66dd..5a059922d 100644 --- a/test/SpokePoolClient.SpeedUp.ts +++ b/test/SpokePoolClient.SpeedUp.ts @@ -3,7 +3,6 @@ import { Deposit, SpeedUp } from "../src/interfaces"; import { bnOne, getMessageHash, toBytes32 } from "../src/utils"; import { destinationChainId, originChainId } from "./constants"; import { - assert, assertPromiseError, Contract, BigNumber, @@ -177,7 +176,7 @@ describe("SpokePoolClient: SpeedUp", function () { let updatedDeposit = spokePoolClient.getDepositsForDestinationChain(deposit.destinationChainId).at(-1); // Convoluted checks to help tsc narrow types. - assert.exists(updatedDeposit); + expect(updatedDeposit).to.exist; updatedDeposit = updatedDeposit!; if (lowestOutputAmount.eq(deposit.outputAmount)) { @@ -218,11 +217,11 @@ describe("SpokePoolClient: SpeedUp", function () { const testOriginChainId = field !== "originChainId" ? originChainId : originChainId + 1; const testDepositId = field !== "depositId" ? depositId : depositId.add(1); const testDepositor = field !== "depositor" ? depositor : (await ethers.getSigners())[0]; - assert.isTrue(field !== "depositor" || testDepositor.address !== depositor.address); // Sanity check + expect(field !== "depositor" || testDepositor.address !== depositor.address).to.be.true; // Sanity check const signature = await getUpdatedV3DepositSignature( testDepositor, - testDepositId.toNumber(), + testDepositId, testOriginChainId, updatedOutputAmount, toBytes32(updatedRecipient), diff --git a/test/SpokePoolClient.ValidateFill.ts b/test/SpokePoolClient.ValidateFill.ts index 2eb3b6939..0b78dc5d2 100644 --- a/test/SpokePoolClient.ValidateFill.ts +++ b/test/SpokePoolClient.ValidateFill.ts @@ -15,7 +15,6 @@ import { import { ZERO_BYTES } from "../src/constants"; import { CHAIN_ID_TEST_LIST, originChainId, destinationChainId, repaymentChainId } from "./constants"; import { - assert, expect, BigNumber, toBNWei, @@ -38,6 +37,7 @@ import { winston, lastSpyLogIncludes, } from "./utils"; +import assert from "assert"; import { MockConfigStoreClient, MockHubPoolClient, MockSpokePoolClient } from "./mocks"; let spokePool_1: Contract, erc20_1: Contract, spokePool_2: Contract, erc20_2: Contract, hubPool: Contract; diff --git a/test/relayFeeCalculator.test.ts b/test/relayFeeCalculator.test.ts index e159998e2..7660eeb47 100644 --- a/test/relayFeeCalculator.test.ts +++ b/test/relayFeeCalculator.test.ts @@ -17,7 +17,6 @@ import { BigNumber, Contract, SignerWithAddress, - assert, assertPromiseError, assertPromisePasses, buildDepositForRelayerFeeTest, @@ -29,6 +28,7 @@ import { setupTokensForWallet, makeCustomTransport, } from "./utils"; +import assert from "assert"; import { TOKEN_SYMBOLS_MAP } from "@across-protocol/constants"; import { EMPTY_MESSAGE, ZERO_ADDRESS } from "../src/constants"; import { SpokePool } from "@across-protocol/contracts"; diff --git a/test/utils/utils.ts b/test/utils/utils.ts index f454a1d80..ef634256f 100644 --- a/test/utils/utils.ts +++ b/test/utils/utils.ts @@ -33,7 +33,7 @@ import { EMPTY_MESSAGE, PROTOCOL_DEFAULT_CHAIN_ID_INDICES, ZERO_ADDRESS } from " import { SpyTransport } from "./SpyTransport"; chai.use(chaiExclude); -const assert = chai.assert; +const chaiAssert = chai.assert; export type SignerWithAddress = utils.SignerWithAddress; @@ -51,7 +51,7 @@ export const { zeroAddress, } = utils; -export { assert, BigNumber, expect, chai, Contract, sinon, toBN, toBNWei, toWei, utf8ToHex, winston }; +export { chaiAssert, BigNumber, expect, chai, Contract, sinon, toBN, toBNWei, toWei, utf8ToHex, winston }; const TokenRolesEnum = { OWNER: "0", @@ -73,7 +73,7 @@ export function deepEqualsWithBigNumber(x: unknown, y: unknown, omitKeys: string .sort() .map((key) => [key, y?.[key]]) ); - assert.deepStrictEqual(_.omit(sortedKeysX, omitKeys), _.omit(sortedKeysY, omitKeys)); + chaiAssert.deepStrictEqual(_.omit(sortedKeysX, omitKeys), _.omit(sortedKeysY, omitKeys)); return true; } @@ -88,7 +88,7 @@ export async function assertPromiseError(promise: Promise, errMessage?: st throw err; } if (errMessage) { - assert.isTrue(err.message.includes(errMessage)); + chaiAssert.isTrue(err.message.includes(errMessage)); } } } @@ -365,7 +365,7 @@ async function _deposit( const lastEvent = events.at(-1); let args = lastEvent?.args; - assert.exists(args); + chaiAssert.exists(args); args = args!; const { blockNumber, transactionHash, transactionIndex, logIndex } = lastEvent!; @@ -402,7 +402,7 @@ export async function requestV3SlowFill( signer: SignerWithAddress ): Promise { const destinationChainId = Number(await spokePool.chainId()); - assert.notEqual(relayData.originChainId, destinationChainId); + chaiAssert.notEqual(relayData.originChainId, destinationChainId); await spokePool.connect(signer).requestSlowFill({ ...relayData, @@ -416,7 +416,7 @@ export async function requestV3SlowFill( const events = await spokePool.queryFilter(spokePool.filters.RequestedSlowFill()); const lastEvent = events.at(-1); let args = lastEvent!.args; - assert.exists(args); + chaiAssert.exists(args); args = args!; const { blockNumber, transactionHash, transactionIndex, logIndex } = lastEvent!; @@ -450,7 +450,7 @@ export async function fillV3Relay( repaymentChainId?: number ): Promise { const destinationChainId = Number(await spokePool.chainId()); - assert.notEqual(deposit.originChainId, destinationChainId); + chaiAssert.notEqual(deposit.originChainId, destinationChainId); // If the input deposit token has a bytes32 on any field, assume it is going to the new fillRelay // spoke pool method. @@ -465,7 +465,7 @@ export async function fillV3Relay( const events = await spokePool.queryFilter(spokePool.filters.FilledRelay()); const lastEvent = events.at(-1); let args = lastEvent!.args; - assert.exists(args); + chaiAssert.exists(args); args = args!; const { blockNumber, transactionHash, transactionIndex, logIndex } = lastEvent!;