Skip to content

Commit

Permalink
refactor: Post-v2 cleanup of TokenClient & InventoryClient
Browse files Browse the repository at this point in the history
  • Loading branch information
pxrl committed Mar 26, 2024
1 parent 286fefb commit c36ef1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/clients/InventoryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<number> {
const { originChainId, destinationChainId } = deposit;
async determineRefundChainId(deposit: V3Deposit, l1Token?: string): Promise<number> {
const { originChainId, destinationChainId, inputToken, outputToken, outputAmount } = deposit;
const hubChainId = this.hubPoolClient.chainId;

// Always refund on L1 if the transfer is to L1.
Expand All @@ -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(
Expand All @@ -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();
Expand Down
20 changes: 6 additions & 14 deletions src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/relayer/Relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
Expand Down

0 comments on commit c36ef1b

Please sign in to comment.