Skip to content

Commit

Permalink
update swap sdk v23 (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-schrammel authored Aug 15, 2024
1 parent add44e3 commit 63ffd19
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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.21.0",
"@rainbow-me/swaps": "0.23.0",
"@rudderstack/analytics-js-service-worker": "3.0.6",
"@scure/bip39": "1.2.1",
"@sentry/browser": "8.15.0",
Expand Down Expand Up @@ -328,4 +328,4 @@
"wagmi>@wagmi/connectors>@metamask/sdk>eciesjs>secp256k1": false
}
}
}
}
4 changes: 3 additions & 1 deletion src/core/raps/actions/claimBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Address } from 'viem';
import { optimism } from 'viem/chains';

import { REFERRER_CLAIM } from '~/core/references';
import { gasStore } from '~/core/state';
import { currentCurrencyStore, gasStore } from '~/core/state';
import { TransactionGasParams } from '~/core/types/gas';
import { NewTransaction, TxHash } from '~/core/types/transactions';
import { calculateL1FeeOptimism } from '~/core/utils/gas';
Expand Down Expand Up @@ -55,6 +55,7 @@ export async function claimBridge({
sellAmount: sellAmount,
slippage: 2,
swapType: SwapType.crossChain,
currency: currentCurrencyStore.getState().currentCurrency,
});

// if we don't get a quote or there's an error we can't continue
Expand Down Expand Up @@ -122,6 +123,7 @@ export async function claimBridge({
sellAmount: maxBridgeableAmount,
slippage: 2,
swapType: SwapType.crossChain,
currency: currentCurrencyStore.getState().currentCurrency,
});

if (!newQuote || (newQuote as QuoteError)?.error) {
Expand Down
1 change: 1 addition & 0 deletions src/core/raps/actions/crosschainSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_3,
swapType: SwapType.crossChain,
toChainId: ChainId.arbitrum,
currency: 'USD',
});
}, 20000);

Expand Down
1 change: 1 addition & 0 deletions src/core/raps/actions/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
}, 10000);

Expand Down
15 changes: 14 additions & 1 deletion src/core/raps/unlockAndCrosschainSwap.test.ts
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,
TokenAsset,
} from '@rainbow-me/swaps';
import { mainnet } from 'viem/chains';
import { beforeAll, expect, test } from 'vitest';

Expand All @@ -24,6 +29,7 @@ const needsUnlockQuote: Quote | QuoteError | null = {
chainId: 1,
buyAmount: '22815411',
buyAmountDisplay: '22815411',
buyAmountDisplayMinimum: '22815411',
buyAmountInEth: '7674057708816777',
buyAmountMinusFees: '7674057708816777',
buyTokenAddress: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
Expand All @@ -48,12 +54,16 @@ const needsUnlockQuote: Quote | QuoteError | null = {
to: '0xdef1c0ded9bec7f1a1670819833240f027b25eff',
tradeAmountUSD: 21.84463710898238,
value: '0',
buyTokenAsset: {} as TokenAsset, // not used in this test
feeTokenAsset: {} as TokenAsset, // not used in this test
sellTokenAsset: {} as TokenAsset, // not used in this test
};

const doesntNeedUnlockQuote: Quote | QuoteError | null = {
chainId: 1,
buyAmount: '2934529154',
buyAmountDisplay: '2934529154',
buyAmountDisplayMinimum: '2934529154',
buyAmountInEth: '988585673036047522',
buyAmountMinusFees: '988585673036047522',
buyTokenAddress: '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8',
Expand Down Expand Up @@ -82,6 +92,9 @@ const doesntNeedUnlockQuote: Quote | QuoteError | null = {
to: TEST_ADDRESS_2,
tradeAmountUSD: 2963.84,
value: '0x0de0b6b3a7640000',
buyTokenAsset: {} as TokenAsset, // not used in this test
feeTokenAsset: {} as TokenAsset, // not used in this test
sellTokenAsset: {} as TokenAsset, // not used in this test
};

beforeAll(async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/core/raps/unlockAndSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
ethToEnsQuote = await getQuote({
chainId: 1,
Expand All @@ -83,6 +84,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
needsUnlockQuote = await getQuote({
chainId: 1,
Expand All @@ -94,6 +96,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
wrapEthQuote = await getQuote({
chainId: 1,
Expand All @@ -105,6 +108,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
unwrapEthQuote = await getQuote({
chainId: 1,
Expand All @@ -116,6 +120,7 @@ beforeAll(async () => {
destReceiver: TEST_ADDRESS_2,
swapType: SwapType.normal,
toChainId: 1,
currency: 'USD',
});
}, 10000);

Expand Down
42 changes: 15 additions & 27 deletions src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import { useMemo } from 'react';
import { supportedCurrencies } from '~/core/references';
import { useCurrentCurrencyStore } from '~/core/state';
import { ParsedSearchAsset } from '~/core/types/assets';
import { ChainId } from '~/core/types/chains';
import {
convertAmountAndPriceToNativeDisplay,
convertRawAmountToNativeDisplay,
handleSignificantDecimalsAsNumber,
} from '~/core/utils/numbers';

import { useNativeAssetForNetwork } from '../useNativeAssetForNetwork';

export const useSwapNativeAmounts = ({
assetToSell,
assetToSellValue,
Expand All @@ -29,24 +26,17 @@ export const useSwapNativeAmounts = ({
isWrapOrUnwrapEth: boolean;
}) => {
const { currentCurrency } = useCurrentCurrencyStore();
const sellNativeAsset = useNativeAssetForNetwork({
chainId: assetToSell?.chainId || ChainId.mainnet,
});

const buyNativeAsset = useNativeAssetForNetwork({
chainId: assetToBuy?.chainId || ChainId.mainnet,
});

const assetToSellNativeDisplay = useMemo(() => {
let nativeDisplay = null;
if (isWrapOrUnwrapEth) {
nativeDisplay =
!quote?.sellAmount || !assetToSell?.price?.value
!quote?.sellAmount || !quote.sellTokenAsset.price.value
? null
: convertRawAmountToNativeDisplay(
quote?.sellAmount?.toString(),
assetToSell?.decimals || 18,
assetToSell?.price?.value,
quote.sellTokenAsset.decimals || 18,
quote.sellTokenAsset.price.value,
currentCurrency,
);
} else if (assetToSell?.native?.price?.amount && assetToSellValue) {
Expand All @@ -57,12 +47,12 @@ export const useSwapNativeAmounts = ({
);
} else {
nativeDisplay =
!quote?.sellAmountInEth || !sellNativeAsset?.price?.value
!quote?.sellAmountInEth || !quote.sellTokenAsset.price.value
? null
: convertRawAmountToNativeDisplay(
quote?.sellAmountInEth.toString(),
sellNativeAsset?.decimals || 18,
sellNativeAsset?.price?.value,
quote.sellTokenAsset.decimals || 18,
quote.sellTokenAsset.price?.value,
currentCurrency,
);
}
Expand All @@ -79,14 +69,12 @@ export const useSwapNativeAmounts = ({
}, [
isWrapOrUnwrapEth,
assetToSell?.native?.price?.amount,
assetToSell?.price?.value,
assetToSell?.decimals,
assetToSellValue,
currentCurrency,
quote?.sellAmount,
quote?.sellTokenAsset.price.value,
quote?.sellTokenAsset.decimals,
quote?.sellAmountInEth,
currentCurrency,
assetToSellValue,
sellNativeAsset?.price?.value,
sellNativeAsset?.decimals,
]);

const assetToBuyNativeDisplay = useMemo(() => {
Expand All @@ -109,12 +97,12 @@ export const useSwapNativeAmounts = ({
);
} else {
nativeDisplay =
!quote?.buyAmountInEth || !buyNativeAsset?.price?.value
!quote?.buyAmountInEth || !quote.buyTokenAsset.price?.value
? null
: convertRawAmountToNativeDisplay(
quote?.buyAmountInEth.toString(),
buyNativeAsset?.decimals || 18,
buyNativeAsset?.price?.value,
quote.buyTokenAsset.decimals || 18,
quote.buyTokenAsset.price?.value,
currentCurrency,
);
}
Expand All @@ -136,8 +124,8 @@ export const useSwapNativeAmounts = ({
quote?.buyAmount,
quote?.buyAmountInEth,
currentCurrency,
buyNativeAsset?.price?.value,
buyNativeAsset?.decimals,
quote?.buyTokenAsset.price?.value,
quote?.buyTokenAsset.decimals,
]);

return {
Expand Down
5 changes: 4 additions & 1 deletion src/entries/popup/hooks/swap/useSwapQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';

import { useCurrentAddressStore } from '~/core/state';
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';
Expand Down Expand Up @@ -46,6 +46,7 @@ export const useSwapQuote = ({
isClaim,
}: UseSwapQuotesProps) => {
const { currentAddress } = useCurrentAddressStore();
const currency = useCurrentCurrencyStore((s) => s.currentCurrency);

const isCrosschainSwap = useMemo(
() =>
Expand Down Expand Up @@ -95,6 +96,7 @@ export const useSwapQuote = ({
swapType: isCrosschainSwap ? SwapType.crossChain : SwapType.normal,
toChainId: isCrosschainSwap ? assetToBuy.chainId : assetToSell.chainId,
feePercentageBasisPoints: INTERNAL_BUILD || isClaim ? 0 : undefined,
currency,
};
}, [
assetToBuy,
Expand All @@ -107,6 +109,7 @@ export const useSwapQuote = ({
slippage,
source,
isClaim,
currency,
]);

const { data, isLoading, isError, fetchStatus } = useQuery({
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3932,10 +3932,10 @@
eventemitter3 "5.0.1"
viem "1.21.4"

"@rainbow-me/swaps@0.21.0":
version "0.21.0"
resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.21.0.tgz#c04e72c9bc8fccaa6865dba2386e78665c551747"
integrity sha512-ULTlMrPmJzsgKXJxDtsGMMPsFMtDG0HvAc2BS7LSfmg8IsBHOavr7fo+7LohV5IUnpCVTDYtsZei/mVBUgxSDg==
"@rainbow-me/swaps@0.23.0":
version "0.23.0"
resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.23.0.tgz#888e61a302e0f642e4ac7c5f58e6bd6d90b7c953"
integrity sha512-r6xF2v6bPguz7ytHO/UY+IjvyI8QqbytuXikXT8zCp1TVcXYWESsxJbVsGHd4utJOswSt5M7H1He0wY7shBh6g==
dependencies:
"@ethereumjs/util" "9.0.0"
"@ethersproject/abi" "5.7.0"
Expand Down

0 comments on commit 63ffd19

Please sign in to comment.