Skip to content

Commit ec698f8

Browse files
authored
chore: set bridge src network, tokens and top assets (#26214)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** * Implements a `selectSrcNetwork` bridge controller action, which sets a state value for the cross-chain swaps source network * On src network change, the controller fetches the bridgeable tokens for the network and also the top assets list * Adds a call to the `useBridging` hook within the bridge route, which sets the src network, tokens and topAssets when the bridge experience is loaded <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26214?quickstart=1) ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/METABRIDGE-866 ## **Manual testing steps** N/A, changes only affect state and are not visible ## **Screenshots/Recordings** New values added to state: ``` { metamask: { bridgeState: { srcTokens: { [tokenAddress.toLowerCase()]: { ...tokenDetails } }, srcTopAssets: [ // list of tokens sorted by popularity ], } } } ``` ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent a451a40 commit ec698f8

21 files changed

+177
-13
lines changed

app/scripts/constants/sentry-state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export const SENTRY_BACKGROUND_STATE = {
104104
},
105105
destTokens: {},
106106
destTopAssets: [],
107+
srcTokens: {},
108+
srcTopAssets: [],
107109
},
108110
},
109111
CronjobController: {

app/scripts/controllers/bridge/bridge-controller.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,28 @@ describe('BridgeController', function () {
9595
{ address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984', symbol: 'ABC' },
9696
]);
9797
});
98+
99+
it('selectSrcNetwork should set the bridge src tokens and top assets', async function () {
100+
await bridgeController.selectSrcNetwork('0xa');
101+
expect(bridgeController.state.bridgeState.srcTokens).toStrictEqual({
102+
'0x0000000000000000000000000000000000000000': {
103+
address: '0x0000000000000000000000000000000000000000',
104+
decimals: 18,
105+
iconUrl: './images/eth_logo.svg',
106+
name: 'Ether',
107+
symbol: 'ETH',
108+
},
109+
'0x1f9840a85d5af5bf1d1762f925bdaddc4201f984': {
110+
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
111+
symbol: 'ABC',
112+
decimals: 16,
113+
},
114+
});
115+
expect(bridgeController.state.bridgeState.srcTopAssets).toStrictEqual([
116+
{
117+
address: '0x1f9840a85d5af5bf1d1762f925bdaddc4201f984',
118+
symbol: 'ABC',
119+
},
120+
]);
121+
});
98122
});

app/scripts/controllers/bridge/bridge-controller.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export default class BridgeController extends BaseController<
3939
`${BRIDGE_CONTROLLER_NAME}:setBridgeFeatureFlags`,
4040
this.setBridgeFeatureFlags.bind(this),
4141
);
42+
this.messagingSystem.registerActionHandler(
43+
`${BRIDGE_CONTROLLER_NAME}:selectSrcNetwork`,
44+
this.selectSrcNetwork.bind(this),
45+
);
4246
this.messagingSystem.registerActionHandler(
4347
`${BRIDGE_CONTROLLER_NAME}:selectDestNetwork`,
4448
this.selectDestNetwork.bind(this),
@@ -61,6 +65,11 @@ export default class BridgeController extends BaseController<
6165
});
6266
};
6367

68+
selectSrcNetwork = async (chainId: Hex) => {
69+
await this.#setTopAssets(chainId, 'srcTopAssets');
70+
await this.#setTokens(chainId, 'srcTokens');
71+
};
72+
6473
selectDestNetwork = async (chainId: Hex) => {
6574
await this.#setTopAssets(chainId, 'destTopAssets');
6675
await this.#setTokens(chainId, 'destTokens');

app/scripts/controllers/bridge/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const DEFAULT_BRIDGE_CONTROLLER_STATE: BridgeControllerState = {
88
[BridgeFeatureFlagsKey.NETWORK_SRC_ALLOWLIST]: [],
99
[BridgeFeatureFlagsKey.NETWORK_DEST_ALLOWLIST]: [],
1010
},
11+
srcTokens: {},
12+
srcTopAssets: [],
1113
destTokens: {},
1214
destTopAssets: [],
1315
};

app/scripts/controllers/bridge/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ export type BridgeFeatureFlags = {
2121

2222
export type BridgeControllerState = {
2323
bridgeFeatureFlags: BridgeFeatureFlags;
24+
srcTokens: Record<string, SwapsTokenObject>;
25+
srcTopAssets: { address: string }[];
2426
destTokens: Record<string, SwapsTokenObject>;
2527
destTopAssets: { address: string }[];
2628
};
2729

2830
export enum BridgeUserAction {
31+
SELECT_SRC_NETWORK = 'selectSrcNetwork',
2932
SELECT_DEST_NETWORK = 'selectDestNetwork',
3033
}
3134
export enum BridgeBackgroundAction {
@@ -40,6 +43,7 @@ type BridgeControllerAction<FunctionName extends keyof BridgeController> = {
4043
// Maps to BridgeController function names
4144
type BridgeControllerActions =
4245
| BridgeControllerAction<BridgeBackgroundAction.SET_FEATURE_FLAGS>
46+
| BridgeControllerAction<BridgeUserAction.SELECT_SRC_NETWORK>
4347
| BridgeControllerAction<BridgeUserAction.SELECT_DEST_NETWORK>;
4448

4549
type BridgeControllerEvents = ControllerStateChangeEvent<

app/scripts/metamask-controller.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3891,6 +3891,10 @@ export default class MetamaskController extends EventEmitter {
38913891
this.controllerMessenger,
38923892
`${BRIDGE_CONTROLLER_NAME}:${BridgeBackgroundAction.SET_FEATURE_FLAGS}`,
38933893
),
3894+
[BridgeUserAction.SELECT_SRC_NETWORK]: this.controllerMessenger.call.bind(
3895+
this.controllerMessenger,
3896+
`${BRIDGE_CONTROLLER_NAME}:${BridgeUserAction.SELECT_SRC_NETWORK}`,
3897+
),
38943898
[BridgeUserAction.SELECT_DEST_NETWORK]:
38953899
this.controllerMessenger.call.bind(
38963900
this.controllerMessenger,

test/e2e/default-fixture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ function defaultFixture(inputChainId = CHAIN_IDS.LOCALHOST) {
129129
},
130130
destTokens: {},
131131
destTopAssets: [],
132+
srcTokens: {},
133+
srcTopAssets: [],
132134
},
133135
},
134136
CurrencyController: {

test/e2e/fixture-builder.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ class FixtureBuilder {
402402
},
403403
destTokens: {},
404404
destTopAssets: [],
405+
srcTokens: {},
406+
srcTopAssets: [],
405407
},
406408
};
407409
return this;

test/e2e/tests/bridge/bridge-test-utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,17 @@ const mockServer =
120120
};
121121
}),
122122
);
123-
return Promise.all(featureFlagMocks);
123+
const portfolioMock = async () =>
124+
await mockServer_
125+
.forGet('https://portfolio.metamask.io/bridge')
126+
.always()
127+
.thenCallback(() => {
128+
return {
129+
statusCode: 200,
130+
json: {},
131+
};
132+
});
133+
return Promise.all([...featureFlagMocks, portfolioMock]);
124134
};
125135

126136
export const getBridgeFixtures = (

test/e2e/tests/metrics/state-snapshots/errors-after-init-opt-in-background-state.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@
6767
"destNetworkAllowlist": { "0": "string", "1": "string", "2": "string" }
6868
},
6969
"destTokens": {},
70-
"destTopAssets": {}
70+
"destTopAssets": {},
71+
"srcTokens": {},
72+
"srcTopAssets": {}
7173
}
7274
},
7375
"CronjobController": { "jobs": "object" },

0 commit comments

Comments
 (0)