Skip to content

Commit

Permalink
Merge pull request #312 from oceanprotocol/fix/config-helper-development
Browse files Browse the repository at this point in the history
refactor getting local contract addresses
  • Loading branch information
kremalicious authored Sep 25, 2020
2 parents eaffea8 + 9ed0089 commit 62f1156
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/metadatastore/OnChainMetaData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class OnChainMetadataStore {
.send({ from: consumerAccount })
return trxReceipt
} catch (e) {
console.error(e)
console.error(e.message)
return null
}
}
Expand Down
47 changes: 31 additions & 16 deletions src/utils/ConfigHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Config from '../models/Config'
import { Logger } from '../lib'
import fs from 'fs'
import { homedir } from 'os'

export declare type ConfigHelperNetworkName =
| 'mainnet'
Expand Down Expand Up @@ -54,37 +55,51 @@ const configs: ConfigHelperConfig[] = [
]

export class ConfigHelper {
/* Load config from env ADDRESS_FILE (generated by ocean-contracts) */
public loadAddressesFromEnv() {
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
public getAddressesFromEnv(): Partial<ConfigHelperConfig> {
try {
const data = JSON.parse(fs.readFileSync(process.env.ADDRESS_FILE, 'utf8'))
if (data) {
if (data.ganache) {
if (data.ganache.DTFactory) configs[0].factoryAddress = data.ganache.DTFactory
if (data.ganache.BFactory) configs[0].poolFactoryAddress = data.ganache.BFactory
if (data.ganache.FixedRateExchange)
configs[0].fixedRateExchangeAddress = data.ganache.FixedRateExchange
if (data.ganache.Metadata)
configs[0].metadataContractAddress = data.ganache.Metadata
}
const data = JSON.parse(
fs.readFileSync(
process.env.ADDRESS_FILE ||
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
'utf8'
)
)

const { DTFactory, BFactory, FixedRateExchange, Metadata } = data?.ganache

const configAddresses: Partial<ConfigHelperConfig> = {
factoryAddress: DTFactory,
poolFactoryAddress: BFactory,
fixedRateExchangeAddress: FixedRateExchange,
metadataContractAddress: Metadata,
...(process.env.AQUARIUS_URI && { metadataStoreUri: process.env.AQUARIUS_URI })
}
if (process.env.AQUARIUS_URI) configs[0].metadataStoreUri = process.env.AQUARIUS_URI
} catch (e) {}

return configAddresses
} catch (e) {
console.error(`Could not load local contract address file: ${e.message}`)
return null
}
}

public getConfig(
network: ConfigHelperNetworkName | ConfigHelperNetworkId,
infuraProjectId?: string
): Config {
if (network === 'development') this.loadAddressesFromEnv()
const filterBy = typeof network === 'string' ? 'network' : 'chainId'
const config = configs.find((c) => c[filterBy] === network)
let config = configs.find((c) => c[filterBy] === network)

if (!config) {
Logger.error(`No config found for given network '${network}'`)
return null
}

if (network === 'development') {
const contractAddressesConfig = this.getAddressesFromEnv()
config = { ...config, ...contractAddressesConfig }
}

const nodeUri = infuraProjectId
? `${config.nodeUri}/${infuraProjectId}`
: config.nodeUri
Expand Down

0 comments on commit 62f1156

Please sign in to comment.