Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ tsconfig.tsbuildinfo
package-lock.json

tmp/

# Incubed (in3) nodelist
packages/web3/.in3/nodelist_1_0xac1b824795e1eb1f6e609fe0da9b9af8beaab60f
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ should use 4.0.1-alpha.0 for testing.
#### web3-utils

- Use Uuid for the response id, to fix the issue "Responses get mixed up due to conflicting payload IDs" (#5373).
- Enable passing a starting number, to increment based on it, for the Json Rpc Request `id` (#5309).

### Removed

Expand Down
4 changes: 2 additions & 2 deletions packages/web3-eth/src/utils/reject_if_block_timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ export async function rejectIfBlockTimeout(
web3Context: Web3Context<EthExecutionAPI>,
transactionHash?: Bytes,
): Promise<[Promise<never>, ResourceCleaner]> {
const provider: Web3BaseProvider = web3Context.requestManager.provider as Web3BaseProvider;
const { provider } = web3Context.requestManager;
let callingRes: [Promise<never>, ResourceCleaner];
const starterBlockNumber = await getBlockNumber(web3Context, NUMBER_DATA_FORMAT);
// TODO: once https://github.com/web3/web3.js/issues/5521 is implemented, remove checking for `enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout`
if (
provider.supportsSubscriptions() &&
(provider as Web3BaseProvider).supportsSubscriptions?.() &&
web3Context.enableExperimentalFeatures.useSubscriptionWhenCheckingBlockTimeout
) {
callingRes = await resolveBySubscription(web3Context, starterBlockNumber, transactionHash);
Expand Down
1 change: 1 addition & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Export a new function `uuidV4` that generates a random v4 Uuid (#5373).
- Enable passing a starting number, to increment based on it, for the Json Rpc Request `id` (#5309).

### Fixed

Expand Down
32 changes: 26 additions & 6 deletions packages/web3-utils/src/json_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,34 @@ export const isBatchResponse = <Result = unknown, Error = unknown>(
): response is JsonRpcBatchResponse<Result, Error> =>
Array.isArray(response) && response.length > 1 && isValidResponse(response);

// internal optional variable to increment and use for the jsonrpc `id`
let requestIdSeed: number | undefined;

/**
* Optionally use to make the jsonrpc `id` start from a specific number.
* Without calling this function, the `id` will be filled with a Uuid.
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable.
* However, if `undefined` was passed to this function, the `id` will be a Uuid again.
* @param start - a number to start incrementing from.
* Or `undefined` to use a new Uuid (this is the default behavior)
*/
export const setRequestIdStart = (start: number | undefined) => {
requestIdSeed = start;
};

export const toPayload = <ParamType = unknown[]>(
request: JsonRpcOptionalRequest<ParamType>,
): JsonRpcPayload<ParamType> => ({
jsonrpc: request.jsonrpc ?? '2.0',
id: request.id ?? uuidV4(),
method: request.method,
params: request.params ?? undefined,
});
): JsonRpcPayload<ParamType> => {
if (typeof requestIdSeed !== 'undefined') {
requestIdSeed += 1;
}
return {
jsonrpc: request.jsonrpc ?? '2.0',
id: request.id ?? requestIdSeed ?? uuidV4(),
method: request.method,
params: request.params ?? undefined,
};
};

export const toBatchPayload = (requests: JsonRpcOptionalRequest<unknown>[]): JsonRpcBatchRequest =>
requests.map(request => toPayload<unknown>(request)) as JsonRpcBatchRequest;
Expand Down
2 changes: 2 additions & 0 deletions packages/web3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"eslint-config-prettier": "^8.5.0",
"eslint-config-web3-base": "0.1.0",
"eslint-plugin-import": "^2.26.0",
"ganache": "^7.5.0",
"in3": "^3.3.3",
"jest": "^28.1.3",
"jest-extended": "^3.0.1",
"prettier": "^2.7.1",
Expand Down
84 changes: 84 additions & 0 deletions packages/web3/test/integration/external-providers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
This file is part of web3.js.

web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

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 import/no-extraneous-dependencies
import ganache from 'ganache';

import In3Client from 'in3';

import { setRequestIdStart } from 'web3-utils';

import Web3 from '../../src/index';
import { getSystemTestMnemonic } from '../shared_fixtures/system_tests_utils';

describe('compatibility with external providers', () => {
it('should accept a simple EIP1193 provider', () => {
interface RequestArguments {
readonly method: string;
readonly params?: readonly unknown[] | object;
}

class Provider {
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
public async request(_: RequestArguments): Promise<unknown> {
return undefined as unknown;
}
}

const testProvider = new Provider();
const { provider } = new Web3(testProvider);
expect(provider).toBeDefined();
});

it('should accept a `ganache` provider', async () => {
const { provider } = ganache.server({
wallet: {
mnemonic: getSystemTestMnemonic(),
},
});
const web3 = new Web3(provider);

const accounts = await web3.eth.getAccounts();

const tx = web3.eth.sendTransaction({
to: accounts[1],
from: accounts[0],
value: '1',
});

await expect(tx).resolves.not.toThrow();
});

it('should accept an `in3` provider', async () => {
// use the In3Client as Http-Provider for web3.js
const web3 = new Web3(
new In3Client({
proof: 'standard',
signatureCount: 1,
requestCount: 1,
chainId: 'mainnet',
}).createWeb3Provider(),
);

// TODO: remove the next line after this issue is closed: https://github.com/blockchainsllc/in3/issues/46
setRequestIdStart(0);

// get the last block number
const block = await web3.eth.getBlockNumber();
expect(typeof block).toBe('bigint');
});
});
2 changes: 1 addition & 1 deletion packages/web3/test/integration/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
// eslint-disable-next-line @typescript-eslint/no-require-imports
require('../config/setup');

const jestTimeout = 15000;
const jestTimeout = 30000;

jest.setTimeout(jestTimeout);
Loading