Skip to content

Commit

Permalink
fix(ramp): use hexToBN for ContractBalances balance (#10271)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachunei authored Jul 9, 2024
1 parent b2cce87 commit 4fa9dc3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions app/components/UI/Ramp/hooks/useBalance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hexToBN } from '@metamask/controller-utils';
import { useSelector } from 'react-redux';
import { hexToBN } from '@metamask/controller-utils';
import { NATIVE_ADDRESS } from '../../../../constants/on-ramp';
import { selectAccountsByChainId } from '../../../../selectors/accountTrackerController';
import {
Expand All @@ -19,13 +19,18 @@ import {
weiToFiat,
} from '../../../../util/number';

const defaultReturn = {
balance: null,
balanceFiat: null,
balanceBN: null,
};

interface Asset {
address: string;
decimals: number;
}

export default function useBalance(asset?: Asset) {
const assetAddress = safeToChecksumAddress(asset?.address);
const accountsByChainId = useSelector(selectAccountsByChainId);
const chainId = useSelector(selectChainId);
const selectedAddress = useSelector(
Expand All @@ -36,8 +41,14 @@ export default function useBalance(asset?: Asset) {
const tokenExchangeRates = useSelector(selectContractExchangeRates);
const balances = useSelector(selectContractBalances);

if (!asset || !assetAddress) {
return { balance: null, balanceFiat: null, balanceBN: null };
if (!asset) {
return defaultReturn;
}

const assetAddress = safeToChecksumAddress(asset.address);

if (!assetAddress) {
return defaultReturn;
}

let balance, balanceFiat, balanceBN;
Expand Down Expand Up @@ -66,7 +77,9 @@ export default function useBalance(asset?: Asset) {
currentCurrency,
);
balanceBN =
assetAddress && assetAddress in balances ? balances[assetAddress] : null;
assetAddress && assetAddress in balances
? hexToBN(balances[assetAddress])
: null;
}

return { balance, balanceFiat, balanceBN };
Expand Down
2 changes: 1 addition & 1 deletion app/util/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function resemblesAddress(address: string) {

export function safeToChecksumAddress(address: string) {
if (!address) return undefined;
return toChecksumAddress(address);
return toChecksumAddress(address) as Hex;
}

/**
Expand Down

0 comments on commit 4fa9dc3

Please sign in to comment.