import { BigNumber } from 'ethers'; import { SystemContext } from '../contracts/SystemContext'; import chains from './chains'; import { ChainId } from './constants'; import { Env, WalletDefinition } from './types'; class Config { public readonly apiBaseUrl: string = 'https://api.omnite.io'; public readonly ipfsPinataGatewayUrl: string = 'https://omnite.mypinata.cloud'; public readonly ipfsGatewayUrl: string = 'https://dweb.link'; public readonly omniteApiBaseUrl: string = 'https://api.omnite.io'; public readonly moralisBaseUrl: string = 'https://deep-index.moralis.io/'; defaults: { deploymentGas: BigNumber; bridgeFee: string; } = { bridgeFee: BigNumber.from(1).toString(), deploymentGas: BigNumber.from(1000000), }; env: Env = Env.development; //private private _availableWallets: WalletDefinition[] = []; private _cachedSystemData: Record< string, { contractFactory: string; collectionRegistry: string; omniteLayerZeroBridgeSender: string; omniteLayerZeroBridgeReceiver: string; omniteAxelarBridgeSender: string; omniteAxelarBridgeReceiver: string; diamondFacetsRegistry: string; } > = {}; private _moralisApiKey: string = ''; private _pinataJWTToken: string = ''; // getters get availableWallets() { if (this._availableWallets.length === 0) { throw new Error('No available wallets'); } return this._availableWallets; } get moralisApiKey() { if (!this._moralisApiKey) { throw new Error('No Moralis API key'); } return this._moralisApiKey; } get pinataJWTToken() { if (!this._pinataJWTToken) { throw new Error('No Pinata JWT token'); } return this._pinataJWTToken; } /** * Initialize config. Should be called only once. * * @param {{supportedChains:Array;moralisApiKey:string;pinataJWTToken:string;providerRpcUrls:Partial>;} */ async initialize(config: { supportedChains: Array; moralisApiKey: string; pinataJWTToken: string; providerRpcUrls: Partial>; }) { for (const supportedChain of config.supportedChains) { if (!config.providerRpcUrls[supportedChain]) { throw new Error( 'No provider RPC URL for chain id ' + supportedChain ); } } this._availableWallets = chains .filter((v) => config.supportedChains.includes(v.chainId)) .map((v) => ({ ...v, rpcUrl: config.providerRpcUrls[v.chainId] as string, })); this._moralisApiKey = config.moralisApiKey; this._pinataJWTToken = config.pinataJWTToken; await this.loadSystemData(); } private loadSystemData = () => Promise.all( this._availableWallets.map(async (chainConfig) => { if ( this._cachedSystemData[chainConfig.chainId] ?.omniteLayerZeroBridgeReceiver ) { console.warn( `system data for ${chainConfig.chainId} has already been loaded` ); return this._cachedSystemData[chainConfig.chainId]; } try { this._cachedSystemData[chainConfig.chainId] = await SystemContext.getSystemConfig( chainConfig.chainId ); return this._cachedSystemData[chainConfig.chainId]; } catch (err) { console.error({ chainId: chainConfig.chainId, systemContext: chainConfig.contractAddress.systemContext, err, }); throw err; } }) ); /** * Return contract addresses from system context for given chain id * * @param {string} chainId */ getSystemData(chainId: string) { return this._cachedSystemData[chainId]; } getChainConfig(chainId: ChainId) { const config = this._availableWallets.find( (v) => v.chainId === chainId ); if (!config) { throw new Error('No chain config for given chain id'); } return config; } } export default new Config();