From 07bb1bfde0fe8f0fdf01ba63f793a0decad7cd43 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 18:58:54 +0200 Subject: [PATCH 1/6] refactor config helper --- src/utils/ConfigHelper.ts | 84 ++++++++++----------------------------- 1 file changed, 22 insertions(+), 62 deletions(-) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index e31b3011..f49575f7 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -1,19 +1,15 @@ -export interface ConfigHelper { +import Config from '../models/Config' + +export interface ConfigHelperConfig extends Config { chainId?: number - network: string - url: string - factoryAddress: string - poolFactoryAddress: string - oceanTokenAddress: string - metadataStoreUri: string - providerUri: string + network: 'mainnet' | 'rinkeby' | 'development' | string } -const configs = [ +const configs: ConfigHelperConfig[] = [ { - chaindId: null, + chainId: null, network: 'development', - url: 'http://localhost:8545', + nodeUri: 'http://localhost:8545', factoryAddress: null, metadataStoreUri: 'http://127.0.0.1:5000', providerUri: 'http://127.0.0.1:8030', @@ -23,7 +19,7 @@ const configs = [ { chainId: 4, network: 'rinkeby', - url: 'https://rinkeby.infura.io/v3', + nodeUri: 'https://rinkeby.infura.io/v3', factoryAddress: '0x3ECd1429101f93149D799Ef257C07a2B1Dc30897', oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07', metadataStoreUri: 'https://aquarius.rinkeby.v3.dev-ocean.com', @@ -34,7 +30,7 @@ const configs = [ { chainId: 1, network: 'mainnet', - url: 'https://mainnet.infura.io/v3', + nodeUri: 'https://mainnet.infura.io/v3', factoryAddress: '0x1234', oceanTokenAddress: '0x985dd3d42de1e256d09e1c10f112bccb8015ad41', metadataStoreUri: null, @@ -45,59 +41,23 @@ const configs = [ ] export class ConfigHelper { - public getConfig(network: string, infuraProjectId?: string): ConfigHelper { - const confighelp = new ConfigHelper() - // fill unknown values - confighelp.chainId = null - confighelp.factoryAddress = null - confighelp.url = null - confighelp.network = network - confighelp.oceanTokenAddress = null - confighelp.metadataStoreUri = null - confighelp.providerUri = null - confighelp.poolFactoryAddress = null + private getNodeUri(config: ConfigHelperConfig, infuraProjectId?: string) { + const nodeUri = infuraProjectId + ? `${config.nodeUri}/${infuraProjectId}` + : config.nodeUri - const knownconfig = configs.find((c) => c.network === network) - - if (knownconfig) { - confighelp.chainId = knownconfig.chainId - confighelp.factoryAddress = knownconfig.factoryAddress - confighelp.oceanTokenAddress = knownconfig.oceanTokenAddress - confighelp.url = `${knownconfig.url}/${infuraProjectId}` - confighelp.network = knownconfig.network - confighelp.metadataStoreUri = knownconfig.metadataStoreUri - confighelp.providerUri = knownconfig.providerUri - confighelp.poolFactoryAddress = knownconfig.poolFactoryAddress - } - - return confighelp + return nodeUri } - public getConfigById(chainId: number, infuraProjectId?: string): ConfigHelper { - const confighelp = new ConfigHelper() - // fill unknown values - confighelp.chainId = chainId - confighelp.factoryAddress = null - confighelp.url = null - confighelp.network = null - confighelp.oceanTokenAddress = null - confighelp.metadataStoreUri = null - confighelp.providerUri = null - confighelp.poolFactoryAddress = null + public getConfig(network: string, infuraProjectId?: string): ConfigHelperConfig { + const knownconfig = configs.find((c) => c.network === network) + const nodeUri = this.getNodeUri(knownconfig, infuraProjectId) + return knownconfig ? { ...knownconfig, nodeUri } : null + } + public getConfigById(chainId: number, infuraProjectId?: string): ConfigHelperConfig { const knownconfig = configs.find((c) => c.chainId === chainId) - - if (knownconfig) { - confighelp.chainId = knownconfig.chainId - confighelp.factoryAddress = knownconfig.factoryAddress - confighelp.oceanTokenAddress = knownconfig.oceanTokenAddress - confighelp.url = `${knownconfig.url}/${infuraProjectId}` - confighelp.network = knownconfig.network - confighelp.metadataStoreUri = knownconfig.metadataStoreUri - confighelp.providerUri = knownconfig.providerUri - confighelp.poolFactoryAddress = knownconfig.poolFactoryAddress - } - - return confighelp + const nodeUri = this.getNodeUri(knownconfig, infuraProjectId) + return knownconfig ? { ...knownconfig, nodeUri } : null } } From ba53d6ab6ae138f4c99ea58f9c01089e42089958 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 19:10:34 +0200 Subject: [PATCH 2/6] add config helper test --- src/utils/ConfigHelper.ts | 2 +- test/unit/utils/ConfigHelper.test.ts | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/unit/utils/ConfigHelper.test.ts diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index f49575f7..ebcb0360 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -1,7 +1,7 @@ import Config from '../models/Config' export interface ConfigHelperConfig extends Config { - chainId?: number + chainId: 1 | 4 | number network: 'mainnet' | 'rinkeby' | 'development' | string } diff --git a/test/unit/utils/ConfigHelper.test.ts b/test/unit/utils/ConfigHelper.test.ts new file mode 100644 index 00000000..a36962ac --- /dev/null +++ b/test/unit/utils/ConfigHelper.test.ts @@ -0,0 +1,23 @@ +import { assert } from 'chai' +import { ConfigHelper } from '../../../src/lib' + +describe('ConfigHelper', () => { + it('should get config based on network name', () => { + const network = 'rinkeby' + const config = new ConfigHelper().getConfig(network) + assert(config.network === network) + }) + + it('should get config based on network name, and add passed Infura ID', () => { + const network = 'rinkeby' + const infuraId = 'helloInfura' + const config = new ConfigHelper().getConfig(network, infuraId) + assert(config.nodeUri.includes(infuraId)) + }) + + it('should get config based on chain ID', () => { + const chainId = 4 + const config = new ConfigHelper().getConfigById(chainId) + assert(config.chainId === chainId) + }) +}) From b2ccad50919516989331ccb43cb4fd80b9ed1fee Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 19:45:24 +0200 Subject: [PATCH 3/6] simplify, only one method needed --- src/utils/ConfigHelper.ts | 34 +++++++++++++++------------- test/unit/utils/ConfigHelper.test.ts | 6 ----- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index ebcb0360..27a74a7d 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -1,8 +1,16 @@ import Config from '../models/Config' +export declare type ConfigHelperNetworkName = + | 'mainnet' + | 'rinkeby' + | 'development' + | string + +export declare type ConfigHelperNetworkId = 1 | 4 | number + export interface ConfigHelperConfig extends Config { - chainId: 1 | 4 | number - network: 'mainnet' | 'rinkeby' | 'development' | string + chainId: ConfigHelperNetworkId + network: ConfigHelperNetworkName } const configs: ConfigHelperConfig[] = [ @@ -41,23 +49,17 @@ const configs: ConfigHelperConfig[] = [ ] export class ConfigHelper { - private getNodeUri(config: ConfigHelperConfig, infuraProjectId?: string) { + public getConfig( + network: ConfigHelperNetworkName | ConfigHelperNetworkId, + infuraProjectId?: string + ): ConfigHelperConfig { + const filterBy = typeof network === 'string' ? 'network' : 'chainId' + const config = configs.find((c) => c[filterBy] === network) + const nodeUri = infuraProjectId ? `${config.nodeUri}/${infuraProjectId}` : config.nodeUri - return nodeUri - } - - public getConfig(network: string, infuraProjectId?: string): ConfigHelperConfig { - const knownconfig = configs.find((c) => c.network === network) - const nodeUri = this.getNodeUri(knownconfig, infuraProjectId) - return knownconfig ? { ...knownconfig, nodeUri } : null - } - - public getConfigById(chainId: number, infuraProjectId?: string): ConfigHelperConfig { - const knownconfig = configs.find((c) => c.chainId === chainId) - const nodeUri = this.getNodeUri(knownconfig, infuraProjectId) - return knownconfig ? { ...knownconfig, nodeUri } : null + return config ? { ...config, nodeUri } : null } } diff --git a/test/unit/utils/ConfigHelper.test.ts b/test/unit/utils/ConfigHelper.test.ts index a36962ac..8d504587 100644 --- a/test/unit/utils/ConfigHelper.test.ts +++ b/test/unit/utils/ConfigHelper.test.ts @@ -14,10 +14,4 @@ describe('ConfigHelper', () => { const config = new ConfigHelper().getConfig(network, infuraId) assert(config.nodeUri.includes(infuraId)) }) - - it('should get config based on chain ID', () => { - const chainId = 4 - const config = new ConfigHelper().getConfigById(chainId) - assert(config.chainId === chainId) - }) }) From 2cb4478ba2c76aa0bf391ce1d68f51b19b7a0fd7 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 19:48:54 +0200 Subject: [PATCH 4/6] change mainnet oceanTokenAddress --- src/utils/ConfigHelper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 27a74a7d..3dfde7cb 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -40,7 +40,7 @@ const configs: ConfigHelperConfig[] = [ network: 'mainnet', nodeUri: 'https://mainnet.infura.io/v3', factoryAddress: '0x1234', - oceanTokenAddress: '0x985dd3d42de1e256d09e1c10f112bccb8015ad41', + oceanTokenAddress: '0x7AFeBBB46fDb47ed17b22ed075Cde2447694fB9e', metadataStoreUri: null, providerUri: null, poolFactoryAddress: null, From 44a26f687f12c799c962385da0a476a2f18de7ff Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 20:08:08 +0200 Subject: [PATCH 5/6] more tests, document config usage --- README.md | 17 +++++++++++++++++ src/utils/ConfigHelper.ts | 8 +++++++- test/unit/utils/ConfigHelper.test.ts | 12 ++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 60957b72..cd0759f0 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,23 @@ npm install @oceanprotocol/lib ## 🏄 Quick Start +```js +import { Ocean, ConfigHelper } from '@oceanprotocol/lib' + +const defaultConfig = new ConfigHelper().getConfig('rinkeby', 'YOUR_INFURA_PROJECT_ID') + +const config = { + ...defaultConfig, + metadataStoreUri: 'https://your-metadata-store.com', + providerUri: 'https://your-provider.com' +} + +async function init() { + const ocean = await Ocean.getInstance(config) + return ocean +} +``` + ### Simple Flow This stripped-down flow shows the essence of Ocean. Just downloading, no metadata. diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 3dfde7cb..b359e1e5 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -1,4 +1,5 @@ import Config from '../models/Config' +import { Logger } from '../lib' export declare type ConfigHelperNetworkName = | 'mainnet' @@ -56,10 +57,15 @@ export class ConfigHelper { const filterBy = typeof network === 'string' ? 'network' : 'chainId' const config = configs.find((c) => c[filterBy] === network) + if (!config) { + Logger.error(`No config found for given network '${network}'`) + return null + } + const nodeUri = infuraProjectId ? `${config.nodeUri}/${infuraProjectId}` : config.nodeUri - return config ? { ...config, nodeUri } : null + return { ...config, nodeUri } } } diff --git a/test/unit/utils/ConfigHelper.test.ts b/test/unit/utils/ConfigHelper.test.ts index 8d504587..46468086 100644 --- a/test/unit/utils/ConfigHelper.test.ts +++ b/test/unit/utils/ConfigHelper.test.ts @@ -14,4 +14,16 @@ describe('ConfigHelper', () => { const config = new ConfigHelper().getConfig(network, infuraId) assert(config.nodeUri.includes(infuraId)) }) + + it('should get config based on chain ID', () => { + const network = 4 + const config = new ConfigHelper().getConfig(network) + assert(config.chainId === network) + }) + + it('should return nothing with unknown network', () => { + const network = 'blabla' + const config = new ConfigHelper().getConfig(network) + assert(config === null) + }) }) From 785aac313722eff8583f7af6655ee8c42510d300 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 8 Sep 2020 20:30:52 +0200 Subject: [PATCH 6/6] typing & test tweaks --- README.md | 9 ++++++--- src/utils/ConfigHelper.ts | 2 +- test/unit/utils/ConfigHelper.test.ts | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cd0759f0..fa004cd7 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,13 @@ npm install @oceanprotocol/lib ## 🏄 Quick Start -```js -import { Ocean, ConfigHelper } from '@oceanprotocol/lib' +```ts +import { Ocean, Config, ConfigHelper } from '@oceanprotocol/lib' -const defaultConfig = new ConfigHelper().getConfig('rinkeby', 'YOUR_INFURA_PROJECT_ID') +const defaultConfig: Config = new ConfigHelper().getConfig( + 'rinkeby', + 'YOUR_INFURA_PROJECT_ID' +) const config = { ...defaultConfig, diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index b359e1e5..73cb6322 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -53,7 +53,7 @@ export class ConfigHelper { public getConfig( network: ConfigHelperNetworkName | ConfigHelperNetworkId, infuraProjectId?: string - ): ConfigHelperConfig { + ): Config { const filterBy = typeof network === 'string' ? 'network' : 'chainId' const config = configs.find((c) => c[filterBy] === network) diff --git a/test/unit/utils/ConfigHelper.test.ts b/test/unit/utils/ConfigHelper.test.ts index 46468086..7b74e082 100644 --- a/test/unit/utils/ConfigHelper.test.ts +++ b/test/unit/utils/ConfigHelper.test.ts @@ -5,7 +5,7 @@ describe('ConfigHelper', () => { it('should get config based on network name', () => { const network = 'rinkeby' const config = new ConfigHelper().getConfig(network) - assert(config.network === network) + assert(config.nodeUri.includes(network)) }) it('should get config based on network name, and add passed Infura ID', () => { @@ -18,7 +18,7 @@ describe('ConfigHelper', () => { it('should get config based on chain ID', () => { const network = 4 const config = new ConfigHelper().getConfig(network) - assert(config.chainId === network) + assert(config.nodeUri.includes('rinkeby')) }) it('should return nothing with unknown network', () => {