Skip to content

Commit

Permalink
Fix Wrap / Unwrap (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobar79 authored Aug 21, 2024
1 parent 5d7ed14 commit 394240c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 14 deletions.
9 changes: 7 additions & 2 deletions lavamoat/build-webpack/policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -1156,8 +1156,13 @@
"define": true
},
"packages": {
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true,
"webpack>terser-webpack-plugin>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": true,
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/resolve-uri": {
"globals": {
"define": true
}
},
"jest>@jest/core>jest-snapshot>@babel/traverse>@babel/generator>@jridgewell/trace-mapping>@jridgewell/sourcemap-codec": {
Expand Down
2 changes: 1 addition & 1 deletion 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.23.0",
"@rainbow-me/swaps": "0.24.0",
"@rudderstack/analytics-js-service-worker": "3.0.6",
"@scure/bip39": "1.2.1",
"@sentry/browser": "8.15.0",
Expand Down
3 changes: 2 additions & 1 deletion src/core/types/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ export type ProtocolType =
| 'yearn-v3'
| 'venus'
| 'sushiswap'
| 'native';
| 'native'
| 'wrapped-native';

export type AssetMetadata = {
circulatingSupply: number;
Expand Down
16 changes: 16 additions & 0 deletions src/core/utils/swaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

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

import { isLowerCaseMatch } from './strings';
Expand Down Expand Up @@ -90,3 +91,18 @@ export const isWrapEth = ({
)
);
};

export const isWrapOrUnwrapEth = ({
assetToBuy,
assetToSell,
}: {
assetToBuy: ParsedSearchAsset | null;
assetToSell: ParsedSearchAsset | null;
}) => {
return (
assetToBuy?.chainId === assetToSell?.chainId &&
((assetToBuy?.type === 'native' &&
assetToSell?.type === 'wrapped-native') ||
(assetToSell?.type === 'native' && assetToBuy?.type === 'wrapped-native'))
);
};
7 changes: 4 additions & 3 deletions src/entries/popup/hooks/swap/useSwapNativeAmounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export const useSwapNativeAmounts = ({
let nativeDisplay = null;
if (isWrapOrUnwrapEth) {
nativeDisplay =
!quote?.sellAmount || !quote?.sellTokenAsset?.price?.value
!quote?.sellAmount || !assetToSell?.price?.value
? null
: convertRawAmountToNativeDisplay(
quote?.sellAmount?.toString(),
quote?.sellTokenAsset?.decimals || 18,
quote?.sellTokenAsset?.price?.value,
assetToSell?.price?.value,
currentCurrency,
);
} else if (assetToSell?.native?.price?.amount && assetToSellValue) {
Expand Down Expand Up @@ -69,11 +69,12 @@ export const useSwapNativeAmounts = ({
}, [
isWrapOrUnwrapEth,
assetToSell?.native?.price?.amount,
assetToSell?.price?.value,
assetToSellValue,
currentCurrency,
quote?.sellAmount,
quote?.sellTokenAsset?.price?.value,
quote?.sellTokenAsset?.decimals,
quote?.sellTokenAsset?.price?.value,
quote?.sellAmountInEth,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
lessThan,
subtract,
} from '~/core/utils/numbers';
import { isWrapOrUnwrapEth } from '~/core/utils/swaps';
import {
Box,
Column,
Expand All @@ -24,6 +25,7 @@ import { CursorTooltip } from '~/entries/popup/components/Tooltip/CursorTooltip'

export const TokenToBuyInfo = ({
assetToBuy,
assetToSell,
assetToBuyValue,
assetToBuyNativeDisplay,
assetToSellNativeDisplay,
Expand All @@ -47,13 +49,17 @@ export const TokenToBuyInfo = ({
}, [assetToBuy?.native?.price?.amount, currentCurrency, assetToBuyValue]);

const nativeValueDifferenceDisplay = useMemo(() => {
const isWrapOrUnwrap = isWrapOrUnwrapEth({ assetToBuy, assetToSell });

if (
!assetToSellNativeDisplay?.amount ||
assetToSellNativeDisplay?.amount === '0' ||
!assetToBuyNativeDisplay?.amount ||
assetToBuyNativeDisplay?.amount === '0'
)
assetToBuyNativeDisplay?.amount === '0' ||
isWrapOrUnwrap
) {
return null;
}
const division = divide(
subtract(assetToBuyNativeDisplay.amount, assetToSellNativeDisplay.amount),
assetToBuyNativeDisplay.amount,
Expand All @@ -62,7 +68,12 @@ export const TokenToBuyInfo = ({
lessThan(abs(division), 0.01) ? '-0.01' : division,
);
return nativeDifference;
}, [assetToBuyNativeDisplay, assetToSellNativeDisplay]);
}, [
assetToBuy,
assetToSell,
assetToBuyNativeDisplay,
assetToSellNativeDisplay,
]);

if (!assetToBuy) return null;
return (
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.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==
"@rainbow-me/swaps@0.24.0":
version "0.24.0"
resolved "https://registry.yarnpkg.com/@rainbow-me/swaps/-/swaps-0.24.0.tgz#c5436bb1caf584a4e580cf6ca5117a998152010c"
integrity sha512-j696H7RUdu4KeHZR55bGBMJ6a+dx3zGF5Y7Nv89vUrkSGaXfakDCBvYlGhHzNMxYX/jFd+jXm5v0Y8ZQUNBQcg==
dependencies:
"@ethereumjs/util" "9.0.0"
"@ethersproject/abi" "5.7.0"
Expand Down

0 comments on commit 394240c

Please sign in to comment.