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 })
|
.send({ from: consumerAccount })
|
||||||
return trxReceipt
|
return trxReceipt
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e.message)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import Config from '../models/Config'
|
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'
|
||||||
|
|
||||||
export declare type ConfigHelperNetworkName =
|
export declare type ConfigHelperNetworkName =
|
||||||
| 'mainnet'
|
| 'mainnet'
|
||||||
@ -54,37 +55,51 @@ const configs: ConfigHelperConfig[] = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
export class ConfigHelper {
|
export class ConfigHelper {
|
||||||
/* Load config from env ADDRESS_FILE (generated by ocean-contracts) */
|
/* Load contract addresses from env ADDRESS_FILE (generated by ocean-contracts) */
|
||||||
public loadAddressesFromEnv() {
|
public getAddressesFromEnv(): Partial<ConfigHelperConfig> {
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(fs.readFileSync(process.env.ADDRESS_FILE, 'utf8'))
|
const data = JSON.parse(
|
||||||
if (data) {
|
fs.readFileSync(
|
||||||
if (data.ganache) {
|
process.env.ADDRESS_FILE ||
|
||||||
if (data.ganache.DTFactory) configs[0].factoryAddress = data.ganache.DTFactory
|
`${homedir}/.ocean/ocean-contracts/artifacts/address.json`,
|
||||||
if (data.ganache.BFactory) configs[0].poolFactoryAddress = data.ganache.BFactory
|
'utf8'
|
||||||
if (data.ganache.FixedRateExchange)
|
)
|
||||||
configs[0].fixedRateExchangeAddress = data.ganache.FixedRateExchange
|
)
|
||||||
if (data.ganache.Metadata)
|
|
||||||
configs[0].metadataContractAddress = data.ganache.Metadata
|
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(
|
public getConfig(
|
||||||
network: ConfigHelperNetworkName | ConfigHelperNetworkId,
|
network: ConfigHelperNetworkName | ConfigHelperNetworkId,
|
||||||
infuraProjectId?: string
|
infuraProjectId?: string
|
||||||
): Config {
|
): Config {
|
||||||
if (network === 'development') this.loadAddressesFromEnv()
|
|
||||||
const filterBy = typeof network === 'string' ? 'network' : 'chainId'
|
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) {
|
if (!config) {
|
||||||
Logger.error(`No config found for given network '${network}'`)
|
Logger.error(`No config found for given network '${network}'`)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (network === 'development') {
|
||||||
|
const contractAddressesConfig = this.getAddressesFromEnv()
|
||||||
|
config = { ...config, ...contractAddressesConfig }
|
||||||
|
}
|
||||||
|
|
||||||
const nodeUri = infuraProjectId
|
const nodeUri = infuraProjectId
|
||||||
? `${config.nodeUri}/${infuraProjectId}`
|
? `${config.nodeUri}/${infuraProjectId}`
|
||||||
: config.nodeUri
|
: config.nodeUri
|
||||||
|
Loading…
x
Reference in New Issue
Block a user