Skip to content

Commit

Permalink
chore: make a decToPrefixedHex util and use it in bridgeStatus code
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed Nov 22, 2024
1 parent 076a868 commit d1ac948
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
20 changes: 7 additions & 13 deletions app/scripts/controllers/bridge-status/bridge-status-controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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,
StatusRequest,
StatusTypes,
BridgeStatusControllerState,
} from '../../../../shared/types/bridge-status';
import { decimalToPrefixedHex } from '../../../../shared/modules/conversion.utils';
import {
BRIDGE_STATUS_CONTROLLER_NAME,
DEFAULT_BRIDGE_STATUS_CONTROLLER_STATE,
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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 &&
Expand Down
6 changes: 6 additions & 0 deletions shared/modules/conversion.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
8 changes: 4 additions & 4 deletions ui/pages/bridge/hooks/useAddToken.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useDispatch, useSelector } from 'react-redux';
import { NetworkConfiguration } from '@metamask/network-controller';
import { Numeric } from '../../../../shared/modules/Numeric';
import { QuoteResponse } from '../types';
import {
getNetworkConfigurationsByChainId,
getSelectedNetworkClientId,
} 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();
Expand All @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions ui/pages/bridge/hooks/useHandleApprovalTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions ui/pages/bridge/hooks/useHandleTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -42,10 +42,7 @@ export default function useHandleTx() {
meta: Partial<TransactionMeta>;
};
}) => {
const hexChainId = new Numeric(
txParams.chainId,
10,
).toPrefixedHexString() as `0x${string}`;
const hexChainId = decimalToPrefixedHex(txParams.chainId);

const { maxFeePerGas, maxPriorityFeePerGas } = await getTxGasEstimates({
networkAndAccountSupports1559,
Expand Down

0 comments on commit d1ac948

Please sign in to comment.