feat: update ethers@6

fix: bigint errors

fix: TS errors

fix: gasLimit fetch

fix: review comments

fix: contracts errors

chore: bump rc version

fix: revert wrong fix

fix: ts config

fix: ts config

fix: ts config

feat: update signer
This commit is contained in:
Oleg Nechiporenko
2023-09-07 00:12:07 +03:00
parent 27c426e815
commit c2a4084158
38 changed files with 409 additions and 608 deletions

View File

@@ -1,7 +1,7 @@
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import clone from 'just-clone';
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v6';
import { APPROVE_ERC20_GAS_LIMIT, NATIVE_CURRENCY_PRECISION } from './constants/index.js';
import type {
AggregatedBalanceRequirement, ApproveFix, Asset, BalanceIssue, BalanceRequirement, Source,
@@ -24,7 +24,7 @@ export default class BalanceGuard {
private readonly nativeCryptocurrency: Asset;
private readonly provider: ethers.providers.Provider;
private readonly provider: ethers.Provider;
private readonly signer: ethers.Signer;
@@ -33,7 +33,7 @@ export default class BalanceGuard {
constructor(
balances: Partial<Record<string, Record<'exchange' | 'wallet', BigNumber>>>,
nativeCryptocurrency: Asset,
provider: ethers.providers.Provider,
provider: ethers.Provider,
signer: ethers.Signer,
logger?: (message: string) => void,
) {
@@ -62,10 +62,10 @@ export default class BalanceGuard {
const walletAddress = await this.signer.getAddress();
const tokenContract = ERC20__factory
.connect(assetAddress, this.provider);
const unsignedTx = await tokenContract.populateTransaction
.approve(
const unsignedTx = await tokenContract.approve
.populateTransaction(
spenderAddress,
ethers.constants.MaxUint256,
ethers.MaxUint256,
);
unsignedTx.from = walletAddress;
let resetRequired = false;
@@ -117,21 +117,23 @@ export default class BalanceGuard {
const approve = async ({ spenderAddress, targetAmount }: ApproveFix) => {
const bnTargetAmount = new BigNumber(targetAmount);
const unsignedApproveTx = await tokenContract
.populateTransaction
.approve(
.approve.populateTransaction(
spenderAddress,
bnTargetAmount.isZero()
? '0' // Reset
: ethers.constants.MaxUint256, // Infinite approve
: ethers.MaxUint256, // Infinite approve
);
const walletAddress = await this.signer.getAddress();
const nonce = await this.provider.getTransactionCount(walletAddress, 'pending');
const gasPrice = await this.provider.getGasPrice();
const { gasPrice, maxFeePerGas } = await this.provider.getFeeData();
const network = await this.provider.getNetwork();
if (gasPrice !== null && maxFeePerGas !== null) {
unsignedApproveTx.gasPrice = gasPrice;
unsignedApproveTx.maxFeePerGas = maxFeePerGas;
}
unsignedApproveTx.chainId = network.chainId;
unsignedApproveTx.gasPrice = gasPrice;
unsignedApproveTx.nonce = nonce;
unsignedApproveTx.from = walletAddress;
const gasLimit = await this.provider.estimateGas(unsignedApproveTx);
@@ -139,7 +141,7 @@ export default class BalanceGuard {
this.logger?.('Approve transaction signing...');
const signedTx = await this.signer.signTransaction(unsignedApproveTx);
const txResponse = await this.provider.sendTransaction(signedTx);
const txResponse = await this.provider.broadcastTransaction(signedTx);
this.logger?.(`${issue.asset.name} approve transaction sent ${txResponse.hash}. Waiting for confirmation...`);
await txResponse.wait();
this.logger?.(`${issue.asset.name} approve transaction confirmed.`);
@@ -230,7 +232,7 @@ export default class BalanceGuard {
const lackAmount = remainingBalance.exchange.abs(); // e.g. -435.234234 to 434.234234
let denormalizedAllowance: BigNumber;
if (asset.address === ethers.constants.AddressZero) {
if (asset.address === ethers.ZeroAddress) {
denormalizedAllowance = remainingBalance.wallet;
} else {
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
@@ -272,13 +274,11 @@ export default class BalanceGuard {
asset.address,
spenderAddress,
);
const gasPriceWei = await this.provider.getGasPrice();
const approveTransactionCost = ethers.BigNumber
.from(APPROVE_ERC20_GAS_LIMIT)
.mul(gasPriceWei);
const { gasPrice: gasPriceWei } = await this.provider.getFeeData();
const approveTransactionCost = BigInt(APPROVE_ERC20_GAS_LIMIT) * (gasPriceWei ?? 0n);
const denormalizedApproveTransactionCost = denormalizeNumber(
approveTransactionCost,
NATIVE_CURRENCY_PRECISION
BigInt(NATIVE_CURRENCY_PRECISION)
);
requiredApproves.items = {
@@ -329,7 +329,7 @@ export default class BalanceGuard {
.reduce<BigNumber>((p, c) => (c !== undefined ? p.plus(c) : p), new BigNumber(0));
let denormalizedAllowance: BigNumber;
if (asset.address === ethers.constants.AddressZero) {
if (asset.address === ethers.ZeroAddress) {
denormalizedAllowance = remainingBalance.wallet;
} else {
if (spenderAddress === undefined) throw new Error(`Spender address is required for ${asset.name}`);
@@ -365,13 +365,11 @@ export default class BalanceGuard {
asset.address,
spenderAddress,
);
const gasPriceWei = await this.provider.getGasPrice();
const approveTransactionCost = ethers.BigNumber
.from(APPROVE_ERC20_GAS_LIMIT)
.mul(gasPriceWei);
const { gasPrice: gasPriceWei } = await this.provider.getFeeData();
const approveTransactionCost = BigInt(APPROVE_ERC20_GAS_LIMIT) * (gasPriceWei ?? 0n);
const denormalizedApproveTransactionCost = denormalizeNumber(
approveTransactionCost,
NATIVE_CURRENCY_PRECISION
BigInt(NATIVE_CURRENCY_PRECISION)
);
requiredApproves.items = {