From e87e17d2417fad834c11b64ef18e270927794b21 Mon Sep 17 00:00:00 2001 From: BenRey <44082144+Ben-Rey@users.noreply.github.com> Date: Thu, 1 Feb 2024 15:43:39 +0100 Subject: [PATCH] Move DefaultProviderUrls to web3 utils (#526) * Move DefaultProviderUrls to web3 utils * prettier --- packages/massa-web3/src/index.ts | 2 +- packages/massa-web3/src/interfaces/IClient.ts | 2 +- packages/massa-web3/src/web3/Client.ts | 2 +- packages/massa-web3/src/web3/ClientFactory.ts | 10 +--------- packages/massa-web3/test/web3/Client.spec.ts | 7 ++++--- .../massa-web3/test/web3/clientFactory.spec.ts | 6 ++---- .../massa-web3/test/web3/evenPoller.spec.ts | 7 ++----- .../massa-web3/test/web3/eventPoller.spec.ts | 7 ++----- packages/web3-utils/src/constants.ts | 18 ++++++++++++++++-- 9 files changed, 30 insertions(+), 31 deletions(-) diff --git a/packages/massa-web3/src/index.ts b/packages/massa-web3/src/index.ts index fcc0797b..9ded25ce 100644 --- a/packages/massa-web3/src/index.ts +++ b/packages/massa-web3/src/index.ts @@ -68,7 +68,7 @@ export { ISubscribedFullBlocksMessage } from './interfaces/ISubscribedFullBlocks export { IBaseAccount } from './interfaces/IBaseAccount'; /** Exposed clients and factories */ -export { ClientFactory, DefaultProviderUrls } from './web3/ClientFactory'; +export { ClientFactory } from './web3/ClientFactory'; export { Client } from './web3/Client'; export { PublicApiClient } from './web3/PublicApiClient'; export { PrivateApiClient } from './web3/PrivateApiClient'; diff --git a/packages/massa-web3/src/interfaces/IClient.ts b/packages/massa-web3/src/interfaces/IClient.ts index 3a55fac8..d518a6f4 100644 --- a/packages/massa-web3/src/interfaces/IClient.ts +++ b/packages/massa-web3/src/interfaces/IClient.ts @@ -1,4 +1,4 @@ -import { DefaultProviderUrls } from '../web3/ClientFactory'; +import { DefaultProviderUrls } from '@massalabs/web3-utils'; import { IPrivateApiClient } from './IPrivateApiClient'; import { IProvider } from './IProvider'; import { IPublicApiClient } from './IPublicApiClient'; diff --git a/packages/massa-web3/src/web3/Client.ts b/packages/massa-web3/src/web3/Client.ts index a6ef398c..5fa796ea 100755 --- a/packages/massa-web3/src/web3/Client.ts +++ b/packages/massa-web3/src/web3/Client.ts @@ -4,9 +4,9 @@ import { PublicApiClient } from './PublicApiClient'; import { WalletClient } from './WalletClient'; import { SmartContractsClient } from './SmartContractsClient'; import { IProvider, ProviderType } from '../interfaces/IProvider'; -import { DefaultProviderUrls } from './ClientFactory'; import { IClient } from '../interfaces/IClient'; import { IBaseAccount } from '../interfaces/IBaseAccount'; +import { DefaultProviderUrls } from '@massalabs/web3-utils'; /** * Massa Web3 Client object wraps all public, private, wallet and smart-contracts-related functionalities. diff --git a/packages/massa-web3/src/web3/ClientFactory.ts b/packages/massa-web3/src/web3/ClientFactory.ts index 63f1fa13..49a07b73 100644 --- a/packages/massa-web3/src/web3/ClientFactory.ts +++ b/packages/massa-web3/src/web3/ClientFactory.ts @@ -9,15 +9,7 @@ import { import { Web3Account } from './accounts/Web3Account'; import { PublicApiClient } from './PublicApiClient'; import { WalletProviderAccount } from './accounts/WalletProviderAccount'; - -/** Global connection urls, for Massa's MAINNET, TESTNET, LABNET, LOCALNET and BUILDNET */ -export enum DefaultProviderUrls { - MAINNET = 'https://mainnet.massa.net/api/v2', - TESTNET = 'https://test.massa.net/api/v2', - BUILDNET = 'https://buildnet.massa.net/api/v2', - LABNET = 'https://labnet.massa.net/api/v2', - LOCALNET = 'http://127.0.0.1', -} +import { DefaultProviderUrls } from '@massalabs/web3-utils'; /** * Massa Web3 ClientFactory class allows you to easily initialize a client to diff --git a/packages/massa-web3/test/web3/Client.spec.ts b/packages/massa-web3/test/web3/Client.spec.ts index 577ca24a..5693647a 100644 --- a/packages/massa-web3/test/web3/Client.spec.ts +++ b/packages/massa-web3/test/web3/Client.spec.ts @@ -2,7 +2,7 @@ import { Client } from '../../src/web3/Client'; import { IClientConfig } from '../../src/interfaces/IClientConfig'; import { IProvider } from '../../src/interfaces/IProvider'; import { ProviderType } from '../../src/interfaces/IProvider'; -import { DefaultProviderUrls } from '../../src/web3/ClientFactory'; +import { DefaultProviderUrls } from '@massalabs/web3-utils'; describe('Client Class', () => { let clientConfig: IClientConfig; @@ -53,8 +53,9 @@ describe('Client Class', () => { }); test('should set new default provider', () => { - const newDefaultProvider = 'https://new-default-provider.com'; - client.setNewDefaultProvider(newDefaultProvider as DefaultProviderUrls); + const newDefaultProvider = + 'https://new-default-provider.com' as DefaultProviderUrls; + client.setNewDefaultProvider(newDefaultProvider); const currentProviders = client.getProviders(); expect(currentProviders).toHaveLength(2); diff --git a/packages/massa-web3/test/web3/clientFactory.spec.ts b/packages/massa-web3/test/web3/clientFactory.spec.ts index c53a71e9..711c1cfb 100644 --- a/packages/massa-web3/test/web3/clientFactory.spec.ts +++ b/packages/massa-web3/test/web3/clientFactory.spec.ts @@ -1,12 +1,10 @@ -import { - ClientFactory, - DefaultProviderUrls, -} from '../../src/web3/ClientFactory'; +import { ClientFactory } from '../../src/web3/ClientFactory'; import { WalletClient } from '../../src/web3/WalletClient'; import { ProviderType } from '../../src/interfaces/IProvider'; import { Client } from '../../src/web3/Client'; import { IAccount } from '../../src/interfaces/IAccount'; import { BUILDNET_CHAIN_ID } from './mockData'; +import { DefaultProviderUrls } from '@massalabs/web3-utils'; const publicApi = 'https://mock-public-api.com'; const privateApi = 'https://mock-private-api.com'; diff --git a/packages/massa-web3/test/web3/evenPoller.spec.ts b/packages/massa-web3/test/web3/evenPoller.spec.ts index b877d32d..0c7fefbb 100644 --- a/packages/massa-web3/test/web3/evenPoller.spec.ts +++ b/packages/massa-web3/test/web3/evenPoller.spec.ts @@ -7,13 +7,10 @@ import { IEventFilter } from '../../src/interfaces/IEventFilter'; import { IEventRegexFilter } from '../../src/interfaces/IEventRegexFilter'; import { Client } from '../../src/web3/Client'; import { WalletClient } from '../../src/web3/WalletClient'; -import { - ClientFactory, - DefaultProviderUrls, -} from '../../src/web3/ClientFactory'; +import { ClientFactory } from '../../src/web3/ClientFactory'; import { IAccount } from '../../src/interfaces/IAccount'; import { Timeout } from '../../src/utils/time'; -import { IEvent, ISlot } from '@massalabs/web3-utils'; +import { IEvent, ISlot, DefaultProviderUrls } from '@massalabs/web3-utils'; import { BUILDNET_CHAIN_ID } from './mockData'; // mock axios to intercept any axios POST request and resolve it immediately with an empty object, so diff --git a/packages/massa-web3/test/web3/eventPoller.spec.ts b/packages/massa-web3/test/web3/eventPoller.spec.ts index dde0be99..9536b95e 100644 --- a/packages/massa-web3/test/web3/eventPoller.spec.ts +++ b/packages/massa-web3/test/web3/eventPoller.spec.ts @@ -7,13 +7,10 @@ import { IEventFilter } from '../../src/interfaces/IEventFilter'; import { IEventRegexFilter } from '../../src/interfaces/IEventRegexFilter'; import { Client } from '../../src/web3/Client'; import { WalletClient } from '../../src/web3/WalletClient'; -import { - ClientFactory, - DefaultProviderUrls, -} from '../../src/web3/ClientFactory'; +import { ClientFactory } from '../../src/web3/ClientFactory'; import { IAccount } from '../../src/interfaces/IAccount'; import { Timeout } from '../../src/utils/time'; -import { IEvent, ISlot } from '@massalabs/web3-utils'; +import { IEvent, ISlot, DefaultProviderUrls } from '@massalabs/web3-utils'; import { BUILDNET_CHAIN_ID } from './mockData'; // Mock the Timeout class diff --git a/packages/web3-utils/src/constants.ts b/packages/web3-utils/src/constants.ts index ed18cac4..03c7fd31 100644 --- a/packages/web3-utils/src/constants.ts +++ b/packages/web3-utils/src/constants.ts @@ -46,7 +46,7 @@ export const CHAIN_ID_TO_NETWORK_NAME = { [SECURENET_CHAIN_ID.toString()]: SECURENET, [LABNET_CHAIN_ID.toString()]: LABNET, [SANDBOX_CHAIN_ID.toString()]: SANDBOX, -} as const; +} as const; // type is inferred as the specific, unchangeable structure // Define ChainId type as the keys of CHAIN_ID_TO_NETWORK_NAME export type ChainId = keyof typeof CHAIN_ID_TO_NETWORK_NAME; @@ -58,7 +58,21 @@ export const CHAIN_ID = { [SECURENET]: SECURENET_CHAIN_ID, [LABNET]: LABNET_CHAIN_ID, [SANDBOX]: SANDBOX_CHAIN_ID, -} as const; +} as const; // type is inferred as the specific, unchangeable structure // Define NetworkName type as the keys of NETWORK_NAME_TO_CHAIN_ID export type NetworkName = keyof typeof CHAIN_ID; + +export enum DefaultProviderUrls { + MAINNET = 'https://mainnet.massa.net/api/v2', + TESTNET = 'https://test.massa.net/api/v2', + BUILDNET = 'https://buildnet.massa.net/api/v2', + LABNET = 'https://labnet.massa.net/api/v2', + LOCALNET = 'http://127.0.0.1', +} + +export const CHAIN_ID_RPC_URL_MAP = { + [MAINNET_CHAIN_ID.toString()]: DefaultProviderUrls.MAINNET, + [BUILDNET_CHAIN_ID.toString()]: DefaultProviderUrls.BUILDNET, + [SANDBOX_CHAIN_ID.toString()]: DefaultProviderUrls.LOCALNET, +} as const; // type is inferred as the specific, unchangeable structure