Skip to content
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
3 changes: 3 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type IPrepareXchainRequestFulfillmentTransactionProps = {
provider: ethers.providers.Provider
tokenType: EPeanutLinkType
slippagePercentage?: number
slippageIncrement?: number
linkDetails: {
chainId: string
recipientAddress: string | null
Expand Down Expand Up @@ -49,6 +50,7 @@ export async function prepareXchainRequestFulfillmentTransaction(
provider,
tokenType,
slippagePercentage,
slippageIncrement,
linkDetails,
} = props
let {
Expand Down Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -830,6 +839,7 @@ export async function routeForTargetAmount({
squidRouterUrl,
fromAddress,
toAddress,
slippageIncrement,
}: {
slippagePercentage?: number
fromToken: TokenData
Expand All @@ -838,6 +848,7 @@ export async function routeForTargetAmount({
squidRouterUrl: string
fromAddress: string
toAddress: string
slippageIncrement?: number
}): Promise<{
estimatedFromAmount: string
weiFromAmount: ethers.BigNumber
Expand Down Expand Up @@ -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,
Expand All @@ -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')
Expand Down
Loading