Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ramp): add non evm balance #13948

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/components/UI/Ramp/Views/BuildQuote/BuildQuote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,11 @@ const BuildQuote = () => {
selectedAddress,
);

const { balanceFiat, balanceBN } = useBalance(
const { balanceFiat, balanceBN, balance } = useBalance(
selectedAsset
? {
chainId: selectedAsset.network.chainId,
assetId: selectedAsset.assetId,
address: selectedAsset.address,
decimals: selectedAsset.decimals,
}
Expand Down Expand Up @@ -813,7 +815,7 @@ const BuildQuote = () => {
color={TextColor.Alternative}
>
{strings('fiat_on_ramp_aggregator.current_balance')}:{' '}
{addressBalance}
{selectedAsset?.assetId && balance ? balance : addressBalance}
{balanceFiat ? ` ≈ ${balanceFiat}` : null}
</Text>
</Row>
Expand Down
43 changes: 29 additions & 14 deletions app/components/UI/Ramp/hooks/useBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
selectConversionRate,
selectCurrentCurrency,
} from '../../../../selectors/currencyRateController';
import { selectSelectedInternalAccountFormattedAddress } from '../../../../selectors/accountsController';
import {
selectSelectedInternalAccount,
selectSelectedInternalAccountFormattedAddress,
} from '../../../../selectors/accountsController';
import { selectContractBalances } from '../../../../selectors/tokenBalancesController';
import { selectContractExchangeRates } from '../../../../selectors/tokenRatesController';
import { selectEvmChainId } from '../../../../selectors/networkController';
Expand All @@ -18,6 +21,8 @@ import {
toHexadecimal,
weiToFiat,
} from '../../../../util/number';
import { selectMultichainBalances } from '../../../../selectors/multichain';
import { Hex } from '@metamask/utils';

const defaultReturn = {
balance: null,
Expand All @@ -26,51 +31,61 @@ const defaultReturn = {
};

interface Asset {
address: string;
address?: string;
decimals: number;
chainId?: string;
assetId?: string;
}

export default function useBalance(asset?: Asset) {
const accountsByChainId = useSelector(selectAccountsByChainId);
const multiChainTokenBalance = useSelector(selectMultichainBalances);
const chainId = useSelector(selectEvmChainId);
const selectedAddress = useSelector(
selectSelectedInternalAccountFormattedAddress,
);
const selectedAccount = useSelector(selectSelectedInternalAccount);
const conversionRate = useSelector(selectConversionRate);
const currentCurrency = useSelector(selectCurrentCurrency);
const tokenExchangeRates = useSelector(selectContractExchangeRates);
const balances = useSelector(selectContractBalances);

if (!asset) {
return defaultReturn;
}

const assetAddress = safeToChecksumAddress(asset.address);

if (!assetAddress) {
if (
!asset ||
(!asset.address && !asset.assetId) ||
!selectedAddress ||
!selectedAccount
) {
return defaultReturn;
}

let balance, balanceFiat, balanceBN;
if (assetAddress === NATIVE_ADDRESS) {
if (asset.assetId) {
// CAIP19 asset identifier
const assetCaip19Identifier = `${asset.chainId}/${asset.assetId}`;
const assetBalance =
multiChainTokenBalance?.[selectedAccount.id]?.[assetCaip19Identifier];
balance = `${assetBalance?.amount ?? ''} ${
assetBalance?.unit ?? ''
}`.trim();
} else if (asset.address === NATIVE_ADDRESS) {
// Chain id should exist in accountsByChainId in AccountTrackerController at this point in time
if (!accountsByChainId[toHexadecimal(chainId)]) {
return defaultReturn;
}

balance = renderFromWei(
//@ts-expect-error - TODO: Ramps team
accountsByChainId[toHexadecimal(chainId)][selectedAddress]?.balance,
);

balanceBN = hexToBN(
//@ts-expect-error - TODO: Ramps team
accountsByChainId[toHexadecimal(chainId)][selectedAddress]?.balance,
);

balanceFiat = weiToFiat(balanceBN, conversionRate, currentCurrency);
} else {
const exchangeRate = tokenExchangeRates?.[assetAddress]?.price;
} else if (asset.address) {
const assetAddress = safeToChecksumAddress(asset.address);
const exchangeRate = tokenExchangeRates?.[assetAddress as Hex]?.price;
balance =
assetAddress && assetAddress in balances
? renderFromTokenMinimalUnit(
Expand Down
Loading