mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
simplify, only one method needed
This commit is contained in:
parent
ba53d6ab6a
commit
b2ccad5091
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user