mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
Merge pull request #390 from oceanprotocol/feature/confighelper_refactor
refactor confighelper to use ocean-contracts
This commit is contained in:
commit
5577e1d594
@ -2,7 +2,7 @@ import Config from '../models/Config'
|
||||
import { Logger } from '../lib'
|
||||
import fs from 'fs'
|
||||
import { homedir } from 'os'
|
||||
|
||||
import * as DefaultContractsAddresses from '@oceanprotocol/contracts/artifacts/address.json'
|
||||
export declare type ConfigHelperNetworkName =
|
||||
| 'mainnet'
|
||||
| 'rinkeby'
|
||||
@ -21,9 +21,10 @@ const configs: ConfigHelperConfig[] = [
|
||||
networkId: null,
|
||||
network: 'unknown',
|
||||
nodeUri: 'http://localhost:8545',
|
||||
factoryAddress: null,
|
||||
metadataCacheUri: 'http://127.0.0.1:5000',
|
||||
providerUri: 'http://127.0.0.1:8030',
|
||||
oceanTokenAddress: null,
|
||||
factoryAddress: '0x1234',
|
||||
poolFactoryAddress: null,
|
||||
fixedRateExchangeAddress: null,
|
||||
metadataContractAddress: null
|
||||
@ -33,9 +34,10 @@ const configs: ConfigHelperConfig[] = [
|
||||
networkId: 8996,
|
||||
network: 'development',
|
||||
nodeUri: 'http://localhost:8545',
|
||||
factoryAddress: null,
|
||||
metadataCacheUri: 'http://127.0.0.1:5000',
|
||||
providerUri: 'http://127.0.0.1:8030',
|
||||
oceanTokenAddress: null,
|
||||
factoryAddress: null,
|
||||
poolFactoryAddress: null,
|
||||
fixedRateExchangeAddress: null,
|
||||
metadataContractAddress: null
|
||||
@ -44,22 +46,22 @@ const configs: ConfigHelperConfig[] = [
|
||||
networkId: 4,
|
||||
network: 'rinkeby',
|
||||
nodeUri: 'https://rinkeby.infura.io/v3',
|
||||
factoryAddress: '0x3fd7A00106038Fb5c802c6d63fa7147Fe429E83a',
|
||||
oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07',
|
||||
metadataCacheUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
|
||||
providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
|
||||
poolFactoryAddress: '0x53eDF9289B0898e1652Ce009AACf8D25fA9A42F8',
|
||||
fixedRateExchangeAddress: '0xeD1DfC5F3a589CfC4E8B91C1fbfC18FC6699Fbde',
|
||||
metadataContractAddress: '0xFD8a7b6297153397B7eb4356C47dbd381d58bFF4'
|
||||
oceanTokenAddress: null,
|
||||
factoryAddress: null,
|
||||
poolFactoryAddress: null,
|
||||
fixedRateExchangeAddress: null,
|
||||
metadataContractAddress: null
|
||||
},
|
||||
{
|
||||
networkId: 1,
|
||||
network: 'mainnet',
|
||||
nodeUri: 'https://mainnet.infura.io/v3',
|
||||
factoryAddress: '0x1234',
|
||||
oceanTokenAddress: '0x7AFeBBB46fDb47ed17b22ed075Cde2447694fB9e',
|
||||
metadataCacheUri: null,
|
||||
providerUri: null,
|
||||
oceanTokenAddress: null,
|
||||
factoryAddress: null,
|
||||
poolFactoryAddress: null,
|
||||
fixedRateExchangeAddress: null,
|
||||
metadataContractAddress: null
|
||||
@ -69,6 +71,27 @@ const configs: ConfigHelperConfig[] = [
|
||||
export class ConfigHelper {
|
||||
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
|
||||
public getAddressesFromEnv(network: string): Partial<ConfigHelperConfig> {
|
||||
// use the defaults first
|
||||
let configAddresses: Partial<ConfigHelperConfig>
|
||||
if (DefaultContractsAddresses[network]) {
|
||||
const {
|
||||
DTFactory,
|
||||
BFactory,
|
||||
FixedRateExchange,
|
||||
Metadata,
|
||||
Ocean
|
||||
} = DefaultContractsAddresses[network]
|
||||
configAddresses = {
|
||||
factoryAddress: DTFactory,
|
||||
poolFactoryAddress: BFactory,
|
||||
fixedRateExchangeAddress: FixedRateExchange,
|
||||
metadataContractAddress: Metadata,
|
||||
oceanTokenAddress: Ocean,
|
||||
...(process.env.AQUARIUS_URI && { metadataCacheUri: process.env.AQUARIUS_URI })
|
||||
}
|
||||
}
|
||||
// try ADDRESS_FILE env
|
||||
if (fs && process.env.ADDRESS_FILE) {
|
||||
try {
|
||||
const data = JSON.parse(
|
||||
fs.readFileSync(
|
||||
@ -77,10 +100,8 @@ export class ConfigHelper {
|
||||
'utf8'
|
||||
)
|
||||
)
|
||||
|
||||
const { DTFactory, BFactory, FixedRateExchange, Metadata, Ocean } = data[network]
|
||||
|
||||
const configAddresses: Partial<ConfigHelperConfig> = {
|
||||
configAddresses = {
|
||||
factoryAddress: DTFactory,
|
||||
poolFactoryAddress: BFactory,
|
||||
fixedRateExchangeAddress: FixedRateExchange,
|
||||
@ -88,13 +109,13 @@ export class ConfigHelper {
|
||||
oceanTokenAddress: Ocean,
|
||||
...(process.env.AQUARIUS_URI && { metadataCacheUri: process.env.AQUARIUS_URI })
|
||||
}
|
||||
|
||||
return configAddresses
|
||||
} catch (e) {
|
||||
console.error(`ERROR: Could not load local contract address file: ${e.message}`)
|
||||
return null
|
||||
// console.error(`ERROR: Could not load local contract address file: ${e.message}`)
|
||||
// return null
|
||||
}
|
||||
}
|
||||
return configAddresses
|
||||
}
|
||||
|
||||
public getConfig(
|
||||
network: ConfigHelperNetworkName | ConfigHelperNetworkId,
|
||||
|
@ -6,6 +6,7 @@ describe('ConfigHelper', () => {
|
||||
const network = 'rinkeby'
|
||||
const config = new ConfigHelper().getConfig(network)
|
||||
assert(config.nodeUri.includes(network))
|
||||
assert(config.factoryAddress != null)
|
||||
})
|
||||
|
||||
it('should get config based on network name, and add passed Infura ID', () => {
|
||||
@ -13,12 +14,14 @@ describe('ConfigHelper', () => {
|
||||
const infuraId = 'helloInfura'
|
||||
const config = new ConfigHelper().getConfig(network, infuraId)
|
||||
assert(config.nodeUri.includes(infuraId))
|
||||
assert(config.factoryAddress != null)
|
||||
})
|
||||
|
||||
it('should get config based on chain ID', () => {
|
||||
const network = 4
|
||||
const config = new ConfigHelper().getConfig(network)
|
||||
assert(config.nodeUri.includes('rinkeby'))
|
||||
assert(config.factoryAddress != null)
|
||||
})
|
||||
|
||||
it('should return nothing with unknown network', () => {
|
||||
@ -26,4 +29,9 @@ describe('ConfigHelper', () => {
|
||||
const config = new ConfigHelper().getConfig(network)
|
||||
assert(config === null)
|
||||
})
|
||||
it('should get a custom config', () => {
|
||||
const network = 'unknown'
|
||||
const config = new ConfigHelper().getConfig(network)
|
||||
assert(config.factoryAddress === '0x1234')
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user