This commit is contained in:
Aleksandr Kraiz
2023-02-13 19:35:58 +04:00
parent 0ed3611a31
commit 40fa9d6b52
9 changed files with 31 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@orionprotocol/sdk",
"version": "0.17.7-rc.0",
"version": "0.17.7-rc.1",
"description": "Orion Protocol SDK",
"main": "./lib/esm/index.js",
"module": "./lib/esm/index.js",

View File

@@ -11,12 +11,13 @@ import arrayEquals from './utils/arrayEquals';
export default class BalanceGuard {
private readonly balances: Partial<
Record<
string,
Record<
'exchange' | 'wallet',
BigNumber>
>
Record<
string,
Record<
'exchange' | 'wallet',
BigNumber
>
>
>;
public readonly requirements: BalanceRequirement[] = [];
@@ -50,7 +51,7 @@ export default class BalanceGuard {
// Used for case feeAsset === assetOut
setExtraBalance(assetName: string, amount: BigNumber.Value, source: Source) {
const assetBalance = this.balances[assetName];
if (assetBalance === undefined) throw Error(`Can't set extra balance. Asset ${assetName} not found`);
if (!assetBalance) throw Error(`Can't set extra balance. Asset ${assetName} not found`);
assetBalance[source] = assetBalance[source].plus(amount);
}
@@ -86,7 +87,7 @@ export default class BalanceGuard {
item.spenderAddress === curr.spenderAddress,
);
if (aggregatedBalanceRequirement !== undefined) {
if (aggregatedBalanceRequirement) {
aggregatedBalanceRequirement.items = {
...aggregatedBalanceRequirement.items,
[curr.reason]: curr.amount,
@@ -193,7 +194,7 @@ export default class BalanceGuard {
exchangeOnlyAggregatedRequirements.forEach(({ asset, items }) => {
const remainingBalance = remainingBalances[asset.name];
if (remainingBalance === undefined) throw new Error(`No ${asset.name} balance`);
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
@@ -220,7 +221,7 @@ export default class BalanceGuard {
await Promise.all(exchangePlusWalletAggregatedRequirements
.map(async ({ asset, spenderAddress, items }) => {
const remainingBalance = remainingBalances[asset.name];
if (remainingBalance === undefined) throw new Error(`No ${asset.name} balance`);
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
@@ -323,7 +324,7 @@ export default class BalanceGuard {
await Promise.all(walletTokensAggregatedRequirements
.map(async ({ asset, spenderAddress, items }) => {
const remainingBalance = remainingBalances[asset.name];
if (remainingBalance === undefined) throw new Error(`No ${asset.name} balance`);
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
@@ -414,7 +415,7 @@ export default class BalanceGuard {
walletNativeAggregatedRequirements.forEach(({ asset, items }) => {
const remainingBalance = remainingBalances[asset.name];
if (remainingBalance === undefined) throw new Error(`No ${asset.name} balance`);
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values({ ...items, ...requiredApproves.items })
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));

View File

@@ -42,7 +42,7 @@ export default class Orion {
let config: EnvConfig;
if (typeof envOrConfig === 'string') {
const envConfig = envs[envOrConfig];
if (envConfig === undefined) {
if (!envConfig) {
throw new Error(`Invalid environment: ${envOrConfig}. Available environments: ${Object.keys(envs).join(', ')}`);
}
this.env = envOrConfig;
@@ -52,7 +52,7 @@ export default class Orion {
networks: Object.entries(envConfig.networks).map(([chainId, networkConfig]) => {
if (!isValidChainId(chainId)) throw new Error(`Invalid chainId: ${chainId}`);
const chainConfig = chains[chainId];
if (chainConfig === undefined) {
if (!chainConfig) {
throw new Error(`Chain config not found: ${chainId}. Available chains: ${Object.keys(chains).join(', ')}`);
}
@@ -81,7 +81,7 @@ export default class Orion {
}, {}),
};
if (overrides !== undefined) {
if (overrides) {
// Recursive merge of config and overrides. Ignore undefined values.
config = merge(config, overrides);
}
@@ -96,7 +96,7 @@ export default class Orion {
.reduce<Partial<Record<SupportedChainId, OrionUnit>>>((acc, [chainId, networkConfig]) => {
if (!isValidChainId(chainId)) throw new Error(`Invalid chainId: ${chainId}`);
const chainConfig = chains[chainId];
if (chainConfig === undefined) throw new Error(`Invalid chainId: ${chainId}`);
if (!chainConfig) throw new Error(`Chain config not found: ${chainId}`);
const orionUnit = new OrionUnit({
// env: networkConfig.env,
@@ -123,7 +123,7 @@ export default class Orion {
} else {
unit = this.unitsArray.find((u) => u.networkCode === networkCodeOrChainId);
}
if (unit === undefined) {
if (!unit) {
throw new Error(
`Invalid network code: ${networkCodeOrChainId}. ` +
`Available network codes: ${this.unitsArray.map((u) => u.networkCode).join(', ')}`);

View File

@@ -90,7 +90,7 @@ export default class FarmingManager {
const poolsConfig = await simpleFetch(this.orionUnit.orionBlockchain.getPoolsConfig)();
const pool = poolsConfig.pools[poolName];
if (pool === undefined) throw new Error(`Pool ${poolName} not found`);
if (!pool) throw new Error(`Pool ${poolName} not found`);
const pairContract = IUniswapV2Pair__factory
.connect(pool.lpTokenAddress, this.orionUnit.provider);
@@ -183,7 +183,7 @@ export default class FarmingManager {
if (assetAIsNativeCurrency || assetBIsNativeCurrency) {
const contractBalance = balances[nativeCryptocurrency]?.exchange;
if (contractBalance === undefined) throw new Error(`No balance for '${nativeCryptocurrency}'`);
if (!contractBalance) throw new Error(`No balance for '${nativeCryptocurrency}'`);
const nativeAssetAmount = assetBIsNativeCurrency ? assetBAmount : assetAAmount;
if (nativeAssetAmount.gt(contractBalance)) {
@@ -242,7 +242,7 @@ export default class FarmingManager {
const poolsConfig = await simpleFetch(this.orionUnit.orionBlockchain.getPoolsConfig)();
const pool = poolsConfig.pools[poolName];
if (pool === undefined) throw new Error(`Pool ${poolName} not found`);
if (!pool) throw new Error(`Pool ${poolName} not found`);
const walletAddress = await signer.getAddress();

View File

@@ -42,7 +42,7 @@ export default class OrionUnit {
constructor(config: VerboseOrionUnitConfig) {
this.config = config;
const chainInfo = chains[config.chainId];
if (chainInfo === undefined) throw new Error('Chain info is required');
if (!chainInfo) throw new Error('Chain info is required');
// if ('env' in config)
// this.env = config.env;

View File

@@ -29,7 +29,7 @@ export default async function fetchWithValidation<DataOut, DataIn, ErrorOut, Err
...options ?? {},
headers: {
'Cache-Control': 'no-store, max-age=0',
...(options !== undefined ? options.headers : {}),
...(options ? options.headers : {}),
},
}), (e) => {
if (e instanceof Error) {
@@ -111,7 +111,7 @@ export default async function fetchWithValidation<DataOut, DataIn, ErrorOut, Err
const json: unknown = jsonResult.value;
if (response.status >= 400) { // Client error
if (errorSchema !== undefined) {
if (errorSchema) {
const serverError = errorSchema.safeParse(json);
if (serverError.success) {
return err({

View File

@@ -206,10 +206,10 @@ class OrionAggregatorWS {
type: T,
subscription: Subscription[T],
) {
if (this.ws === undefined) this.init();
if (!this.ws) this.init();
const isExclusive = exclusiveSubscriptions.some((t) => t === type);
const subs = this.subscriptions[type];
if (isExclusive && (subs !== undefined) && Object.keys(subs).length > 0) {
if (isExclusive && subs && Object.keys(subs).length > 0) {
throw new Error(`Subscription '${type}' already exists. Please unsubscribe first.`);
}
@@ -527,7 +527,7 @@ class OrionAggregatorWS {
const balances = (json.b)
? Object.entries(json.b)
.reduce<Partial<Record<string, Balance>>>((prev, [asset, assetBalances]) => {
if (assetBalances === undefined) return prev;
if (!assetBalances) return prev;
const [tradable, reserved, contract, wallet, allowance] = assetBalances;
prev[asset] = {
@@ -539,7 +539,7 @@ class OrionAggregatorWS {
: {};
switch (json.k) { // message kind
case 'i': { // initial
const fullOrders = (json.o !== undefined)
const fullOrders = json.o
? json.o.reduce<Array<z.infer<typeof fullOrderSchema>>>((prev, o) => {
prev.push(o);

View File

@@ -121,7 +121,7 @@ export default class PriceFeedSubscription<T extends SubscriptionType = Subscrip
};
this.ws.onclose = () => {
if (this.heartbeatInterval !== undefined) clearInterval(this.heartbeatInterval);
if (this.heartbeatInterval) clearInterval(this.heartbeatInterval);
if (!this.isClosedIntentionally) this.init();
};

View File

@@ -6,7 +6,7 @@ export default function isNetworkCodeInEnvironment(networkCode: string, env: str
}
const envInfo = envs[env];
const envNetworks = envInfo?.networks;
if (envNetworks === undefined) throw new Error('Env networks is undefined (isNetworkCodeInEnvironment)');
if (!envNetworks) throw new Error('Env networks is undefined (isNetworkCodeInEnvironment)');
return Object.values(chains)
.some((chain) => chain.code.toLowerCase() === networkCode.toLowerCase() &&