Skip to content

Commit

Permalink
Merge pull request #162 from peanutprotocol:fix/xchain-request-native…
Browse files Browse the repository at this point in the history
…-amount

fix: don't send double amount for native xchain requests
  • Loading branch information
Hugo0 authored Oct 29, 2024
2 parents 5c90b19 + acfc97c commit 59df5ee
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 28 deletions.
28 changes: 8 additions & 20 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export async function prepareXchainRequestFulfillmentTransaction(
fromToken: fromTokenData,
toAmount: destinationTokenAmount,
toToken: toTokenData,
slippagePercentage: 0.3, // this can be low because squid will add slippage
})

console.log('estimatedFromAmount', estimatedFromAmount)
Expand Down Expand Up @@ -282,21 +283,20 @@ export async function prepareXchainRequestFulfillmentTransaction(

let feeEstimation = 0
if (routeResult.txEstimation.feeCosts.length > 0) {
routeResult.txEstimation.feeCosts.forEach((fee) => {
routeResult.txEstimation.feeCosts.forEach((fee: { amountUsd: string }) => {
feeEstimation += Number(fee.amountUsd)
})
}

if (routeResult.txEstimation.gasCosts.length > 0) {
routeResult.txEstimation.gasCosts.forEach((gas) => {
routeResult.txEstimation.gasCosts.forEach((gas: { amountUsd: string }) => {
feeEstimation += Number(gas.amountUsd)
})
}

if (tokenType == EPeanutLinkType.native) {
txOptions = {
...txOptions,
value: tokenAmount,
}
} else if (tokenType == EPeanutLinkType.erc20) {
config.verbose && console.log('checking allowance...')
Expand Down Expand Up @@ -328,23 +328,11 @@ export async function prepareXchainRequestFulfillmentTransaction(

config.verbose && console.log('Squid route calculated :)', { routeResult })

let unsignedTx: IPeanutUnsignedTransaction = {}

if (tokenType == EPeanutLinkType.native) {
unsignedTx = {
data: routeResult.calldata,
to: routeResult.to,
value: BigInt(routeResult.value.toString()) + BigInt(txOptions.value.toString()),
}
} else if (tokenType == EPeanutLinkType.erc20) {
unsignedTx = {
data: routeResult.calldata,
to: routeResult.to,
value: BigInt(routeResult.value.toString()),
}
}

unsignedTxs.push(unsignedTx)
unsignedTxs.push({
data: routeResult.calldata,
to: routeResult.to,
value: BigInt(routeResult.value.toString()),
})

return { unsignedTxs, feeEstimation: feeEstimation.toString(), estimatedFromAmount }
}
Expand Down
11 changes: 3 additions & 8 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ export async function prepareXchainFromAmountCalculation({
fromToken,
toAmount,
toToken,
slippagePercentage = 1.5,
slippagePercentage = 0.3, // 0.3%
}: {
fromToken: TokenData
toToken: TokenData
Expand Down Expand Up @@ -736,17 +736,12 @@ export async function prepareXchainFromAmountCalculation({
const toTokenPriceBN = ethers.utils.parseUnits(toTokenPrice.toString(), normalizedDecimalCount)
const toAmountBN = ethers.utils.parseUnits(toAmount, 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.
// TODO: use bigint
const slippageFractionBN = ethers.BigNumber.from(Math.floor(slippagePercentage * 1000))

// For example, a 10.5% slippage is represented here as 10,500 (after scaling),
// and dividing by 100,000 effectively applies the 10.5% to the fromAmountBN.
const slippageBN = fromAmountBN.mul(slippageFractionBN).div(100000)

const slippageBN = fromAmountBN.mul(slippageFractionBN).div(100000) // 1000 * 100 (10e5)
const totalFromAmountBN = fromAmountBN.add(slippageBN)

return ethers.utils.formatUnits(totalFromAmountBN, fromToken.decimals)
} catch (error) {
console.error('Failed to calculate fromAmount:', error)
Expand Down

0 comments on commit 59df5ee

Please sign in to comment.