Skip to content

Commit

Permalink
feat: further support for the prepareForSend flow
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Feb 10, 2025
1 parent 7edd94b commit d82d9f2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
32 changes: 24 additions & 8 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,27 +654,43 @@ export class Account extends AbstractAccount implements WithAddress {
transactionRequestLike: TransactionRequestLike,
{ estimateTxDependencies = true, onBeforeSend, skipCustomFee = false }: AccountSendTxParams = {}
): Promise<TransactionResponse> {
// Check if the account is using a connector, and therefore we do not have direct access to the
// private key.
if (this._connector) {
// 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) {
return this.provider.sendTransaction(
await this._connector.prepareForSend(this.address.toString(), transactionRequestLike),
{
estimateTxDependencies: false,
}
const preparedTransaction = await this._connector.prepareForSend(
this.address.toString(),
transactionRequestLike
);
// Submit the prepared transaction using the provider.
return this.provider.sendTransaction(preparedTransaction, {
estimateTxDependencies: false,
});
}

return this.provider.getTransactionResponse(
await this._connector.sendTransaction(this.address.toString(), transactionRequestLike, {
// Otherwise, the connector itself will submit the transaction, and the app will use
// the tx id to create the summary, requiring multiple network requests.
const txId = await this._connector.sendTransaction(
this.address.toString(),
transactionRequestLike,
{
onBeforeSend,
skipCustomFee,
})
}
);
// And return the transaction response for the returned tx id.
return this.provider.getTransactionResponse(txId);
}

const transactionRequest = transactionRequestify(transactionRequestLike);

if (estimateTxDependencies) {
await this.provider.estimateTxDependencies(transactionRequest);
}

return this.provider.sendTransaction(transactionRequest, {
estimateTxDependencies: false,
});
Expand Down
1 change: 1 addition & 0 deletions packages/account/src/connectors/fuel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ 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
1 change: 1 addition & 0 deletions packages/account/src/connectors/types/connector-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export enum FuelConnectorMethods {
// Signature methods
signMessage = 'signMessage',
sendTransaction = 'sendTransaction',
prepareForSend = 'prepareForSend',
// Assets metadata methods
assets = 'assets',
addAsset = 'addAsset',
Expand Down

0 comments on commit d82d9f2

Please sign in to comment.