Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing fee estimation function #150

Merged
merged 3 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
estimate?: 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 @@ -2318,6 +2318,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,
estimate: data.route.estimate,
}
}

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

let feeEstimation = 0
if (routeResult.estimate.feeCosts.length > 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,
Expand Down Expand Up @@ -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({
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
Loading