diff --git a/api/swap/_utils.ts b/api/swap/_utils.ts index 25e8a91b3..7a762feca 100644 --- a/api/swap/_utils.ts +++ b/api/swap/_utils.ts @@ -114,11 +114,7 @@ export async function handleBaseSwapQueryParams( const refundToken = refundOnOrigin ? inputToken : outputToken; - // 3. Calculate fees based for full route - const fees = await calculateCrossSwapFees(crossSwapQuotes); - return { - fees, inputToken, outputToken, amount, @@ -295,6 +291,7 @@ export function buildBaseSwapResponseJson(params: { gasPrice: BigNumber; }; permitSwapTx?: AuthTxPayload | PermitTxPayload; + fees: CrossSwapFees; }) { return stringifyBigNumProps({ checks: { @@ -328,6 +325,7 @@ export function buildBaseSwapResponseJson(params: { outputAmount: params.originSwapQuote.expectedAmountOut, minOutputAmount: params.originSwapQuote.minAmountOut, maxInputAmount: params.originSwapQuote.maximumAmountIn, + fees: params.fees.originSwapFees, } : undefined, bridge: { @@ -335,6 +333,7 @@ export function buildBaseSwapResponseJson(params: { outputAmount: params.bridgeQuote.outputAmount, tokenIn: params.bridgeQuote.inputToken, tokenOut: params.bridgeQuote.outputToken, + fees: params.fees.bridgeFees, }, destinationSwap: params.destinationSwapQuote ? { @@ -344,6 +343,7 @@ export function buildBaseSwapResponseJson(params: { maxInputAmount: params.destinationSwapQuote.maximumAmountIn, outputAmount: params.destinationSwapQuote.expectedAmountOut, minOutputAmount: params.destinationSwapQuote.minAmountOut, + fees: params.fees.destinationSwapFees, } : undefined, }, diff --git a/api/swap/approval/index.ts b/api/swap/approval/index.ts index 44f87c9d6..3007b7e12 100644 --- a/api/swap/approval/index.ts +++ b/api/swap/approval/index.ts @@ -15,6 +15,7 @@ import { BaseSwapQueryParams, getApprovalTxns, buildBaseSwapResponseJson, + calculateCrossSwapFees, } from "../_utils"; import { getBalanceAndAllowance } from "../../_erc20"; import { getCrossSwapQuotes } from "../../_dexes/cross-swap-service"; @@ -77,10 +78,10 @@ const handler = async ( quoteFetchStrategies ); - const crossSwapTx = await buildCrossSwapTxForAllowanceHolder( - crossSwapQuotes, - integratorId - ); + const [crossSwapTx, fees] = await Promise.all([ + buildCrossSwapTxForAllowanceHolder(crossSwapQuotes, integratorId), + calculateCrossSwapFees(crossSwapQuotes), + ]); const { originSwapQuote, bridgeQuote, destinationSwapQuote, crossSwap } = crossSwapQuotes; @@ -140,6 +141,7 @@ const handler = async ( originChainId, inputTokenAddress, inputAmount, + fees, approvalSwapTx: { ...crossSwapTx, gas: originTxGas, diff --git a/api/swap/auth/index.ts b/api/swap/auth/index.ts index 8af00b28b..87e1e440d 100644 --- a/api/swap/auth/index.ts +++ b/api/swap/auth/index.ts @@ -9,6 +9,7 @@ import { handleBaseSwapQueryParams, BaseSwapQueryParams, buildBaseSwapResponseJson, + calculateCrossSwapFees, } from "../_utils"; import { getSwapRouter02Strategy } from "../../_dexes/uniswap/swap-router-02"; import { InvalidParamError } from "../../_errors"; @@ -109,11 +110,14 @@ const handler = async ( }, }); - const balance = await getBalance({ - chainId: inputToken.chainId, - tokenAddress: inputToken.address, - owner: crossSwapQuotes.crossSwap.depositor, - }); + const [balance, fees] = await Promise.all([ + getBalance({ + chainId: inputToken.chainId, + tokenAddress: inputToken.address, + owner: crossSwapQuotes.crossSwap.depositor, + }), + calculateCrossSwapFees(crossSwapQuotes), + ]); const responseJson = buildBaseSwapResponseJson({ inputTokenAddress: inputToken.address, @@ -127,6 +131,7 @@ const handler = async ( balance, // Allowance does not matter for auth-based flows allowance: BigNumber.from(0), + fees, }); logger.debug({ diff --git a/api/swap/permit/index.ts b/api/swap/permit/index.ts index cdf04ece7..57ebbf962 100644 --- a/api/swap/permit/index.ts +++ b/api/swap/permit/index.ts @@ -9,6 +9,7 @@ import { handleBaseSwapQueryParams, BaseSwapQueryParams, buildBaseSwapResponseJson, + calculateCrossSwapFees, } from "../_utils"; import { getSwapRouter02Strategy } from "../../_dexes/uniswap/swap-router-02"; import { InvalidParamError } from "../../_errors"; @@ -92,6 +93,7 @@ const handler = async ( }, quoteFetchStrategies ); + // Build tx for permit const crossSwapTxForPermit = await buildPermitTxPayload({ crossSwapQuotes, @@ -103,11 +105,14 @@ const handler = async ( }, }); - const balance = await getBalance({ - chainId: inputToken.chainId, - tokenAddress: inputToken.address, - owner: crossSwapQuotes.crossSwap.depositor, - }); + const [balance, fees] = await Promise.all([ + getBalance({ + chainId: inputToken.chainId, + tokenAddress: inputToken.address, + owner: crossSwapQuotes.crossSwap.depositor, + }), + calculateCrossSwapFees(crossSwapQuotes), + ]); const responseJson = buildBaseSwapResponseJson({ inputTokenAddress: inputToken.address, @@ -121,6 +126,7 @@ const handler = async ( balance, // Allowance does not matter for permit-based flows allowance: BigNumber.from(0), + fees, }); logger.debug({