Skip to content

Commit

Permalink
perf: bridge quote for min output
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki committed Dec 17, 2024
1 parent 5eebd02 commit 0a14853
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions api/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
TokenNotFoundError,
} from "./_errors";
import { Token } from "./_dexes/types";
import { addMarkupToAmount } from "./_dexes/uniswap/utils";

export { InputError, handleErrorCondition } from "./_errors";
export const { Profiler } = sdk.utils;
Expand Down Expand Up @@ -897,6 +898,8 @@ export async function getBridgeQuoteForMinOutput(params: {
recipient?: string;
message?: string;
}) {
const maxTries = 3;
const tryChunkSize = 3;
const baseParams = {
inputToken: params.inputToken.address,
outputToken: params.outputToken.address,
Expand All @@ -911,7 +914,7 @@ export async function getBridgeQuoteForMinOutput(params: {
// 1. Use the suggested fees to get an indicative quote with
// input amount equal to minOutputAmount
let tries = 0;
let adjustedInputAmount = params.minOutputAmount;
let adjustedInputAmount = addMarkupToAmount(params.minOutputAmount, 0.005);
let indicativeQuote = await getSuggestedFees({
...baseParams,
amount: adjustedInputAmount.toString(),
Expand All @@ -921,27 +924,44 @@ export async function getBridgeQuoteForMinOutput(params: {
undefined;

// 2. Adjust input amount to meet minOutputAmount
while (tries < 3) {
adjustedInputAmount = adjustedInputAmount
.mul(utils.parseEther("1").add(adjustmentPct))
.div(sdk.utils.fixedPointAdjustment);
const adjustedQuote = await getSuggestedFees({
...baseParams,
amount: adjustedInputAmount.toString(),
while (tries < maxTries) {
const inputAmounts = Array.from({ length: tryChunkSize }).map((_, i) => {
const buffer = 0.001 * i;
return addMarkupToAmount(
adjustedInputAmount
.mul(utils.parseEther("1").add(adjustmentPct))
.div(sdk.utils.fixedPointAdjustment),
buffer
);
});
const outputAmount = adjustedInputAmount.sub(
adjustedInputAmount
.mul(adjustedQuote.totalRelayFee.pct)
.div(sdk.utils.fixedPointAdjustment)
const quotes = await Promise.all(
inputAmounts.map((inputAmount) => {
return getSuggestedFees({
...baseParams,
amount: inputAmount.toString(),
});
})
);

if (outputAmount.gte(params.minOutputAmount)) {
finalQuote = adjustedQuote;
for (const [i, quote] of Object.entries(quotes)) {
const inputAmount = inputAmounts[Number(i)];
const outputAmount = inputAmount.sub(
inputAmount
.mul(quote.totalRelayFee.pct)
.div(sdk.utils.fixedPointAdjustment)
);
if (outputAmount.gte(params.minOutputAmount)) {
finalQuote = quote;
break;
}
}

if (finalQuote) {
break;
} else {
adjustmentPct = adjustedQuote.totalRelayFee.pct;
tries++;
}

adjustedInputAmount = inputAmounts[inputAmounts.length - 1];
tries++;
}

if (!finalQuote) {
Expand Down

0 comments on commit 0a14853

Please sign in to comment.