Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/web3-core/src/web3_batch_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ 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/>.
*/

import { JsonRpcBatchResponse, JsonRpcOptionalRequest, JsonRpcRequest } from 'web3-types';
import { JsonRpcBatchResponse, JsonRpcOptionalRequest, JsonRpcRequest, Web3APISpec } from 'web3-types';
import { jsonRpc, Web3DeferredPromise } from 'web3-utils';
import { OperationAbortError, OperationTimeoutError, ResponseError } from 'web3-errors';
import { Web3RequestManager } from './web3_request_manager';

export const DEFAULT_BATCH_REQUEST_TIMEOUT = 1000;

export class Web3BatchRequest {
private readonly _requestManager: Web3RequestManager;
export class Web3BatchRequest<API extends Web3APISpec> {
private readonly _requestManager: Web3RequestManager<API>;
private readonly _requests: Map<
number,
{ payload: JsonRpcRequest; promise: Web3DeferredPromise<unknown> }
>;

public constructor(requestManager: Web3RequestManager) {
public constructor(requestManager: Web3RequestManager<API>) {
this._requestManager = requestManager;
this._requests = new Map();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core/src/web3_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,10 +273,10 @@ export class Web3Context<
return true;
}

public get BatchRequest(): new () => Web3BatchRequest {
public get BatchRequest(): new () => Web3BatchRequest<API> {
return Web3BatchRequest.bind(
undefined,
this._requestManager as unknown as Web3RequestManager,
this._requestManager as unknown as Web3RequestManager<API>,
);
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import HttpProvider from 'web3-providers-http';
import IpcProvider from 'web3-providers-ipc';
import WSProvider from 'web3-providers-ws';
import {
EthExecutionAPI,
JsonRpcBatchRequest,
JsonRpcBatchResponse,
JsonRpcPayload,
Expand Down Expand Up @@ -63,7 +62,7 @@ const availableProviders = {
};

export class Web3RequestManager<
API extends Web3APISpec = EthExecutionAPI,
API extends Web3APISpec,
> extends Web3EventEmitter<{
[key in Web3RequestManagerEvent]: SupportedProviders<API> | undefined;
}> {
Expand Down
17 changes: 12 additions & 5 deletions packages/web3-core/src/web3_subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,22 @@ import {
Web3BaseProvider,
Web3APISpec,
Web3APIParams,
EthExecutionAPI,
Log,
JsonRpcNotification,
JsonRpcSubscriptionResult,
Web3APIMethod,
} from 'web3-types';
import { jsonRpc } from 'web3-utils';
import { Web3EventEmitter, Web3EventMap } from './web3_event_emitter';

import { Web3RequestManager } from './web3_request_manager';

export abstract class Web3Subscription<
API extends Web3APISpec,
EventMap extends Web3EventMap,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ArgsType = any,
API extends Web3APISpec = EthExecutionAPI,
Method extends Web3APIMethod<API> = any
> extends Web3EventEmitter<EventMap> {
private readonly _requestManager: Web3RequestManager<API>;
private readonly _lastBlock?: BlockOutput;
Expand Down Expand Up @@ -65,7 +66,7 @@ export abstract class Web3Subscription<

public async subscribe() {
this._id = await this._requestManager.send({
method: 'eth_subscribe',
method: this._buildSubscriptionMethod(),
params: this._buildSubscriptionParams(),
});

Expand Down Expand Up @@ -121,7 +122,13 @@ export abstract class Web3Subscription<
}

// eslint-disable-next-line class-methods-use-this
protected _buildSubscriptionParams(): Web3APIParams<API, 'eth_subscribe'> {
protected _buildSubscriptionMethod(): Method {
// This should be overridden in the subclass
throw new Error('Implement in the child class');
}

// eslint-disable-next-line class-methods-use-this
protected _buildSubscriptionParams(): Web3APIParams<API, Method> {
// This should be overridden in the subclass
throw new Error('Implement in the child class');
}
Expand All @@ -130,7 +137,7 @@ export abstract class Web3Subscription<
export type Web3SubscriptionConstructor<
API extends Web3APISpec,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
SubscriptionType extends Web3Subscription<any, any, API> = Web3Subscription<any, any, API>,
SubscriptionType extends Web3Subscription<API, any, any> = Web3Subscription<API, any, any>,
> = new (
// We accept any type of arguments here and don't deal with this type internally
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
19 changes: 15 additions & 4 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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/>.
*/

import { Web3Context, Web3EventEmitter, Web3PromiEvent } from 'web3-core';
import { Web3Context, Web3EventEmitter, Web3PromiEvent, Web3SubscriptionConstructor } from 'web3-core';
import { ContractExecutionError, SubscriptionError, Web3ContractError } from 'web3-errors';
import {
call,
Expand Down Expand Up @@ -49,12 +49,12 @@ import {
BlockNumberOrTag,
BlockTags,
Bytes,
EthExecutionAPI,
Filter,
HexString,
LogsInput,
Mutable,
Common,
Web3APISpec,
} from 'web3-types';
import {
DataFormat,
Expand Down Expand Up @@ -154,12 +154,23 @@ const contractSubscriptions = {
newHeads: NewHeadsSubscription,
newBlockHeaders: NewHeadsSubscription,
};
type ContractSubscriptions = {
logs: typeof LogsSubscription,
newHeads: typeof NewHeadsSubscription,
newBlockHeaders: typeof NewHeadsSubscription,
};

/**
* The class designed to interact with smart contracts on the Ethereum blockchain.
*/
export class Contract<Abi extends ContractAbi>
extends Web3Context<EthExecutionAPI, typeof contractSubscriptions>
export class Contract<
API extends Web3APISpec,
Abi extends ContractAbi,
RegisteredSubs extends {
[key: string]: Web3SubscriptionConstructor<API>;
} = ContractSubscriptions
>
extends Web3Context<API, RegisteredSubs>
implements Web3EventEmitter<ContractEventEmitterInterface<Abi>>
{
/**
Expand Down
9 changes: 6 additions & 3 deletions packages/web3-eth-contract/src/log_subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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/>.
*/

import { LogsInput, HexString, Topic } from 'web3-types';
import { LogsInput, HexString, Topic, Web3APISpec } from 'web3-types';
import { Web3RequestManager, Web3Subscription } from 'web3-core';
import { AbiEventFragment } from 'web3-eth-abi';
// eslint-disable-next-line import/no-cycle
Expand Down Expand Up @@ -79,7 +79,8 @@ import { ContractAbiWithSignature, EventLog } from './types';
* }
* ```
*/
export class LogsSubscription extends Web3Subscription<
export class LogsSubscription<API extends Web3APISpec> extends Web3Subscription<
API,
{
error: Error;
connected: number;
Expand Down Expand Up @@ -113,7 +114,7 @@ export class LogsSubscription extends Web3Subscription<
jsonInterface: ContractAbiWithSignature;
},
options: {
requestManager: Web3RequestManager;
requestManager: Web3RequestManager<API>;
returnFormat?: DataFormat;
},
) {
Expand All @@ -125,6 +126,8 @@ export class LogsSubscription extends Web3Subscription<
this.jsonInterface = args.jsonInterface;
}

// TODO
// @ts-ignore
protected _buildSubscriptionParams() {
return ['logs', { address: this.address, topics: this.topics }] as [
'logs',
Expand Down
18 changes: 15 additions & 3 deletions packages/web3-eth/src/web3_subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {
Topic,
BlockHeaderOutput,
LogsOutput,
EthExecutionAPI,
Web3APIMethod,
Web3APIParams,
} from 'web3-types';
import { Web3Subscription } from 'web3-core';
import { blockHeaderSchema, logSchema, syncSchema } from './schemas';
Expand All @@ -34,19 +37,25 @@ type CommonSubscriptionEvents = {
connected: number;
};

export class LogsSubscription extends Web3Subscription<
export class LogsSubscription<Method extends Web3APIMethod<EthExecutionAPI> = 'eth_subscribe'> extends Web3Subscription<
EthExecutionAPI,
CommonSubscriptionEvents & {
data: LogsOutput;
},
{
readonly fromBlock?: BlockNumberOrTag;
readonly address?: Address | Address[];
readonly topics?: Topic[];
}
},
Method
> {
protected _buildSubscriptionMethod() {
return 'eth_subscribe' as Method
}

protected _buildSubscriptionParams() {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return ['logs', this.args] as ['logs', any];
return ['logs', this.args] as Web3APIParams<EthExecutionAPI, Method>
}

public _processSubscriptionResult(data: LogsOutput) {
Expand All @@ -59,6 +68,7 @@ export class LogsSubscription extends Web3Subscription<
}

export class NewPendingTransactionsSubscription extends Web3Subscription<
EthExecutionAPI,
CommonSubscriptionEvents & {
data: HexString;
}
Expand All @@ -78,6 +88,7 @@ export class NewPendingTransactionsSubscription extends Web3Subscription<
}

export class NewHeadsSubscription extends Web3Subscription<
EthExecutionAPI,
CommonSubscriptionEvents & {
data: BlockHeaderOutput;
}
Expand All @@ -97,6 +108,7 @@ export class NewHeadsSubscription extends Web3Subscription<
}

export class SyncingSubscription extends Web3Subscription<
EthExecutionAPI,
CommonSubscriptionEvents & {
data: SyncOutput;
changed: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-plugin-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ declare module 'web3' {
}
}

export class ChainlinkPlugin extends Web3PluginBase<Web3APISpec> {
export class ChainlinkPlugin extends Web3PluginBase<ChainlinkPluginAPI> {
public pluginNamespace = 'chainlink';

protected readonly _contract: Contract<typeof AggregatorV3InterfaceABI>;
Expand Down
Loading