Skip to content

Commit

Permalink
Add layerswap bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
junjieit committed Aug 28, 2024
1 parent 011142e commit f4b4137
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 73 deletions.
4 changes: 2 additions & 2 deletions packages/dodoex-widgets/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dodoex/widgets",
"version": "2.6.12-beta.5",
"version": "2.6.12-beta.6",
"description": "DODO Widgets",
"source": "src/index.tsx",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -138,4 +138,4 @@
"ts-jest": "^29.0.1",
"typescript": "^4.7.3"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,37 @@ import { connectWalletBtn } from '../../../../constants/testId';
import SwitchChainDialog from '../../../SwitchChainDialog';
import { useWalletState } from '../../../../hooks/ConnectWallet/useWalletState';
import useTonConnectStore from '../../../../hooks/ConnectWallet/TonConnect';
import { useWeb3React } from '@web3-react/core';

export interface ConnectWalletProps {
needSwitchChain?: ChainId;
needEvmChainId?: ChainId;
/** If true is returned, the default wallet connection logic will not be executed */
onConnectWalletClick?: () => boolean | Promise<boolean>;
needConnectTwoWallet?: boolean;
}

export default function ConnectWallet({
needSwitchChain,
needEvmChainId,
onConnectWalletClick,
needConnectTwoWallet,
}: ConnectWalletProps) {
const { chainId } = useWalletState();
const tonChainId = useTonConnectStore((state) => state.connected?.chainId);
const { chainId: evmChainId } = useWeb3React();
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const [openSwitchChainDialog, setOpenSwitchChainDialog] = useState(false);

let needConnectChainId = undefined as undefined | ChainId;
if (needEvmChainId) {
if (!tonChainId) {
needConnectChainId = ChainId.TON;
} else if (!evmChainId || needEvmChainId !== evmChainId) {
needConnectChainId = needEvmChainId;
}
} else if (needSwitchChain !== chainId) {
needConnectChainId = needSwitchChain;
}
return (
<>
<Button
Expand All @@ -33,7 +47,7 @@ export default function ConnectWallet({
if (onConnectWalletClick) {
// switch chain
if (
!needConnectTwoWallet &&
!needEvmChainId &&
chainId &&
needSwitchChain &&
chainId !== needSwitchChain
Expand All @@ -49,7 +63,7 @@ export default function ConnectWallet({
return;
}
}
if (needSwitchChain !== ChainId.TON) {
if (needConnectChainId !== ChainId.TON) {
setOpen(true);
} else {
setLoading(true);
Expand All @@ -62,9 +76,9 @@ export default function ConnectWallet({
>
{loading ? (
<Trans>Connecting...</Trans>
) : needConnectTwoWallet ? (
) : needConnectChainId ? (
<Trans>
Connect to a {needSwitchChain === ChainId.TON ? 'Ton' : 'EVM'}{' '}
Connect to a {needConnectChainId === ChainId.TON ? 'Ton' : 'EVM'}{' '}
wallet
</Trans>
) : (
Expand All @@ -74,7 +88,7 @@ export default function ConnectWallet({
<ConnectWalletDialog
open={open}
onClose={() => setOpen(false)}
chainId={needSwitchChain}
chainId={needConnectChainId}
/>
{/* switch chain */}
<SwitchChainDialog
Expand Down
17 changes: 9 additions & 8 deletions packages/dodoex-widgets/src/components/Swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -700,22 +700,23 @@ export function Swap({
]);

const swapButton = useMemo(() => {
const needConnectTwoWallet =
fromToken?.chainId === ChainId.TON || toToken?.chainId === ChainId.TON;

let needEvmChainId = undefined as undefined | ChainId;
const isFromTon = fromToken?.chainId === ChainId.TON;
const isToTon = toToken?.chainId === ChainId.TON;
if (!(isFromTon && isToTon) && (isFromTon || isToTon)) {
needEvmChainId = isFromTon ? toToken?.chainId : fromToken?.chainId;
}
if (
!account ||
(fromToken?.chainId && chainId !== fromToken.chainId) ||
(needConnectTwoWallet && !tonConnect.connected) ||
(needEvmChainId && !tonConnect.connected) ||
!web3React.account
)
return (
<ConnectWallet
needSwitchChain={
needConnectTwoWallet ? toToken?.chainId : fromToken?.chainId
}
needSwitchChain={fromToken?.chainId}
onConnectWalletClick={onConnectWalletClick}
needConnectTwoWallet={needConnectTwoWallet}
needEvmChainId={needEvmChainId}
/>
);
if (isInflight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,10 @@ export function useFetchRoutePriceBridge({
);
const needLayerSwap = useMemo(
() =>
fromToken?.chainId === ChainId.TON &&
fromToken?.chainId !== toToken?.chainId,
(fromToken?.chainId === ChainId.TON &&
fromToken?.chainId !== toToken?.chainId) ||
(toToken?.chainId === ChainId.TON &&
fromToken?.chainId !== toToken?.chainId),
[fromToken, toToken],
);

Expand Down Expand Up @@ -544,10 +546,18 @@ export function useFetchRoutePriceBridge({
}, [status, fromAmount, bridgeRouteList, orbiterRouter, layerSwapRouter]);

const statusRes = useMemo(() => {
if (orbiterRouter) return orbiterStatus;
if (needLayerSwap) return layerSwapRouter.status;
if (needOrbiterQuery) return orbiterStatus;
return status;
}, [status, orbiterStatus, needOrbiterQuery, needLayerSwap, layerSwapRouter]);
}, [
status,
orbiterStatus,
needOrbiterQuery,
needLayerSwap,
layerSwapRouter,
orbiterRouter,
]);

const limit = useMemo<null | {
minAmt?: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function getQuote({
toToken: TokenInfo | null;
fromNetworkName: string | undefined | null;
toNetworkName: string | undefined | null;
slippage: number;
slippage?: number;
fromAmount: string;
}) {
if (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query';
import BigNumber from 'bignumber.js';
import React from 'react';
import { ChainId } from '../../../constants/chains';
import {
BridgeRouteI,
RoutePriceStatus,
Expand All @@ -19,6 +20,7 @@ import {
getQuote,
submitTonWalletWithdraw,
} from './data';
import { Network } from './types';

function convertAvgCompletionTime(completionTime: string) {
const timeStr = completionTime.split('.')[0];
Expand All @@ -39,37 +41,58 @@ export function useLayerswapRouters({
fromAmount: string;
evmAccount: string | undefined;
tonAccount: string | undefined;
slippage: number;
slippage?: number;
};
}) {
const networkQuery = useQuery({
queryKey: ['layerSwap', 'networkQuery'],
enabled: !skip,
queryFn: getNetworks,
});
const fromNetworkName = 'TON_MAINNET';
const toNetwork = toToken
? networkQuery.data?.find(
let fromNetwork = null as Network | null | undefined;
if (fromToken) {
if (fromToken.chainId === ChainId.TON) {
fromNetwork = networkQuery.data?.find(
(item) => item.name === 'TON_MAINNET',
);
} else {
fromNetwork = networkQuery.data?.find(
(item) =>
item.chain_id === String(fromToken.chainId) && item.type === 'evm',
);
}
}
const fromNetworkName = fromNetwork?.name;
let toNetwork = null as Network | null | undefined;
if (toToken) {
if (toToken.chainId === ChainId.TON) {
toNetwork = networkQuery.data?.find(
(item) => item.name === 'TON_MAINNET',
);
} else {
toNetwork = networkQuery.data?.find(
(item) =>
item.chain_id === String(toToken.chainId) && item.type === 'evm',
)
: null;
);
}
}
const toNetworkName = toNetwork?.name;

const limitQuery = useQuery({
queryKey: [
'layerSwap',
'limitQuery',
fromToken?.address,
toToken?.address,
toNetwork?.name,
toNetworkName,
],
enabled: !skip && !!(fromToken && toToken && toNetwork),
enabled: !skip && !!(fromToken && toToken && toNetworkName),
queryFn: () =>
getLimits({
fromToken,
toToken,
fromNetworkName,
toNetworkName: toNetwork?.name,
toNetworkName,
}),
});

Expand All @@ -79,18 +102,18 @@ export function useLayerswapRouters({
'quoteQuery',
fromToken?.address,
toToken?.address,
toNetwork?.name,
toNetworkName,
fromAmount,
],
enabled: !skip && !!(fromToken && toToken && toNetwork && fromAmount),
enabled: !skip && !!(fromToken && toToken && toNetworkName && fromAmount),
refetchInterval: refreshTime,
queryFn: async () => {
if (!fromToken || !toToken || !toNetwork) return;
if (!fromToken || !toToken || !toNetworkName) return;
const data = await getQuote({
fromToken,
toToken,
fromNetworkName,
toNetworkName: toNetwork?.name,
toNetworkName,
fromAmount,
slippage,
});
Expand Down Expand Up @@ -140,7 +163,7 @@ export function useLayerswapRouters({
fromToken,
toToken,
fromNetworkName,
toNetworkName: toNetwork.name,
toNetworkName,
fromAmount,
params,
}),
Expand Down
Loading

0 comments on commit f4b4137

Please sign in to comment.