Skip to content

Commit

Permalink
refactor(relayer): Factor out MDC computation (#1327)
Browse files Browse the repository at this point in the history
Making room for follow-up changes to improve relayer performance.
  • Loading branch information
pxrl authored Mar 21, 2024
1 parent 90b221c commit 434e64c
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/relayer/Relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,54 +208,60 @@ export class Relayer {
return true;
}

async checkForUnfilledDepositsAndFill(sendSlowRelays = true): Promise<void> {
// Fetch all unfilled deposits, order by total earnable fee.
const { config } = this;
const { hubPoolClient, profitClient, spokePoolClients, tokenClient, multiCallerClient } = this.clients;

// Flush any pre-existing enqueued transactions that might not have been executed.
multiCallerClient.clearTransactionQueue();

// Fetch unfilled deposits and filter out deposits upfront before we compute the minimum deposit confirmation
// per chain, which is based on the deposit volume we could fill.
const unfilledDeposits = await this._getUnfilledDeposits();
computeRequiredDepositConfirmations(deposits: V3Deposit[]): { [chainId: number]: number } {
const { profitClient } = this.clients;
const { minDepositConfirmations } = this.config;

// Sum the total unfilled deposit amount per origin chain and set a MDC for that chain.
const unfilledDepositAmountsPerChain: { [chainId: number]: BigNumber } = unfilledDeposits.reduce(
(agg, { deposit }) => {
const unfilledAmountUsd = profitClient.getFillAmountInUsd(deposit, deposit.outputAmount);
agg[deposit.originChainId] = (agg[deposit.originChainId] ?? bnZero).add(unfilledAmountUsd);
return agg;
},
{}
);
const unfilledDepositAmountsPerChain: { [chainId: number]: BigNumber } = deposits.reduce((agg, deposit) => {
const unfilledAmountUsd = profitClient.getFillAmountInUsd(deposit, deposit.outputAmount);
agg[deposit.originChainId] = (agg[deposit.originChainId] ?? bnZero).add(unfilledAmountUsd);
return agg;
}, {});

// Sort thresholds in ascending order.
const minimumDepositConfirmationThresholds = Object.keys(config.minDepositConfirmations)
const minimumDepositConfirmationThresholds = Object.keys(minDepositConfirmations)
.filter((x) => x !== "default")
.sort((x, y) => Number(x) - Number(y));

// Set the MDC for each origin chain equal to lowest threshold greater than the unfilled USD deposit amount.
// If we can't find a threshold greater than the USD amount, then use the default.
const mdcPerChain = Object.fromEntries(
Object.entries(unfilledDepositAmountsPerChain).map(([chainId, unfilledAmount]) => {
const usdThreshold = minimumDepositConfirmationThresholds.find((_usdThreshold) => {
return (
toBNWei(_usdThreshold).gte(unfilledAmount) &&
isDefined(config.minDepositConfirmations[_usdThreshold][chainId])
);
});
const usdThreshold = minimumDepositConfirmationThresholds.find(
(usdThreshold) =>
toBNWei(usdThreshold).gte(unfilledAmount) && isDefined(minDepositConfirmations[usdThreshold][chainId])
);

// If no thresholds are greater than unfilled amount, then use fallback which should have largest MDCs.
return [chainId, config.minDepositConfirmations[usdThreshold ?? "default"][chainId]];
return [chainId, minDepositConfirmations[usdThreshold ?? "default"][chainId]];
})
);
this.logger.debug({
at: "Relayer::checkForUnfilledDepositsAndFill",
message: "Setting minimum deposit confirmation based on origin chain aggregate deposit amount",
unfilledDepositAmountsPerChain,
mdcPerChain,
minDepositConfirmations: config.minDepositConfirmations,
minDepositConfirmations,
});
return mdcPerChain;
}

async checkForUnfilledDepositsAndFill(sendSlowRelays = true): Promise<void> {
// Fetch all unfilled deposits, order by total earnable fee.
const { config } = this;
const { hubPoolClient, profitClient, spokePoolClients, tokenClient, multiCallerClient } = this.clients;

// Flush any pre-existing enqueued transactions that might not have been executed.
multiCallerClient.clearTransactionQueue();

// Fetch unfilled deposits and filter out deposits upfront before we compute the minimum deposit confirmation
// per chain, which is based on the deposit volume we could fill.
const unfilledDeposits = await this._getUnfilledDeposits();

const mdcPerChain = this.computeRequiredDepositConfirmations(
Object.values(unfilledDeposits.map(({ deposit }) => deposit))
);

// Filter out deposits whose block time does not meet the minimum number of confirmations for the origin chain.
const confirmedUnfilledDeposits = unfilledDeposits
Expand Down

0 comments on commit 434e64c

Please sign in to comment.