From 20cc5daa6d92007dfdca62fa2ec414036a740153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Thu, 23 Nov 2023 17:58:09 +0100 Subject: [PATCH] Proposal: Expose plugin functionality to plugins so it's easier to discover what plugins could do (#6538) * feat: easier discoverability of plugin capabilities Signed-off-by: Marin Petrunic * fix lint Signed-off-by: Marin Petrunic * chore: address PR comment Signed-off-by: Marin Petrunic * address pr comments Signed-off-by: Marin Petrunic * 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 Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> --- packages/web3-core/package.json | 1 + packages/web3-core/src/web3_context.ts | 84 +++++++++---------- .../integration/web3-plugin-add-tx.test.ts | 4 +- 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/packages/web3-core/package.json b/packages/web3-core/package.json index 8db0707032b..72ba5ee3cd8 100644 --- a/packages/web3-core/package.json +++ b/packages/web3-core/package.json @@ -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", diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 7861c6a816a..f4c72f9ab61 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -15,28 +15,23 @@ You should have received a copy of the GNU Lesser General Public License along with web3.js. If not, see . */ // 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< @@ -395,16 +390,6 @@ export class Web3Context< } } -// To avoid cycle dependency declare this type in this file -export type TransactionBuilder = < - ReturnType = Transaction, ->(options: { - transaction: Transaction; - web3Context: Web3Context; - privateKey?: HexString | Uint8Array; - fillGasPrice?: boolean; -}) => Promise; - /** * 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}. @@ -422,29 +407,44 @@ export type TransactionBuilder = < * class CustomPlugin extends Web3PluginBase {...} * ``` */ -export abstract class Web3PluginBase< - API extends Web3APISpec = Web3APISpec, + export abstract class Web3PluginBase< + API extends Web3APISpec = Web3APISpec, > extends Web3Context { - public abstract pluginNamespace: string; + public abstract pluginNamespace: string; + + // eslint-disable-next-line class-methods-use-this + protected registerNewTransactionType>(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 {...} - * ``` - */ +* 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 {...} +* ``` +*/ export abstract class Web3EthPluginBase extends Web3PluginBase< - API & EthExecutionAPI + API & EthExecutionAPI > {} + +// To avoid cycle dependency declare this type in this file +export type TransactionBuilder = < + ReturnType = Transaction, +>(options: { + transaction: Transaction; + web3Context: Web3Context; + privateKey?: HexString | Uint8Array; + fillGasPrice?: boolean; +}) => Promise; diff --git a/packages/web3/test/integration/web3-plugin-add-tx.test.ts b/packages/web3/test/integration/web3-plugin-add-tx.test.ts index d1971b7f4ef..13a3e0df615 100644 --- a/packages/web3/test/integration/web3-plugin-add-tx.test.ts +++ b/packages/web3/test/integration/web3-plugin-add-tx.test.ts @@ -17,7 +17,7 @@ along with web3.js. If not, see . /* 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, @@ -31,7 +31,7 @@ class Eip4844Plugin extends Web3PluginBase { public pluginNamespace = 'txType3'; public constructor() { super(); - TransactionFactory.registerTransactionType(TRANSACTION_TYPE, SomeNewTxTypeTransaction); + this.registerNewTransactionType(TRANSACTION_TYPE, SomeNewTxTypeTransaction); } }