Skip to content

Commit

Permalink
Merge pull request #150 from peanutprotocol/feat/x-chain-request
Browse files Browse the repository at this point in the history
Changing fee estimation function
  • Loading branch information
Hugo0 authored Sep 17, 2024
2 parents f022cbc + 65ea9b9 commit e27a639
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/consts/interfaces.consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export interface IPrepareDepositTxsResponse {

export interface IPrepareXchainRequestFulfillmentTransactionProps {
unsignedTxs: IPeanutUnsignedTransaction[]
feeEstimation: string
}

//signAndSubmitTx
Expand Down Expand Up @@ -206,6 +207,7 @@ export interface ISquidRoute {
value: BigNumber
calldata: string
to: string
txEstimation?: any
}

//getCrossChainoptionsForLink
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,7 @@ async function getSquidRoute(args: interfaces.IGetSquidRouteParams): Promise<int
value: BigNumber.from(data.route.transactionRequest.value),
calldata: data.route.transactionRequest.data,
to: data.route.transactionRequest.target,
txEstimation: data.route.estimate,
}
}

Expand Down
47 changes: 22 additions & 25 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ export async function prepareXchainRequestFulfillmentTransaction({
toAddress: recipientAddress,
enableBoost: true,
})

// 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) => {
feeEstimation += Number(fee.amountUsd)
})
}

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

if (tokenType == EPeanutLinkType.native) {
txOptions = {
...txOptions,
Expand Down Expand Up @@ -297,31 +318,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({
Expand Down
14 changes: 0 additions & 14 deletions test/basic/RequestLinkXChain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e27a639

Please sign in to comment.