From c36ef1bd92f80e2be67787cca06a05e592aa6ea6 Mon Sep 17 00:00:00 2001 From: Paul <108695806+pxrl@users.noreply.github.com> Date: Mon, 25 Mar 2024 23:26:04 +0100 Subject: [PATCH] refactor: Post-v2 cleanup of TokenClient & InventoryClient --- src/clients/InventoryClient.ts | 10 +++------- src/clients/TokenClient.ts | 20 ++++++-------------- src/relayer/Relayer.ts | 4 ++-- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/clients/InventoryClient.ts b/src/clients/InventoryClient.ts index f8ef8eb51..cec4f1430 100644 --- a/src/clients/InventoryClient.ts +++ b/src/clients/InventoryClient.ts @@ -18,7 +18,7 @@ import { } from "../utils"; import { HubPoolClient, TokenClient, BundleDataClient } from "."; import { AdapterManager, CrossChainTransferClient } from "./bridges"; -import { Deposit, InventoryConfig } from "../interfaces"; +import { InventoryConfig, V3Deposit } from "../interfaces"; import lodash from "lodash"; import { CONTRACT_ADDRESSES } from "../common"; import { CombinedRefunds } from "../dataworker/DataworkerUtils"; @@ -186,8 +186,8 @@ export class InventoryClient { // number to the target threshold and: // If this number of more than the target for the designation chain + rebalance overshoot then refund on L1. // Else, the post fill amount is within the target, so refund on the destination chain. - async determineRefundChainId(deposit: Deposit, l1Token?: string): Promise { - const { originChainId, destinationChainId } = deposit; + async determineRefundChainId(deposit: V3Deposit, l1Token?: string): Promise { + const { originChainId, destinationChainId, inputToken, outputToken, outputAmount } = deposit; const hubChainId = this.hubPoolClient.chainId; // Always refund on L1 if the transfer is to L1. @@ -199,8 +199,6 @@ export class InventoryClient { // for disparate output tokens, so if one appears here then something is wrong. Throw hard and fast in that case. // In future, fills for disparate output tokens should probably just take refunds on the destination chain and // outsource inventory management to the operator. - const inputToken = sdkUtils.getDepositInputToken(deposit); - const outputToken = sdkUtils.getDepositOutputToken(deposit); if (!this.hubPoolClient.areTokensEquivalent(inputToken, originChainId, outputToken, destinationChainId)) { const [srcChain, dstChain] = [getNetworkName(originChainId), getNetworkName(destinationChainId)]; throw new Error( @@ -219,8 +217,6 @@ export class InventoryClient { const chainVirtualBalanceWithShortfall = chainVirtualBalance.sub(chainShortfall); const cumulativeVirtualBalance = this.getCumulativeBalance(l1Token); let cumulativeVirtualBalanceWithShortfall = cumulativeVirtualBalance.sub(chainShortfall); - - const outputAmount = sdkUtils.getDepositOutputAmount(deposit); let chainVirtualBalanceWithShortfallPostRelay = chainVirtualBalanceWithShortfall.sub(outputAmount); const startTime = Date.now(); diff --git a/src/clients/TokenClient.ts b/src/clients/TokenClient.ts index e0b102afa..faecf27c8 100644 --- a/src/clients/TokenClient.ts +++ b/src/clients/TokenClient.ts @@ -1,10 +1,8 @@ -import { utils as sdkUtils } from "@across-protocol/sdk-v2"; import { HubPoolClient, SpokePoolClient } from "."; -import { Deposit } from "../interfaces"; +import { V3Deposit } from "../interfaces"; import { BigNumber, bnZero, - bnOne, Contract, ERC20, MAX_SAFE_ALLOWANCE, @@ -62,14 +60,8 @@ export class TokenClient { return this.tokenShortfall?.[chainId]?.[token]?.deposits || []; } - hasBalanceForFill(deposit: Deposit, fillAmount: BigNumber): boolean { - const outputToken = sdkUtils.getDepositOutputToken(deposit); - return this.getBalance(deposit.destinationChainId, outputToken).gte(fillAmount); - } - - hasBalanceForZeroFill(deposit: Deposit): boolean { - const outputToken = sdkUtils.getDepositOutputToken(deposit); - return this.getBalance(deposit.destinationChainId, outputToken).gte(bnOne); + hasBalanceForFill(deposit: V3Deposit): boolean { + return this.getBalance(deposit.destinationChainId, deposit.outputToken).gte(deposit.outputAmount); } // If the relayer tries to execute a relay but does not have enough tokens to fully fill it it will capture the @@ -83,10 +75,10 @@ export class TokenClient { assign(this.tokenShortfall, [chainId, token], { deposits, totalRequirement }); } - captureTokenShortfallForFill(deposit: Deposit, unfilledAmount: BigNumber): void { + captureTokenShortfallForFill(deposit: V3Deposit): void { + const { outputAmount: unfilledAmount } = deposit; this.logger.debug({ at: "TokenBalanceClient", message: "Handling token shortfall", deposit, unfilledAmount }); - const outputToken = sdkUtils.getDepositOutputToken(deposit); - this.captureTokenShortfall(deposit.destinationChainId, outputToken, deposit.depositId, unfilledAmount); + this.captureTokenShortfall(deposit.destinationChainId, deposit.outputToken, deposit.depositId, unfilledAmount); } // Returns the total token shortfall the client has seen. Shortfall is defined as the difference between the total diff --git a/src/relayer/Relayer.ts b/src/relayer/Relayer.ts index 596b4b05c..d39e57a53 100644 --- a/src/relayer/Relayer.ts +++ b/src/relayer/Relayer.ts @@ -288,7 +288,7 @@ export class Relayer { const l1Token = hubPoolClient.getL1TokenInfoForL2Token(inputToken, originChainId); const selfRelay = [depositor, recipient].every((address) => address === this.relayerAddress); - if (tokenClient.hasBalanceForFill(deposit, outputAmount) && !selfRelay) { + if (tokenClient.hasBalanceForFill(deposit) && !selfRelay) { const { repaymentChainId, realizedLpFeePct, @@ -315,7 +315,7 @@ export class Relayer { } else { // TokenClient.getBalance returns that we don't have enough balance to submit the fast fill. // At this point, capture the shortfall so that the inventory manager can rebalance the token inventory. - tokenClient.captureTokenShortfallForFill(deposit, outputAmount); + tokenClient.captureTokenShortfallForFill(deposit); if (sendSlowRelays) { this.requestSlowFill(deposit); }