From 765e07caf99d28d6bc66f4e5e224f5b265a6bfcb Mon Sep 17 00:00:00 2001 From: Daniel Bate Date: Wed, 6 Dec 2023 14:54:22 -0300 Subject: [PATCH] Revert "feat: add `Predicate.getTransferTxId` helper (#1467)" This reverts commit 70233c1e579e607bdc2714b9b8039ea0eaac40a9. --- ...nd-and-spend-funds-from-predicates.test.ts | 21 ++----------- .../send-and-spend-funds-from-predicates.md | 6 ---- packages/predicate/package.json | 1 - packages/predicate/src/predicate.ts | 30 +------------------ packages/wallet/src/account.ts | 30 ++++--------------- pnpm-lock.yaml | 7 +++-- 6 files changed, 12 insertions(+), 83 deletions(-) diff --git a/apps/docs-snippets/src/guide/predicates/send-and-spend-funds-from-predicates.test.ts b/apps/docs-snippets/src/guide/predicates/send-and-spend-funds-from-predicates.test.ts index 6fcddf6add..0f7ce1ae33 100644 --- a/apps/docs-snippets/src/guide/predicates/send-and-spend-funds-from-predicates.test.ts +++ b/apps/docs-snippets/src/guide/predicates/send-and-spend-funds-from-predicates.test.ts @@ -9,10 +9,7 @@ import { BaseAssetId, } from 'fuels'; -import { - DocSnippetProjectsEnum, - getDocsSnippetsForcProject, -} from '../../../test/fixtures/forc-projects'; +import { DocSnippetProjectsEnum, getDocsSnippetsForcProject } from '../../../test/fixtures/forc-projects'; import { getTestWallet } from '../../utils'; describe(__filename, () => { @@ -53,22 +50,11 @@ describe(__filename, () => { predicate.setData(inputAddress); // #endregion send-and-spend-funds-from-predicates-4 + // #region send-and-spend-funds-from-predicates-5 const receiverWallet = WalletUnlocked.generate({ provider, }); - // #region send-and-spend-funds-from-predicates-8 - const txId = await predicate.getTransferTxId( - receiverWallet.address, - amountToPredicate - 150_000, - BaseAssetId, - { - gasPrice, - } - ); - // #endregion send-and-spend-funds-from-predicates-8 - - // #region send-and-spend-funds-from-predicates-5 const tx2 = await predicate.transfer( receiverWallet.address, amountToPredicate - 150_000, @@ -80,9 +66,6 @@ describe(__filename, () => { await tx2.waitForResult(); // #endregion send-and-spend-funds-from-predicates-5 - const txIdFromExecutedTx = tx2.id; - - expect(txId).toEqual(txIdFromExecutedTx); }); it('should fail when trying to spend predicates entire amount', async () => { diff --git a/apps/docs/src/guide/predicates/send-and-spend-funds-from-predicates.md b/apps/docs/src/guide/predicates/send-and-spend-funds-from-predicates.md index 68c8695edd..337b0d79d8 100644 --- a/apps/docs/src/guide/predicates/send-and-spend-funds-from-predicates.md +++ b/apps/docs/src/guide/predicates/send-and-spend-funds-from-predicates.md @@ -38,12 +38,6 @@ Note the method transfer has two parameters: the recipient's address and the int Once the predicate resolves with a return value `true` based on its predefined condition, our predicate successfully spends its funds by means of a transfer to a desired wallet. ---- - -You can also use the `getTransferTxId` helper to obtain the transaction ID of the transfer beforehand, without actually executing it. - -<<< @/../../docs-snippets/src/guide/predicates/send-and-spend-funds-from-predicates.test.ts#send-and-spend-funds-from-predicates-8{ts:line-numbers} - ## Spending Entire Predicate Held Amount Trying to forward the entire amount held by the predicate results in an error because no funds are left to cover the transaction fees. Attempting this will result in an error message like: diff --git a/packages/predicate/package.json b/packages/predicate/package.json index 15e7bf9481..a45badd0f8 100644 --- a/packages/predicate/package.json +++ b/packages/predicate/package.json @@ -28,7 +28,6 @@ "@fuel-ts/abi-coder": "workspace:*", "@fuel-ts/address": "workspace:*", "@fuel-ts/interfaces": "workspace:*", - "@fuel-ts/math": "workspace:*", "@fuel-ts/merkle": "workspace:*", "@fuel-ts/providers": "workspace:*", "@fuel-ts/transactions": "workspace:*", diff --git a/packages/predicate/src/predicate.ts b/packages/predicate/src/predicate.ts index 673337bc40..7449097e07 100644 --- a/packages/predicate/src/predicate.ts +++ b/packages/predicate/src/predicate.ts @@ -7,11 +7,8 @@ import { calculateVmTxMemory, } from '@fuel-ts/abi-coder'; import { Address } from '@fuel-ts/address'; -import { BaseAssetId } from '@fuel-ts/address/configs'; import { ErrorCode, FuelError } from '@fuel-ts/errors'; -import { hashTransaction } from '@fuel-ts/hasher'; -import type { AbstractAddress, AbstractPredicate } from '@fuel-ts/interfaces'; -import type { BigNumberish } from '@fuel-ts/math'; +import type { AbstractPredicate } from '@fuel-ts/interfaces'; import type { CallResult, Provider, @@ -20,7 +17,6 @@ import type { } from '@fuel-ts/providers'; import { transactionRequestify } from '@fuel-ts/providers'; import { ByteArrayCoder, InputType } from '@fuel-ts/transactions'; -import type { TxParamsType } from '@fuel-ts/wallet'; import { Account } from '@fuel-ts/wallet'; import type { BytesLike } from 'ethers'; import { getBytesCopy, hexlify } from 'ethers'; @@ -96,30 +92,6 @@ export class Predicate extends Account implements Abs return super.sendTransaction(transactionRequest); } - /** - * Returns the transaction ID for a transfer transaction, without sending it. - * - * @param destination - The address of the destination. - * @param amount - The amount of coins to transfer. - * @param assetId - The asset ID of the coins to transfer. - * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity). - * @returns A promise that resolves to the transaction ID. - */ - async getTransferTxId( - /** Address of the destination */ - destination: AbstractAddress, - /** Amount of coins */ - amount: BigNumberish, - /** Asset ID of coins */ - assetId: BytesLike = BaseAssetId, - /** Tx Params */ - txParams: TxParamsType = {} - ): Promise { - const request = await super.prepareTransferTxRequest(destination, amount, assetId, txParams); - const populatedRequest = this.populateTransactionPredicateData(request); - return hashTransaction(populatedRequest, this.provider.getChainId()); - } - /** * Simulates a transaction with the populated predicate data. * diff --git a/packages/wallet/src/account.ts b/packages/wallet/src/account.ts index 18bbedbc51..12695799a6 100644 --- a/packages/wallet/src/account.ts +++ b/packages/wallet/src/account.ts @@ -32,7 +32,7 @@ import { formatScriptDataForTransferringToContract, } from './utils'; -export type TxParamsType = Pick; +type TxParamsType = Pick; /** * `Account` provides an abstraction for interacting with accounts or wallets on the network. @@ -239,36 +239,16 @@ export class Account extends AbstractAccount { /** Tx Params */ txParams: TxParamsType = {} ): Promise { - const request = await this.prepareTransferTxRequest(destination, amount, assetId, txParams); - return this.sendTransaction(request); - } - - /** - * A helper that prepares a transaction request for calculating the transaction ID. - * - * @param destination - The address of the destination. - * @param amount - The amount of coins to transfer. - * @param assetId - The asset ID of the coins to transfer. - * @param txParams - The transaction parameters (gasLimit, gasPrice, maturity). - * @returns A promise that resolves to the prepared transaction request. - */ - protected async prepareTransferTxRequest( - /** Address of the destination */ - destination: AbstractAddress, - /** Amount of coins */ - amount: BigNumberish, - /** Asset ID of coins */ - assetId: BytesLike = BaseAssetId, - /** Tx Params */ - txParams: TxParamsType = {} - ): Promise { const { maxGasPerTx } = this.provider.getGasConfig(); const params: TxParamsType = { gasLimit: maxGasPerTx, ...txParams }; const request = new ScriptTransactionRequest(params); request.addCoinOutput(destination, amount, assetId); + const { maxFee, requiredQuantities } = await this.provider.getTransactionCost(request); + await this.fund(request, requiredQuantities, maxFee); - return request; + + return this.sendTransaction(request); } /** diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 088ad532f3..39b72e6804 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -870,9 +870,6 @@ importers: '@fuel-ts/interfaces': specifier: workspace:* version: link:../interfaces - '@fuel-ts/math': - specifier: workspace:* - version: link:../math '@fuel-ts/merkle': specifier: workspace:* version: link:../merkle @@ -894,6 +891,10 @@ importers: ethers: specifier: ^6.7.1 version: 6.7.1 + devDependencies: + '@fuel-ts/math': + specifier: workspace:* + version: link:../math packages/program: dependencies: