Linter standard (#49)

* Impl

* Fix

* Fix

* Bump

* Bump

* Fix

* Bump
This commit is contained in:
Aleksandr Kraiz
2023-02-14 00:34:37 +04:00
committed by GitHub
parent 7040df6142
commit c12a4e8e7a
48 changed files with 964 additions and 594 deletions

View File

@@ -2,7 +2,6 @@ import BigNumber from 'bignumber.js';
import { ethers } from 'ethers';
import clone from 'just-clone';
import { ERC20__factory } from '@orionprotocol/contracts';
import { utils } from '.';
import { APPROVE_ERC20_GAS_LIMIT, NATIVE_CURRENCY_PRECISION } from './constants';
import type {
AggregatedBalanceRequirement, ApproveFix, Asset, BalanceIssue, BalanceRequirement, Source,
@@ -14,11 +13,12 @@ export default class BalanceGuard {
private readonly balances: Partial<
Record<
string,
Record<
'exchange' | 'wallet',
BigNumber>
Record<
'exchange' | 'wallet',
BigNumber
>
>
>;
>;
public readonly requirements: BalanceRequirement[] = [];
@@ -109,7 +109,7 @@ export default class BalanceGuard {
}, []);
}
private fixAllAutofixableBalanceIssues = async (
private readonly fixAllAutofixableBalanceIssues = async (
balanceIssues: BalanceIssue[],
) => {
const fixBalanceIssue = async (issue: BalanceIssue) => {
@@ -146,8 +146,8 @@ export default class BalanceGuard {
};
await issue.fixes?.reduce(async (promise, item) => {
await promise;
if (item.type === 'byApprove') return approve(item);
return promise;
if (item.type === 'byApprove') { await approve(item); return; }
await promise;
}, Promise.resolve());
};
@@ -155,7 +155,7 @@ export default class BalanceGuard {
await autofixableBalanceIssues.reduce(async (promise, item) => {
await promise;
return fixBalanceIssue(item);
await fixBalanceIssue(item);
}, Promise.resolve());
return balanceIssues.filter((item) => !autofixableBalanceIssues.includes(item));
@@ -196,18 +196,18 @@ export default class BalanceGuard {
const remainingBalance = remainingBalances[asset.name];
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c ? p.plus(c) : p), new BigNumber(0));
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
remainingBalance.exchange = remainingBalance.exchange.minus(itemsAmountSum);
if (remainingBalance.exchange.lt(0)) {
const lackAmount = remainingBalance.exchange.abs();
const exchangeBalance = this.balances?.[asset.name]?.exchange;
const exchangeBalance = this.balances[asset.name]?.exchange;
balanceIssues.push({
asset,
sources: ['exchange'],
message: `Not enough ${asset.name} on exchange balance. ` +
`Needed: ${itemsAmountSum.toString()}, available: ${(exchangeBalance ?? '[UNDEFINED]')?.toString()}. ` +
`Needed: ${itemsAmountSum.toString()}, available: ${(exchangeBalance ?? '[UNDEFINED]').toString()}. ` +
`You need to deposit at least ${lackAmount.toString()} ${asset.name} into exchange contract`,
});
}
@@ -223,7 +223,7 @@ export default class BalanceGuard {
const remainingBalance = remainingBalances[asset.name];
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c ? p.plus(c) : p), new BigNumber(0));
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
remainingBalance.exchange = remainingBalance.exchange.minus(itemsAmountSum);
if (remainingBalance.exchange.lt(0)) {
@@ -233,7 +233,7 @@ export default class BalanceGuard {
if (asset.address === ethers.constants.AddressZero) {
denormalizedAllowance = remainingBalance.wallet;
} else {
if (!spenderAddress) throw new Error(`Spender address is required for ${asset.name}`);
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
const tokenContract = ERC20__factory.connect(asset.address, this.provider);
const tokenDecimals = await tokenContract.decimals();
const tokenAllowance = await tokenContract.allowance(walletAddress, spenderAddress);
@@ -257,17 +257,17 @@ export default class BalanceGuard {
const approveIsHelpful = approveAvailable.gte(lackAmount);
const targetApprove = approvedWalletBalance.plus(lackAmount);
const exchangeBalance = this.balances?.[asset.name]?.exchange;
const exchangeBalance = this.balances[asset.name]?.exchange;
const available = exchangeBalance?.plus(approvedWalletBalance);
const issueMessage = `Not enough ${asset.name} on exchange + wallet balance. ` +
`Needed: ${itemsAmountSum.toString()}, available: ${(available ?? '[UNDEFINED]')?.toString()} ` +
`(exchange: ${(exchangeBalance ?? '[UNKNOWN]')?.toString()}, available (approved): ${approvedWalletBalance.toString()}).` +
`Needed: ${itemsAmountSum.toString()}, available: ${(available ?? '[UNDEFINED]').toString()} ` +
`(exchange: ${(exchangeBalance ?? '[UNKNOWN]').toString()}, available (approved): ${approvedWalletBalance.toString()}).` +
` ${approveIsHelpful
? `You need approve at least ${lackAmount.toString()} ${asset.name}`
: 'Approve is not helpful'}`;
if (approveIsHelpful) {
if (!spenderAddress) throw new Error(`Spender address is required for ${asset.name}`);
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
const resetRequired = await this.checkResetRequired(
asset.address,
spenderAddress,
@@ -276,8 +276,10 @@ export default class BalanceGuard {
const approveTransactionCost = ethers.BigNumber
.from(APPROVE_ERC20_GAS_LIMIT)
.mul(gasPriceWei);
const denormalizedApproveTransactionCost = utils
.denormalizeNumber(approveTransactionCost, NATIVE_CURRENCY_PRECISION);
const denormalizedApproveTransactionCost = denormalizeNumber(
approveTransactionCost,
NATIVE_CURRENCY_PRECISION
);
requiredApproves.items = {
...requiredApproves.items,
@@ -292,10 +294,10 @@ export default class BalanceGuard {
fixes: [
...resetRequired
? [{
type: 'byApprove' as const,
targetAmount: 0,
spenderAddress,
}]
type: 'byApprove' as const,
targetAmount: 0,
spenderAddress,
}]
: [],
{
type: 'byApprove',
@@ -324,13 +326,13 @@ export default class BalanceGuard {
const remainingBalance = remainingBalances[asset.name];
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values(items)
.reduce<BigNumber>((p, c) => (c ? p.plus(c) : p), new BigNumber(0));
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
let denormalizedAllowance: BigNumber;
if (asset.address === ethers.constants.AddressZero) {
denormalizedAllowance = remainingBalance.wallet;
} else {
if (!spenderAddress) throw new Error(`Spender address is required for ${asset.name}`);
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
const tokenContract = ERC20__factory.connect(asset.address, this.provider);
const tokenDecimals = await tokenContract.decimals();
const tokenAllowance = await tokenContract.allowance(walletAddress, spenderAddress);
@@ -358,7 +360,7 @@ export default class BalanceGuard {
? `You need approve at least ${lackAmount.toString()} ${asset.name}`
: 'Approve is not helpful'}`;
if (approveIsHelpful) {
if (!spenderAddress) throw new Error(`Spender address is required for ${asset.name}`);
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
const resetRequired = await this.checkResetRequired(
asset.address,
spenderAddress,
@@ -367,8 +369,10 @@ export default class BalanceGuard {
const approveTransactionCost = ethers.BigNumber
.from(APPROVE_ERC20_GAS_LIMIT)
.mul(gasPriceWei);
const denormalizedApproveTransactionCost = utils
.denormalizeNumber(approveTransactionCost, NATIVE_CURRENCY_PRECISION);
const denormalizedApproveTransactionCost = denormalizeNumber(
approveTransactionCost,
NATIVE_CURRENCY_PRECISION
);
requiredApproves.items = {
...requiredApproves.items,
@@ -383,10 +387,10 @@ export default class BalanceGuard {
fixes: [
...resetRequired
? [{
type: 'byApprove' as const,
targetAmount: 0,
spenderAddress,
}]
type: 'byApprove' as const,
targetAmount: 0,
spenderAddress,
}]
: [],
{
type: 'byApprove',
@@ -414,7 +418,7 @@ export default class BalanceGuard {
if (!remainingBalance) throw new Error(`No ${asset.name} balance`);
const itemsAmountSum = Object.values({ ...items, ...requiredApproves.items })
.reduce<BigNumber>((p, c) => (c ? p.plus(c) : p), new BigNumber(0));
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
remainingBalance.wallet = remainingBalance.wallet.minus(itemsAmountSum);
if (remainingBalance.wallet.lt(0)) {
@@ -428,7 +432,7 @@ export default class BalanceGuard {
}
});
if (fixAutofixable) {
if (fixAutofixable !== undefined && fixAutofixable) {
const unfixed = await this.fixAllAutofixableBalanceIssues(balanceIssues);
if (unfixed.length > 0) throw new Error(`Balance issues: ${unfixed.map((issue, i) => `${i + 1}. ${issue.message}`).join('\n')}`);
} else if (balanceIssues.length > 0) {