Skip to content

Commit

Permalink
feat: remove usePrepareForSend from connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Mar 6, 2025
1 parent 0fd8a82 commit 5247d16
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
10 changes: 2 additions & 8 deletions apps/docs/src/guide/wallets/connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Providing the message signing flow is successful, it will return the message sig

#### `sendTransaction`

The `signTransaction` method initiates the send transaction flow for the current connection.
The `sendTransaction` method initiates the send transaction flow for the current connection.

It requires two arguments:

Expand All @@ -216,13 +216,7 @@ It will return the prepared transaction (as a [`TransactionRequestLike`](DOCS_AP

<<< @/../../../packages/account/src/connectors/fuel-connector.ts#fuel-connector-method-prepareForSend{ts:line-numbers}

It can be used in tandem with `Account.sendTransaction` to prepare and send a transaction in one go.

This is enabled by setting the `usePrepareForSend` property to `true` on the connector.

<<< @./snippets/connectors.ts#fuel-connector-method-usePrepareForSend{ts:line-numbers}

This can be beneficial for performance and user experience, as it reduces the number of round trips between the dApp and the network.
When used in conjunction with `Account.sendTransaction`, this can be beneficial for performance and user experience, as it reduces the number of round trips between the dApp and the network.

#### `assets`

Expand Down
4 changes: 0 additions & 4 deletions apps/docs/src/guide/wallets/snippets/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class WalletConnector extends FuelConnector {
};
// #endregion fuel-connector-metadata

// #region fuel-connector-method-usePrepareForSend
public override usePrepareForSend = true;
// #endregion fuel-connector-method-usePrepareForSend

private eventsConnectorEvents(): void {
// #region fuel-connector-events-accounts
const accounts: Array<string> = ['0x1234567890abcdef'];
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ export class Account extends AbstractAccount implements WithAddress {
// If the connector is using prepareForSend, the connector will prepare the transaction for the dapp,
// and submission is owned by the dapp. This reduces network requests to submit and create the
// summary for a tx.
if (this._connector.usePrepareForSend) {
if (await this._connector.hasPrepareForSend()) {
const preparedTransaction = await this._connector.prepareForSend(
this.address.toString(),
transactionRequestLike,
Expand Down
23 changes: 20 additions & 3 deletions packages/account/src/connectors/fuel-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { HashableMessage } from '@fuel-ts/hasher';
import { EventEmitter } from 'events';

import type { Asset } from '../assets/types';
import type { TransactionRequestLike } from '../providers';
import { ScriptTransactionRequest, type TransactionRequestLike } from '../providers';

import { FuelConnectorEventTypes } from './types';
import type {
Expand Down Expand Up @@ -53,7 +53,8 @@ interface Connector {
// #region fuel-connector-method-prepareForSend
prepareForSend(
address: string,
transaction: TransactionRequestLike
transaction: TransactionRequestLike,
params?: FuelConnectorSendTxParams
): Promise<TransactionRequestLike>;
// #endregion fuel-connector-method-prepareForSend
// #region fuel-connector-method-currentAccount
Expand Down Expand Up @@ -104,7 +105,6 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
installed: boolean = false;
external: boolean = true;
events = FuelConnectorEventTypes;
usePrepareForSend: boolean = false;

/**
* Should return true if the connector is loaded
Expand Down Expand Up @@ -360,6 +360,23 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

/**
* Checks the presence of a correct implementation of the prepareForSend method on
* the current connector instance.
*
* @returns boolean representing the validity of the prep method.
*/
async hasPrepareForSend(): Promise<boolean> {
try {
await this.prepareForSend('', new ScriptTransactionRequest());
} catch (error) {
if (error instanceof FuelError && error.code === FuelError.CODES.NOT_IMPLEMENTED) {
return false;
}
}
return true;
}

/**
* Event listener for the connector.
*
Expand Down
1 change: 0 additions & 1 deletion packages/account/src/connectors/fuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ export class Fuel extends FuelConnector implements FuelSdk {
const { installed } = await this.fetchConnectorStatus(connector);
if (installed) {
this._currentConnector = connector;
this.usePrepareForSend = connector.usePrepareForSend;
this.emit(this.events.currentConnector, connector);
this.setupConnectorEvents(Object.values(FuelConnectorEventTypes));
await this._storage?.setItem(Fuel.STORAGE_KEY, connector.name);
Expand Down
2 changes: 0 additions & 2 deletions packages/account/test/fixtures/mocked-prep-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { transactionRequestify, type TransactionRequestLike } from '../../src';
import { MockConnector } from './mocked-connector';

export class MockedPrepConnector extends MockConnector {
override usePrepareForSend = true;

override async prepareForSend(
address: string,
transaction: TransactionRequestLike
Expand Down

0 comments on commit 5247d16

Please sign in to comment.