Orion, Orion Unit, Configuration (#40)

* Refactoring

* Better docs

* Bump

* ESLint standard

* Fix

* Bumo

* VerboseOrionUnitConfig to types

* Docs improvements

* Docs improvements. Orion default env
This commit is contained in:
Aleksandr Kraiz
2023-02-08 14:51:58 +04:00
committed by GitHub
parent bf33fbe4f0
commit b2f3cdf5fb
31 changed files with 4386 additions and 2816 deletions

View File

@@ -85,11 +85,11 @@ export default async function getSwapInfo({
if (options?.poolOnly) {
route = 'pool';
} else if (
swapExchanges !== undefined
&& poolExchangesList.length > 0
&& swapExchanges.length === 1
&& firstSwapExchange
&& poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
swapExchanges !== undefined &&
poolExchangesList.length > 0 &&
swapExchanges.length === 1 &&
firstSwapExchange &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
) {
route = 'pool';
} else {

View File

@@ -170,11 +170,11 @@ export default async function swapMarket({
options?.logger?.('Swap is through pool (because "poolOnly" option is true)');
route = 'pool';
} else if (
swapExchanges !== undefined
&& poolExchangesList.length > 0
&& swapExchanges.length === 1
&& firstSwapExchange
&& poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
swapExchanges !== undefined &&
poolExchangesList.length > 0 &&
swapExchanges.length === 1 &&
firstSwapExchange &&
poolExchangesList.some((poolExchange) => poolExchange === firstSwapExchange)
) {
options?.logger?.(`Swap is through pool [via ${firstSwapExchange}] (detected by "exchanges" field)`);
route = 'pool';

View File

@@ -1,34 +1,21 @@
import { ethers } from 'ethers';
import { OrionAggregator } from '../services/OrionAggregator';
import OrionAnalytics from '../services/OrionAnalytics';
import { OrionBlockchain } from '../services/OrionBlockchain';
import { PriceFeed } from '../services/PriceFeed';
import { SupportedChainId } from '../types';
import type { SupportedChainId, VerboseOrionUnitConfig } from '../types';
import Exchange from './Exchange';
import FarmingManager from './FarmingManager';
import { chains, envs } from '../config';
import { isValidChainId } from '../utils';
import { ReferralSystem } from '../services/ReferralSystem';
import { chains } from '../config';
const orionAnalyticsUrl = 'https://trade.orionprotocol.io';
// type KnownConfig = {
// env: string;
// chainId: SupportedChainId;
// }
// type OrionUnitConfig = KnownConfig | VerboseOrionUnitConfig;
type Options = {
api?: string;
nodeJsonRpc?: string;
services?: {
orionBlockchain?: {
api?: string;
},
orionAggregator?: {
api?: string;
},
priceFeed?: {
api?: string;
},
}
};
export default class OrionUnit {
public readonly env: string;
// public readonly env?: string;
public readonly networkCode: string;
@@ -42,122 +29,69 @@ export default class OrionUnit {
public readonly priceFeed: PriceFeed;
public readonly orionAnalytics: OrionAnalytics;
public readonly exchange: Exchange;
public readonly farmingManager: FarmingManager;
public readonly apiUrl: string;
public readonly referralSystem: ReferralSystem;
constructor(
chain: string,
env: string,
options?: Options,
) {
let chainId: SupportedChainId;
let customApi: string | undefined;
let customRpc: string | undefined;
let chainInfo: typeof chains[SupportedChainId] | undefined;
if (!(env in envs)) {
if (env === 'custom') {
if (!options?.api) throw new Error('Your env is custom. You should provide api url in options');
const { api } = options;
customApi = api;
if (isValidChainId(chain)) {
chainId = chain;
chainInfo = chains[chain];
} else throw new Error('Your chainId is invalid');
} else {
throw new Error(`Env '${env}' not found. Available environments is: ${Object.keys(envs).join(', ')}`);
}
} else {
const envInfo = envs[env];
const envNetworks = envInfo?.networks;
if (envNetworks === undefined) throw new Error('Env networks is undefined (constructor)');
if (isValidChainId(chain)) chainId = chain;
else {
const targetChains = Object
.keys(chains)
.filter(isValidChainId)
.filter((ch) => {
const chInfo = chains[ch];
if (!chInfo) return false;
return (chInfo.chainId in envNetworks)
&& (chInfo.code.toLowerCase() === chain.toLowerCase());
});
if (targetChains.length !== 1) {
throw new Error(
targetChains.length > 1
? 'Ambiguation detected. '
+ `Found ${targetChains.length} chain ids [${targetChains.join(', ')}] for chain name '${chain}' in env '${env}'. Expected 1.`
: `Chains not found for chain name '${chain}' in env '${env}'.`,
);
}
const firstTargetChain = targetChains[0];
if (firstTargetChain === undefined) throw new Error('First target chain is undefined');
chainId = firstTargetChain;
}
if (!(chainId in envNetworks)) {
throw new Error(`Chain '${chainId}' not found. `
+ `Available chains in selected environment (${env}) is: ${Object.keys(envNetworks).join(', ')}`);
}
const envNetworkInfo = envNetworks[chainId];
chainInfo = chains[chainId];
if (!envNetworkInfo) throw new Error('Env network info is required');
customApi = envNetworkInfo.api;
customRpc = envNetworkInfo.rpc;
}
// constructor(config: KnownConfig);
// constructor(config: VerboseConfig);
constructor(config: VerboseOrionUnitConfig) {
const chainInfo = chains[config.chainId];
if (!chainInfo) throw new Error('Chain info is required');
this.chainId = chainId;
// if ('env' in config)
// this.env = config.env;
this.chainId = config.chainId;
this.networkCode = chainInfo.code;
this.provider = new ethers.providers.StaticJsonRpcProvider(options?.nodeJsonRpc ?? customRpc ?? chainInfo.rpc);
this.env = env;
this.apiUrl = customApi;
this.provider = new ethers.providers.StaticJsonRpcProvider(config.nodeJsonRpc);
this.orionBlockchain = new OrionBlockchain(
options?.services?.orionBlockchain?.api
?? options?.api
?? customApi,
);
const oaUrl = new URL(options?.services?.orionAggregator?.api ?? options?.api ?? customApi);
const oaWsProtocol = oaUrl.protocol === 'https:' ? 'wss' : 'ws';
const orionAggregatorWsUrl = `${oaWsProtocol}://${oaUrl.host + (oaUrl.pathname === '/' ? '' : oaUrl.pathname)}/v1`;
this.orionBlockchain = new OrionBlockchain(config.services.orionBlockchain.http);
this.orionAggregator = new OrionAggregator(
options?.services?.orionAggregator?.api ?? `${options?.api ?? customApi}/backend`,
orionAggregatorWsUrl,
config.services.orionAggregator.http,
config.services.orionAggregator.ws,
);
this.priceFeed = new PriceFeed(
options?.services?.priceFeed?.api
?? `${options?.api ?? customApi}/price-feed`,
);
this.orionAnalytics = new OrionAnalytics(orionAnalyticsUrl);
this.priceFeed = new PriceFeed(config.services.priceFeed.api);
this.exchange = new Exchange(this);
this.farmingManager = new FarmingManager(this);
this.referralSystem = new ReferralSystem(options?.api ?? customApi, env);
}
get siblings() {
const envInfo = envs[this.env];
const envNetworks = envInfo?.networks;
// get siblings() {
// if (!this.env) throw new Error('Sibling is not available, because env is not set');
if (envNetworks === undefined) throw new Error('Env networks is undefined (siblings)');
// const envInfo = envs[this.env];
// const envNetworks = envInfo?.networks;
const siblingsNetworks = Object
.keys(envNetworks)
.filter(isValidChainId)
.filter((chainId) => chainId !== this.chainId);
return siblingsNetworks.map((chainId) => new OrionUnit(chainId, this.env));
}
// if (envNetworks === undefined) throw new Error('Env networks is undefined (siblings)');
// const orionUnits: OrionUnit[] = [];
// Object
// .entries(envNetworks)
// .forEach(([chainId, config]) => {
// if (!isValidChainId(chainId)) throw new Error('Invalid chainId');
// if (chainId !== this.chainId) {
// const chainConfig = chains[chainId];
// if (!chainConfig) throw new Error('Chain config is required');
// const orionUnit = new OrionUnit({
// api: config.api,
// chainId,
// nodeJsonRpc: chainConfig.rpc ?? config.rpc,
// services: {
// orionBlockchain: {
// http: config.api + config.services.blockchain.http,
// },
// orionAggregator: {
// http: config.api + config.services.aggregator.http,
// ws: config.api + config.services.aggregator.ws,
// },
// priceFeed: {
// api: config.api + config.services.priceFeed.all,
// },
// },
// });
// orionUnits.push(orionUnit);
// }
// });
// return orionUnits;
// }
}