diff --git a/src/metadatastore/OnChainMetaData.ts b/src/metadatastore/OnChainMetaData.ts index aa679ecac..08e929faf 100644 --- a/src/metadatastore/OnChainMetaData.ts +++ b/src/metadatastore/OnChainMetaData.ts @@ -114,7 +114,7 @@ export class OnChainMetadataStore { .send({ from: consumerAccount }) return trxReceipt } catch (e) { - console.error(e) + console.error(e.message) return null } } diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 68aab390b..1b2735dbd 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -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' @@ -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 { 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 = { + 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