Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: set swap input parameters #30284

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading