Skip to content

Commit

Permalink
Move DefaultProviderUrls to web3 utils (#526)
Browse files Browse the repository at this point in the history
* Move DefaultProviderUrls to web3 utils

* prettier
  • Loading branch information
Ben-Rey authored Feb 1, 2024
1 parent 80823f1 commit e87e17d
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/massa-web3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/src/interfaces/IClient.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/massa-web3/src/web3/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 1 addition & 9 deletions packages/massa-web3/src/web3/ClientFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions packages/massa-web3/test/web3/Client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 2 additions & 4 deletions packages/massa-web3/test/web3/clientFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
7 changes: 2 additions & 5 deletions packages/massa-web3/test/web3/evenPoller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions packages/massa-web3/test/web3/eventPoller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions packages/web3-utils/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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

0 comments on commit e87e17d

Please sign in to comment.