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 { Logger } from '../lib'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { homedir } from 'os'
|
import { homedir } from 'os'
|
||||||
|
import * as DefaultContractsAddresses from '@oceanprotocol/contracts/artifacts/address.json'
|
||||||
export declare type ConfigHelperNetworkName =
|
export declare type ConfigHelperNetworkName =
|
||||||
| 'mainnet'
|
| 'mainnet'
|
||||||
| 'rinkeby'
|
| 'rinkeby'
|
||||||
@ -21,9 +21,10 @@ const configs: ConfigHelperConfig[] = [
|
|||||||
networkId: null,
|
networkId: null,
|
||||||
network: 'unknown',
|
network: 'unknown',
|
||||||
nodeUri: 'http://localhost:8545',
|
nodeUri: 'http://localhost:8545',
|
||||||
factoryAddress: null,
|
|
||||||
metadataCacheUri: 'http://127.0.0.1:5000',
|
metadataCacheUri: 'http://127.0.0.1:5000',
|
||||||
providerUri: 'http://127.0.0.1:8030',
|
providerUri: 'http://127.0.0.1:8030',
|
||||||
|
oceanTokenAddress: null,
|
||||||
|
factoryAddress: '0x1234',
|
||||||
poolFactoryAddress: null,
|
poolFactoryAddress: null,
|
||||||
fixedRateExchangeAddress: null,
|
fixedRateExchangeAddress: null,
|
||||||
metadataContractAddress: null
|
metadataContractAddress: null
|
||||||
@ -33,9 +34,10 @@ const configs: ConfigHelperConfig[] = [
|
|||||||
networkId: 8996,
|
networkId: 8996,
|
||||||
network: 'development',
|
network: 'development',
|
||||||
nodeUri: 'http://localhost:8545',
|
nodeUri: 'http://localhost:8545',
|
||||||
factoryAddress: null,
|
|
||||||
metadataCacheUri: 'http://127.0.0.1:5000',
|
metadataCacheUri: 'http://127.0.0.1:5000',
|
||||||
providerUri: 'http://127.0.0.1:8030',
|
providerUri: 'http://127.0.0.1:8030',
|
||||||
|
oceanTokenAddress: null,
|
||||||
|
factoryAddress: null,
|
||||||
poolFactoryAddress: null,
|
poolFactoryAddress: null,
|
||||||
fixedRateExchangeAddress: null,
|
fixedRateExchangeAddress: null,
|
||||||
metadataContractAddress: null
|
metadataContractAddress: null
|
||||||
@ -44,22 +46,22 @@ const configs: ConfigHelperConfig[] = [
|
|||||||
networkId: 4,
|
networkId: 4,
|
||||||
network: 'rinkeby',
|
network: 'rinkeby',
|
||||||
nodeUri: 'https://rinkeby.infura.io/v3',
|
nodeUri: 'https://rinkeby.infura.io/v3',
|
||||||
factoryAddress: '0x3fd7A00106038Fb5c802c6d63fa7147Fe429E83a',
|
|
||||||
oceanTokenAddress: '0x8967BCF84170c91B0d24D4302C2376283b0B3a07',
|
|
||||||
metadataCacheUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
|
metadataCacheUri: 'https://aquarius.rinkeby.v3.dev-ocean.com',
|
||||||
providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
|
providerUri: 'https://provider.rinkeby.v3.dev-ocean.com',
|
||||||
poolFactoryAddress: '0x53eDF9289B0898e1652Ce009AACf8D25fA9A42F8',
|
oceanTokenAddress: null,
|
||||||
fixedRateExchangeAddress: '0xeD1DfC5F3a589CfC4E8B91C1fbfC18FC6699Fbde',
|
factoryAddress: null,
|
||||||
metadataContractAddress: '0xFD8a7b6297153397B7eb4356C47dbd381d58bFF4'
|
poolFactoryAddress: null,
|
||||||
|
fixedRateExchangeAddress: null,
|
||||||
|
metadataContractAddress: null
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
networkId: 1,
|
networkId: 1,
|
||||||
network: 'mainnet',
|
network: 'mainnet',
|
||||||
nodeUri: 'https://mainnet.infura.io/v3',
|
nodeUri: 'https://mainnet.infura.io/v3',
|
||||||
factoryAddress: '0x1234',
|
|
||||||
oceanTokenAddress: '0x7AFeBBB46fDb47ed17b22ed075Cde2447694fB9e',
|
|
||||||
metadataCacheUri: null,
|
metadataCacheUri: null,
|
||||||
providerUri: null,
|
providerUri: null,
|
||||||
|
oceanTokenAddress: null,
|
||||||
|
factoryAddress: null,
|
||||||
poolFactoryAddress: null,
|
poolFactoryAddress: null,
|
||||||
fixedRateExchangeAddress: null,
|
fixedRateExchangeAddress: null,
|
||||||
metadataContractAddress: null
|
metadataContractAddress: null
|
||||||
@ -69,18 +71,17 @@ const configs: ConfigHelperConfig[] = [
|
|||||||
export class ConfigHelper {
|
export class ConfigHelper {
|
||||||
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
|
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
|
||||||
public getAddressesFromEnv(network: string): Partial<ConfigHelperConfig> {
|
public getAddressesFromEnv(network: string): Partial<ConfigHelperConfig> {
|
||||||
try {
|
// use the defaults first
|
||||||
const data = JSON.parse(
|
let configAddresses: Partial<ConfigHelperConfig>
|
||||||
fs.readFileSync(
|
if (DefaultContractsAddresses[network]) {
|
||||||
process.env.ADDRESS_FILE ||
|
const {
|
||||||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
|
DTFactory,
|
||||||
'utf8'
|
BFactory,
|
||||||
)
|
FixedRateExchange,
|
||||||
)
|
Metadata,
|
||||||
|
Ocean
|
||||||
const { DTFactory, BFactory, FixedRateExchange, Metadata, Ocean } = data[network]
|
} = DefaultContractsAddresses[network]
|
||||||
|
configAddresses = {
|
||||||
const configAddresses: Partial<ConfigHelperConfig> = {
|
|
||||||
factoryAddress: DTFactory,
|
factoryAddress: DTFactory,
|
||||||
poolFactoryAddress: BFactory,
|
poolFactoryAddress: BFactory,
|
||||||
fixedRateExchangeAddress: FixedRateExchange,
|
fixedRateExchangeAddress: FixedRateExchange,
|
||||||
@ -88,12 +89,32 @@ export class ConfigHelper {
|
|||||||
oceanTokenAddress: Ocean,
|
oceanTokenAddress: Ocean,
|
||||||
...(process.env.AQUARIUS_URI && { metadataCacheUri: process.env.AQUARIUS_URI })
|
...(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
|
|
||||||
}
|
}
|
||||||
|
// try ADDRESS_FILE env
|
||||||
|
if (fs && process.env.ADDRESS_FILE) {
|
||||||
|
try {
|
||||||
|
const data = JSON.parse(
|
||||||
|
fs.readFileSync(
|
||||||
|
process.env.ADDRESS_FILE ||
|
||||||
|
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
|
||||||
|
'utf8'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
const { DTFactory, BFactory, FixedRateExchange, Metadata, Ocean } = data[network]
|
||||||
|
configAddresses = {
|
||||||
|
factoryAddress: DTFactory,
|
||||||
|
poolFactoryAddress: BFactory,
|
||||||
|
fixedRateExchangeAddress: FixedRateExchange,
|
||||||
|
metadataContractAddress: Metadata,
|
||||||
|
oceanTokenAddress: Ocean,
|
||||||
|
...(process.env.AQUARIUS_URI && { metadataCacheUri: process.env.AQUARIUS_URI })
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// console.error(`ERROR: Could not load local contract address file: ${e.message}`)
|
||||||
|
// return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return configAddresses
|
||||||
}
|
}
|
||||||
|
|
||||||
public getConfig(
|
public getConfig(
|
||||||
|
@ -6,6 +6,7 @@ describe('ConfigHelper', () => {
|
|||||||
const network = 'rinkeby'
|
const network = 'rinkeby'
|
||||||
const config = new ConfigHelper().getConfig(network)
|
const config = new ConfigHelper().getConfig(network)
|
||||||
assert(config.nodeUri.includes(network))
|
assert(config.nodeUri.includes(network))
|
||||||
|
assert(config.factoryAddress != null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should get config based on network name, and add passed Infura ID', () => {
|
it('should get config based on network name, and add passed Infura ID', () => {
|
||||||
@ -13,12 +14,14 @@ describe('ConfigHelper', () => {
|
|||||||
const infuraId = 'helloInfura'
|
const infuraId = 'helloInfura'
|
||||||
const config = new ConfigHelper().getConfig(network, infuraId)
|
const config = new ConfigHelper().getConfig(network, infuraId)
|
||||||
assert(config.nodeUri.includes(infuraId))
|
assert(config.nodeUri.includes(infuraId))
|
||||||
|
assert(config.factoryAddress != null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should get config based on chain ID', () => {
|
it('should get config based on chain ID', () => {
|
||||||
const network = 4
|
const network = 4
|
||||||
const config = new ConfigHelper().getConfig(network)
|
const config = new ConfigHelper().getConfig(network)
|
||||||
assert(config.nodeUri.includes('rinkeby'))
|
assert(config.nodeUri.includes('rinkeby'))
|
||||||
|
assert(config.factoryAddress != null)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return nothing with unknown network', () => {
|
it('should return nothing with unknown network', () => {
|
||||||
@ -26,4 +29,9 @@ describe('ConfigHelper', () => {
|
|||||||
const config = new ConfigHelper().getConfig(network)
|
const config = new ConfigHelper().getConfig(network)
|
||||||
assert(config === null)
|
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