Skip to content

Commit

Permalink
chore: pass in provider into bridgecontroller so we can look up allow…
Browse files Browse the repository at this point in the history
…ances
  • Loading branch information
infiniteflower committed Sep 27, 2024
1 parent e7eda6a commit 6e399ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
32 changes: 31 additions & 1 deletion app/scripts/controllers/bridge/bridge-controller.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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';

Expand All @@ -23,14 +28,23 @@ 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,
messenger,
state: { bridgeState: DEFAULT_BRIDGE_CONTROLLER_STATE },
});

// Register action handlers
this.messagingSystem.registerActionHandler(
`${BRIDGE_CONTROLLER_NAME}:setBridgeFeatureFlags`,
this.setBridgeFeatureFlags.bind(this),
Expand All @@ -43,6 +57,9 @@ export default class BridgeController extends BaseController<
`${BRIDGE_CONTROLLER_NAME}:selectDestNetwork`,
this.selectDestNetwork.bind(this),
);

// Assign vars
this.#provider = provider;
}

resetState = () => {
Expand Down Expand Up @@ -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],
);
};
}
7 changes: 7 additions & 0 deletions app/scripts/controllers/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -13,3 +16,7 @@ export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
destTokens: {},
destTopAssets: [],
};

export const METABRIDGE_CHAIN_TO_ADDRESS_MAP: Record<Hex, string> = {
[CHAIN_IDS.MAINNET]: METABRIDGE_ETHEREUM_ADDRESS,
};
1 change: 1 addition & 0 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,7 @@ export default class MetamaskController extends EventEmitter {
allowedEvents: [],
});
this.bridgeController = new BridgeController({
provider: this.provider,
messenger: bridgeControllerMessenger,
});

Expand Down

0 comments on commit 6e399ea

Please sign in to comment.