From c92a17bab13e5dd1283a8b2d061c3a5fc5a8b1e2 Mon Sep 17 00:00:00 2001 From: pawell24 Date: Mon, 16 Sep 2024 20:03:21 +0200 Subject: [PATCH 1/3] Changing fee estimation function --- src/consts/interfaces.consts.ts | 2 ++ src/index.ts | 1 + src/request.ts | 40 +++++++++++----------------- test/basic/RequestLinkXChain.test.ts | 14 ---------- 4 files changed, 18 insertions(+), 39 deletions(-) diff --git a/src/consts/interfaces.consts.ts b/src/consts/interfaces.consts.ts index 5182afc..f37aaa0 100644 --- a/src/consts/interfaces.consts.ts +++ b/src/consts/interfaces.consts.ts @@ -169,6 +169,7 @@ export interface IPrepareDepositTxsResponse { export interface IPrepareXchainRequestFulfillmentTransactionProps { unsignedTxs: IPeanutUnsignedTransaction[] + feeEstimation: string } //signAndSubmitTx @@ -206,6 +207,7 @@ export interface ISquidRoute { value: BigNumber calldata: string to: string + estimate?: any } //getCrossChainoptionsForLink diff --git a/src/index.ts b/src/index.ts index 5ff2665..e37c79d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2318,6 +2318,7 @@ async function getSquidRoute(args: interfaces.IGetSquidRouteParams): Promise 0) { + routeResult.estimate.feeCosts.forEach((fee) => { + feeEstimation += Number(fee.amountUsd) + }) + } + + if (routeResult.estimate.gasCosts.length > 0) { + routeResult.estimate.gasCosts.forEach((gas) => { + feeEstimation += Number(gas.amountUsd) + }) + } + if (tokenType == EPeanutLinkType.native) { txOptions = { ...txOptions, @@ -297,31 +311,7 @@ export async function prepareXchainRequestFulfillmentTransaction({ unsignedTxs.push(unsignedTx) - return { unsignedTxs } -} - -export function calculateCrossChainTxFee({ - unsignedTxs, - isNativeTxValue, - fromAmount, -}: { - unsignedTxs: IPeanutUnsignedTransaction[] - isNativeTxValue: boolean - fromAmount: string -}): bigint { - let totalFee = BigInt(0) - - for (const tx of unsignedTxs) { - if (tx.value) { - totalFee += BigInt(tx.value.toString()) - } - } - - if (isNativeTxValue) { - totalFee = totalFee - ethers.utils.parseEther(fromAmount).toBigInt() - } - - return totalFee + return { unsignedTxs, feeEstimation: feeEstimation.toString() } } export function prepareRequestLinkFulfillmentTransaction({ diff --git a/test/basic/RequestLinkXChain.test.ts b/test/basic/RequestLinkXChain.test.ts index 59cfe7e..b55e508 100644 --- a/test/basic/RequestLinkXChain.test.ts +++ b/test/basic/RequestLinkXChain.test.ts @@ -63,13 +63,6 @@ describe('Peanut XChain request links fulfillment tests', function () { }) console.log('Computed x chain unsigned fulfillment transactions', xchainUnsignedTxs) - const fee = await peanut.calculateCrossChainTxFee({ - unsignedTxs: xchainUnsignedTxs.unsignedTxs, - isNativeTxValue: false, - fromAmount: amountToTestWith.toString(), - }) - console.log('Fee', ethers.utils.formatEther(fee)) - for (const unsignedTx of xchainUnsignedTxs.unsignedTxs) { const { tx, txHash } = await signAndSubmitTx({ unsignedTx, @@ -144,13 +137,6 @@ describe('Peanut XChain request links fulfillment tests', function () { }) console.log('Computed x chain unsigned fulfillment transactions', xchainUnsignedTxs) - const fee = await peanut.calculateCrossChainTxFee({ - unsignedTxs: xchainUnsignedTxs.unsignedTxs, - isNativeTxValue: true, - fromAmount: amountToTestWith.toString(), - }) - console.log('Fee1', ethers.utils.formatEther(fee)) - for (const unsignedTx of xchainUnsignedTxs.unsignedTxs) { const { tx, txHash } = await signAndSubmitTx({ unsignedTx, From cd5288d95c790500f94184d765f077dc457d8df8 Mon Sep 17 00:00:00 2001 From: pawell24 Date: Tue, 17 Sep 2024 00:22:13 +0200 Subject: [PATCH 2/3] Fixes after code review --- src/consts/interfaces.consts.ts | 2 +- src/index.ts | 2 +- src/request.ts | 9 +++++---- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/consts/interfaces.consts.ts b/src/consts/interfaces.consts.ts index f37aaa0..ce2c662 100644 --- a/src/consts/interfaces.consts.ts +++ b/src/consts/interfaces.consts.ts @@ -207,7 +207,7 @@ export interface ISquidRoute { value: BigNumber calldata: string to: string - estimate?: any + txEstimation?: any } //getCrossChainoptionsForLink diff --git a/src/index.ts b/src/index.ts index e37c79d..bd3ccec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2318,7 +2318,7 @@ async function getSquidRoute(args: interfaces.IGetSquidRouteParams): Promise 0) { - routeResult.estimate.feeCosts.forEach((fee) => { + if (routeResult.txEstimation.feeCosts.length > 0) { + routeResult.txEstimation.feeCosts.forEach((fee) => { feeEstimation += Number(fee.amountUsd) }) } - if (routeResult.estimate.gasCosts.length > 0) { - routeResult.estimate.gasCosts.forEach((gas) => { + if (routeResult.txEstimation.gasCosts.length > 0) { + routeResult.txEstimation.gasCosts.forEach((gas) => { feeEstimation += Number(gas.amountUsd) }) } From 65ea9b94eac0195e8c92ddcd1ca2a7840d55377f Mon Sep 17 00:00:00 2001 From: pawell24 Date: Tue, 17 Sep 2024 16:20:17 +0200 Subject: [PATCH 3/3] Adding a better explanation --- src/request.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/request.ts b/src/request.ts index 49f8565..c44e4b1 100644 --- a/src/request.ts +++ b/src/request.ts @@ -246,6 +246,12 @@ export async function prepareXchainRequestFulfillmentTransaction({ }) // 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 + // Explanation: + // feeCosts: Service fees that may be charged by the Squid. + // gasCosts: Network gas fees charged for blockchain transactions. + // feeEstimation: The total estimated fee, which is the sum of both feeCosts and gasCosts. + // Why loops?: Each of these costs can contain multiple items (multiple txs such as approve, swap, etc), so we iterate through each to add their USD values to the total estimated fee. + let feeEstimation = 0 if (routeResult.txEstimation.feeCosts.length > 0) { routeResult.txEstimation.feeCosts.forEach((fee) => {