Skip to content

Commit

Permalink
chore: filter out TX props on estimatePredicates query (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Feb 5, 2024
1 parent 60da199 commit fe9091f
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 22 deletions.
8 changes: 8 additions & 0 deletions .changeset/clean-windows-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fuel-ts/abi-coder": patch
"fuels": patch
"@fuel-ts/predicate": patch
"@fuel-ts/providers": patch
---

filter out tx properties for estimatePredicates query
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
'no-underscore-dangle': 'off',
'class-methods-use-this': 'off',
'no-plusplus': 'off',
'no-param-reassign': ['error', { props: false }],
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/lines-between-class-members': [
'error',
Expand Down
1 change: 0 additions & 1 deletion packages/abi-coder/src/coders/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export class StructCoder<TCoders extends Record<string, Coder>> extends Coder<
newOffset += getWordSizePadding(newOffset);
}

// eslint-disable-next-line no-param-reassign
obj[fieldName as keyof DecodedValueOf<TCoders>] = decoded;
return obj;
}, {} as DecodedValueOf<TCoders>);
Expand Down
45 changes: 42 additions & 3 deletions packages/fuel-gauge/src/predicate/predicate-estimations.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { seedTestWallet } from '@fuel-ts/wallet/test-utils';
import type { CoinTransactionRequestInput, MessageTransactionRequestInput } from 'fuels';
import type {
CoinTransactionRequestInput,
MessageTransactionRequestInput,
ContractTransactionRequestInput,
} from 'fuels';
import {
BaseAssetId,
Provider,
Expand All @@ -8,6 +12,7 @@ import {
ScriptTransactionRequest,
InputType,
FUEL_NETWORK_URL,
getRandomB256,
} from 'fuels';

import { FuelGaugeProjectsEnum, getFuelGaugeForcProject } from '../../test/fixtures';
Expand Down Expand Up @@ -60,7 +65,7 @@ describe('Predicate', () => {
(<CoinTransactionRequestInput>tx.inputs[0]).predicate = predicateStruct.bytes;

// Create a message input with a predicate that returns true
const inputMessage: MessageTransactionRequestInput = {
const predicateMessage: MessageTransactionRequestInput = {
type: InputType.Message,
amount: bn(0),
sender: '0x00000000000000000000000059f2f1fcfe2474fd5f0b9ba1e73ca90b143eb8d0',
Expand All @@ -74,14 +79,48 @@ describe('Predicate', () => {
predicateGasUsed: bn(0),
predicateData: '0x',
};
tx.inputs.push(inputMessage);

const nonPredicateMessage: MessageTransactionRequestInput = {
type: InputType.Message,
amount: bn(100),
sender: getRandomB256(),
recipient: getRandomB256(),
witnessIndex: 0,
data: '0x',
nonce: getRandomB256(),
};

const contract: ContractTransactionRequestInput = {
type: 1,
contractId: '0xb64fa600c9a940529020fd47a18f9c1395946fbf636aad13fe029db4820ed354',
txPointer: '0x00000000000000000000000000000000',
};

const nonPredicateUtxo: CoinTransactionRequestInput = {
id: '0x5b2f4599a29aea28e325c89249c9e397f8b86bbd2405cf3b0face56c8e0e4dbe01',
amount: bn(100),
assetId: '0x0000000000000000000000000000000000000000000000000000000000000000',
owner: '0xd8813d1f9ca165ce2e8710382c3d65d64e7bd43c0f7a3d51689bcdf9513411cd',
maturity: 0,
type: 0,
txPointer: '0x00000000000000000000000000000000',
witnessIndex: 0,
};

tx.inputs.push(predicateMessage, nonPredicateMessage, contract, nonPredicateUtxo);

const txEstimated = await provider.estimatePredicates(tx);

const predicateCoinInput = <MessageTransactionRequestInput>txEstimated.inputs[0];
expect(Number(predicateCoinInput.predicateGasUsed)).toBeGreaterThan(1);
const predicateMessageInput = <MessageTransactionRequestInput>txEstimated.inputs[1];
expect(Number(predicateMessageInput.predicateGasUsed)).toBeGreaterThan(1);
const nonPredicateMessageInput = <MessageTransactionRequestInput>txEstimated.inputs[2];
expect(nonPredicateMessageInput.predicateGasUsed).toBeUndefined();
const contractInput = <ContractTransactionRequestInput>txEstimated.inputs[3];
expect(contractInput.contractId).toBe(contract.contractId);
const nonPredicateInput = <CoinTransactionRequestInput>txEstimated.inputs[4];
expect(nonPredicateInput.predicateGasUsed).toBeUndefined();
// Because the predicate that owns the coin is more complex
// it should have a bigger gas cost
expect(Number(predicateCoinInput.predicateGasUsed)).toBeGreaterThan(
Expand Down
2 changes: 0 additions & 2 deletions packages/fuels/src/cli/commands/deploy/deployContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export async function deployContract(

if (existsSync(storageSlotsPath)) {
const storageSlots = JSON.parse(readFileSync(storageSlotsPath, 'utf-8'));
// eslint-disable-next-line no-param-reassign
deployConfig.storageSlots = storageSlots;
}

Expand All @@ -27,7 +26,6 @@ export async function deployContract(
const abi = JSON.parse(readFileSync(abiPath, 'utf-8'));
const contractFactory = new ContractFactory(bytecode, abi, wallet);

// eslint-disable-next-line no-param-reassign
deployConfig.gasPrice = deployConfig.gasPrice ?? gasPrice;

const contract = await contractFactory.deployContract(deployConfig);
Expand Down
2 changes: 0 additions & 2 deletions packages/fuels/src/cli/commands/dev/autoStartFuelCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ export const autoStartFuelCore = async (config: FuelsConfig) => {
killChildProcess: cleanup,
};

// eslint-disable-next-line no-param-reassign
config.providerUrl = fuelCore.providerUrl;
// eslint-disable-next-line no-param-reassign
config.privateKey = defaultConsensusKey;
}

Expand Down
2 changes: 0 additions & 2 deletions packages/predicate/src/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export class Predicate<ARGS extends InputValue[]> extends Account implements Abs

request.inputs?.forEach((input) => {
if (input.type === InputType.Coin && hexlify(input.owner) === this.address.toB256()) {
// eslint-disable-next-line no-param-reassign
input.predicate = this.bytes;
// eslint-disable-next-line no-param-reassign
input.predicateData = this.getPredicateData(policies.length);
}
});
Expand Down
17 changes: 16 additions & 1 deletion packages/providers/src/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ fragment transactionFragment on Transaction {
}
}

fragment inputEstimatePredicatesFragment on Input {
... on InputCoin {
predicateGasUsed
}
... on InputMessage {
predicateGasUsed
}
}

fragment transactionEstimatePredicatesFragment on Transaction {
inputs {
...inputEstimatePredicatesFragment
}
}

fragment receiptFragment on Receipt {
contract {
id
Expand Down Expand Up @@ -514,7 +529,7 @@ query getTransactionsByOwner(

query estimatePredicates($encodedTransaction: HexString!) {
estimatePredicates(tx: $encodedTransaction) {
...transactionFragment
...transactionEstimatePredicatesFragment
}
}

Expand Down
18 changes: 8 additions & 10 deletions packages/providers/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,22 +668,20 @@ export default class Provider {
encodedTransaction,
});

const estimatedTransaction = transactionRequest;
const [decodedTransaction] = new TransactionCoder().decode(
getBytesCopy(response.estimatePredicates.rawPayload),
0
);
const {
estimatePredicates: { inputs },
} = response;

if (decodedTransaction.inputs) {
decodedTransaction.inputs.forEach((input, index) => {
if ('predicate' in input && input.predicateGasUsed.gt(0)) {
(<CoinTransactionRequestInput>estimatedTransaction.inputs[index]).predicateGasUsed =
if (inputs) {
inputs.forEach((input, index) => {
if ('predicateGasUsed' in input && bn(input.predicateGasUsed).gt(0)) {
(<CoinTransactionRequestInput>transactionRequest.inputs[index]).predicateGasUsed =
input.predicateGasUsed;
}
});
}

return estimatedTransaction;
return transactionRequest;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/providers/src/utils/json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-param-reassign */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { hexlify } from 'ethers';
import { clone } from 'ramda';
Expand Down

0 comments on commit fe9091f

Please sign in to comment.