Skip to content

Commit

Permalink
Proposal: Expose plugin functionality to plugins so it's easier to di…
Browse files Browse the repository at this point in the history
…scover what plugins could do (#6538)

* feat: easier discoverability of plugin capabilities

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>

* fix lint

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>

* chore: address PR comment

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>

* address pr comments

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>

* Update packages/web3-core/src/web3_context.ts

Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>

---------

Signed-off-by: Marin Petrunic <marin.petrunic@gmail.com>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
  • Loading branch information
mpetrunic and Muhammad-Altabba authored Nov 23, 2023
1 parent f8d8774 commit 20cc5da
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
1 change: 1 addition & 0 deletions packages/web3-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"web3-errors": "^1.1.4",
"web3-eth-iban": "^4.0.7",
"web3-eth-accounts": "^4.1.0",
"web3-providers-http": "^4.1.0",
"web3-providers-ws": "^4.0.7",
"web3-types": "^1.3.1",
Expand Down
84 changes: 42 additions & 42 deletions packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
// eslint-disable-next-line max-classes-per-file
import { ExistingPluginNamespaceError } from 'web3-errors';
import {
Web3APISpec,
Web3BaseWallet,
Web3BaseWalletAccount,
Web3AccountProvider,
SupportedProviders,
HexString,
EthExecutionAPI,
Web3BaseProvider,
Transaction,
HexString, Numbers, SupportedProviders, Transaction, Web3AccountProvider, Web3APISpec, Web3BaseProvider, Web3BaseWallet,
Web3BaseWalletAccount
} from 'web3-types';
import { isNullish } from 'web3-utils';
import { ExistingPluginNamespaceError } from 'web3-errors';

import { BaseTransaction, TransactionFactory } from 'web3-eth-accounts';
import { isSupportedProvider } from './utils.js';
// eslint-disable-next-line import/no-cycle
import { ExtensionObject } from './types.js';
import { Web3BatchRequest } from './web3_batch_request.js';
// eslint-disable-next-line import/no-cycle
import { Web3Config, Web3ConfigEvent, Web3ConfigOptions } from './web3_config.js';
import { Web3RequestManager } from './web3_request_manager.js';
import { Web3SubscriptionConstructor } from './web3_subscriptions.js';
import { Web3SubscriptionManager } from './web3_subscription_manager.js';
import { Web3BatchRequest } from './web3_batch_request.js';
import { ExtensionObject } from './types.js';

// To avoid circular dependencies, we need to export type from here.
export type Web3ContextObject<
Expand Down Expand Up @@ -395,16 +390,6 @@ export class Web3Context<
}
}

// To avoid cycle dependency declare this type in this file
export type TransactionBuilder<API extends Web3APISpec = unknown> = <
ReturnType = Transaction,
>(options: {
transaction: Transaction;
web3Context: Web3Context<API>;
privateKey?: HexString | Uint8Array;
fillGasPrice?: boolean;
}) => Promise<ReturnType>;

/**
* Extend this class when creating a plugin that either doesn't require {@link EthExecutionAPI},
* or interacts with a RPC node that doesn't fully implement {@link EthExecutionAPI}.
Expand All @@ -422,29 +407,44 @@ export type TransactionBuilder<API extends Web3APISpec = unknown> = <
* class CustomPlugin extends Web3PluginBase<CustomRpcApi> {...}
* ```
*/
export abstract class Web3PluginBase<
API extends Web3APISpec = Web3APISpec,
export abstract class Web3PluginBase<
API extends Web3APISpec = Web3APISpec,
> extends Web3Context<API> {
public abstract pluginNamespace: string;
public abstract pluginNamespace: string;

// eslint-disable-next-line class-methods-use-this
protected registerNewTransactionType<NewTxTypeClass extends typeof BaseTransaction<unknown>>(type: Numbers, txClass: NewTxTypeClass): void {
TransactionFactory.registerTransactionType(type, txClass);
}
}

/**
* Extend this class when creating a plugin that makes use of {@link EthExecutionAPI},
* or depends on other Web3 packages (such as `web3-eth-contract`) that depend on {@link EthExecutionAPI}.
*
* To add type support for RPC methods to the {@link Web3RequestManager} (in addition to {@link EthExecutionAPI}),
* define a {@link Web3APISpec} and pass it as a generic to Web3PluginBase like so:
*
* @example
* ```ts
* type CustomRpcApi = {
* custom_rpc_method: () => string;
* custom_rpc_method_with_parameters: (parameter1: string, parameter2: number) => string;
* };
*
* class CustomPlugin extends Web3PluginBase<CustomRpcApi> {...}
* ```
*/
* Extend this class when creating a plugin that makes use of {@link EthExecutionAPI},
* or depends on other Web3 packages (such as `web3-eth-contract`) that depend on {@link EthExecutionAPI}.
*
* To add type support for RPC methods to the {@link Web3RequestManager} (in addition to {@link EthExecutionAPI}),
* define a {@link Web3APISpec} and pass it as a generic to Web3PluginBase like so:
*
* @example
* ```ts
* type CustomRpcApi = {
* custom_rpc_method: () => string;
* custom_rpc_method_with_parameters: (parameter1: string, parameter2: number) => string;
* };
*
* class CustomPlugin extends Web3PluginBase<CustomRpcApi> {...}
* ```
*/
export abstract class Web3EthPluginBase<API extends Web3APISpec = unknown> extends Web3PluginBase<
API & EthExecutionAPI
API & EthExecutionAPI
> {}

// To avoid cycle dependency declare this type in this file
export type TransactionBuilder<API extends Web3APISpec = unknown> = <
ReturnType = Transaction,
>(options: {
transaction: Transaction;
web3Context: Web3Context<API>;
privateKey?: HexString | Uint8Array;
fillGasPrice?: boolean;
}) => Promise<ReturnType>;
4 changes: 2 additions & 2 deletions packages/web3/test/integration/web3-plugin-add-tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

/* eslint-disable @typescript-eslint/no-magic-numbers */

import { Transaction, TransactionFactory, Web3Account } from 'web3-eth-accounts';
import { Transaction, Web3Account } from 'web3-eth-accounts';
import { SupportedProviders, Web3, Web3PluginBase } from '../../src';
import {
createAccount,
Expand All @@ -31,7 +31,7 @@ class Eip4844Plugin extends Web3PluginBase {
public pluginNamespace = 'txType3';
public constructor() {
super();
TransactionFactory.registerTransactionType(TRANSACTION_TYPE, SomeNewTxTypeTransaction);
this.registerNewTransactionType(TRANSACTION_TYPE, SomeNewTxTypeTransaction);
}
}

Expand Down

0 comments on commit 20cc5da

Please sign in to comment.