Skip to content

Commit

Permalink
Revert "feat: add Predicate.getTransferTxId helper (#1467)"
Browse files Browse the repository at this point in the history
This reverts commit 70233c1.
  • Loading branch information
danielbate committed Dec 6, 2023
1 parent 0a502b2 commit 765e07c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, () => {
Expand Down Expand Up @@ -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,
Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 0 additions & 1 deletion packages/predicate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
30 changes: 1 addition & 29 deletions packages/predicate/src/predicate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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';
Expand Down Expand Up @@ -96,30 +92,6 @@ export class Predicate<ARGS extends InputValue[]> 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<string> {
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.
*
Expand Down
30 changes: 5 additions & 25 deletions packages/wallet/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
formatScriptDataForTransferringToContract,
} from './utils';

export type TxParamsType = Pick<TransactionRequestLike, 'gasLimit' | 'gasPrice' | 'maturity'>;
type TxParamsType = Pick<TransactionRequestLike, 'gasLimit' | 'gasPrice' | 'maturity'>;

/**
* `Account` provides an abstraction for interacting with accounts or wallets on the network.
Expand Down Expand Up @@ -239,36 +239,16 @@ export class Account extends AbstractAccount {
/** Tx Params */
txParams: TxParamsType = {}
): Promise<TransactionResponse> {
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<TransactionRequest> {
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);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 765e07c

Please sign in to comment.