Skip to content

Commit

Permalink
fix bsc and some other nuances
Browse files Browse the repository at this point in the history
  • Loading branch information
walmat committed Jun 17, 2024
1 parent c66ddd2 commit cb5c4e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
30 changes: 26 additions & 4 deletions src/__swaps__/screens/Swap/providers/swap-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import { AddressOrEth, ExtendedAnimatedAssetWithColors, ParsedSearchAsset } from
import { useSwapWarning } from '@/__swaps__/screens/Swap/hooks/useSwapWarning';
import { CrosschainQuote, Quote, QuoteError } from '@rainbow-me/swaps';
import { swapsStore, useSwapsStore } from '@/state/swaps/swapsStore';
import { parseAssetAndExtend } from '@/__swaps__/utils/swaps';
import { isUnwrapEth, isWrapEth, parseAssetAndExtend } from '@/__swaps__/utils/swaps';
import { ChainId } from '@/__swaps__/types/chains';
import { RainbowError, logger } from '@/logger';
import { QuoteTypeMap, RapSwapActionParameters } from '@/raps/references';
import { Navigation } from '@/navigation';
import { WrappedAlert as Alert } from '@/helpers/alert';
import Routes from '@/navigation/routesNames';
import { ethereumUtils } from '@/utils';
import { getFlashbotsProvider, getProviderForNetwork, isHardHat } from '@/handlers/web3';
import { getFlashbotsProvider, getIsHardhatConnected, getProviderForNetwork, isHardHat } from '@/handlers/web3';
import { loadWallet } from '@/model/wallet';
import { walletExecuteRap } from '@/raps/execute';
import { queryClient } from '@/react-query';
Expand Down Expand Up @@ -215,6 +215,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {

const { errorMessage } = await walletExecuteRap(wallet, type, {
...parameters,
chainId: getIsHardhatConnected() ? ChainId.hardhat : parameters.chainId,
gasParams,
// @ts-expect-error - collision between old gas types and new
gasFeeParamsBySpeed: gasFeeParamsBySpeed,
Expand Down Expand Up @@ -302,7 +303,7 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
const q = quote.value;

// TODO: What other checks do we need here?
if (!inputAsset || !outputAsset || !q || (q as QuoteError)?.error) {
if (isSwapping.value || !inputAsset || !outputAsset || !q || (q as QuoteError)?.error) {
return;
}

Expand All @@ -313,13 +314,34 @@ export const SwapProvider = ({ children }: SwapProviderProps) => {
const quoteData = q as QuoteTypeMap[typeof type];
const flashbots = (SwapSettings.flashbots.value && inputAsset.chainId === ChainId.mainnet) ?? false;

const isNativeWrapOrUnwrap =
isWrapEth({
buyTokenAddress: quoteData.buyTokenAddress,
sellTokenAddress: quoteData.sellTokenAddress,
chainId: inputAsset.chainId,
}) ||
isUnwrapEth({
buyTokenAddress: quoteData.buyTokenAddress,
sellTokenAddress: quoteData.sellTokenAddress,
chainId: inputAsset.chainId,
});

// Do not deleeeet the comment below 😤
// About to get quote
const parameters: Omit<RapSwapActionParameters<typeof type>, 'gasParams' | 'gasFeeParamsBySpeed' | 'selectedGasFee'> = {
sellAmount: quoteData.sellAmount?.toString(),
buyAmount: quoteData.buyAmount?.toString(),
chainId: inputAsset.chainId,
assetToSell: inputAsset,
assetToBuy: outputAsset,
quote: quoteData,
quote: {
...quoteData,
buyAmountDisplay: isNativeWrapOrUnwrap ? quoteData.buyAmount : quoteData.buyAmountDisplay,
sellAmountDisplay: isNativeWrapOrUnwrap ? quoteData.sellAmount : quoteData.sellAmountDisplay,
feeInEth: isNativeWrapOrUnwrap ? '0' : quoteData.feeInEth,
fromChainId: inputAsset.chainId,
toChainId: outputAsset.chainId,
},
flashbots,
};

Expand Down
7 changes: 3 additions & 4 deletions src/__swaps__/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,13 +740,10 @@ export const buildQuoteParams = ({

return {
source: source === 'auto' ? undefined : source,
swapType: isCrosschainSwap ? SwapType.crossChain : SwapType.normal,
fromAddress: currentAddress,
chainId: inputAsset.chainId,
toChainId: isCrosschainSwap ? outputAsset.chainId : inputAsset.chainId,
fromAddress: currentAddress,
sellTokenAddress: inputAsset.isNativeAsset ? ETH_ADDRESS : inputAsset.address,
buyTokenAddress: outputAsset.isNativeAsset ? ETH_ADDRESS : outputAsset.address,

// TODO: Handle native input cases below
sellAmount:
lastTypedInput === 'inputAmount' || lastTypedInput === 'inputNativeValue'
Expand All @@ -758,5 +755,7 @@ export const buildQuoteParams = ({
: undefined,
slippage: Number(slippage),
refuel: false,
swapType: isCrosschainSwap ? SwapType.crossChain : SwapType.normal,
toChainId: isCrosschainSwap ? outputAsset.chainId : inputAsset.chainId,
};
};

0 comments on commit cb5c4e0

Please sign in to comment.