mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
refactor getting local contract addresses
This commit is contained in:
parent
eaffea87d2
commit
9ed008979a
@ -114,7 +114,7 @@ export class OnChainMetadataStore {
|
||||
.send({ from: consumerAccount })
|
||||
return trxReceipt
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
console.error(e.message)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
@ -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<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
|
||||
|
Loading…
x
Reference in New Issue
Block a user