diff --git a/.changeset/grumpy-fishes-obey.md b/.changeset/grumpy-fishes-obey.md new file mode 100644 index 00000000000..8f11385b5a6 --- /dev/null +++ b/.changeset/grumpy-fishes-obey.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/account": patch +--- + +fix: avoid re-add fake resources at `Account.getTransactionCost` diff --git a/packages/account/src/account.ts b/packages/account/src/account.ts index e6636fab933..522faefdee1 100644 --- a/packages/account/src/account.ts +++ b/packages/account/src/account.ts @@ -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,