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

chore: avoid reassigning inputs and outputs at BaseInvocationScope #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions .changeset/heavy-trainers-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@fuel-ts/account": minor
"@fuel-ts/program": minor
---

- Add `outputVariables` and `missingContractIds` to the return of `estimateTxDependencies`
- Removed `estimatedOutputs` from return of `getTransactionCost`
- Add `outputVariables` and `missingContractIds` to the return of `getTransactionCost`
- Avoid reassigning `inputs` and `outputs` from the estimated TX at `BaseInvocationScope`
8 changes: 6 additions & 2 deletions packages/account/src/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ describe('Account', () => {

const estimateTxDependencies = vi
.spyOn(providersMod.Provider.prototype, 'estimateTxDependencies')
.mockImplementation(() => Promise.resolve({ receipts: [] }));
.mockImplementation(() =>
Promise.resolve({ receipts: [], missingContractIds: [], outputVariables: 0 })
);

const sendTransaction = vi
.spyOn(providersMod.Provider.prototype, 'sendTransaction')
Expand Down Expand Up @@ -341,7 +343,9 @@ describe('Account', () => {

const estimateTxDependencies = vi
.spyOn(providersMod.Provider.prototype, 'estimateTxDependencies')
.mockImplementation(() => Promise.resolve({ receipts: [] }));
.mockImplementation(() =>
Promise.resolve({ receipts: [], missingContractIds: [], outputVariables: 0 })
);

const simulate = vi
.spyOn(providersMod.Provider.prototype, 'simulate')
Expand Down
27 changes: 24 additions & 3 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export type CallResult = {
receipts: TransactionResultReceipt[];
};

export type EstimateTxDependenciesReturns = CallResult & {
outputVariables: number;
missingContractIds: string[];
};

/**
* A Fuel block
*/
Expand Down Expand Up @@ -693,16 +698,22 @@ export default class Provider {
* @param transactionRequest - The transaction request object.
* @returns A promise.
*/
async estimateTxDependencies(transactionRequest: TransactionRequest): Promise<CallResult> {
async estimateTxDependencies(
transactionRequest: TransactionRequest
): Promise<EstimateTxDependenciesReturns> {
if (transactionRequest.type === TransactionType.Create) {
return {
receipts: [],
outputVariables: 0,
missingContractIds: [],
};
}

await this.estimatePredicates(transactionRequest);

let receipts: TransactionResultReceipt[] = [];
const missingContractIds: string[] = [];
let outputVariables = 0;

for (let attempt = 0; attempt < MAX_RETRIES; attempt++) {
const { dryRun: gqlReceipts } = await this.operations.dryRun({
Expand All @@ -717,9 +728,11 @@ export default class Provider {
missingOutputVariables.length !== 0 || missingOutputContractIds.length !== 0;

if (hasMissingOutputs) {
outputVariables += missingOutputVariables.length;
transactionRequest.addVariableOutputs(missingOutputVariables.length);
missingOutputContractIds.forEach(({ contractId }) => {
transactionRequest.addContractInputAndOutput(Address.fromString(contractId));
missingContractIds.push(contractId);
});
} else {
break;
Expand All @@ -728,6 +741,8 @@ export default class Provider {

return {
receipts,
outputVariables,
missingContractIds,
};
}

Expand Down Expand Up @@ -786,7 +801,8 @@ export default class Provider {
): Promise<
TransactionCost & {
estimatedInputs: TransactionRequest['inputs'];
estimatedOutputs: TransactionRequest['outputs'];
outputVariables: number;
missingContractIds: string[];
}
> {
const txRequestClone = clone(transactionRequestify(transactionRequestLike));
Expand Down Expand Up @@ -835,6 +851,8 @@ export default class Provider {
*/

let receipts: TransactionResultReceipt[] = [];
let missingContractIds: string[] = [];
let outputVariables = 0;
// Transactions of type Create does not consume any gas so we can the dryRun
if (isScriptTransaction && estimateTxDependencies) {
/**
Expand All @@ -852,6 +870,8 @@ export default class Provider {
const result = await this.estimateTxDependencies(txRequestClone);

receipts = result.receipts;
outputVariables = result.outputVariables;
missingContractIds = result.missingContractIds;
}

// For CreateTransaction the gasUsed is going to be the minGas
Expand All @@ -877,7 +897,8 @@ export default class Provider {
minFee,
maxFee,
estimatedInputs: txRequestClone.inputs,
estimatedOutputs: txRequestClone.outputs,
outputVariables,
missingContractIds,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,6 @@ export abstract class BaseTransactionRequest implements BaseTransactionRequestLi
this.inputs.forEach((i) => {
let correspondingInput: TransactionRequestInput | undefined;
switch (i.type) {
case InputType.Contract:
return;
case InputType.Coin:
correspondingInput = inputs.find((x) => x.type === InputType.Coin && x.owner === i.owner);
break;
Expand All @@ -653,7 +651,7 @@ export abstract class BaseTransactionRequest implements BaseTransactionRequestLi
);
break;
default:
break;
return;
}
if (
correspondingInput &&
Expand Down
4 changes: 3 additions & 1 deletion packages/account/src/wallet/wallet-unlocked.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ describe('WalletUnlocked', () => {

const estimateTxDependencies = vi
.spyOn(providersMod.Provider.prototype, 'estimateTxDependencies')
.mockImplementation(() => Promise.resolve({ receipts: [] }));
.mockImplementation(() =>
Promise.resolve({ receipts: [], missingContractIds: [], outputVariables: 0 })
);

const call = vi
.spyOn(providersMod.Provider.prototype, 'call')
Expand Down
40 changes: 21 additions & 19 deletions packages/program/src/functions/base-invocation-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import type { InputValue } from '@fuel-ts/abi-coder';
import type { BaseWalletUnlocked, Provider, CoinQuantity } from '@fuel-ts/account';
import { ScriptTransactionRequest } from '@fuel-ts/account';
import { Address } from '@fuel-ts/address';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { AbstractAccount, AbstractContract, AbstractProgram } from '@fuel-ts/interfaces';
import type { BN } from '@fuel-ts/math';
import { bn, toNumber } from '@fuel-ts/math';
import { InputType, OutputType } from '@fuel-ts/transactions';
import { InputType } from '@fuel-ts/transactions';
import * as asm from '@fuels/vm-asm';

import { getContractCallScript } from '../contract-call-script';
Expand Down Expand Up @@ -230,33 +231,34 @@ export class BaseInvocationScope<TReturn = any> {
async fundWithRequiredCoins() {
const transactionRequest = await this.getTransactionRequest();

const { maxFee, gasUsed, minGasPrice, estimatedInputs, estimatedOutputs } =
await this.getTransactionCost();

const {
maxFee,
gasUsed,
minGasPrice,
estimatedInputs,
outputVariables,
missingContractIds,
requiredQuantities,
} = await this.getTransactionCost();
this.setDefaultTxParams(transactionRequest, minGasPrice, gasUsed);

transactionRequest.outputs = estimatedOutputs;

// Clean coin inputs before add new coins to the request
this.transactionRequest.inputs = estimatedInputs.filter((i) => i.type !== InputType.Coin);
this.transactionRequest.inputs = this.transactionRequest.inputs.filter(
(i) => i.type !== InputType.Coin
);

await this.program.account?.fund(this.transactionRequest, this.requiredCoins, maxFee);
await this.program.account?.fund(this.transactionRequest, requiredQuantities, maxFee);

// update predicate inputs with estimated predicate-related info because the funding removes it
this.transactionRequest.updatePredicateInputs(estimatedInputs);

// Update output coin indexes after funding because the funding reordered the inputs
this.transactionRequest.outputs = this.transactionRequest.outputs.filter(
(x) => x.type !== OutputType.Contract
);

this.transactionRequest.inputs.forEach((input, inputIndex) => {
if (input.type !== InputType.Contract) {
return;
}
this.transactionRequest.outputs.push({ type: OutputType.Contract, inputIndex });
// Adding missing contract ids
missingContractIds.forEach((contractId) => {
this.transactionRequest.addContractInputAndOutput(Address.fromString(contractId));
});

// Adding required number of OutputVariables
this.transactionRequest.addVariableOutputs(outputVariables);

return this;
}

Expand Down