Skip to content

Commit

Permalink
fix: avoid re-add fake resources at Account.getTransactionCost (#2982)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf committed Aug 19, 2024
1 parent 4c653d0 commit 2a8cb38
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-fishes-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

fix: avoid re-add fake resources at `Account.getTransactionCost`
36 changes: 33 additions & 3 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,40 @@ export class Account extends AbstractAccount {
const requiredQuantities = mergeQuantities(coinOutputsQuantities, quantities);
// An arbitrary amount of the base asset is added to cover the transaction fee during dry runs
const transactionFeeForDryRun = [{ assetId: baseAssetId, amount: bn('100000000000000000') }];
const resources = this.generateFakeResources(
mergeQuantities(requiredQuantities, transactionFeeForDryRun)

const findAssetInput = (assetId: string) =>
txRequestClone.inputs.find((input) => {
if ('assetId' in input) {
return input.assetId === assetId;
}
if ('recipient' in input) {
return baseAssetId === assetId;
}

return false;
});

const updateAssetInput = (assetId: string, quantity: BN) => {
const assetInput = findAssetInput(assetId);
const usedQuantity = quantity;

if (assetInput && 'amount' in assetInput) {
assetInput.amount = usedQuantity;
} else {
txRequestClone.addResources(
this.generateFakeResources([
{
amount: quantity,
assetId,
},
])
);
}
};

mergeQuantities(requiredQuantities, transactionFeeForDryRun).forEach(({ amount, assetId }) =>
updateAssetInput(assetId, amount)
);
txRequestClone.addResources(resources);

const txCost = await this.provider.getTransactionCost(txRequestClone, {
signatureCallback,
Expand Down

0 comments on commit 2a8cb38

Please sign in to comment.