Skip to content

Commit

Permalink
[NOJIRA] fix utils imports from ethers (#923)
Browse files Browse the repository at this point in the history
Co-authored-by: Zach Couchman <zachary.couchman@immutable.com>
  • Loading branch information
ZacharyCouchman and Zach Couchman committed Oct 3, 2023
1 parent fd21570 commit 3836508
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 36 deletions.
30 changes: 15 additions & 15 deletions packages/checkout/sdk/src/smartCheckout/fees/fees.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseUnits } from 'ethers/lib/utils';
import { utils } from 'ethers';
import { calculateFees } from './fees';
import { BuyToken, ItemType } from '../../types';
import { CheckoutErrorType } from '../../errors';
Expand All @@ -9,7 +9,7 @@ jest.mock('../actions');
describe.only('orderbook fees', () => {
it('should calculate the fees as a percentageDecimal', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.025 },
recipient: '0x222',
Expand All @@ -25,7 +25,7 @@ describe.only('orderbook fees', () => {

it('should return empty array when fee is zero', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0 },
recipient: '0x222',
Expand All @@ -38,7 +38,7 @@ describe.only('orderbook fees', () => {

it('should return empty array when fee is smaller than 6 decimal places', async () => {
const decimals = 6;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.0000001 },
recipient: '0x222',
Expand All @@ -51,7 +51,7 @@ describe.only('orderbook fees', () => {

it('should return the fee when the amount and fee is small', async () => {
const decimals = 18;
const amount = parseUnits('0.000001', 18).toString();
const amount = utils.parseUnits('0.000001', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.000001 },
recipient: '0x222',
Expand All @@ -67,7 +67,7 @@ describe.only('orderbook fees', () => {

it('should return empty array when the amount and fee are small enough to go below 1 wei', async () => {
const decimals = 18;
const amount = parseUnits('0.0000000000001', 18).toString();
const amount = utils.parseUnits('0.0000000000001', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.000001 },
recipient: '0x222',
Expand All @@ -80,7 +80,7 @@ describe.only('orderbook fees', () => {

it('should work when the amount is a decimal', async () => {
const decimals = 18;
const amount = parseUnits('0.5', 18).toString();
const amount = utils.parseUnits('0.5', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.025 },
recipient: '0x222',
Expand All @@ -96,7 +96,7 @@ describe.only('orderbook fees', () => {

it('should calculate the fees with multiple percentageDecimals', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.025 },
recipient: '0x222',
Expand All @@ -118,7 +118,7 @@ describe.only('orderbook fees', () => {

it('should calculate the fees with multiple percentageDecimals that add to the max fee amount', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.7 },
recipient: '0x222',
Expand All @@ -140,7 +140,7 @@ describe.only('orderbook fees', () => {

it('should fail to calculate the fees as a percentageDecimal because the fee is too high', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 1.000001 },
recipient: '0x222',
Expand All @@ -163,7 +163,7 @@ describe.only('orderbook fees', () => {

it('should calculate the fees as a token amount', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { token: '1' },
recipient: '0x222',
Expand All @@ -179,7 +179,7 @@ describe.only('orderbook fees', () => {

it('should work when the amount is a decimal', async () => {
const decimals = 18;
const amount = parseUnits('0.5', 18).toString();
const amount = utils.parseUnits('0.5', 18).toString();
const makerFees = [{
amount: { token: '0.1' },
recipient: '0x222',
Expand All @@ -195,7 +195,7 @@ describe.only('orderbook fees', () => {

it('should calculate the fees with multiple token amounts', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { token: '1' },
recipient: '0x222',
Expand All @@ -217,7 +217,7 @@ describe.only('orderbook fees', () => {

it('should fail to calculate the fees as a token value because the fee is too high', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { token: '11' },
recipient: '0x222',
Expand All @@ -240,7 +240,7 @@ describe.only('orderbook fees', () => {

it('should calculate the fees with a combination of percentageDecimals and token amounts', async () => {
const decimals = 18;
const amount = parseUnits('10', 18).toString();
const amount = utils.parseUnits('10', 18).toString();
const makerFees = [{
amount: { percentageDecimal: 0.025 },
recipient: '0x222',
Expand Down
5 changes: 2 additions & 3 deletions packages/checkout/sdk/src/smartCheckout/fees/fees.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { FeeValue } from '@imtbl/orderbook';
import { BigNumber } from 'ethers';
import { parseUnits } from 'ethers/lib/utils';
import { BigNumber, utils } from 'ethers';
import { FeePercentage, FeeToken, OrderFee } from '../../types/fees';
import { CheckoutError, CheckoutErrorType } from '../../errors';

Expand Down Expand Up @@ -28,7 +27,7 @@ const calculateFeesToken = (
decimals: number,
): BigNumber => {
const feeToken = orderFee.amount as FeeToken;
const bnFeeAmount = parseUnits(feeToken.token, decimals);
const bnFeeAmount = utils.parseUnits(feeToken.token, decimals);
return bnFeeAmount;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Web3Provider } from '@ethersproject/providers';
import { parseUnits } from 'ethers/lib/utils';
import { utils } from 'ethers';
import { getTokenContract } from '../../instance';
import { getItemRequirementsFromRequirements } from './itemRequirements';
import {
Expand Down Expand Up @@ -42,18 +42,18 @@ describe('itemRequirements', () => {
expect(itemRequirements).toEqual([
{
type: ItemType.NATIVE,
amount: parseUnits('2.0', 18),
amount: utils.parseUnits('2.0', 18),
},
{
type: ItemType.ERC20,
spenderAddress: '0xSPENDER',
amount: parseUnits('1.5', 18),
amount: utils.parseUnits('1.5', 18),
contractAddress: '0xCONTRACTADDRESS1',
},
{
type: ItemType.ERC20,
spenderAddress: '0xSPENDER',
amount: parseUnits('0.5', 18),
amount: utils.parseUnits('0.5', 18),
contractAddress: '0xCONTRACTADDRESS2',
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Web3Provider } from '@ethersproject/providers';
import { parseUnits } from 'ethers/lib/utils';
import { utils } from 'ethers';
import {
ERC20ABI,
ERC20Item,
Expand Down Expand Up @@ -33,12 +33,12 @@ export async function getItemRequirementsFromRequirements(
if (itemRequirementParam.type === ItemType.NATIVE) {
return {
...itemRequirementParam,
amount: parseUnits(itemRequirementParam.amount, 18),
amount: utils.parseUnits(itemRequirementParam.amount, 18),
} as NativeItem;
} if (itemRequirementParam.type === ItemType.ERC20) {
return {
...itemRequirementParam,
amount: parseUnits(itemRequirementParam.amount, decimals[index]),
amount: utils.parseUnits(itemRequirementParam.amount, decimals[index]),
} as ERC20Item;
}
return itemRequirementParam as ERC721Item;
Expand Down
5 changes: 2 additions & 3 deletions packages/checkout/sdk/src/smartCheckout/sell/sell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
PrepareListingResponse,
constants,
} from '@imtbl/orderbook';
import { BigNumber, Contract } from 'ethers';
import { parseUnits } from 'ethers/lib/utils';
import { BigNumber, Contract, utils } from 'ethers';
import {
BuyToken, SellOrder, SellResult, SellStatusType,
} from '../../types/sell';
Expand Down Expand Up @@ -47,7 +46,7 @@ export const getBuyToken = (
buyToken: BuyToken,
decimals: number = 18,
): ERC20Item | NativeItem => {
const bnAmount = parseUnits(buyToken.amount, decimals);
const bnAmount = utils.parseUnits(buyToken.amount, decimals);

if (buyToken.type === ItemType.NATIVE) {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WalletProviderName } from '@imtbl/checkout-sdk';
import { isAddress } from 'ethers/lib/utils';
import { utils } from 'ethers';
import { amountInputValidation } from './amountInputValidations';
import { NATIVE } from '../constants';

Expand All @@ -22,5 +22,5 @@ export function isValidAddress(address: string | undefined) {
if (address === undefined) return false;
if (address === '') return true;
if (address === NATIVE || address === NATIVE.toLowerCase()) return true;
return isAddress(address);
return utils.isAddress(address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from 'react';
import { ApproveDepositBridgeResponse, BridgeDepositResponse } from '@imtbl/bridge-sdk';
import { BigNumber, utils } from 'ethers';
import { parseEther } from 'ethers/lib/utils';
import { amountInputValidation } from '../../../lib/validations/amountInputValidations';
import { BridgeContext } from '../context/BridgeContext';
import { SharedViews, ViewActions, ViewContext } from '../../../context/view-context/ViewContext';
Expand Down Expand Up @@ -229,9 +228,9 @@ export function BridgeForm(props: BridgeFormProps) {
}

const tokenIsEth = isNativeToken(token?.token.address);
const gasAmount = parseEther(gasFee.length !== 0 ? gasFee : '0');
const gasAmount = utils.parseEther(gasFee.length !== 0 ? gasFee : '0');
const additionalAmount = tokenIsEth && !Number.isNaN(parseFloat(amount))
? parseEther(amount)
? utils.parseEther(amount)
: BigNumber.from('0');

return gasAmount.add(additionalAmount).gt(ethBalance.balance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { BigNumber, utils } from 'ethers';
import { TokenInfo } from '@imtbl/checkout-sdk';
import { TransactionResponse } from '@imtbl/dex-sdk';
import { parseEther, parseUnits } from 'ethers/lib/utils';
import { text } from '../../../resources/text/textConfig';
import { amountInputValidation as textInputValidator } from '../../../lib/validations/amountInputValidations';
import { SwapContext } from '../context/SwapContext';
Expand Down Expand Up @@ -443,9 +442,9 @@ export function SwapForm({ data }: SwapFromProps) {
if (!imxBalance) return true;

const fromTokenIsImx = fromToken?.address === IMX_ADDRESS_ZKEVM;
const gasAmount = parseEther(gasFeeValue.length !== 0 ? gasFeeValue : '0');
const gasAmount = utils.parseEther(gasFeeValue.length !== 0 ? gasFeeValue : '0');
const additionalAmount = fromTokenIsImx && !Number.isNaN(parseFloat(fromAmount))
? parseUnits(fromAmount, fromToken?.decimals || 18)
? utils.parseUnits(fromAmount, fromToken?.decimals || 18)
: BigNumber.from('0');

return gasAmount.add(additionalAmount).gt(imxBalance.balance);
Expand Down

0 comments on commit 3836508

Please sign in to comment.