Skip to content

Commit 2a8cb38

Browse files
authored
fix: avoid re-add fake resources at Account.getTransactionCost (#2982)
1 parent 4c653d0 commit 2a8cb38

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

.changeset/grumpy-fishes-obey.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
fix: avoid re-add fake resources at `Account.getTransactionCost`

packages/account/src/account.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,40 @@ export class Account extends AbstractAccount {
539539
const requiredQuantities = mergeQuantities(coinOutputsQuantities, quantities);
540540
// An arbitrary amount of the base asset is added to cover the transaction fee during dry runs
541541
const transactionFeeForDryRun = [{ assetId: baseAssetId, amount: bn('100000000000000000') }];
542-
const resources = this.generateFakeResources(
543-
mergeQuantities(requiredQuantities, transactionFeeForDryRun)
542+
543+
const findAssetInput = (assetId: string) =>
544+
txRequestClone.inputs.find((input) => {
545+
if ('assetId' in input) {
546+
return input.assetId === assetId;
547+
}
548+
if ('recipient' in input) {
549+
return baseAssetId === assetId;
550+
}
551+
552+
return false;
553+
});
554+
555+
const updateAssetInput = (assetId: string, quantity: BN) => {
556+
const assetInput = findAssetInput(assetId);
557+
const usedQuantity = quantity;
558+
559+
if (assetInput && 'amount' in assetInput) {
560+
assetInput.amount = usedQuantity;
561+
} else {
562+
txRequestClone.addResources(
563+
this.generateFakeResources([
564+
{
565+
amount: quantity,
566+
assetId,
567+
},
568+
])
569+
);
570+
}
571+
};
572+
573+
mergeQuantities(requiredQuantities, transactionFeeForDryRun).forEach(({ amount, assetId }) =>
574+
updateAssetInput(assetId, amount)
544575
);
545-
txRequestClone.addResources(resources);
546576

547577
const txCost = await this.provider.getTransactionCost(txRequestClone, {
548578
signatureCallback,

0 commit comments

Comments
 (0)