Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added onBeforeSend hook to the connector interface #3487

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/sharp-islands-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: added `onBeforeSend` hook to the connector interface
2 changes: 1 addition & 1 deletion .github/workflows/pr-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: "Release PR to npm"
runs-on: ubuntu-latest
# comment out if:false to enable release PR to npm
if: false
# if: false
permissions: write-all
steps:
- name: Checkout
Expand Down
11 changes: 9 additions & 2 deletions packages/account/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,18 @@ export class Account extends AbstractAccount {
*/
async sendTransaction(
transactionRequestLike: TransactionRequestLike,
{ estimateTxDependencies = true }: ProviderSendTxParams = {}
{
estimateTxDependencies = true,
onBeforeSend,
skipCustomFee = false
}: ProviderSendTxParams = {}
): Promise<TransactionResponse> {
if (this._connector) {
return this.provider.getTransactionResponse(
await this._connector.sendTransaction(this.address.toString(), transactionRequestLike)
await this._connector.sendTransaction(this.address.toString(), transactionRequestLike, {
onBeforeSend,
skipCustomFee,
})
);
}
const transactionRequest = transactionRequestify(transactionRequestLike);
Expand Down
15 changes: 12 additions & 3 deletions packages/account/src/connectors/fuel-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
FuelEventArg,
Version,
SelectNetworkArguments,
SendTransactionParams,
} from './types';

interface Connector {
Expand Down Expand Up @@ -42,7 +43,11 @@ interface Connector {
signTransaction(address: string, transaction: TransactionRequestLike): Promise<string>;
// #endregion fuel-connector-method-signTransaction
// #region fuel-connector-method-sendTransaction
sendTransaction(address: string, transaction: TransactionRequestLike): Promise<string>;
sendTransaction(
address: string,
transaction: TransactionRequestLike,
params?: SendTransactionParams
): Promise<string>;
// #endregion fuel-connector-method-sendTransaction
// #region fuel-connector-method-currentAccount
currentAccount(): Promise<string | null>;
Expand Down Expand Up @@ -193,10 +198,14 @@ export abstract class FuelConnector extends EventEmitter implements Connector {
*
* @param address - The address to sign the transaction
* @param transaction - The transaction to send
*
* @param params - Optional parameters to send the transaction
* @returns The transaction id
*/
async sendTransaction(_address: string, _transaction: TransactionRequestLike): Promise<string> {
async sendTransaction(
_address: string,
_transaction: TransactionRequestLike,
_params?: SendTransactionParams
): Promise<string> {
throw new FuelError(FuelError.CODES.NOT_IMPLEMENTED, 'Method not implemented.');
}

Expand Down
7 changes: 7 additions & 0 deletions packages/account/src/connectors/types/data-type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { JsonAbi } from '@fuel-ts/abi-coder';
import type { RequireAtLeastOne } from 'type-fest';

import type { TransactionRequest } from '../../providers';

/**
* @name Version
*/
Expand Down Expand Up @@ -42,3 +44,8 @@ export type SelectNetworkArguments = RequireAtLeastOne<Network, 'chainId' | 'url
* Read more at: https://docs.fuel.network/docs/specs/abi/json-abi-format/
*/
export type FuelABI = JsonAbi;

export type SendTransactionParams = {
skipCustomFee?: boolean;
onBeforeSend?: (txRequest: TransactionRequest) => Promise<TransactionRequest>;
};
5 changes: 4 additions & 1 deletion packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ export type ProviderCallParams = UTXOValidationParams & EstimateTransactionParam
/**
* Provider Send transaction params
*/
export type ProviderSendTxParams = EstimateTransactionParams;
export type ProviderSendTxParams = EstimateTransactionParams & {
onBeforeSend?: (txRequest: TransactionRequest) => Promise<TransactionRequest>;
skipCustomFee?: boolean;
};

/**
* URL - Consensus Params mapping.
Expand Down
Loading