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

Disable flashbots toggle on appropriate chains #5812

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/__swaps__/screens/Swap/components/AnimatedSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function AnimatedSwitch({ value, onToggle, activeLabel, inactiveLabel, ..
? withTiming(opacityWorklet(inactiveBg, 0.12), TIMING_CONFIGS.fadeConfig)
: withTiming(opacityWorklet(activeBg, 0.64), TIMING_CONFIGS.fadeConfig),
borderColor: opacityWorklet(border, 0.06),
opacity: props.disabled ? 0.4 : 1,
jinchung marked this conversation as resolved.
Show resolved Hide resolved
};
});

Expand All @@ -41,6 +42,8 @@ export function AnimatedSwitch({ value, onToggle, activeLabel, inactiveLabel, ..
});

const labelItem = useDerivedValue(() => {
if (props.disabled) return;

if (!activeLabel && !inactiveLabel) {
return;
}
Expand Down
23 changes: 17 additions & 6 deletions src/__swaps__/screens/Swap/components/ReviewPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ function EstimatedArrivalTime() {
);
}

function FlashbotsToggle() {
const { SwapSettings } = useSwapContext();

const inputAssetChainId = swapsStore(state => state.inputAsset?.chainId) ?? ChainId.mainnet;

return (
<AnimatedSwitch
onToggle={SwapSettings.onToggleFlashbots}
disabled={inputAssetChainId !== ChainId.mainnet}
walmat marked this conversation as resolved.
Show resolved Hide resolved
value={SwapSettings.flashbots}
activeLabel={i18n.t(i18n.l.expanded_state.swap.on)}
inactiveLabel={i18n.t(i18n.l.expanded_state.swap.off)}
/>
);
}

export function ReviewPanel() {
const { navigate } = useNavigation();
const { isDarkMode } = useColorMode();
Expand Down Expand Up @@ -263,12 +279,7 @@ export function ReviewPanel() {
</ButtonPressAnimation>
</Inline>

<AnimatedSwitch
onToggle={SwapSettings.onToggleFlashbots}
value={SwapSettings.flashbots}
activeLabel={i18n.t(i18n.l.expanded_state.swap.on)}
inactiveLabel={i18n.t(i18n.l.expanded_state.swap.off)}
/>
<FlashbotsToggle />
</Inline>

<Inline wrap={false} horizontalSpace="10px" alignVertical="center" alignHorizontal="justify">
Expand Down
11 changes: 11 additions & 0 deletions src/__swaps__/screens/Swap/hooks/useSwapSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ export const useSwapSettings = ({ inputAsset }: { inputAsset: SharedValue<Extend
const setSlippage = swapsStore(state => state.setSlippage);
const setFlashbots = swapsStore(state => state.setFlashbots);

const checkIfFlashbotsIsEnabledOnSelectedInputAssetChain = (asset: ExtendedAnimatedAssetWithColors | null) => {
jinchung marked this conversation as resolved.
Show resolved Hide resolved
'worklet';

if (asset?.chainId === ChainId.mainnet) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to above (though we can remove this function anyway): don't like that we are re-introducing places in the code where we rely on these types of chainID checks vs relying on the source of truth (network config)

flashbots.value = true;
} else {
flashbots.value = false;
}
};
walmat marked this conversation as resolved.
Show resolved Hide resolved

const onToggleFlashbots = () => {
'worklet';

Expand Down Expand Up @@ -41,6 +51,7 @@ export const useSwapSettings = ({ inputAsset }: { inputAsset: SharedValue<Extend
flashbots,
slippage,

checkIfFlashbotsIsEnabledOnSelectedInputAssetChain,
onToggleFlashbots,
onUpdateSlippage,
};
Expand Down
4 changes: 3 additions & 1 deletion src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,15 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
switch (type) {
case SwapAssetType.inputAsset:
internalSelectedInputAsset.value = asset;

SwapSettings.checkIfFlashbotsIsEnabledOnSelectedInputAssetChain(asset);
break;
case SwapAssetType.outputAsset:
internalSelectedOutputAsset.value = asset;
break;
}
},
[internalSelectedInputAsset, internalSelectedOutputAsset]
[SwapSettings, internalSelectedInputAsset, internalSelectedOutputAsset]
);

const chainSetTimeoutId = useRef<NodeJS.Timeout | null>(null);
Expand Down
Loading