diff --git a/app/scripts/controllers/bridge-status/bridge-status-controller.ts b/app/scripts/controllers/bridge-status/bridge-status-controller.ts index 502077543c5d..18010ae0de3d 100644 --- a/app/scripts/controllers/bridge-status/bridge-status-controller.ts +++ b/app/scripts/controllers/bridge-status/bridge-status-controller.ts @@ -1,7 +1,6 @@ import { StateMetadata } from '@metamask/base-controller'; import { StaticIntervalPollingController } from '@metamask/polling-controller'; import { Hex } from '@metamask/utils'; -import { Numeric } from '../../../../shared/modules/Numeric'; // eslint-disable-next-line import/no-restricted-paths import { StartPollingForBridgeTxStatusArgs, @@ -9,6 +8,7 @@ import { StatusTypes, BridgeStatusControllerState, } from '../../../../shared/types/bridge-status'; +import { decimalToPrefixedHex } from '../../../../shared/modules/conversion.utils'; import { BRIDGE_STATUS_CONTROLLER_NAME, DEFAULT_BRIDGE_STATUS_CONTROLLER_STATE, @@ -143,9 +143,7 @@ export default class BridgeStatusController extends StaticIntervalPollingControl refuel: Boolean(historyItem.quote.refuel), }; - const hexSourceChainId = new Numeric(statusRequest.srcChainId, 10) - .toPrefixedHexString() - .toLowerCase() as `0x${string}`; + const hexSourceChainId = decimalToPrefixedHex(statusRequest.srcChainId); const networkClientId = this.messagingSystem.call( 'NetworkController:findNetworkClientIdByChainId', hexSourceChainId, @@ -171,9 +169,7 @@ export default class BridgeStatusController extends StaticIntervalPollingControl initialDestAssetBalance, targetContractAddress, } = startPollingForBridgeTxStatusArgs; - const hexSourceChainId = new Numeric(statusRequest.srcChainId, 10) - .toPrefixedHexString() - .toLowerCase() as `0x${string}`; + const hexSourceChainId = decimalToPrefixedHex(statusRequest.srcChainId); const { bridgeStatusState } = this.state; const { address: account } = this.#getSelectedAccount(); @@ -277,14 +273,12 @@ export default class BridgeStatusController extends StaticIntervalPollingControl const bridgeHistoryItem = this.state.bridgeStatusState.txHistory[sourceTxHash]; - const hexSourceChainId = new Numeric( + const hexSourceChainId = decimalToPrefixedHex( bridgeHistoryItem.quote.srcChainId, - 10, - ).toPrefixedHexString() as `0x${string}`; - const hexDestChainId = new Numeric( + ); + const hexDestChainId = decimalToPrefixedHex( bridgeHistoryItem.quote.destChainId, - 10, - ).toPrefixedHexString() as `0x${string}`; + ); return ( bridgeHistoryItem.account === address && diff --git a/shared/modules/conversion.utils.ts b/shared/modules/conversion.utils.ts index 75da336eb8e6..9e24a8427cce 100644 --- a/shared/modules/conversion.utils.ts +++ b/shared/modules/conversion.utils.ts @@ -184,6 +184,12 @@ export function decimalToHex(decimal: number | string | BigNumber | BN) { return new Numeric(decimal, 10).toBase(16).toString(); } +export function decimalToPrefixedHex( + decimal: number | string | BigNumber | BN, +): Hex { + return new Numeric(decimal, 10).toPrefixedHexString() as Hex; +} + export function hexToDecimal(hexValue: number | string | BigNumber | BN) { return new Numeric(hexValue, 16).toBase(10).toString(); } diff --git a/ui/pages/bridge/hooks/useAddToken.ts b/ui/pages/bridge/hooks/useAddToken.ts index 597149b16e49..0a2ed28ef10a 100644 --- a/ui/pages/bridge/hooks/useAddToken.ts +++ b/ui/pages/bridge/hooks/useAddToken.ts @@ -1,6 +1,5 @@ import { useDispatch, useSelector } from 'react-redux'; import { NetworkConfiguration } from '@metamask/network-controller'; -import { Numeric } from '../../../../shared/modules/Numeric'; import { QuoteResponse } from '../types'; import { getNetworkConfigurationsByChainId, @@ -8,6 +7,7 @@ import { } from '../../../selectors'; import { FEATURED_RPCS } from '../../../../shared/constants/network'; import { addToken, addNetwork } from '../../../store/actions'; +import { decimalToPrefixedHex } from '../../../../shared/modules/conversion.utils'; export default function useAddToken() { const dispatch = useDispatch(); @@ -34,9 +34,9 @@ export default function useAddToken() { const addDestToken = async (quoteResponse: QuoteResponse) => { // Look up the destination chain - const hexDestChainId = new Numeric(quoteResponse.quote.destChainId, 10) - .toPrefixedHexString() - .toLowerCase() as `0x${string}`; + const hexDestChainId = decimalToPrefixedHex( + quoteResponse.quote.destChainId, + ); const foundDestNetworkConfig: NetworkConfiguration | undefined = networkConfigurations[hexDestChainId]; let addedDestNetworkConfig: NetworkConfiguration | undefined; diff --git a/ui/pages/bridge/hooks/useHandleApprovalTx.ts b/ui/pages/bridge/hooks/useHandleApprovalTx.ts index c6c28eab91cc..c0fb7812cce0 100644 --- a/ui/pages/bridge/hooks/useHandleApprovalTx.ts +++ b/ui/pages/bridge/hooks/useHandleApprovalTx.ts @@ -3,9 +3,9 @@ import { Hex } from '@metamask/utils'; import { BigNumber } from 'bignumber.js'; import { TxData, QuoteResponse, FeeType } from '../types'; import { isEthUsdt, getEthUsdtResetData } from '../bridge.util'; -import { Numeric } from '../../../../shared/modules/Numeric'; import { ETH_USDT_ADDRESS } from '../../../../shared/constants/bridge'; import { getBridgeERC20Allowance } from '../../../ducks/bridge/actions'; +import { decimalToPrefixedHex } from '../../../../shared/modules/conversion.utils'; import useHandleTx from './useHandleTx'; export default function useHandleApprovalTx() { @@ -59,10 +59,7 @@ export default function useHandleApprovalTx() { approval: TxData; quoteResponse: QuoteResponse; }) => { - const hexChainId = new Numeric( - approval.chainId, - 10, - ).toPrefixedHexString() as `0x${string}`; + const hexChainId = decimalToPrefixedHex(approval.chainId); // On Ethereum, we need to reset the allowance to 0 for USDT first if we need to set a new allowance // https://www.google.com/url?q=https://docs.unizen.io/trade-api/before-you-get-started/token-allowance-management-for-non-updatable-allowance-tokens&sa=D&source=docs&ust=1727386175513609&usg=AOvVaw3Opm6BSJeu7qO0Ve5iLTOh diff --git a/ui/pages/bridge/hooks/useHandleTx.ts b/ui/pages/bridge/hooks/useHandleTx.ts index a4cbf631c338..ffd378449bc7 100644 --- a/ui/pages/bridge/hooks/useHandleTx.ts +++ b/ui/pages/bridge/hooks/useHandleTx.ts @@ -14,7 +14,7 @@ import { import { getGasFeeEstimates } from '../../../ducks/metamask/metamask'; import { checkNetworkAndAccountSupports1559 } from '../../../selectors'; import { ChainId } from '../types'; -import { Numeric } from '../../../../shared/modules/Numeric'; +import { decimalToPrefixedHex } from '../../../../shared/modules/conversion.utils'; export default function useHandleTx() { const dispatch = useDispatch(); @@ -42,10 +42,7 @@ export default function useHandleTx() { meta: Partial; }; }) => { - const hexChainId = new Numeric( - txParams.chainId, - 10, - ).toPrefixedHexString() as `0x${string}`; + const hexChainId = decimalToPrefixedHex(txParams.chainId); const { maxFeePerGas, maxPriorityFeePerGas } = await getTxGasEstimates({ networkAndAccountSupports1559,