Skip to content

Commit

Permalink
✨get exa
Browse files Browse the repository at this point in the history
  • Loading branch information
sebipap committed Aug 17, 2023
1 parent 279166c commit 08edac1
Show file tree
Hide file tree
Showing 25 changed files with 1,777 additions and 49 deletions.
8 changes: 3 additions & 5 deletions components/BridgeContent/SocketPlugIn/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import { optimism } from 'viem/chains';
import useAnalytics from 'hooks/useAnalytics';
import { hexToRgb } from './utils';
import useAssetAddresses from 'hooks/useAssetAddresses';
import { Asset, TokensResponse } from 'types/Bridge';
import { Asset, NATIVE_TOKEN_ADDRESS, TokensResponse } from 'types/Bridge';

const DynamicBridge = dynamic(() => import('@socket.tech/plugin').then((mod) => mod.Bridge), {
ssr: false,
});

const NATIVE_TOKEN = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';

type Props = {
updateRoutes: () => void;
};
Expand Down Expand Up @@ -53,7 +51,7 @@ const SocketPlugIn = ({ updateRoutes }: Props) => {
}, [fetchAssets]);

const tokenList = useMemo(() => {
const markets = [...assets, NATIVE_TOKEN];
const markets = [...assets, NATIVE_TOKEN_ADDRESS];
if (!tokens) return [];

return tokens
Expand Down Expand Up @@ -144,7 +142,7 @@ const SocketPlugIn = ({ updateRoutes }: Props) => {
API_KEY={process.env.NEXT_PUBLIC_SOCKET_API_KEY || ''}
defaultSourceNetwork={chain?.id || optimism.id}
defaultDestNetwork={optimism.id}
defaultDestToken={NATIVE_TOKEN}
defaultDestToken={NATIVE_TOKEN_ADDRESS}
customize={{
primary: hexToRgb(palette.components.bg),
secondary: hexToRgb(palette.components.bg),
Expand Down
9 changes: 7 additions & 2 deletions components/BridgeContent/SocketTxHistory/AssetAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import Image from 'next/image';
import formatNumber from 'utils/formatNumber';
import { formatUnits } from 'viem';

type Props = { asset: Asset; amount: number; mobile?: boolean; chains?: Chain[] };
type Props = {
asset: Pick<Asset, 'chainId' | 'decimals' | 'logoURI' | 'symbol'>;
amount: number;
mobile?: boolean;
chains?: Chain[];
};

const AssetAmount = ({ asset, amount, mobile, chains }: Props) => {
const chain = useMemo(() => {
Expand All @@ -17,7 +22,7 @@ const AssetAmount = ({ asset, amount, mobile, chains }: Props) => {
<Box display={'flex'} flexDirection={'column'} gap={1.5}>
<Box display={'flex'} alignItems={'center'} height={16} gap={0.5}>
<Image
src={asset.logoURI}
src={asset.logoURI || ''}
alt={asset.symbol}
width={mobile ? 24 : 16}
height={mobile ? 24 : 16}
Expand Down
2 changes: 1 addition & 1 deletion components/DropdownMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const DropdownMenu = <T,>({
},
}}
>
{Object.values(options).map((o) => (
{options.map((o) => (
<OptionItem
key={JSON.stringify(o, (_, value) => (typeof value === 'bigint' ? String(value) : value))}
onClick={() => {
Expand Down
97 changes: 97 additions & 0 deletions components/GetExa/AssetSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';

import DropdownMenu from 'components/DropdownMenu';
import { Asset } from 'types/Bridge';
import { optimism } from 'wagmi/chains';
import { Box, Skeleton, Typography } from '@mui/material';
import Image from 'next/image';
import useBalance from 'hooks/useBalance';
import formatNumber from 'utils/formatNumber';
import { useGetEXA } from 'contexts/GetEXAContext';
import useSocketAssets from 'hooks/useSocketAssets';

type AssetOptionProps = {
asset?: Asset;
option?: boolean;
optionSize?: number;
selectedSize?: number;
chainId: number;
};

function AssetOption({ asset, option = false, optionSize = 17, selectedSize = 14, chainId }: AssetOptionProps) {
const size = option ? optionSize : selectedSize;
const balance = useBalance(asset?.symbol, asset?.address as `0x${string}`, true, chainId);

if (!asset) {
return <Skeleton width={80} />;
}

return (
<Box
display="flex"
gap={1}
my={0.5}
mx={option ? 0.5 : 0}
alignContent="center"
alignItems="center"
justifyContent="center"
>
{asset.logoURI && (
<Image
src={asset.logoURI}
alt={asset.symbol}
width={size}
height={size}
style={{
borderRadius: '100%',
}}
/>
)}
<>
{option ? (
<>
<Box display="flex" alignItems="center">
<Box>
<Typography fontWeight={600} fontSize={14} color="grey.900">
{asset.name}
</Typography>
<Typography fontSize={10} color="grey.900">
{asset.symbol}
</Typography>
</Box>
</Box>
<Box ml="auto">
<Typography fontSize={8}>{balance && Number(balance) ? formatNumber(balance) : ''}</Typography>
</Box>
</>
) : (
<Typography fontWeight={600} fontSize={14} color="grey.900">
{asset.symbol}
</Typography>
)}
</>
</Box>
);
}

function AssetSelector() {
const { t } = useTranslation();
const { setAsset, asset, chain } = useGetEXA();
const assets = useSocketAssets();

if (!assets) return null;

return (
<DropdownMenu
label={t('Asset')}
options={assets}
onChange={setAsset}
renderValue={<AssetOption asset={asset} chainId={chain?.chainId || optimism.id} />}
renderOption={(o: Asset) => <AssetOption option asset={o} chainId={chain?.chainId || optimism.id} />}
data-testid="modal-asset-selector"
/>
);
}

export default memo(AssetSelector);
79 changes: 79 additions & 0 deletions components/GetExa/ChainSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { memo, useCallback } from 'react';
import { useTranslation } from 'react-i18next';

import { Box, Skeleton, Typography } from '@mui/material';
import DropdownMenu from 'components/DropdownMenu';
import { useGetEXA } from 'contexts/GetEXAContext';
import Image from 'next/image';
import { Chain } from 'types/Bridge';
type AssetOptionProps = {
chain?: Chain;
option?: boolean;
optionSize?: number;
selectedSize?: number;
};

function ChainOption({ chain, option = false, optionSize = 17, selectedSize = 14 }: AssetOptionProps) {
const size = option ? optionSize : selectedSize;

if (!chain) {
return <Skeleton width={80} />;
}

return (
<Box
display="flex"
gap={0.5}
my={0.5}
mx={option ? 0.5 : 0}
alignContent="center"
alignItems="center"
justifyContent="center"
>
{chain.icon && (
<Image
src={chain.icon}
alt={chain.name}
width={size}
height={size}
style={{
borderRadius: '100%',
}}
/>
)}
<Typography fontWeight={600} fontSize={size} color="grey.900">
{chain.name}
</Typography>
</Box>
);
}

const ChainSelector = ({ disabled }: { disabled?: boolean }) => {
const { t } = useTranslation();

const { setChain: onChainChange, chains, chain } = useGetEXA();

const handleChainChange = useCallback(
(value: string) => {
const c = chains?.find(({ name }) => value === name);
if (c) onChainChange(c);
},
[chains, onChainChange],
);

if (!chains) return <Skeleton width={100} />;

return (
<DropdownMenu
label={t('Asset')}
options={chains.map(({ name }) => name)}
onChange={handleChainChange}
renderValue={<ChainOption chain={chain} />}
renderOption={(o: string) => <ChainOption option chain={chains.find(({ name }) => o === name)} />}
data-testid="modal-asset-selector"
disabled={disabled}
/>
);
};

export default memo(ChainSelector);
Loading

0 comments on commit 08edac1

Please sign in to comment.