Skip to content

Commit

Permalink
enable the jsonrpc id to optionally be incremented starting from a …
Browse files Browse the repository at this point in the history
…number

 (Inspired by: #5373 (comment) and needed as a work around for blockchainsllc/in3#46)
  • Loading branch information
Muhammad-Altabba committed Nov 23, 2022
1 parent e593bf6 commit b3cdf3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
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
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

0 comments on commit b3cdf3f

Please sign in to comment.