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

Ankr provider #7260

Closed
wants to merge 4 commits into from
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
1 change: 1 addition & 0 deletions packages/web3-rpc-providers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './types.js';
export * from './web3_provider_quicknode.js';
export * from './web3_provider.js';
export * from './errors.js';
export * from './web3_provider_ankr.js';

// default providers
export const mainnet = new QuickNodeProvider();
2 changes: 1 addition & 1 deletion packages/web3-rpc-providers/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export enum Network {

POLYGON_MAINNET = 'polygon_mainnet',
POLYGON_MUMBAI = 'polygon_mumbai',
POLYGON_AMONY = 'polygon_amony',
POLYGON_AMOY = 'polygon_amoy',

ARBITRUM_MAINNET = 'arbitrum_mainnet',
ARBITRUM_SEPOLIA = 'arbitrum_sepolia',
Expand Down
58 changes: 58 additions & 0 deletions packages/web3-rpc-providers/src/web3_provider_ankr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
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 { HttpProviderOptions } from 'web3-providers-http';
import { Transport, Network, SocketOptions } from './types.js';
import { Web3ExternalProvider } from './web3_provider.js';

export class AnkrProvider extends Web3ExternalProvider {
// eslint-disable-next-line default-param-last
public constructor(
network: Network = Network.ETH_MAINNET,
transport: Transport = Transport.HTTPS,
key = 'ADD_KEY_HERE',
host = 'rpc.ankr.com',
providerConfigOptions?: HttpProviderOptions | SocketOptions,
) {
super(network, transport, key, host, providerConfigOptions);

if (transport === Transport.WebSocket)
throw new Error('Websocket is not avalible for Ankr free account.');
}

public static readonly networkStringMap: { [key: string]: string } = {
[Network.ETH_MAINNET]: 'eth',
[Network.ETH_SEPOLIA]: Network.ETH_SEPOLIA,
[Network.ETH_HOLESKY]: Network.ETH_HOLESKY,
[Network.ARBITRUM_MAINNET]: 'arbitrum',
[Network.ARBITRUM_SEPOLIA]: Network.ARBITRUM_SEPOLIA,
[Network.BNB_MAINNET]: 'bsc',
[Network.BNB_TESTNET]: 'bsc_testnet_chapel',
[Network.POLYGON_MAINNET]: 'polygon',
[Network.POLYGON_AMOY]: Network.POLYGON_AMOY,
};

// eslint-disable-next-line class-methods-use-this
public getRPCURL(network: Network, transport: Transport, key: string, host: string) {
const networkString = AnkrProvider.networkStringMap[network] || '';
if (!networkString) {
throw new Error('Network info not available.');
}

return `${transport}://${host}/${networkString}/${key}`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class QuickNodeProvider<
host = isValid(_host) ? _host : 'small-chaotic-moon.matic.quiknode.pro';
token = isValid(_token) ? _token : '847569f8a017e84d985e10d0f44365d965a951f1';
break;
case Network.POLYGON_AMONY:
case Network.POLYGON_AMOY:
host = isValid(_host) ? _host : 'prettiest-side-shape.matic-amoy.quiknode.pro';
token = isValid(_token) ? _token : '79a9476eea661d4f82de614db1d8a895b14b881c';
break;
Expand Down
16 changes: 14 additions & 2 deletions packages/web3/test/integration/web3RPCProviders.test.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 { mainnet, Network, QuickNodeProvider, Transport } from 'web3-rpc-providers';
import { mainnet, Network, QuickNodeProvider, Transport, AnkrProvider } from 'web3-rpc-providers';
import { Web3 } from '../../src/index';

describe('Web3 RPC Provider Integration tests', () => {
Expand All @@ -29,7 +29,7 @@ describe('Web3 RPC Provider Integration tests', () => {
Network.BNB_MAINNET,
Network.BNB_TESTNET,
Network.POLYGON_MAINNET,
Network.POLYGON_AMONY,
Network.POLYGON_AMOY,
];

transports.forEach(transport => {
Expand All @@ -46,8 +46,20 @@ describe('Web3 RPC Provider Integration tests', () => {
web3.provider?.disconnect();
}
});

if (transport !== Transport.WebSocket) {
it.skip(`AnkrProvider should work with ${transport} transport and ${network} network`, async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skipped as key not added yet

const provider = new AnkrProvider(network, transport);
const web3 = new Web3(provider);
const result = await web3.eth.getBlockNumber();

expect(typeof result).toBe('bigint');
expect(result > 0).toBe(true);
});
}
});
});

it(`should work with mainnet provider`, async () => {
const web3 = new Web3(mainnet);
const result = await web3.eth.getBlockNumber();
Expand Down
Loading