Skip to content
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
27 changes: 27 additions & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
BRIDGE_CONTROLLER_NAME,
BridgeUserAction,
BridgeBackgroundAction,
calcLatestSrcBalance,
} from '@metamask/bridge-controller';

import {
Expand Down Expand Up @@ -2615,6 +2616,19 @@
this.networkController.getNetworkConfigurationByNetworkClientId.bind(
this.networkController,
),
getBalanceAmount: async (selectedAddress, tokenAddress, chainId) => {
const networkClientId =
await this.networkController.findNetworkClientIdByChainId(chainId);
const networkClient =
await this.networkController.getNetworkClientById(networkClientId);
const balance = await calcLatestSrcBalance(
networkClient.provider,
selectedAddress,
tokenAddress,
chainId,
);
return balance?.toString();
},
// PreferencesController
setSelectedAddress: (address) => {
const account = this.accountsController.getAccountByAddress(address);
Expand Down Expand Up @@ -3174,6 +3188,19 @@
this.controllerMessenger,
`${BRIDGE_CONTROLLER_NAME}:${BridgeBackgroundAction.FETCH_QUOTES}`,
),
getBalanceAmount: async (selectedAddress, tokenAddress, chainId) => {
const networkClientId =
await this.networkController.findNetworkClientIdByChainId(chainId);
const networkClient =
await this.networkController.getNetworkClientById(networkClientId);
const balance = await calcLatestSrcBalance(

Check failure on line 3196 in app/scripts/metamask-controller.js

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Duplicate key 'getBalanceAmount'
networkClient.provider,
selectedAddress,
tokenAddress,
chainId,
);
return balance?.toString();
},

// Bridge Tx submission
[BridgeStatusAction.SUBMIT_TX]: this.controllerMessenger.call.bind(
Expand Down
31 changes: 30 additions & 1 deletion shared/constants/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
BRIDGE_DEV_API_BASE_URL,
BRIDGE_PROD_API_BASE_URL,
ChainId,
formatChainIdToCaip,
} from '@metamask/bridge-controller';
import { MultichainNetworks } from './multichain/networks';
import { CHAIN_IDS, NETWORK_TO_NAME_MAP } from './network';
import {
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP,
CHAIN_IDS,
NETWORK_TO_NAME_MAP,
} from './network';

const ALLOWED_MULTICHAIN_BRIDGE_CHAIN_IDS = [
MultichainNetworks.SOLANA,
Expand Down Expand Up @@ -56,6 +61,18 @@
? BRIDGE_DEV_API_BASE_URL
: BRIDGE_PROD_API_BASE_URL;

export const BRIDGE_CHAIN_ID_TO_NETWORK_IMAGE_MAP: Record<

Check failure on line 64 in shared/constants/bridge.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Multiple exports of name 'BRIDGE_CHAIN_ID_TO_NETWORK_IMAGE_MAP'
(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number],
string
> = ALLOWED_BRIDGE_CHAIN_IDS.reduce(
(acc, chainId) => {
acc[formatChainIdToCaip(chainId)] =
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP[chainId];
return acc;
},
{} as Record<(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number], string>,
);

export const ETH_USDT_ADDRESS = '0xdac17f958d2ee523a2206206994597c13d831ec7';
export const NETWORK_TO_SHORT_NETWORK_NAME_MAP: Record<
AllowedBridgeChainIds,
Expand Down Expand Up @@ -95,6 +112,18 @@
///: END:ONLY_INCLUDE_IF
};

export const BRIDGE_CHAIN_ID_TO_NETWORK_IMAGE_MAP: Record<

Check failure on line 115 in shared/constants/bridge.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

Multiple exports of name 'BRIDGE_CHAIN_ID_TO_NETWORK_IMAGE_MAP'
(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number],
string
> = ALLOWED_BRIDGE_CHAIN_IDS.reduce(
(acc, chainId) => {
acc[formatChainIdToCaip(chainId)] =
CHAIN_ID_TO_NETWORK_IMAGE_URL_MAP[chainId];
return acc;
},
{} as Record<(typeof ALLOWED_BRIDGE_CHAIN_IDS_IN_CAIP)[number], string>,
);

export const STATIC_METAMASK_BASE_URL = 'https://static.cx.metamask.io';

export const BRIDGE_CHAINID_COMMON_TOKEN_PAIR: Partial<
Expand Down
15 changes: 15 additions & 0 deletions shared/constants/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { toEvmCaipChainId } from '@metamask/multichain-network-controller';
import { CHAIN_IDS } from './network';
import { CaipChainId, isStrictHexString } from '@metamask/utils';

Check failure on line 3 in shared/constants/common.ts

View workflow job for this annotation

GitHub Actions / test-lint / Test lint

`@metamask/utils` import should occur before import of `./network`

export enum EtherDenomination {
ETH = 'ETH',
Expand Down Expand Up @@ -50,6 +52,19 @@
[CHAIN_IDS.MONAD]: MONAD_DEFAULT_BLOCK_EXPLORER_URL,
} as const;

export const CAIP_CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP = Object.entries(
CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP,
).reduce(
(acc, [chainId, url]) => {
if (isStrictHexString(chainId)) {
const caipChainId = toEvmCaipChainId(chainId);
acc[caipChainId] = url;
}
return acc;
},
{} as Record<CaipChainId, string>,
);

export const CHAINID_DEFAULT_BLOCK_EXPLORER_HUMAN_READABLE_URL_MAP: BlockExplorerUrlMap =
{
[CHAIN_IDS.BSC]: BSC_DEFAULT_BLOCK_EXPLORER_HUMAN_READABLE_URL,
Expand Down
14 changes: 11 additions & 3 deletions shared/lib/asset-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,23 @@ import { TEN_SECONDS_IN_MILLISECONDS } from './transactions-controller-utils';
const TOKEN_API_V3_BASE_URL = 'https://tokens.api.cx.metamask.io/v3';
const STATIC_METAMASK_BASE_URL = 'https://static.cx.metamask.io';

/**
* Converts an address and chainId to a CAIP asset type
*
* @param address - The address of the asset
* @param chainId - The chainId of the asset
* @returns The CAIP asset type
* @throws An error if the chainId is not supported by the Swap and Bridge APIs
*/
export const toAssetId = (
address: Hex | CaipAssetType | string,
chainId: CaipChainId,
): CaipAssetType | undefined => {
): CaipAssetType => {
if (isCaipAssetType(address)) {
return address;
}
if (isNativeAddress(address)) {
return getNativeAssetForChainId(chainId)?.assetId;
return getNativeAssetForChainId(chainId).assetId;
}
if (chainId === MultichainNetwork.Solana) {
return CaipAssetTypeStruct.create(`${chainId}/token:${address}`);
Expand All @@ -43,7 +51,7 @@ export const toAssetId = (
`${chainId}/erc20:${address.toLowerCase()}`,
);
}
return undefined;
throw new Error(`Invalid address or chainId: ${address} ${chainId}`);
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useAssetMetadata = (
searchQuery: string,
shouldFetchMetadata: boolean,
abortControllerRef: React.MutableRefObject<AbortController | null>,
chainId?: Hex | CaipChainId,
chainId: CaipChainId,
) => {
const allowExternalServices = useSelector(getUseExternalServices);

Expand All @@ -31,15 +31,15 @@ export const useAssetMetadata = (
symbol: string;
decimals: number;
image: string;
chainId: Hex | CaipChainId;
chainId: CaipChainId;
isNative: boolean;
type: AssetType.token;
balance: string;
string: string;
}
| undefined
>(async () => {
if (!chainId || !searchQuery) {
if (!searchQuery) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const NetworkListItem = ({
disabled = false,
variant,
notSelectable = false,
avatarNetworkProps = {},
}: {
name: string;
iconSrc?: string;
Expand All @@ -81,6 +82,7 @@ export const NetworkListItem = ({
disabled?: boolean;
variant?: TextVariant;
notSelectable?: boolean;
avatarNetworkProps?: Partial<React.ComponentProps<typeof AvatarNetwork>>;
}) => {
const t = useI18nContext();
const networkRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -175,6 +177,7 @@ export const NetworkListItem = ({
name={name}
src={iconSrc}
size={iconSize as AvatarNetworkSize}
{...avatarNetworkProps}
/>
)}
<Box
Expand Down
Loading
Loading