Skip to content

Commit

Permalink
Merge branch 'develop' into jb-privacy-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnyhoward authored Oct 30, 2024
2 parents ac9c738 + 7df5cef commit 3d0eeda
Show file tree
Hide file tree
Showing 60 changed files with 783 additions and 415 deletions.
3 changes: 3 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module.exports = {
config.resolve.alias['../../../../../../store/actions'] = require.resolve(
'../ui/__mocks__/actions.js',
);
config.resolve.alias['../../../store/actions'] = require.resolve(
'../ui/__mocks__/actions.js',
);
// Import within controller-utils crashes storybook.
config.resolve.alias['@ethereumjs/util'] = require.resolve(
'../ui/__mocks__/ethereumjs-util.js',
Expand Down
10 changes: 9 additions & 1 deletion app/_locales/en/messages.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions app/scripts/constants/sentry-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const SENTRY_BACKGROUND_STATE = {
BridgeController: {
bridgeState: {
bridgeFeatureFlags: {
extensionConfig: false,
extensionSupport: false,
destNetworkAllowlist: [],
srcNetworkAllowlist: [],
Expand All @@ -106,6 +107,15 @@ export const SENTRY_BACKGROUND_STATE = {
destTopAssets: [],
srcTokens: {},
srcTopAssets: [],
quoteRequest: {
walletAddress: false,
srcTokenAddress: true,
slippage: true,
srcChainId: true,
destChainId: true,
destTokenAddress: true,
srcTokenAmount: true,
},
},
},
CronjobController: {
Expand Down
85 changes: 85 additions & 0 deletions app/scripts/controllers/bridge/bridge-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe('BridgeController', function () {
nock(BRIDGE_API_BASE_URL)
.get('/getAllFeatureFlags')
.reply(200, {
'extension-config': {
refreshRate: 3,
maxRefreshCount: 1,
},
'extension-support': true,
'src-network-allowlist': [10, 534352],
'dest-network-allowlist': [137, 42161],
Expand All @@ -55,6 +59,7 @@ describe('BridgeController', function () {
symbol: 'ABC',
},
]);
bridgeController.resetState();
});

it('constructor should setup correctly', function () {
Expand All @@ -66,6 +71,10 @@ describe('BridgeController', function () {
extensionSupport: true,
destNetworkAllowlist: [CHAIN_IDS.POLYGON, CHAIN_IDS.ARBITRUM],
srcNetworkAllowlist: [CHAIN_IDS.OPTIMISM, CHAIN_IDS.SCROLL],
extensionConfig: {
maxRefreshCount: 1,
refreshRate: 3,
},
};
expect(bridgeController.state).toStrictEqual(EMPTY_INIT_STATE);

Expand Down Expand Up @@ -94,6 +103,11 @@ describe('BridgeController', function () {
expect(bridgeController.state.bridgeState.destTopAssets).toStrictEqual([
{ address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', symbol: 'ABC' },
]);
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});
});

it('selectSrcNetwork should set the bridge src tokens and top assets', async function () {
Expand All @@ -118,5 +132,76 @@ describe('BridgeController', function () {
symbol: 'ABC',
},
]);
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});
});

it('updateBridgeQuoteRequestParams should update the quoteRequest state', function () {
bridgeController.updateBridgeQuoteRequestParams({ srcChainId: 1 });
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
srcChainId: 1,
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});

bridgeController.updateBridgeQuoteRequestParams({ destChainId: 10 });
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
destChainId: 10,
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});

bridgeController.updateBridgeQuoteRequestParams({ destChainId: undefined });
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
destChainId: undefined,
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});

bridgeController.updateBridgeQuoteRequestParams({
srcTokenAddress: undefined,
});
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
slippage: 0.5,
srcTokenAddress: undefined,
walletAddress: undefined,
});

bridgeController.updateBridgeQuoteRequestParams({
srcTokenAmount: '100000',
destTokenAddress: '0x123',
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
srcTokenAmount: '100000',
destTokenAddress: '0x123',
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});

bridgeController.updateBridgeQuoteRequestParams({
srcTokenAddress: '0x2ABC',
});
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
slippage: 0.5,
srcTokenAddress: '0x2ABC',
walletAddress: undefined,
});

bridgeController.resetState();
expect(bridgeController.state.bridgeState.quoteRequest).toStrictEqual({
slippage: 0.5,
srcTokenAddress: '0x0000000000000000000000000000000000000000',
walletAddress: undefined,
});
});
});
24 changes: 24 additions & 0 deletions app/scripts/controllers/bridge/bridge-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { fetchTopAssetsList } from '../../../../ui/pages/swaps/swaps.util';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { QuoteRequest } from '../../../../ui/pages/bridge/types';
import {
BRIDGE_CONTROLLER_NAME,
DEFAULT_BRIDGE_CONTROLLER_STATE,
Expand Down Expand Up @@ -47,8 +50,29 @@ export default class BridgeController extends BaseController<
`${BRIDGE_CONTROLLER_NAME}:selectDestNetwork`,
this.selectDestNetwork.bind(this),
);
this.messagingSystem.registerActionHandler(
`${BRIDGE_CONTROLLER_NAME}:updateBridgeQuoteRequestParams`,
this.updateBridgeQuoteRequestParams.bind(this),
);
}

updateBridgeQuoteRequestParams = (paramsToUpdate: Partial<QuoteRequest>) => {
const { bridgeState } = this.state;
const updatedQuoteRequest = {
...DEFAULT_BRIDGE_CONTROLLER_STATE.quoteRequest,
...paramsToUpdate,
};

this.update((_state) => {
_state.bridgeState = {
...bridgeState,
quoteRequest: {
...updatedQuoteRequest,
},
};
});
};

resetState = () => {
this.update((_state) => {
_state.bridgeState = {
Expand Down
13 changes: 13 additions & 0 deletions app/scripts/controllers/bridge/constants.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { zeroAddress } from 'ethereumjs-util';
import { BridgeControllerState, BridgeFeatureFlagsKey } from './types';

export const BRIDGE_CONTROLLER_NAME = 'BridgeController';
export const REFRESH_INTERVAL_MS = 30 * 1000;
const DEFAULT_MAX_REFRESH_COUNT = 5;
const DEFAULT_SLIPPAGE = 0.5;

export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
bridgeFeatureFlags: {
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {
refreshRate: REFRESH_INTERVAL_MS,
maxRefreshCount: DEFAULT_MAX_REFRESH_COUNT,
},
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: false,
[BridgeFeatureFlagsKey.NETWORK_SRC_ALLOWLIST]: [],
[BridgeFeatureFlagsKey.NETWORK_DEST_ALLOWLIST]: [],
Expand All @@ -12,4 +20,9 @@ export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
srcTopAssets: [],
destTokens: {},
destTopAssets: [],
quoteRequest: {
walletAddress: undefined,
srcTokenAddress: zeroAddress(),
slippage: DEFAULT_SLIPPAGE,
},
};
13 changes: 12 additions & 1 deletion app/scripts/controllers/bridge/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@ import {
} from '@metamask/base-controller';
import { Hex } from '@metamask/utils';
import { SwapsTokenObject } from '../../../../shared/constants/swaps';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { QuoteRequest } from '../../../../ui/pages/bridge/types';
import BridgeController from './bridge-controller';
import { BRIDGE_CONTROLLER_NAME } from './constants';

export enum BridgeFeatureFlagsKey {
EXTENSION_CONFIG = 'extensionConfig',
EXTENSION_SUPPORT = 'extensionSupport',
NETWORK_SRC_ALLOWLIST = 'srcNetworkAllowlist',
NETWORK_DEST_ALLOWLIST = 'destNetworkAllowlist',
}

export type BridgeFeatureFlags = {
[BridgeFeatureFlagsKey.EXTENSION_CONFIG]: {
refreshRate: number;
maxRefreshCount: number;
};
[BridgeFeatureFlagsKey.EXTENSION_SUPPORT]: boolean;
[BridgeFeatureFlagsKey.NETWORK_SRC_ALLOWLIST]: Hex[];
[BridgeFeatureFlagsKey.NETWORK_DEST_ALLOWLIST]: Hex[];
Expand All @@ -25,11 +33,13 @@ export type BridgeControllerState = {
srcTopAssets: { address: string }[];
destTokens: Record<string, SwapsTokenObject>;
destTopAssets: { address: string }[];
quoteRequest: Partial<QuoteRequest>;
};

export enum BridgeUserAction {
SELECT_SRC_NETWORK = 'selectSrcNetwork',
SELECT_DEST_NETWORK = 'selectDestNetwork',
UPDATE_QUOTE_PARAMS = 'updateBridgeQuoteRequestParams',
}
export enum BridgeBackgroundAction {
SET_FEATURE_FLAGS = 'setBridgeFeatureFlags',
Expand All @@ -44,7 +54,8 @@ type BridgeControllerAction<FunctionName extends keyof BridgeController> = {
type BridgeControllerActions =
| BridgeControllerAction<BridgeBackgroundAction.SET_FEATURE_FLAGS>
| BridgeControllerAction<BridgeUserAction.SELECT_SRC_NETWORK>
| BridgeControllerAction<BridgeUserAction.SELECT_DEST_NETWORK>;
| BridgeControllerAction<BridgeUserAction.SELECT_DEST_NETWORK>
| BridgeControllerAction<BridgeUserAction.UPDATE_QUOTE_PARAMS>;

type BridgeControllerEvents = ControllerStateChangeEvent<
typeof BRIDGE_CONTROLLER_NAME,
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/transaction/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe('Transaction metrics', () => {
transaction_type: TransactionType.simpleSend,
ui_customizations: null,
transaction_advanced_view: null,
transaction_contract_method: undefined,
};

expectedSensitiveProperties = {
Expand All @@ -165,7 +166,6 @@ describe('Transaction metrics', () => {
first_seen: 1624408066355,
gas_limit: '0x7b0d',
gas_price: '2',
transaction_contract_method: undefined,
transaction_envelope_type: TRANSACTION_ENVELOPE_TYPE_NAMES.LEGACY,
transaction_replaced: undefined,
};
Expand Down
2 changes: 1 addition & 1 deletion app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ async function buildEventFragmentProperties({
// ui_customizations must come after ...blockaidProperties
ui_customizations: uiCustomizations.length > 0 ? uiCustomizations : null,
transaction_advanced_view: isAdvancedDetailsOpen,
transaction_contract_method: transactionContractMethod,
...smartTransactionMetricsProperties,
...swapAndSendMetricsProperties,
// TODO: Replace `any` with type
Expand All @@ -1086,7 +1087,6 @@ async function buildEventFragmentProperties({
: TRANSACTION_ENVELOPE_TYPE_NAMES.LEGACY,
first_seen: time,
gas_limit: gasLimit,
transaction_contract_method: transactionContractMethod,
transaction_replaced: transactionReplaced,
...extraParams,
...gasParamsInGwei,
Expand Down
4 changes: 3 additions & 1 deletion app/scripts/lib/transaction/smart-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,9 @@ class SmartTransactionHook {
signedTransactions,
signedCanceledTransactions: [],
txParams: this.#txParams,
transactionMeta: this.#transactionMeta,
// TODO: Replace `any` with type - version mismatch between smart-transactions-controller and transaction-controller breaking type safety
// eslint-disable-next-line @typescript-eslint/no-explicit-any
transactionMeta: this.#transactionMeta as any,
});
}

Expand Down
12 changes: 8 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,11 @@ export default class MetamaskController extends EventEmitter {
this.controllerMessenger,
`${BRIDGE_CONTROLLER_NAME}:${BridgeUserAction.SELECT_DEST_NETWORK}`,
),
[BridgeUserAction.UPDATE_QUOTE_PARAMS]:
this.controllerMessenger.call.bind(
this.controllerMessenger,
`${BRIDGE_CONTROLLER_NAME}:${BridgeUserAction.UPDATE_QUOTE_PARAMS}`,
),

// Smart Transactions
fetchSmartTransactionFees: smartTransactionsController.getFees.bind(
Expand Down Expand Up @@ -3995,10 +4000,9 @@ export default class MetamaskController extends EventEmitter {
),

// CurrencyRateController
currencyRateStartPollingByNetworkClientId:
currencyRateController.startPollingByNetworkClientId.bind(
currencyRateController,
),
currencyRateStartPolling: currencyRateController.startPolling.bind(
currencyRateController,
),
currencyRateStopPollingByPollingToken:
currencyRateController.stopPollingByPollingToken.bind(
currencyRateController,
Expand Down
8 changes: 1 addition & 7 deletions lavamoat/browserify/beta/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,13 @@
"@ethersproject/providers": true,
"@metamask/abi-utils": true,
"@metamask/assets-controllers>@metamask/polling-controller": true,
"@metamask/assets-controllers>@metamask/rpc-errors": true,
"@metamask/base-controller": true,
"@metamask/contract-metadata": true,
"@metamask/controller-utils": true,
"@metamask/eth-query": true,
"@metamask/metamask-eth-abis": true,
"@metamask/name-controller>async-mutex": true,
"@metamask/rpc-errors": true,
"@metamask/utils": true,
"bn.js": true,
"cockatiel": true,
Expand All @@ -702,12 +702,6 @@
"uuid": true
}
},
"@metamask/assets-controllers>@metamask/rpc-errors": {
"packages": {
"@metamask/rpc-errors>fast-safe-stringify": true,
"@metamask/utils": true
}
},
"@metamask/base-controller": {
"globals": {
"setTimeout": true
Expand Down
Loading

0 comments on commit 3d0eeda

Please sign in to comment.