Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ProfitClient): Fix fill amount USD computation #1688

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/clients/ProfitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
BigNumber,
formatFeePct,
getCurrentTime,
getNetworkName,
isDefined,
min,
winston,
Expand Down Expand Up @@ -390,16 +391,26 @@ export class ProfitClient {
}

// Return USD amount of fill amount for deposited token, should always return in wei as the units.
getFillAmountInUsd(deposit: Deposit, fillAmount = deposit.outputAmount): BigNumber {
const l1TokenInfo = this.hubPoolClient.getTokenInfoForDeposit(deposit);
if (!l1TokenInfo) {
const { inputToken } = deposit;
throw new Error(
`ProfitClient#getFillAmountInUsd missing l1TokenInfo for deposit with origin token: ${inputToken}`
);
getFillAmountInUsd(
deposit: Pick<Deposit, "destinationChainId" | "outputToken" | "outputAmount">
): BigNumber | undefined {
const { destinationChainId, outputToken, outputAmount } = deposit;
let l1Token: L1Token;

try {
l1Token = this.hubPoolClient.getL1TokenInfoForL2Token(outputToken, destinationChainId);
} catch {
pxrl marked this conversation as resolved.
Show resolved Hide resolved
this.logger.info({
at: "ProfitClient#getFillAmountInUsd",
message: `Cannot resolve output token ${outputToken} on ${getNetworkName(destinationChainId)}.`,
});
return undefined;
}
const tokenPriceInUsd = this.getPriceOfToken(l1TokenInfo.symbol);
return fillAmount.mul(tokenPriceInUsd).div(bn10.pow(l1TokenInfo.decimals));

const tokenPriceInUsd = this.getPriceOfToken(l1Token.symbol);

// The USD amount of a fill must be normalised to 18 decimals, so factor out the token's own decimal promotion.
return outputAmount.mul(tokenPriceInUsd).div(bn10.pow(l1Token.decimals));
}

async getFillProfitability(
Expand Down
11 changes: 11 additions & 0 deletions src/relayer/Relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ export class Relayer {

// Ensure that the individual deposit meets the minimum deposit confirmation requirements for its value.
const fillAmountUsd = profitClient.getFillAmountInUsd(deposit);
if (!isDefined(fillAmountUsd)) {
this.logger.debug({
at: "Relayer::evaluateFill",
message: `Skipping ${srcChain} deposit due to uncertain fill amount.`,
destinationChainId,
outputToken: deposit.outputToken,
transactionHash: deposit.transactionHash,
});
return false;
}

const { minConfirmations } = minDepositConfirmations[originChainId].find(({ usdThreshold }) =>
usdThreshold.gte(fillAmountUsd)
);
Expand Down