From 00321e49b5a8f77319803fd4003d628de8a64e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Sant=C3=A1ngelo?= Date: Tue, 16 Feb 2021 11:56:43 -0300 Subject: [PATCH] feat: update ChainId --- README.md | 35 +++++++++++++++++-------------- src/ConnectionManager.ts | 4 ++-- src/configuration.ts | 16 +++++++------- src/types.ts | 11 ++++++---- test/ConnectionManager.spec.ts | 38 ++++++++++++++++++++-------------- test/configuration.spec.ts | 16 +++++++------- test/connectors.spec.ts | 6 +++--- test/utils.ts | 2 +- 8 files changed, 70 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index dd60f3d..1205575 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Connects to the supplied provider type and chain. It'll default to `mainnet` if ```typescript async connect( providerType: ProviderType, - chainId: ChainId = ChainId.MAINNET + chainId: ChainId = ChainId.ETHEREUM_MAINNET ): Promise ``` @@ -44,7 +44,7 @@ async connect( ```typescript const connection = new ConnectionManager(new Storage()) -await connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN) +await connection.connect(ProviderType.INJECTED, ChainId.ETHEREUM_ROPSTEN) ``` ### .tryPreviousConnection() @@ -62,9 +62,9 @@ async tryPreviousConnection(): Promise ```typescript // Calls connect first const connection = new ConnectionManager(new Storage()) -await connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN) +await connection.connect(ProviderType.INJECTED, ChainId.ETHEREUM_ROPSTEN) -await connection.tryPreviousConnection() // Connects with ProviderType.INJECTED ChainId.ROPSTEN +await connection.tryPreviousConnection() // Connects with ProviderType.INJECTED ChainId.ETHEREUM.ROPSTEN ``` ### .disconnect() @@ -81,7 +81,7 @@ async disconnect() ```typescript const connection = new ConnectionManager(new Storage()) -connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN) +connection.connect(ProviderType.INJECTED, ChainId.ETHEREUM_ROPSTEN) // (...) @@ -102,7 +102,7 @@ getConnectionData(): ConnectionData | undefined ```typescript const connection = new ConnectionManager(new Storage()) -connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN) +connection.connect(ProviderType.INJECTED, ChainId.ETHEREUM_ROPSTEN) // (...) @@ -152,7 +152,7 @@ It creates a new provider using the supplied arguments. Similar to calling `.con ```typescript async createProvider( providerType: ProviderType, - chainId: ChainId = ChainId.MAINNET + chainId: ChainId = ChainId.ETHEREUM_MAINNET ): Promise { ``` @@ -161,7 +161,7 @@ async createProvider( ```typescript const provider = await connection.createProvider( Provider.FORTMATIC, - ChainId.ROPSTEN + ChainId.ETHEREUM_ROPSTEN ) ``` @@ -194,7 +194,7 @@ An implementation of the Storage engine which uses `window.localStorage` to stor Represents the different types of connectors to the Ethereum Network ```typescript -export enum ProviderType { +enum ProviderType { INJECTED = 'injected', FORTMATIC = 'formatic', WALLET_CONNECT = 'wallet_connect' @@ -206,18 +206,21 @@ export enum ProviderType { Different Ethereum chains ```typescript -export enum ChainId { - MAINNET = 1, - ROPSTEN = 3, - RINKEBY = 4, - KOVAN = 42 +enum ChainId { + ETHEREUM_MAINNET = 1, + ETHEREUM_ROPSTEN = 3, + ETHEREUM_RINKEBY = 4, + ETHEREUM_GOERLI = 5, + ETHEREUM_KOVAN = 42, + MATIC_MUMBAI = 13881, + MATIC_MAINNET = 89 } ``` ### ConnectionResponse ```typescript -export type ConnectionResponse = { +type ConnectionResponse = { provider: Provider chainId: ChainId account: null | string @@ -227,7 +230,7 @@ export type ConnectionResponse = { ### ConnectionData ```typescript -export type ConnectionData = { +type ConnectionData = { providerType: ProviderType chainId: ChainId } diff --git a/src/ConnectionManager.ts b/src/ConnectionManager.ts index 2a7ac44..8edcdbf 100644 --- a/src/ConnectionManager.ts +++ b/src/ConnectionManager.ts @@ -26,7 +26,7 @@ export class ConnectionManager { async connect( providerType: ProviderType, - chainId: ChainId = ChainId.MAINNET + chainId: ChainId = ChainId.ETHEREUM_MAINNET ): Promise { this.setConnectionData(providerType, chainId) this.connector = this.buildConnector(providerType, chainId) @@ -84,7 +84,7 @@ export class ConnectionManager { async createProvider( providerType: ProviderType, - chainId: ChainId = ChainId.MAINNET + chainId: ChainId = ChainId.ETHEREUM_MAINNET ): Promise { const connector = this.buildConnector(providerType, chainId) const provider = await connector.getProvider() diff --git a/src/configuration.ts b/src/configuration.ts index b92bf5f..160fc46 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -7,19 +7,19 @@ const configuration = Object.freeze({ [ProviderType.FORTMATIC]: { apiKeys: { - [ChainId.MAINNET]: 'pk_live_D7297F51E9776DD2', - [ChainId.ROPSTEN]: 'pk_test_198DDD3CA646DE2F', - [ChainId.RINKEBY]: 'pk_test_198DDD3CA646DE2F', - [ChainId.KOVAN]: 'pk_test_198DDD3CA646DE2F' + [ChainId.ETHEREUM_MAINNET]: 'pk_live_D7297F51E9776DD2', + [ChainId.ETHEREUM_ROPSTEN]: 'pk_test_198DDD3CA646DE2F', + [ChainId.ETHEREUM_RINKEBY]: 'pk_test_198DDD3CA646DE2F', + [ChainId.ETHEREUM_KOVAN]: 'pk_test_198DDD3CA646DE2F' } }, [ProviderType.WALLET_CONNECT]: { urls: { - [ChainId.MAINNET]: 'https://mainnet.mycustomnode.com', - [ChainId.ROPSTEN]: 'https://ropsten.mycustomnode.com', - [ChainId.RINKEBY]: 'https://ropsten.mycustomnode.com', - [ChainId.KOVAN]: 'https://ropsten.mycustomnode.com' + [ChainId.ETHEREUM_MAINNET]: 'https://mainnet.mycustomnode.com', + [ChainId.ETHEREUM_ROPSTEN]: 'https://ropsten.mycustomnode.com', + [ChainId.ETHEREUM_RINKEBY]: 'https://ropsten.mycustomnode.com', + [ChainId.ETHEREUM_KOVAN]: 'https://ropsten.mycustomnode.com' } } }) diff --git a/src/types.ts b/src/types.ts index 2b953de..1841aa1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,10 +8,13 @@ export enum ProviderType { } export enum ChainId { - MAINNET = 1, - ROPSTEN = 3, - RINKEBY = 4, - KOVAN = 42 + ETHEREUM_MAINNET = 1, + ETHEREUM_ROPSTEN = 3, + ETHEREUM_RINKEBY = 4, + ETHEREUM_GOERLI = 5, + ETHEREUM_KOVAN = 42, + MATIC_MUMBAI = 13881, + MATIC_MAINNET = 89 } export type RequestArguments = { diff --git a/test/ConnectionManager.spec.ts b/test/ConnectionManager.spec.ts index ad754a2..d21906a 100644 --- a/test/ConnectionManager.spec.ts +++ b/test/ConnectionManager.spec.ts @@ -64,7 +64,7 @@ describe('ConnectionManager', () => { const result = await connectionManager.connect( ProviderType.INJECTED, - ChainId.ROPSTEN + ChainId.ETHEREUM_ROPSTEN ) const activateResult = await stubConnector.activate() @@ -75,7 +75,7 @@ describe('ConnectionManager', () => { send: () => {} }, account: activateResult.account, - chainId: ChainId.ROPSTEN + chainId: ChainId.ETHEREUM_ROPSTEN }) ) }) @@ -86,7 +86,7 @@ describe('ConnectionManager', () => { const result = await connectionManager.connect( ProviderType.INJECTED, - ChainId.ROPSTEN + ChainId.ETHEREUM_ROPSTEN ) const { account } = await stubConnector.activate() @@ -96,7 +96,7 @@ describe('ConnectionManager', () => { request: () => {} }, account, - chainId: ChainId.ROPSTEN + chainId: ChainId.ETHEREUM_ROPSTEN }) ) }) @@ -106,11 +106,14 @@ describe('ConnectionManager', () => { const configuration = getConfiguration() sinon.stub(connectionManager, 'buildConnector').returns(stubConnector) - await connectionManager.connect(ProviderType.INJECTED, ChainId.KOVAN) + await connectionManager.connect( + ProviderType.INJECTED, + ChainId.ETHEREUM_KOVAN + ) const value = JSON.stringify({ providerType: ProviderType.INJECTED, - chainId: ChainId.KOVAN + chainId: ChainId.ETHEREUM_KOVAN }) expect(storage.get(configuration.storageKey)).to.eq(value) }) @@ -148,7 +151,7 @@ describe('ConnectionManager', () => { request: () => {} }, account, - chainId: ChainId.MAINNET + chainId: ChainId.ETHEREUM_MAINNET }) ) }) @@ -159,11 +162,14 @@ describe('ConnectionManager', () => { const stubConnector = new StubConnector() sinon.stub(connectionManager, 'buildConnector').returns(stubConnector) - await connectionManager.connect(ProviderType.INJECTED, ChainId.KOVAN) + await connectionManager.connect( + ProviderType.INJECTED, + ChainId.ETHEREUM_KOVAN + ) expect(connectionManager.getConnectionData()).to.deep.eq({ providerType: ProviderType.INJECTED, - chainId: ChainId.KOVAN + chainId: ChainId.ETHEREUM_KOVAN }) }) @@ -298,35 +304,35 @@ describe('ConnectionManager', () => { it('should throw if an invalid provider type is supplied', () => { const providerType = 'Invalid Provider Type' as any expect(() => - connectionManager.buildConnector(providerType, ChainId.MAINNET) + connectionManager.buildConnector(providerType, ChainId.ETHEREUM_MAINNET) ).to.throw(`Invalid provider ${providerType}`) }) it('should return an instance of FortmaticConnector for the supplied chain', () => { const connector = connectionManager.buildConnector( ProviderType.FORTMATIC, - ChainId.KOVAN + ChainId.ETHEREUM_KOVAN ) expect(connector).to.be.instanceOf(FortmaticConnector) - expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN) + expect(connector.getChainId()).to.eventually.eq(ChainId.ETHEREUM_KOVAN) }) it('should return an instance of InjectedConnector for the supplied chain', () => { const connector = connectionManager.buildConnector( ProviderType.INJECTED, - ChainId.KOVAN + ChainId.ETHEREUM_KOVAN ) expect(connector).to.be.instanceOf(InjectedConnector) - expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN) + expect(connector.getChainId()).to.eventually.eq(ChainId.ETHEREUM_KOVAN) }) it('should return an instance of WalletConnectConnector for the supplied chain', () => { const connector = connectionManager.buildConnector( ProviderType.WALLET_CONNECT, - ChainId.KOVAN + ChainId.ETHEREUM_KOVAN ) expect(connector).to.be.instanceOf(WalletConnectConnector) - expect(connector.getChainId()).to.eventually.eq(ChainId.KOVAN) + expect(connector.getChainId()).to.eventually.eq(ChainId.ETHEREUM_KOVAN) }) }) }) diff --git a/test/configuration.spec.ts b/test/configuration.spec.ts index 932be83..6c16191 100644 --- a/test/configuration.spec.ts +++ b/test/configuration.spec.ts @@ -10,18 +10,18 @@ describe('#getConfiguration', () => { [ProviderType.INJECTED]: {}, [ProviderType.FORTMATIC]: { apiKeys: { - [ChainId.MAINNET]: 'pk_live_D7297F51E9776DD2', - [ChainId.ROPSTEN]: 'pk_test_198DDD3CA646DE2F', - [ChainId.RINKEBY]: 'pk_test_198DDD3CA646DE2F', - [ChainId.KOVAN]: 'pk_test_198DDD3CA646DE2F' + [ChainId.ETHEREUM_MAINNET]: 'pk_live_D7297F51E9776DD2', + [ChainId.ETHEREUM_ROPSTEN]: 'pk_test_198DDD3CA646DE2F', + [ChainId.ETHEREUM_RINKEBY]: 'pk_test_198DDD3CA646DE2F', + [ChainId.ETHEREUM_KOVAN]: 'pk_test_198DDD3CA646DE2F' } }, [ProviderType.WALLET_CONNECT]: { urls: { - [ChainId.MAINNET]: 'https://mainnet.mycustomnode.com', - [ChainId.ROPSTEN]: 'https://ropsten.mycustomnode.com', - [ChainId.RINKEBY]: 'https://ropsten.mycustomnode.com', - [ChainId.KOVAN]: 'https://ropsten.mycustomnode.com' + [ChainId.ETHEREUM_MAINNET]: 'https://mainnet.mycustomnode.com', + [ChainId.ETHEREUM_ROPSTEN]: 'https://ropsten.mycustomnode.com', + [ChainId.ETHEREUM_RINKEBY]: 'https://ropsten.mycustomnode.com', + [ChainId.ETHEREUM_KOVAN]: 'https://ropsten.mycustomnode.com' } } }) diff --git a/test/connectors.spec.ts b/test/connectors.spec.ts index 06e2bf3..69d35ff 100644 --- a/test/connectors.spec.ts +++ b/test/connectors.spec.ts @@ -14,7 +14,7 @@ describe('connectors', () => { describe('FortmaticConnector', () => { describe('#constructor', () => { it('should call super with the Fortmatic configuration for the supplied chain id', () => { - const chainId = ChainId.ROPSTEN + const chainId = ChainId.ETHEREUM_ROPSTEN const apiKey = 'test-api-key' const mockConfiguration = { @@ -44,7 +44,7 @@ describe('connectors', () => { describe('InjectedConnector', () => { describe('#constructor', () => { it('should call super with the supplied chain id as supported chain ids', () => { - const chainId = ChainId.RINKEBY + const chainId = ChainId.ETHEREUM_RINKEBY const connector = new InjectedConnector(chainId) expect(connector.getChainId()).to.eventually.eq(chainId) @@ -56,7 +56,7 @@ describe('connectors', () => { describe('WalletConnectConnector', () => { describe('#constructor', () => { it('should call super with the configuration and supplied chain id', () => { - const chainId = ChainId.KOVAN + const chainId = ChainId.ETHEREUM_KOVAN const url = 'some-weird-url' const mockConfiguration = { diff --git a/test/utils.ts b/test/utils.ts index a0db250..1a9e8d7 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -20,7 +20,7 @@ export class StubConnector extends AbstractConnector { } async getChainId(): Promise { - return ChainId.MAINNET + return ChainId.ETHEREUM_MAINNET } async getAccount(): Promise {