Skip to content

Commit

Permalink
chore: set swap input parameters (#30284)
Browse files Browse the repository at this point in the history
<!--
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**

Modifying bridge input component logic to allow fetching swap quotes
<!--
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/30284?quickstart=1)

## **Related issues**

Fixes:NA

## **Manual testing steps**

1. Load flask build and activate non-EVM chain
3. Click "Swap"
4. Page should match Swap mocks (using Bridge ui)
5. Select dest token
6. "Switch inputs" button should be enabled and switch token selections
on click
7. Verify that quotes are fetched in the background
8. Bridge experience should not be affected

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [X] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [X] 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/main/.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.
  • Loading branch information
micaelae authored Feb 19, 2025
1 parent 03c5f58 commit 67aa6aa
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 65 deletions.
4 changes: 1 addition & 3 deletions app/scripts/controllers/bridge/bridge-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ export default class BridgeController extends StaticIntervalPollingController<Br
}: BridgePollingInput) => {
this.#abortController?.abort('New quote request');
this.#abortController = new AbortController();
if (updatedQuoteRequest.srcChainId === updatedQuoteRequest.destChainId) {
return;
}

const { bridgeState } = this.state;
this.update((_state) => {
_state.bridgeState = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export const BridgeAssetPickerButton = ({
<BadgeWrapper
marginRight={2}
badge={
asset && networkProps?.network?.name ? (
asset ? (
<AvatarNetwork
name={networkProps.network.name}
name={networkProps?.network?.name ?? ''}
src={networkImageSrc}
size={AvatarNetworkSize.Xs}
/>
Expand Down
137 changes: 77 additions & 60 deletions ui/pages/bridge/prepare/prepare-bridge-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ const PrepareBridgePage = () => {

const millisecondsUntilNextRefresh = useCountdownTimer();

const isSwap = useIsMultichainSwap();

const [rotateSwitchTokens, setRotateSwitchTokens] = useState(false);

// Resets the banner visibility when the estimated return is low
Expand Down Expand Up @@ -275,16 +277,18 @@ const PrepareBridgePage = () => {
srcChainId: fromChain?.chainId
? Number(hexToDecimal(fromChain.chainId))
: undefined,
destChainId: toChain?.chainId
? Number(hexToDecimal(toChain.chainId))
: undefined,
destChainId:
isSwap && fromChain?.chainId
? Number(hexToDecimal(fromChain.chainId))
: toChain?.chainId && Number(hexToDecimal(toChain.chainId)),
// This override allows quotes to be returned when the rpcUrl is a tenderly fork
// Otherwise quotes get filtered out by the bridge-api when the wallet's real
// balance is less than the tenderly balance
insufficientBal: Boolean(providerConfig?.rpcUrl?.includes('tenderly')),
slippage,
}),
[
isSwap,
fromToken,
toToken,
fromChain?.chainId,
Expand Down Expand Up @@ -366,8 +370,6 @@ const PrepareBridgePage = () => {
}
}, [fromChain, fromToken, fromTokens, search, isFromTokensLoading]);

const isSwap = useIsMultichainSwap();

return (
<Column className="prepare-bridge-page" gap={8}>
<BridgeInputGroup
Expand All @@ -385,34 +387,38 @@ const PrepareBridgePage = () => {
value: token.address,
});
}}
networkProps={{
network: fromChain,
networks: fromChains,
onNetworkChange: (networkConfig) => {
networkConfig.chainId !== fromChain?.chainId &&
trackInputEvent({
input: 'chain_source',
value: networkConfig.chainId,
});
if (networkConfig.chainId === toChain?.chainId) {
dispatch(setToChainId(null));
dispatch(setToToken(null));
}
if (isNetworkAdded(networkConfig)) {
dispatch(
setActiveNetwork(
networkConfig.rpcEndpoints[
networkConfig.defaultRpcEndpointIndex
].networkClientId,
),
);
}
dispatch(setFromToken(null));
dispatch(setFromTokenInputValue(null));
},
header: t('yourNetworks'),
}}
isMultiselectEnabled
networkProps={
isSwap
? undefined
: {
network: fromChain,
networks: fromChains,
onNetworkChange: (networkConfig) => {
networkConfig.chainId !== fromChain?.chainId &&
trackInputEvent({
input: 'chain_source',
value: networkConfig.chainId,
});
if (networkConfig.chainId === toChain?.chainId) {
dispatch(setToChainId(null));
dispatch(setToToken(null));
}
if (isNetworkAdded(networkConfig)) {
dispatch(
setActiveNetwork(
networkConfig.rpcEndpoints[
networkConfig.defaultRpcEndpointIndex
].networkClientId,
),
);
}
dispatch(setFromToken(null));
dispatch(setFromTokenInputValue(null));
},
header: t('yourNetworks'),
}
}
isMultiselectEnabled={!isSwap}
onMaxButtonClick={(value: string) => {
dispatch(setFromTokenInputValue(value));
}}
Expand Down Expand Up @@ -468,10 +474,10 @@ const PrepareBridgePage = () => {
disabled={
isSwitchingTemporarilyDisabled ||
!isValidQuoteRequest(quoteRequest, false) ||
!isNetworkAdded(toChain)
(!isSwap && !isNetworkAdded(toChain))
}
onClick={() => {
if (!isNetworkAdded(toChain)) {
if (!isSwap && !isNetworkAdded(toChain)) {
return;
}
setRotateSwitchTokens(!rotateSwitchTokens);
Expand All @@ -480,15 +486,19 @@ const PrepareBridgePage = () => {
event: MetaMetricsEventName.InputSourceDestinationFlipped,
properties: flippedRequestProperties,
});
const toChainClientId =
toChain?.defaultRpcEndpointIndex !== undefined &&
toChain?.rpcEndpoints
? toChain.rpcEndpoints[toChain.defaultRpcEndpointIndex]
.networkClientId
: undefined;
toChainClientId && dispatch(setActiveNetwork(toChainClientId));
if (!isSwap) {
// Only flip networks if bridging
const toChainClientId =
toChain?.defaultRpcEndpointIndex !== undefined &&
toChain?.rpcEndpoints &&
isNetworkAdded(toChain)
? toChain.rpcEndpoints[toChain.defaultRpcEndpointIndex]
.networkClientId
: undefined;
toChainClientId && dispatch(setActiveNetwork(toChainClientId));
fromChain?.chainId && dispatch(setToChainId(fromChain.chainId));
}
dispatch(setFromToken(toToken));
fromChain?.chainId && dispatch(setToChainId(fromChain.chainId));
dispatch(setToToken(fromToken));
}}
/>
Expand All @@ -505,23 +515,30 @@ const PrepareBridgePage = () => {
});
dispatch(setToToken(token));
}}
networkProps={{
network: toChain,
networks: toChains,
onNetworkChange: (networkConfig) => {
networkConfig.chainId !== toChain?.chainId &&
trackInputEvent({
input: 'chain_destination',
value: networkConfig.chainId,
});
dispatch(setToChainId(networkConfig.chainId));
dispatch(setToToken(null));
},
header: isSwap ? t('swapSwapTo') : t('bridgeTo'),
shouldDisableNetwork: ({ chainId }) =>
chainId === fromChain?.chainId,
}}
customTokenListGenerator={toChain ? toTokenListGenerator : undefined}
networkProps={
isSwap
? undefined
: {
network: toChain,
networks: toChains,
onNetworkChange: (networkConfig) => {
networkConfig.chainId !== toChain?.chainId &&
trackInputEvent({
input: 'chain_destination',
value: networkConfig.chainId,
});
dispatch(setToChainId(networkConfig.chainId));
dispatch(setToToken(null));
},
header: isSwap ? t('swapSwapTo') : t('bridgeTo'),
shouldDisableNetwork: ({ chainId }) =>
chainId === fromChain?.chainId,
}
}
customTokenListGenerator={
// TODO use custom generator when we have a way to get all tokens for an unimported chain
toChain && !isSwap ? toTokenListGenerator : undefined
}
amountInFiat={
activeQuote?.toTokenAmount?.valueInCurrency || undefined
}
Expand Down

0 comments on commit 67aa6aa

Please sign in to comment.