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

@@ -8,6 +8,6 @@ export default function calculateNetworkFee(
) {
const networkFeeGwei = new BigNumber(gasPriceGwei).multipliedBy(gasLimit);
const bn = new BigNumber(ethers.utils.parseUnits(networkFeeGwei.toString(), 'gwei').toString());
const bn = new BigNumber(ethers.parseUnits(networkFeeGwei.toString(), 'gwei').toString());
return bn.div(new BigNumber(10).pow(NATIVE_CURRENCY_PRECISION)).toString();
}

View File

@@ -1,8 +1,8 @@
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v6/index.js';
import { ethers } from 'ethers';
import invariant from 'tiny-invariant';
const checkIsToken = async (address: string, provider?: ethers.providers.Provider) => {
const checkIsToken = async (address: string, provider?: ethers.Provider) => {
invariant(provider, 'No provider for token checking');
const tokenContract = ERC20__factory.connect(address, provider);
try {
@@ -12,7 +12,7 @@ const checkIsToken = async (address: string, provider?: ethers.providers.Provide
tokenContract.symbol(),
tokenContract.decimals(),
tokenContract.totalSupply(),
tokenContract.balanceOf(ethers.constants.AddressZero),
tokenContract.balanceOf(ethers.ZeroAddress),
],
);

View File

@@ -1,5 +1,4 @@
import { BigNumber } from 'bignumber.js';
import type { ethers } from 'ethers';
/**
* Converts normalized blockchain ("machine-readable") number to denormalized ("human-readable") number.
@@ -7,8 +6,8 @@ import type { ethers } from 'ethers';
* @param decimals Blockchain asset precision
* @returns BigNumber
*/
export default function denormalizeNumber(input: ethers.BigNumber, decimals: BigNumber.Value) {
const decimalsBN = new BigNumber(decimals);
export default function denormalizeNumber(input: bigint, decimals: bigint) {
const decimalsBN = new BigNumber(decimals.toString());
if (!decimalsBN.isInteger()) throw new Error(`Decimals '${decimalsBN.toString()}' is not an integer`);
return new BigNumber(input.toString()).div(new BigNumber(10).pow(decimalsBN));
}

View File

@@ -45,7 +45,7 @@ function isomorphicCryptoRandomBytes(size: number): Uint8Array {
const generateSecret = () => {
const RANDOM_BITS = 256;
const rand = isomorphicCryptoRandomBytes(RANDOM_BITS);
const secret = ethers.utils.keccak256(rand);
const secret = ethers.keccak256(rand);
return secret;
};

View File

@@ -8,7 +8,7 @@ export default function getAvailableFundsSources(
): Source[] {
switch (route) {
case 'aggregator':
if (assetAddress === ethers.constants.AddressZero) return ['exchange']; // We can't take native crypto from wallet
if (assetAddress === ethers.ZeroAddress) return ['exchange']; // We can't take native crypto from wallet
return ['exchange', 'wallet']; // We can take any token amount from exchange + wallet. Order is important!
case 'pool':
if (expenseType === 'network_fee') return ['wallet']; // Network fee is always taken from wallet

View File

@@ -1,5 +1,5 @@
import type { Exchange } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import type { Exchange } from '@orionprotocol/contracts/lib/ethers-v6';
import { ERC20__factory } from '@orionprotocol/contracts/lib/ethers-v6';
import type { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import { INTERNAL_PROTOCOL_PRECISION, NATIVE_CURRENCY_PRECISION } from '../constants/index.js';
@@ -12,11 +12,11 @@ export default async function getBalance(
assetAddress: string,
walletAddress: string,
exchangeContract: Exchange,
provider: ethers.providers.Provider,
provider: ethers.Provider,
) {
const assetIsNativeCryptocurrency = assetAddress === ethers.constants.AddressZero;
const assetIsNativeCryptocurrency = assetAddress === ethers.ZeroAddress;
let assetWalletBalance: ethers.BigNumber | undefined;
let assetWalletBalance: bigint | undefined;
let denormalizedAssetInWalletBalance: BigNumber | undefined;
@@ -28,10 +28,10 @@ export default async function getBalance(
denormalizedAssetInWalletBalance = denormalizeNumber(assetWalletBalance, assetDecimals);
} else {
assetWalletBalance = await provider.getBalance(walletAddress);
denormalizedAssetInWalletBalance = denormalizeNumber(assetWalletBalance, NATIVE_CURRENCY_PRECISION);
denormalizedAssetInWalletBalance = denormalizeNumber(assetWalletBalance, BigInt(NATIVE_CURRENCY_PRECISION));
}
const assetContractBalance = await exchangeContract.getBalance(assetAddress, walletAddress);
const denormalizedAssetInContractBalance = denormalizeNumber(assetContractBalance, INTERNAL_PROTOCOL_PRECISION);
const denormalizedAssetInContractBalance = denormalizeNumber(assetContractBalance, BigInt(INTERNAL_PROTOCOL_PRECISION));
const denormalizedAssetLockedBalanceResult = await aggregator.getLockedBalance(walletAddress, assetName);
if (denormalizedAssetLockedBalanceResult.isErr()) {
throw new Error(denormalizedAssetLockedBalanceResult.error.message);

View File

@@ -1,4 +1,4 @@
import type { Exchange } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import type { Exchange } from '@orionprotocol/contracts/lib/ethers-v6/index.js';
import type { BigNumber } from 'bignumber.js';
import type { ethers } from 'ethers';
import type { Aggregator } from '../services/Aggregator/index.js';
@@ -9,7 +9,7 @@ export default async (
aggregator: Aggregator,
walletAddress: string,
exchangeContract: Exchange,
provider: ethers.providers.Provider,
provider: ethers.Provider,
) => {
const balances = await Promise.all(
Object.entries(balancesRequired)

View File

@@ -11,7 +11,7 @@ const getNativeCryptocurrencyName = (assetToAddress: Partial<Record<string, stri
};
}, {});
const nativeCryptocurrencyName = addressToAssetName[ethers.constants.AddressZero];
const nativeCryptocurrencyName = addressToAssetName[ethers.ZeroAddress];
if (nativeCryptocurrencyName === undefined) {
throw new Error('Native cryptocurrency asset name is not found');
}

View File

@@ -1,25 +1,25 @@
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';
import { BigNumber } from "bignumber.js";
/**
* Converts denormalized ("human-readable") number to normalized ("machine-readable") number.
* @param input Any numeric value
* @param decimals Blockchain asset precision
* @param roundingMode Rounding mode
* @returns ethers.BigNumber
* @returns bigint
*/
export default function normalizeNumber(
input: BigNumber.Value,
decimals: BigNumber.Value,
roundingMode: BigNumber.RoundingMode,
roundingMode: BigNumber.RoundingMode
) {
const decimalsBN = new BigNumber(decimals);
if (!decimalsBN.isInteger()) throw new Error(`Decimals '${decimalsBN.toString()}' is not an integer`);
if (!decimalsBN.isInteger())
throw new Error(`Decimals '${decimalsBN.toString()}' is not an integer`);
const inputBN = new BigNumber(input);
return ethers.BigNumber.from(
return BigInt(
inputBN
.multipliedBy(new BigNumber(10).pow(decimals))
.integerValue(roundingMode)
.toString(),
.toString()
);
}

View File

@@ -1,13 +1,13 @@
import { Exchange__factory } from '@orionprotocol/contracts/lib/ethers-v5/index.js';
import { Exchange__factory } from '@orionprotocol/contracts/lib/ethers-v6';
import { ethers } from 'ethers';
import { z } from 'zod';
const swapThroughOrionPoolSchema = z.object({
name: z.literal('swapThroughOrionPool'),
args: z.tuple([
z.instanceof(ethers.BigNumber), // amount_spend
z.instanceof(ethers.BigNumber), // amount_receive
z.string().refine(ethers.utils.isAddress).array().nonempty(), // path
z.bigint(), // amount_spend
z.bigint(), // amount_receive
z.string().refine(ethers.isAddress).array().nonempty(), // path
z.boolean(), // is_exact_spend
]),
}).transform((data) => ({
@@ -21,34 +21,34 @@ const swapThroughOrionPoolSchema = z.object({
}));
const buyOrderSchema = z.tuple([ // buy order
z.string().refine(ethers.utils.isAddress), // senderAddress
z.string().refine(ethers.utils.isAddress), // matcherAddress
z.string().refine(ethers.utils.isAddress), // baseAsset
z.string().refine(ethers.utils.isAddress), // quoteAsset
z.string().refine(ethers.utils.isAddress), // matcherFeeAsset
z.instanceof(ethers.BigNumber), // amount
z.instanceof(ethers.BigNumber), // price
z.instanceof(ethers.BigNumber), // matcherFee
z.instanceof(ethers.BigNumber), // nonce
z.instanceof(ethers.BigNumber), // expiration
z.string().refine(ethers.isAddress), // senderAddress
z.string().refine(ethers.isAddress), // matcherAddress
z.string().refine(ethers.isAddress), // baseAsset
z.string().refine(ethers.isAddress), // quoteAsset
z.string().refine(ethers.isAddress), // matcherFeeAsset
z.bigint(), // amount
z.bigint(), // price
z.bigint(), // matcherFee
z.bigint(), // nonce
z.bigint(), // expiration
z.literal(1), // buySide
z.boolean(), // isPersonalSign
z.string().refine(ethers.utils.isHexString), // signature
z.string().refine(ethers.isHexString), // signature
]);
const sellOrderSchema = z.tuple([ // sell orer
z.string().refine(ethers.utils.isAddress), // senderAddress
z.string().refine(ethers.utils.isAddress), // matcherAddress
z.string().refine(ethers.utils.isAddress), // baseAsset
z.string().refine(ethers.utils.isAddress), // quoteAsset
z.string().refine(ethers.utils.isAddress), // matcherFeeAsset
z.instanceof(ethers.BigNumber), // amount
z.instanceof(ethers.BigNumber), // price
z.instanceof(ethers.BigNumber), // matcherFee
z.instanceof(ethers.BigNumber), // nonce
z.instanceof(ethers.BigNumber), // expiration
z.string().refine(ethers.isAddress), // senderAddress
z.string().refine(ethers.isAddress), // matcherAddress
z.string().refine(ethers.isAddress), // baseAsset
z.string().refine(ethers.isAddress), // quoteAsset
z.string().refine(ethers.isAddress), // matcherFeeAsset
z.bigint(), // amount
z.bigint(), // price
z.bigint(), // matcherFee
z.bigint(), // nonce
z.bigint(), // expiration
z.literal(0), // buySide
z.boolean(), // isPersonalSign
z.string().refine(ethers.utils.isHexString), // signature
z.string().refine(ethers.isHexString), // signature
]);
const toOrder = <T extends z.infer<typeof buyOrderSchema> | z.infer<typeof sellOrderSchema>>(data: T) => ({
@@ -71,9 +71,9 @@ const fillThroughOrionPoolSchema = z.object({
name: z.literal('fillThroughOrionPool'),
args: z.tuple([
sellOrderSchema,
z.instanceof(ethers.BigNumber), // filled amount
z.instanceof(ethers.BigNumber), // blockchainFee
z.string().refine(ethers.utils.isAddress).array().nonempty(), // path
z.bigint(), // filled amount
z.bigint(), // blockchainFee
z.string().refine(ethers.isAddress).array().nonempty(), // path
]),
}).transform((data) => ({
name: data.name,
@@ -90,8 +90,8 @@ const fillOrdersSchema = z.object({
args: z.tuple([
buyOrderSchema,
sellOrderSchema,
z.instanceof(ethers.BigNumber), // filledPrice
z.instanceof(ethers.BigNumber), // filledAmount
z.bigint(), // filledPrice
z.bigint(), // filledAmount
]),
}).transform((data) => ({
name: data.name,