diff --git a/src/request.ts b/src/request.ts index 7398566..74e68b3 100644 --- a/src/request.ts +++ b/src/request.ts @@ -22,6 +22,7 @@ export type IPrepareXchainRequestFulfillmentTransactionProps = { provider: ethers.providers.Provider tokenType: EPeanutLinkType slippagePercentage?: number + slippageIncrement?: number linkDetails: { chainId: string recipientAddress: string | null @@ -49,6 +50,7 @@ export async function prepareXchainRequestFulfillmentTransaction( provider, tokenType, slippagePercentage, + slippageIncrement, linkDetails, } = props let { @@ -109,6 +111,7 @@ export async function prepareXchainRequestFulfillmentTransaction( squidRouterUrl, fromAddress: senderAddress, toAddress: recipientAddress, + slippageIncrement, }) // Transaction estimation from Squid API allows us to know the transaction fees (gas and fee), then we can iterate over them and add the values ​​that are in dollars diff --git a/src/util.ts b/src/util.ts index d7b2c80..3c5fd3a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -748,9 +748,18 @@ export async function prepareXchainFromAmountCalculation({ // This ensures calculations are consistent and prevents issues with scientific notation // that could arise from small price values or different token decimals. const normalizedDecimalCount = Math.max(fromToken.decimals, toToken.decimals) - const fromTokenPriceBN = ethers.utils.parseUnits(fromTokenPrice.toFixed(normalizedDecimalCount), normalizedDecimalCount) - const toTokenPriceBN = ethers.utils.parseUnits(toTokenPrice.toFixed(normalizedDecimalCount), normalizedDecimalCount) - const toAmountBN = ethers.utils.parseUnits(Number(toAmount).toFixed(normalizedDecimalCount), normalizedDecimalCount) + const fromTokenPriceBN = ethers.utils.parseUnits( + fromTokenPrice.toFixed(normalizedDecimalCount), + normalizedDecimalCount + ) + const toTokenPriceBN = ethers.utils.parseUnits( + toTokenPrice.toFixed(normalizedDecimalCount), + normalizedDecimalCount + ) + const toAmountBN = ethers.utils.parseUnits( + Number(toAmount).toFixed(normalizedDecimalCount), + normalizedDecimalCount + ) const fromAmountBN = toTokenPriceBN.mul(toAmountBN).div(fromTokenPriceBN) // Slippage percentage is multiplied by 1000 to convert it into an integer form that represents the fraction. // because BigNumber cannot handle floating points directly. @@ -830,6 +839,7 @@ export async function routeForTargetAmount({ squidRouterUrl, fromAddress, toAddress, + slippageIncrement, }: { slippagePercentage?: number fromToken: TokenData @@ -838,6 +848,7 @@ export async function routeForTargetAmount({ squidRouterUrl: string fromAddress: string toAddress: string + slippageIncrement?: number }): Promise<{ estimatedFromAmount: string weiFromAmount: ethers.BigNumber @@ -877,6 +888,7 @@ export async function routeForTargetAmount({ const weiToAmount = ethers.utils.parseUnits(targetAmount, toToken.decimals) let minToAmount: ethers.BigNumber = ethers.BigNumber.from(0) slippagePercentage = 0 + slippageIncrement ??= 0.4 while (minToAmount.lt(weiToAmount)) { result = await estimateRouteWithMinSlippage({ slippagePercentage, @@ -890,7 +902,7 @@ export async function routeForTargetAmount({ toTokenPrice, }) minToAmount = ethers.BigNumber.from(result.routeResult.txEstimation.toAmountMin) - slippagePercentage += 0.1 + slippagePercentage += slippageIncrement if (5.0 < slippagePercentage) { // we dont want to go over 5% slippage throw new Error('Slippage percentage exceeded maximum allowed value')