Skip to content

Commit

Permalink
fix: Change ethers.utils imports (#1915)
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbro-imx authored Jun 18, 2024
1 parent 30ab311 commit c3e3981
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AXELAR_SCAN_URL } from 'lib';
import { Transaction, TransactionStatus } from 'lib/clients';
import { CryptoFiatContext } from 'context/crypto-fiat-context/CryptoFiatContext';
import { calculateCryptoToFiat, getTokenImageByAddress, isNativeToken } from 'lib/utils';
import { formatUnits } from 'ethers/lib/utils';
import { utils } from 'ethers';
import { useTranslation } from 'react-i18next';
import { TransactionItem } from './TransactionItem';
import { KnownNetworkMap } from './transactionsType';
Expand Down Expand Up @@ -81,7 +81,7 @@ export function TransactionList({
const hash = transaction.blockchain_metadata.transaction_hash;
const tokens = knownTokenMap[transaction.details.from_chain];
const token = tokens[transaction.details.from_token_address.toLowerCase()];
const amount = formatUnits(transaction.details.amount, token.decimals);
const amount = utils.formatUnits(transaction.details.amount, token.decimals);
const fiat = calculateCryptoToFiat(amount, token.symbol, cryptoFiatState.conversions);

if (transaction.details.current_status.status === TransactionStatus.WITHDRAWAL_PENDING) {
Expand Down
9 changes: 4 additions & 5 deletions packages/internal/bridge/sdk/src/lib/axelarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { Address } from 'types';
import { WITHDRAW_SIG, NATIVE } from 'constants/bridges';
import { CHILD_ERC20 } from 'contracts/ABIs/ChildERC20';
import { withBridgeError, BridgeErrorType } from 'errors';
import { ethers } from 'ethers';
import { keccak256, defaultAbiCoder } from 'ethers/lib/utils';
import { ethers, utils } from 'ethers';
import { createContract } from 'contracts/createContract';
import { isWrappedIMX, getRootIMX } from './utils';

Expand All @@ -16,8 +15,8 @@ import { isWrappedIMX, getRootIMX } from './utils';
* @returns hash of payload and current time.
*/
export function genUniqueAxelarCommandId(payload: string) {
return keccak256(
defaultAbiCoder.encode(['bytes', 'uint256'], [payload, new Date().getTime()]),
return utils.keccak256(
utils.defaultAbiCoder.encode(['bytes', 'uint256'], [payload, new Date().getTime()]),
);
}

Expand All @@ -31,7 +30,7 @@ export function genAxelarWithdrawPayload(
recipient: string,
amount: string,
) {
return defaultAbiCoder.encode(
return utils.defaultAbiCoder.encode(
['bytes32', 'address', 'address', 'address', 'uint256'],
[WITHDRAW_SIG, rootToken, sender, recipient, amount],
);
Expand Down
18 changes: 9 additions & 9 deletions packages/internal/bridge/sdk/src/tokenBridge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable no-console */
/* eslint-disable class-methods-use-this */
import axios, { AxiosResponse } from 'axios';
import { ethers } from 'ethers';
import {
concat, defaultAbiCoder, hexValue, hexlify, keccak256, zeroPad,
} from 'ethers/lib/utils';
import { ethers, utils } from 'ethers';
import { ROOT_AXELAR_ADAPTOR } from 'contracts/ABIs/RootAxelarBridgeAdaptor';
import {
checkReceiver, validateBridgeReqArgs, validateChainConfiguration, validateChainIds,
Expand Down Expand Up @@ -853,7 +850,7 @@ export class TokenBridge {

// tx value for simulation mocked as amount + 1 wei for a native bridge and 1 wei for token bridges
// hexValue() is required to remove leading zeros, which tenderly does not support.
const txValue = (token.toUpperCase() !== NATIVE) ? '0x1' : hexValue(amount.add('1').toHexString());
const txValue = (token.toUpperCase() !== NATIVE) ? '0x1' : utils.hexValue(amount.add('1').toHexString());

simulations.push({
from: sender,
Expand Down Expand Up @@ -895,15 +892,18 @@ export class TokenBridge {
const sourceChain = getChildchain(destinationChainId);
const sourceAddress = ethers.utils.getAddress(getChildAdaptor(destinationChainId)).toString();
const destinationAddress = getRootAdaptor(destinationChainId);
const payloadHash = keccak256(payload);
const payloadHash = utils.keccak256(payload);

// Calculate slot key for given command ID.
const command = defaultAbiCoder.encode(
const command = utils.defaultAbiCoder.encode(
['bytes32', 'bytes32', 'string', 'string', 'address', 'bytes32'],
[SLOT_PREFIX_CONTRACT_CALL_APPROVED, commandId, sourceChain, sourceAddress, destinationAddress, payloadHash],
);
const commandHash = keccak256(command);
const slot = keccak256(concat([commandHash, hexlify(zeroPad(hexlify(SLOT_POS_CONTRACT_CALL_APPROVED), 32))]));
const commandHash = utils.keccak256(command);
const slot = utils.keccak256(utils.concat([
commandHash,
utils.hexlify(utils.zeroPad(utils.hexlify(SLOT_POS_CONTRACT_CALL_APPROVED), 32)),
]));

// Encode execute data
const axelarAdapterContract = await createContract(
Expand Down
14 changes: 7 additions & 7 deletions packages/internal/dex/sdk/src/lib/transactionUtils/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { isNative, toCurrencyAmount, toPublicAmount } from 'lib/utils';
import { QuoteResult } from 'lib/getQuotesForRoutes';
import { NativeTokenService, canUnwrapToken } from 'lib/nativeTokenService';
import { Coin, CoinAmount, Native } from 'types';
import { Interface } from 'ethers/lib/utils';
import { IImmutableSwapProxy, ImmutableSwapProxyInterface } from 'contracts/types/ImmutableSwapProxy';
import { ImmutableSwapProxy__factory } from 'contracts/types';
import { utils } from 'ethers';
import { SecondaryFee, TransactionDetails } from '../../types';
import { calculateGasFee } from './gas';
import { slippageToFraction } from './slippage';
Expand All @@ -30,8 +30,8 @@ function buildSinglePoolSwap(
route: Route<Uniswap.Token, Uniswap.Token>,
amountIn: string,
amountOut: string,
routerContract: Interface,
paymentsContract: Interface,
routerContract: utils.Interface,
paymentsContract: utils.Interface,
) {
const calldatas: string[] = [];

Expand Down Expand Up @@ -153,8 +153,8 @@ function buildMultiPoolSwap(
route: Route<Uniswap.Token, Uniswap.Token>,
amountIn: string,
amountOut: string,
routerContract: Interface,
paymentsContract: Interface,
routerContract: utils.Interface,
paymentsContract: utils.Interface,
) {
const path: string = encodeRouteToPath(route, trade.tradeType === Uniswap.TradeType.EXACT_OUTPUT);
const calldatas: string[] = [];
Expand Down Expand Up @@ -280,8 +280,8 @@ function buildSwapParameters(
trade: Trade<Uniswap.Token, Uniswap.Token, Uniswap.TradeType>,
secondaryFees: SecondaryFee[],
swapProxyContract: ImmutableSwapProxyInterface,
routerContract: Interface,
paymentsContract: Interface,
routerContract: utils.Interface,
paymentsContract: utils.Interface,
maximumAmountIn: string,
minimumAmountOut: string,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Accordion, Form } from 'react-bootstrap';
import { usePassportProvider } from '@/context/PassportProvider';
import WorkflowButton from '@/components/WorkflowButton';
import { RequestExampleProps, EnvironmentNames } from '@/types';
import { Interface } from 'ethers/lib/utils';
import { useImmutableProvider } from '@/context/ImmutableProvider';
import { utils } from 'ethers';

enum ApproveType {
NFTApprove = 'NFT_APPROVE',
Expand All @@ -33,7 +33,7 @@ function NFTApproval({ disabled, handleExampleSubmitted }: RequestExampleProps)
'function setApprovalForAll(address to, bool approved)',
'function approve(address to, uint256 tokenId)',
];
return new Interface(abi);
return new utils.Interface(abi);
}, []);

const [choosedApproveType, setChoosedApproveType] = useState<ApproveType>(ApproveType.NFTApprove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import React, {
useMemo,
useState,
} from 'react';
import { Interface } from 'ethers/lib/utils';
import InputGroup from 'react-bootstrap/InputGroup';
import {
Accordion, Form, Image, Table,
Expand All @@ -14,6 +13,7 @@ import { useImmutableProvider } from '@/context/ImmutableProvider';
import { usePassportProvider } from '@/context/PassportProvider';
import { NFT } from '@imtbl/generated-clients/dist/blockchain-data';
import WorkflowButton from '@/components/WorkflowButton';
import { utils } from 'ethers';

type GroupedAsset = {
contract_address: string;
Expand Down Expand Up @@ -50,7 +50,7 @@ function NFTTransfer({ disabled, handleExampleSubmitted }: RequestExampleProps)
'function safeTransferFrom(address from, address to, uint256 token_id)',
'function safeTransferFromBatch((address, address[], uint256[]))',
];
return new Interface(abi);
return new utils.Interface(abi);
}, []);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Accordion, Form } from 'react-bootstrap';
import { usePassportProvider } from '@/context/PassportProvider';
import WorkflowButton from '@/components/WorkflowButton';
import { RequestExampleProps, EnvironmentNames } from '@/types';
import { Interface } from 'ethers/lib/utils';
import { useImmutableProvider } from '@/context/ImmutableProvider';
import { utils } from 'ethers';

const getErc20DefaultContractAddress = (environment: EnvironmentNames) => {
switch (environment) {
Expand All @@ -27,7 +27,7 @@ function SpendingCapApproval({ disabled, handleExampleSubmitted }: RequestExampl
const abi = [
'function approve(address spender, uint256 amount)',
];
return new Interface(abi);
return new utils.Interface(abi);
}, []);
const [fromAddress, setFromAddress] = useState<string>('');
const [erc20ContractAddress, setErc20ContractAddress] = useState<string>(getErc20DefaultContractAddress(environment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import InputGroup from 'react-bootstrap/InputGroup';
import { usePassportProvider } from '@/context/PassportProvider';
import WorkflowButton from '@/components/WorkflowButton';
import { RequestExampleProps, EnvironmentNames } from '@/types';
import { Interface } from 'ethers/lib/utils';
import { useImmutableProvider } from '@/context/ImmutableProvider';
import { utils } from 'ethers';

const getErc20DefaultContractAddress = (environment: EnvironmentNames) => {
switch (environment) {
Expand Down Expand Up @@ -42,7 +42,7 @@ function TransferERC20({ disabled, handleExampleSubmitted }: RequestExampleProps
'function transfer(address to, uint256 amount)',
'function transferFrom(address from, address to, uint256 amount)',
];
return new Interface(abi);
return new utils.Interface(abi);
}, []);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import InputGroup from 'react-bootstrap/InputGroup';
import { usePassportProvider } from '@/context/PassportProvider';
import WorkflowButton from '@/components/WorkflowButton';
import { RequestExampleProps } from '@/types';
import { parseUnits } from 'ethers/lib/utils';
import { utils } from 'ethers';

function TransferImx({ disabled, handleExampleSubmitted }: RequestExampleProps) {
const [fromAddress, setFromAddress] = useState<string>('');
Expand All @@ -23,7 +23,7 @@ function TransferImx({ disabled, handleExampleSubmitted }: RequestExampleProps)
if (Number(rawAmount) < 0) {
setAmountConvertError(amountRange);
}
const value = parseUnits(rawAmount, imxTokenDecimal).toString();
const value = utils.parseUnits(rawAmount, imxTokenDecimal).toString();
setParams([{
from: fromAddress,
to: toAddress,
Expand Down
18 changes: 17 additions & 1 deletion packages/x-client/src/utils/crypto/crypto.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getDefaultProvider } from '@ethersproject/providers';
import { Wallet } from '@ethersproject/wallet';
import { signMessage, signRaw } from './crypto';
import { signMessage, signRaw, signRegisterEthAddress } from './crypto';
import { generateLegacyStarkPrivateKey } from '../stark/starkCurve';
import { createStarkSigner } from '../stark/starkSigner';

describe('signRaw()', () => {
test('Correctly signs string', async () => {
Expand Down Expand Up @@ -36,3 +38,17 @@ describe('signMessage()', () => {
});
});
});

describe('signRegisterEthAddress()', () => {
test('Correctly signs message', async () => {
const signer = new Wallet('5c7b4b5cad9a3fc7b1ba235a49cd74e615488a18b0d6a531739fd1062935104d');
const starkKey = await generateLegacyStarkPrivateKey(signer);
const starkSigner = createStarkSigner(starkKey);
const starkPublicKey = await createStarkSigner(starkKey).getAddress();
const ethSignature = await signRegisterEthAddress(starkSigner, signer.publicKey, starkPublicKey);
expect(ethSignature).toEqual(
// eslint-disable-next-line max-len
'0x03fd522589dd46a74cc7d004eea819c111294dbb1a1af9ccdb4d42565730134c05d33b76e5fe733314051420c2c76aea1f6d677394eb7b92369d2a2bab2674ab02603b092fe208e20cd5187a4ded4645e2f55605988c91579b3822bb63629e21',
);
});
});
4 changes: 2 additions & 2 deletions packages/x-client/src/utils/crypto/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import BN from 'bn.js';
import elliptic from 'elliptic';
import * as encUtils from 'enc-utils';
import { Signer } from '@ethersproject/abstract-signer';
import { solidityKeccak256 } from 'ethers/lib/utils';
import { utils } from 'ethers';
import { StarkSigner } from '../../types';
import { starkEcOrder } from '../stark/starkCurve';

Expand Down Expand Up @@ -105,7 +105,7 @@ export async function signRegisterEthAddress(
ethAddress: string,
starkPublicKey: string,
): Promise<string> {
const hash: string = solidityKeccak256(
const hash: string = utils.solidityKeccak256(
['string', 'address', 'uint256'],
['UserRegistration:', ethAddress, starkPublicKey],
);
Expand Down

0 comments on commit c3e3981

Please sign in to comment.