Skip to content

Commit

Permalink
chore: update swaps sdk & rip out WRAPPED_ASSET dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
welps authored and DanielSinclair committed Dec 16, 2024
1 parent c1e66b8 commit f59a4cf
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 129 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@
"@ethersproject/hdnode": "5.7.0",
"@ethersproject/keccak256": "5.7.0",
"@ethersproject/providers": "5.7.2",
"@ethersproject/sha2": "5.7.0",
"@ethersproject/strings": "5.7.0",
"@ethersproject/transactions": "5.7.0",
"@ethersproject/units": "5.7.0",
"@ethersproject/wallet": "5.7.0",
"@ledgerhq/hw-app-eth": "6.38.1",
"@ethersproject/sha2": "5.7.0",
"@ledgerhq/hw-transport-webhid": "6.29.3",
"@metamask/browser-passworder": "4.1.0",
"@metamask/eth-sig-util": "7.0.1",
Expand All @@ -109,7 +109,7 @@
"@radix-ui/react-tabs": "1.0.4",
"@radix-ui/react-tooltip": "1.0.3",
"@rainbow-me/provider": "0.1.1",
"@rainbow-me/swaps": "0.29.0",
"@rainbow-me/swaps": "0.30.1",
"@rudderstack/analytics-js-service-worker": "3.0.6",
"@scure/bip39": "1.2.1",
"@sentry/browser": "8.33.0",
Expand Down Expand Up @@ -348,4 +348,4 @@
"@trezor/connect-web>@trezor/connect>@trezor/utxo-lib>blake-hash": false
}
}
}
}
34 changes: 0 additions & 34 deletions patches/@rainbow-me+swaps+0.29.0.patch

This file was deleted.

20 changes: 6 additions & 14 deletions src/core/raps/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { StaticJsonRpcProvider } from '@ethersproject/providers';
import { Transaction } from '@ethersproject/transactions';
import {
CrosschainQuote,
ETH_ADDRESS as ETH_ADDRESS_AGGREGATORS,
Quote,
ChainId as SwapChainId,
SwapType,
WRAPPED_ASSET,
fillQuote,
getQuoteExecutionDetails,
getRainbowRouterContractAddress,
getWrappedAssetAddress,
getWrappedAssetMethod,
unwrapNativeAsset,
wrapNativeAsset,
Expand All @@ -22,7 +21,6 @@ import { getChainGasUnits } from '~/core/references/chains';
import { ChainId } from '~/core/types/chains';
import { NewTransaction, TxHash } from '~/core/types/transactions';
import { add } from '~/core/utils/numbers';
import { isLowerCaseMatch } from '~/core/utils/strings';
import { addNewTransaction } from '~/core/utils/transactions';
import { getProvider } from '~/core/wagmi/clientToProvider';
import { TransactionSimulationResponse } from '~/entries/popup/pages/messages/useSimulateTransaction';
Expand Down Expand Up @@ -64,14 +62,8 @@ export const estimateSwapGasLimit = async ({
return getChainGasUnits(chainId).basic.swap;
}

const { sellTokenAddress, buyTokenAddress } = quote;
const isWrapNativeAsset =
isLowerCaseMatch(sellTokenAddress, ETH_ADDRESS_AGGREGATORS) &&
isLowerCaseMatch(buyTokenAddress, WRAPPED_ASSET[chainId]);

const isUnwrapNativeAsset =
isLowerCaseMatch(sellTokenAddress, WRAPPED_ASSET[chainId]) &&
isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS_AGGREGATORS);
const isWrapNativeAsset = quote.swapType === SwapType.wrap;
const isUnwrapNativeAsset = quote.swapType === SwapType.unwrap;

// Wrap / Unwrap Eth
if (isWrapNativeAsset || isUnwrapNativeAsset) {
Expand All @@ -87,7 +79,7 @@ export const estimateSwapGasLimit = async ({
contractCallEstimateGas: getWrappedAssetMethod(
isWrapNativeAsset ? 'deposit' : 'withdraw',
provider as StaticJsonRpcProvider,
chainId as unknown as SwapChainId,
getWrappedAssetAddress(quote),
),
callArguments: isWrapNativeAsset ? [] : [quote.buyAmount.toString()],
provider,
Expand Down Expand Up @@ -241,15 +233,15 @@ export const executeSwap = async ({
return wrapNativeAsset(
quote.buyAmount,
wallet,
chainId as unknown as SwapChainId,
getWrappedAssetAddress(quote),
transactionParams,
);
// Unwrap native
} else if (quote.swapType === SwapType.unwrap) {
return unwrapNativeAsset(
quote.sellAmount,
wallet,
chainId as unknown as SwapChainId,
getWrappedAssetAddress(quote),
transactionParams,
);
// Swap
Expand Down
47 changes: 1 addition & 46 deletions src/core/utils/swaps.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {
CrosschainQuote,
ETH_ADDRESS,
Quote,
WRAPPED_ASSET,
} from '@rainbow-me/swaps';
import { CrosschainQuote, Quote } from '@rainbow-me/swaps';

import { i18n } from '../languages';
import { useConnectedToHardhatStore } from '../state/currentSettings/connectedToHardhat';
import { ParsedSearchAsset } from '../types/assets';
import { ChainId } from '../types/chains';

import { isLowerCaseMatch } from './strings';

export const getQuoteServiceTime = ({
quote,
Expand Down Expand Up @@ -55,42 +46,6 @@ export const getCrossChainTimeEstimate = ({
timeEstimateDisplay,
};
};
export const isUnwrapEth = ({
buyTokenAddress,
chainId,
sellTokenAddress,
}: {
chainId: ChainId;
sellTokenAddress: string;
buyTokenAddress: string;
}) => {
const { connectedToHardhat } = useConnectedToHardhatStore.getState();
return (
isLowerCaseMatch(
sellTokenAddress,
WRAPPED_ASSET[connectedToHardhat ? ChainId.mainnet : chainId],
) && isLowerCaseMatch(buyTokenAddress, ETH_ADDRESS)
);
};

export const isWrapEth = ({
buyTokenAddress,
chainId,
sellTokenAddress,
}: {
chainId: ChainId;
sellTokenAddress: string;
buyTokenAddress: string;
}) => {
const { connectedToHardhat } = useConnectedToHardhatStore.getState();
return (
isLowerCaseMatch(sellTokenAddress, ETH_ADDRESS) &&
isLowerCaseMatch(
buyTokenAddress,
WRAPPED_ASSET[connectedToHardhat ? ChainId.mainnet : chainId],
)
);
};

export const isWrapOrUnwrapEth = ({
assetToBuy,
Expand Down
17 changes: 2 additions & 15 deletions src/entries/popup/hooks/swap/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import { useMemo } from 'react';

import { useCurrentAddressStore, useCurrentCurrencyStore } from '~/core/state';
import { ParsedAsset, ParsedSearchAsset } from '~/core/types/assets';
import { ChainId } from '~/core/types/chains';
import { convertAmountToRawAmount } from '~/core/utils/numbers';
import { isUnwrapEth, isWrapEth } from '~/core/utils/swaps';

import { IndependentField } from './useSwapInputs';

Expand Down Expand Up @@ -128,19 +126,8 @@ export const useSwapQuote = ({
const isWrapOrUnwrapEth = useMemo(() => {
if (!data || (data as QuoteError).error) return false;
const quote = data as Quote | CrosschainQuote;
return (
isWrapEth({
buyTokenAddress: quote?.buyTokenAddress,
sellTokenAddress: quote?.sellTokenAddress,
chainId: assetToSell?.chainId || ChainId.mainnet,
}) ||
isUnwrapEth({
buyTokenAddress: quote?.buyTokenAddress,
sellTokenAddress: quote?.sellTokenAddress,
chainId: assetToSell?.chainId || ChainId.mainnet,
})
);
}, [assetToSell?.chainId, data]);
return quote.swapType == SwapType.wrap || quote.swapType == SwapType.unwrap;
}, [data]);

const quote = useMemo(() => {
if (!data || (data as QuoteError)?.error) return data;
Expand Down
21 changes: 8 additions & 13 deletions src/entries/popup/pages/swap/SwapReviewSheet/SwapReviewSheet.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CrosschainQuote, Quote, QuoteError } from '@rainbow-me/swaps';
import {
CrosschainQuote,
Quote,
QuoteError,
SwapType,
} from '@rainbow-me/swaps';
import { motion } from 'framer-motion';
import React, {
useCallback,
Expand All @@ -16,7 +21,6 @@ import { ChainId } from '~/core/types/chains';
import { KeychainType } from '~/core/types/keychainTypes';
import { truncateAddress } from '~/core/utils/address';
import { processExchangeRateArray } from '~/core/utils/numbers';
import { isUnwrapEth, isWrapEth } from '~/core/utils/swaps';
import {
Bleed,
Box,
Expand Down Expand Up @@ -243,18 +247,9 @@ const SwapReviewSheetWithQuote = ({

const isWrapOrUnwrapEth = useMemo(() => {
return (
isWrapEth({
buyTokenAddress: quote.buyTokenAddress,
sellTokenAddress: quote.sellTokenAddress,
chainId: assetToSell.chainId,
}) ||
isUnwrapEth({
buyTokenAddress: quote.buyTokenAddress,
sellTokenAddress: quote.sellTokenAddress,
chainId: assetToSell.chainId,
})
quote.swapType === SwapType.wrap || quote.swapType === SwapType.unwrap
);
}, [assetToSell.chainId, quote.buyTokenAddress, quote.sellTokenAddress]);
}, [quote]);

const openMoreDetails = useCallback(() => setShowDetails(true), []);
const closeMoreDetails = useCallback(() => setShowDetails(false), []);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4187,10 +4187,10 @@
eventemitter3 "5.0.1"
viem "1.21.4"

"@rainbow-me/swaps@0.29.0":
version "0.29.0"
resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.29.0.tgz#24a6b15017c05211c871ff9c365b643fc4a7802f"
integrity sha512-NTe4uogcHvwBUr3ePmIKJlE45AP+nyhMrCR4ZO1psVKwsLGIbchL6p0s5c6pl+ZaYUARKbT39Q+AVB9308MhNA==
"@rainbow-me/swaps@0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.30.1.tgz#e598c01933cbba5c1360380511a54668ce4d6921"
integrity sha512-ppigkIdaP08VvoCWLRsbHZubAsUMJ3mGmufm8CIfH62YGunV+LFQY+lsiOsmYMfRXDsrw9h50EW+ILByb1EltQ==
dependencies:
"@ethereumjs/util" "9.0.0"
"@ethersproject/abi" "5.7.0"
Expand Down

0 comments on commit f59a4cf

Please sign in to comment.