mirror of
https://github.com/orionprotocol/sdk.git
synced 2026-03-17 00:31:34 +03:00
Added possibility to init OrionUnit by code
This commit is contained in:
@@ -57,7 +57,7 @@
|
||||
"chainId": "137",
|
||||
"label": "Polygon Mainnet",
|
||||
"shortName": "Polygon",
|
||||
"code": "poly",
|
||||
"code": "polygon",
|
||||
"baseCurrencyName": "MATIC",
|
||||
"rpc": "https://polygon-rpc.com/",
|
||||
"explorer": "https://polygonscan.com/"
|
||||
@@ -66,7 +66,7 @@
|
||||
"chainId": "80001",
|
||||
"label": "Polygon Mumbai",
|
||||
"shortName": "Polygon Mumbai",
|
||||
"code": "poly",
|
||||
"code": "polygon",
|
||||
"baseCurrencyName": "MATIC",
|
||||
"rpc": "https://rpc-mumbai.matic.today",
|
||||
"explorer": "https://mumbai.polygonscan.com/"
|
||||
|
||||
@@ -1,27 +1,52 @@
|
||||
import OrionUnit from './OrionUnit';
|
||||
import { isValidChainId } from './utils';
|
||||
import { chains, envs } from './config';
|
||||
import { SupportedChainId } from './types';
|
||||
|
||||
export default function initOrionUnit(chain: string, env: string) {
|
||||
if (!isValidChainId(chain)) throw new Error(`Chain '${chain}' is not valid.`);
|
||||
if (!(env in envs)) {
|
||||
throw new Error(`Env '${env}' not found. Available environments is: ${Object.keys(envs).join(', ')}`);
|
||||
}
|
||||
|
||||
if (!(env in envs)) throw new Error(`Env '${env}' not found. Available environments is: ${Object.keys(envs).join(', ')}`);
|
||||
const envInfo = envs[env];
|
||||
const envNetworks = envInfo?.networks;
|
||||
let chainId: SupportedChainId;
|
||||
|
||||
if (!(chain in envNetworks)) {
|
||||
throw new Error(`Chain '${chain}' not found. `
|
||||
if (isValidChainId(chain)) chainId = chain;
|
||||
else {
|
||||
const targetChains = Object
|
||||
.keys(chains)
|
||||
.filter(isValidChainId)
|
||||
.filter((ch) => {
|
||||
const chainInfo = chains[ch];
|
||||
if (!chainInfo) return false;
|
||||
return (chainInfo.chainId in envNetworks)
|
||||
&& (chainInfo.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}'.`,
|
||||
);
|
||||
}
|
||||
[chainId] = targetChains;
|
||||
}
|
||||
|
||||
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[chain];
|
||||
const chainInfo = chains[chain];
|
||||
const envNetworkInfo = envNetworks[chainId];
|
||||
const chainInfo = chains[chainId];
|
||||
|
||||
if (!envNetworkInfo) throw new Error('Env network info is required');
|
||||
if (!chainInfo) throw new Error('Chain info is required');
|
||||
|
||||
return new OrionUnit(
|
||||
chainInfo.chainId,
|
||||
chainId,
|
||||
envNetworkInfo.rpc ?? chainInfo.rpc,
|
||||
env,
|
||||
envNetworkInfo.api,
|
||||
|
||||
Reference in New Issue
Block a user