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

fix: avoid re-add fake resources at Account.getTransactionCost #2982

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading