Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update http quicknode rpc-provider #7102

Merged
merged 13 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
3 changes: 3 additions & 0 deletions packages/web3-errors/src/error_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ export const ERR_CORE_CHAIN_MISMATCH = 1102;
// Schema error codes
export const ERR_SCHEMA_FORMAT = 1200;

export const ERR_QUICK_NODE_RATE_LIMIT = 1300;
luu-alex marked this conversation as resolved.
Show resolved Hide resolved


// RPC error codes (EIP-1474)
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
export const ERR_RPC_INVALID_JSON = -32700;
Expand Down
27 changes: 27 additions & 0 deletions packages/web3-errors/src/errors/quicknode_errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
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/>.
*/

import { BaseWeb3Error } from '../web3_error_base.js';
import { ERR_QUICK_NODE_RATE_LIMIT } from '../error_codes.js';

export class QuickNodeRateLimitError extends BaseWeb3Error {
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
public code = ERR_QUICK_NODE_RATE_LIMIT;

Check warning on line 22 in packages/web3-errors/src/errors/quicknode_errors.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-errors/src/errors/quicknode_errors.ts#L22

Added line #L22 was not covered by tests

public constructor() {
super(`Too many requests, Quicknode has reached its rate limit.`);

Check warning on line 25 in packages/web3-errors/src/errors/quicknode_errors.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-errors/src/errors/quicknode_errors.ts#L24-L25

Added lines #L24 - L25 were not covered by tests
}
}
1 change: 1 addition & 0 deletions packages/web3-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
export * from './errors/contract_errors.js';
export * from './errors/ens_errors.js';
export * from './errors/generic_errors.js';
export * from './errors/quicknode_errors.js';

Check warning on line 25 in packages/web3-errors/src/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-errors/src/index.ts#L25

Added line #L25 was not covered by tests
export * from './errors/provider_errors.js';
export * from './errors/signature_errors.js';
export * from './errors/transaction_errors.js';
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-errors/test/unit/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as transactionErrors from '../../src/errors/transaction_errors';
import * as utilsErrors from '../../src/errors/utils_errors';
import * as responseErrors from '../../src/errors/response_errors';
import * as schemaErrors from '../../src/errors/schema_errors';
import * as quickNodeErrors from '../../src/errors/quicknode_errors';

import { ConvertValueToString } from '../fixtures/errors';
import { BaseWeb3Error } from '../../src/web3_error_base';
Expand All @@ -52,6 +53,7 @@ describe('errors', () => {
...transactionErrors,
...utilsErrors,
...schemaErrors,
...quickNodeErrors
})) {
if (ErrorClass === transactionErrors.InvalidPropertiesForTransactionTypeError) break;
// To disable error for the abstract class
Expand Down
7 changes: 4 additions & 3 deletions packages/web3-providers-http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ export default class HttpProvider<
},
body: JSON.stringify(payload),
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
if (!response.ok) throw new ResponseError(await response.json());
if (!response.ok) {
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new ResponseError(await response.json())
};

return (await response.json()) as JsonRpcResponseWithResult<ResultType>;
}
Expand Down
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- RC release

## [Unreleased]
- When error is returned with code 429, throw rate limit error (#7102)
2 changes: 2 additions & 0 deletions packages/web3-rpc-providers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
"typescript": "^4.7.4"
},
"dependencies": {
"web3-errors": "^1.2.0",
"web3-providers-http": "^4.1.0",
"web3-providers-ws": "^4.0.7",
"web3-validator": "^2.0.6",
"web3-types": "^1.7.0",
"web3-utils": "^4.3.0"
}
Expand Down
13 changes: 12 additions & 1 deletion packages/web3-rpc-providers/src/web3_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.

import HttpProvider from "web3-providers-http";
import WebSocketProvider from "web3-providers-ws";
import { isNullish } from "web3-validator";
import {
EthExecutionAPI, JsonRpcResult, ProviderConnectInfo, ProviderMessage,
ProviderRpcError, Web3APIMethod, Web3APIPayload, Web3APIReturnType, Web3APISpec, Web3BaseProvider,
Expand All @@ -25,6 +26,7 @@ import {
Web3ProviderMessageEventCallback,
Web3ProviderStatus
} from "web3-types";
import { QuickNodeRateLimitError } from "web3-errors";
import { Eip1193Provider } from "web3-utils";
import { Transport, Network } from "./types.js";

Expand Down Expand Up @@ -71,9 +73,18 @@ API extends Web3APISpec = EthExecutionAPI,
): Promise<ResultType> {

if (this.transport === Transport.HTTPS) {
return ( (this.provider as HttpProvider).request(payload, requestOptions)) as unknown as Promise<ResultType>;
try {
return ( (this.provider as HttpProvider).request(payload, requestOptions)) as unknown as Promise<ResultType>;
} catch(e: unknown) {
if (typeof e === 'object' && !isNullish(e) && 'code' in e && (e as { code: number }).code === 429){
luu-alex marked this conversation as resolved.
Show resolved Hide resolved
// rate limiting error by quicknode;
throw new QuickNodeRateLimitError();

}
}
}


return ( (this.provider as WebSocketProvider).request(payload)) as unknown as Promise<ResultType>;

}
Expand Down
Loading