From 503bf523b6dab8bfebd50c95fd712db2f6f95869 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Mon, 23 Oct 2023 12:56:13 +0200 Subject: [PATCH 1/5] feat: easier discoverability of plugin capabilities Signed-off-by: Marin Petrunic --- packages/web3-core/package.json | 1 + packages/web3-core/src/web3_context.ts | 94 +++++++++++--------------- 2 files changed, 39 insertions(+), 56 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 f653b581456..fed380e6d9b 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -15,28 +15,22 @@ 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'; 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,6 +389,38 @@ export class Web3Context< } } + export abstract class Web3PluginBase< + API extends Web3APISpec = Web3APISpec, +> extends Web3Context { + 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 {...} +* ``` +*/ +export abstract class Web3EthPluginBase extends Web3PluginBase< + API & EthExecutionAPI +> {} + // To avoid cycle dependency declare this type in this file export type TransactionBuilder = < ReturnType = Transaction, @@ -404,47 +430,3 @@ export type TransactionBuilder = < 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}. - * - * To add type support for RPC methods to the {@link Web3RequestManager}, - * 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 Web3PluginBase< - API extends Web3APISpec = Web3APISpec, -> extends Web3Context { - public abstract pluginNamespace: string; -} - -/** - * 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 -> {} From 6bbc160dbe189c17d8eb65dfb112067f39e65520 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Mon, 23 Oct 2023 13:41:41 +0200 Subject: [PATCH 2/5] fix lint Signed-off-by: Marin Petrunic --- packages/web3-core/src/web3_context.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index fed380e6d9b..433889ab76c 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -27,6 +27,7 @@ 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'; From 8306f34bea8af8dbf0123d710bb7ece954887868 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Mon, 30 Oct 2023 10:32:47 +0100 Subject: [PATCH 3/5] chore: address PR comment Signed-off-by: Marin Petrunic --- packages/web3/test/integration/web3-plugin-add-tx.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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..d735c6d8a3d 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.registerTransaction(TRANSACTION_TYPE, SomeNewTxTypeTransaction); } } From 2ed6b330bef3c82a1208fe024a07956058e1c576 Mon Sep 17 00:00:00 2001 From: Marin Petrunic Date: Mon, 30 Oct 2023 12:22:50 +0100 Subject: [PATCH 4/5] address pr comments Signed-off-by: Marin Petrunic --- packages/web3/test/integration/web3-plugin-add-tx.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d735c6d8a3d..13a3e0df615 100644 --- a/packages/web3/test/integration/web3-plugin-add-tx.test.ts +++ b/packages/web3/test/integration/web3-plugin-add-tx.test.ts @@ -31,7 +31,7 @@ class Eip4844Plugin extends Web3PluginBase { public pluginNamespace = 'txType3'; public constructor() { super(); - this.registerTransaction(TRANSACTION_TYPE, SomeNewTxTypeTransaction); + this.registerNewTransactionType(TRANSACTION_TYPE, SomeNewTxTypeTransaction); } } From 297565a0e8c5acc9983e48acfa00c55f68bb6675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Petruni=C4=87?= Date: Tue, 14 Nov 2023 10:16:37 +0100 Subject: [PATCH 5/5] Update packages/web3-core/src/web3_context.ts Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> --- packages/web3-core/src/web3_context.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/web3-core/src/web3_context.ts b/packages/web3-core/src/web3_context.ts index 433889ab76c..d29833c513e 100644 --- a/packages/web3-core/src/web3_context.ts +++ b/packages/web3-core/src/web3_context.ts @@ -390,6 +390,23 @@ export class Web3Context< } } +/** + * 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}. + * + * To add type support for RPC methods to the {@link Web3RequestManager}, + * 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 Web3PluginBase< API extends Web3APISpec = Web3APISpec, > extends Web3Context {