diff --git a/app/scripts/controllers/bridge/bridge-controller.ts b/app/scripts/controllers/bridge/bridge-controller.ts index 0d74c2bff5e0..d7f8dae89dcb 100644 --- a/app/scripts/controllers/bridge/bridge-controller.ts +++ b/app/scripts/controllers/bridge/bridge-controller.ts @@ -1,5 +1,9 @@ +import { Provider } from '@metamask/network-controller'; import { BaseController, StateMetadata } from '@metamask/base-controller'; import { Hex } from '@metamask/utils'; +import { Contract } from '@ethersproject/contracts'; +import { abiERC20 } from '@metamask/metamask-eth-abis'; +import { Web3Provider } from '@ethersproject/providers'; import { fetchBridgeFeatureFlags, fetchBridgeTokens, @@ -8,6 +12,7 @@ import { fetchTopAssetsList } from '../../../../ui/pages/swaps/swaps.util'; import { BRIDGE_CONTROLLER_NAME, DEFAULT_BRIDGE_CONTROLLER_STATE, + METABRIDGE_CHAIN_TO_ADDRESS_MAP, } from './constants'; import { BridgeControllerState, BridgeControllerMessenger } from './types'; @@ -23,7 +28,15 @@ export default class BridgeController extends BaseController< { bridgeState: BridgeControllerState }, BridgeControllerMessenger > { - constructor({ messenger }: { messenger: BridgeControllerMessenger }) { + #provider: Provider; + + constructor({ + provider, + messenger, + }: { + provider: Provider; + messenger: BridgeControllerMessenger; + }) { super({ name: BRIDGE_CONTROLLER_NAME, metadata, @@ -31,6 +44,7 @@ export default class BridgeController extends BaseController< state: { bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE }, }); + // Register action handlers this.messagingSystem.registerActionHandler( `${BRIDGE_CONTROLLER_NAME}:setBridgeFeatureFlags`, this.setBridgeFeatureFlags.bind(this), @@ -43,6 +57,9 @@ export default class BridgeController extends BaseController< `${BRIDGE_CONTROLLER_NAME}:selectDestNetwork`, this.selectDestNetwork.bind(this), ); + + // Assign vars + this.#provider = provider; } resetState = () => { @@ -89,4 +106,17 @@ export default class BridgeController extends BaseController< _state.bridgeState = { ...bridgeState, [stateKey]: tokens }; }); }; + + getErc20Allowance = async ( + contractAddress: string, + walletAddress: string, + chainId: Hex, + ) => { + const web3Provider = new Web3Provider(this.#provider); + const contract = new Contract(contractAddress, abiERC20, web3Provider); + return await contract.allowance( + walletAddress, + METABRIDGE_CHAIN_TO_ADDRESS_MAP[chainId], + ); + }; } diff --git a/app/scripts/controllers/bridge/constants.ts b/app/scripts/controllers/bridge/constants.ts index 58c7d015b7bb..2ba313045455 100644 --- a/app/scripts/controllers/bridge/constants.ts +++ b/app/scripts/controllers/bridge/constants.ts @@ -1,3 +1,6 @@ +import { Hex } from '@metamask/utils'; +import { METABRIDGE_ETHEREUM_ADDRESS } from '../../../../shared/constants/bridge'; +import { CHAIN_IDS } from '../../../../shared/constants/network'; import { BridgeControllerState, BridgeFeatureFlagsKey } from './types'; export const BRIDGE_CONTROLLER_NAME = 'BridgeController'; @@ -13,3 +16,7 @@ export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = { destTokens: {}, destTopAssets: [], }; + +export const METABRIDGE_CHAIN_TO_ADDRESS_MAP: Record = { + [CHAIN_IDS.MAINNET]: METABRIDGE_ETHEREUM_ADDRESS, +}; diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 97f863dee02d..581750663460 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1981,6 +1981,7 @@ export default class MetamaskController extends EventEmitter { allowedEvents: [], }); this.bridgeController = new BridgeController({ + provider: this.provider, messenger: bridgeControllerMessenger, });