Skip to content

Commit

Permalink
Merge pull request #4 from decentraland/feat/update-chain-id
Browse files Browse the repository at this point in the history
feat: update ChainId
  • Loading branch information
nicosantangelo authored Feb 16, 2021
2 parents 66ecb82 + 00321e4 commit 0c7a7fc
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 58 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ 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<ConnectionResponse>
```

**Usage**

```typescript
const connection = new ConnectionManager(new Storage())
await connection.connect(ProviderType.INJECTED, ChainId.ROPSTEN)
await connection.connect(ProviderType.INJECTED, ChainId.ETHEREUM_ROPSTEN)
```

### .tryPreviousConnection()
Expand All @@ -62,9 +62,9 @@ async tryPreviousConnection(): Promise<ConnectionResponse>
```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()
Expand All @@ -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)

// (...)

Expand All @@ -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)

// (...)

Expand Down Expand Up @@ -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<Provider> {
```
Expand All @@ -161,7 +161,7 @@ async createProvider(
```typescript
const provider = await connection.createProvider(
Provider.FORTMATIC,
ChainId.ROPSTEN
ChainId.ETHEREUM_ROPSTEN
)
```
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -227,7 +230,7 @@ export type ConnectionResponse = {
### ConnectionData
```typescript
export type ConnectionData = {
type ConnectionData = {
providerType: ProviderType
chainId: ChainId
}
Expand Down
4 changes: 2 additions & 2 deletions src/ConnectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class ConnectionManager {

async connect(
providerType: ProviderType,
chainId: ChainId = ChainId.MAINNET
chainId: ChainId = ChainId.ETHEREUM_MAINNET
): Promise<ConnectionResponse> {
this.setConnectionData(providerType, chainId)
this.connector = this.buildConnector(providerType, chainId)
Expand Down Expand Up @@ -84,7 +84,7 @@ export class ConnectionManager {

async createProvider(
providerType: ProviderType,
chainId: ChainId = ChainId.MAINNET
chainId: ChainId = ChainId.ETHEREUM_MAINNET
): Promise<Provider> {
const connector = this.buildConnector(providerType, chainId)
const provider = await connector.getProvider()
Expand Down
16 changes: 8 additions & 8 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
})
Expand Down
11 changes: 7 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
38 changes: 22 additions & 16 deletions test/ConnectionManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('ConnectionManager', () => {

const result = await connectionManager.connect(
ProviderType.INJECTED,
ChainId.ROPSTEN
ChainId.ETHEREUM_ROPSTEN
)
const activateResult = await stubConnector.activate()

Expand All @@ -75,7 +75,7 @@ describe('ConnectionManager', () => {
send: () => {}
},
account: activateResult.account,
chainId: ChainId.ROPSTEN
chainId: ChainId.ETHEREUM_ROPSTEN
})
)
})
Expand All @@ -86,7 +86,7 @@ describe('ConnectionManager', () => {

const result = await connectionManager.connect(
ProviderType.INJECTED,
ChainId.ROPSTEN
ChainId.ETHEREUM_ROPSTEN
)
const { account } = await stubConnector.activate()

Expand All @@ -96,7 +96,7 @@ describe('ConnectionManager', () => {
request: () => {}
},
account,
chainId: ChainId.ROPSTEN
chainId: ChainId.ETHEREUM_ROPSTEN
})
)
})
Expand All @@ -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)
})
Expand Down Expand Up @@ -148,7 +151,7 @@ describe('ConnectionManager', () => {
request: () => {}
},
account,
chainId: ChainId.MAINNET
chainId: ChainId.ETHEREUM_MAINNET
})
)
})
Expand All @@ -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
})
})

Expand Down Expand Up @@ -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)
})
})
})
16 changes: 8 additions & 8 deletions test/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
})
Expand Down
6 changes: 3 additions & 3 deletions test/connectors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class StubConnector extends AbstractConnector {
}

async getChainId(): Promise<number | string> {
return ChainId.MAINNET
return ChainId.ETHEREUM_MAINNET
}

async getAccount(): Promise<null | string> {
Expand Down

0 comments on commit 0c7a7fc

Please sign in to comment.