Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/components/Swap/Swap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,7 @@
.mytonswap-app.rtl {
direction: rtl;
}

.raw-bottom-telegram {
padding-bottom: var(--telegram-padding-bottom, 8px) !important;
}
17 changes: 3 additions & 14 deletions lib/components/Swap/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SwapDetails from '../SwapDetails/SwapDetails';
import clsx from 'clsx';
import { TonConnectUI } from '@tonconnect/ui-react';
import { useOptionsStore, SwapOptions } from '../../store/options.store';
import { ModalState, useSwapStore } from '../../store/swap.store';
import { useSwapStore } from '../../store/swap.store';
import { useWalletStore } from '../../store/wallet.store';
import SwapButton from '../SwapButton/SwapButton';
import './Swap.scss';
Expand Down Expand Up @@ -92,7 +92,7 @@ export const SwapComponent: FC<SwapProps> = ({
}
const {
setWallet,
refetch,

wallet: stateWallet,
disconnect,
} = useWalletStore();
Expand All @@ -115,24 +115,16 @@ export const SwapComponent: FC<SwapProps> = ({
const {
initializeApp,
receive_token,
refetchBestRoute,
swapModal,

pay_token,
} = useSwapStore();

const isInitMount = useRef(true);

useEffect(() => {
if (isInitMount) {
let refetchInterval: ReturnType<typeof setInterval>;
if (!pay_token) {
initializeApp();
refetchInterval = setInterval(() => {
if (swapModal === ModalState.NONE) {
refetch();
refetchBestRoute();
}
}, 10000);
}
if (
(window as NewWindow)?.Telegram?.WebApp?.initData?.length !== 0
Expand All @@ -158,9 +150,6 @@ export const SwapComponent: FC<SwapProps> = ({
setOnSwap(onSwap);
}
isInitMount.current = false;
return () => {
clearInterval(refetchInterval);
};
}
}, []);

Expand Down
61 changes: 28 additions & 33 deletions lib/components/SwapButton/SwapButton.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
import { useWalletStore } from "../../store/wallet.store";
import { ModalState, useSwapStore } from "../../store/swap.store";
import { AnimatePresence, motion } from "framer-motion";
import ErrorTonConnect from "./ErrorTonConnect";
import ConfirmationModal from "./ConfirmationModal";
import WaitingForWallet from "./WaitingForWallet";
import Inprogress from "./Inprogress";
import Done from "./Done";
import "./SwapButton.scss";
import { useMediaQuery, useOnClickOutside } from "usehooks-ts";
import { useWalletStore } from '../../store/wallet.store';
import { ModalState, useSwapStore } from '../../store/swap.store';
import { AnimatePresence, motion } from 'framer-motion';
import ErrorTonConnect from './ErrorTonConnect';
import ConfirmationModal from './ConfirmationModal';
import WaitingForWallet from './WaitingForWallet';
import Inprogress from './Inprogress';
import Done from './Done';
import './SwapButton.scss';
import { useMediaQuery } from 'usehooks-ts';
import {
modalAnimationDesktop,
modalAnimationMobile,
WIDGET_VERSION,
} from "../../constants";
import { useLongPress } from "@uidotdev/usehooks";
import toast from "react-hot-toast";
import { useOptionsStore } from "../../store/options.store";
import { useTranslation } from "react-i18next";
import { cn } from "../../utils/cn";
import { useRef } from "react";
import { CgSpinnerTwo } from "react-icons/cg";
} from '../../constants';
import { useLongPress } from '@uidotdev/usehooks';
import toast from 'react-hot-toast';
import { useOptionsStore } from '../../store/options.store';
import { useTranslation } from 'react-i18next';
import { cn } from '../../utils/cn';
import { CgSpinnerTwo } from 'react-icons/cg';

const SwapButton = () => {
const { t } = useTranslation();
const { tonConnectInstance } = useOptionsStore();

const refModal = useRef(null);
useOnClickOutside(refModal, () => [setModalState(ModalState.NONE)]);

const isDesktop = useMediaQuery("(min-width: 768px)");
const isDesktop = useMediaQuery('(min-width: 768px)');
const { balance } = useWalletStore();
const {
pay_amount,
Expand All @@ -47,16 +43,16 @@ const SwapButton = () => {
<CgSpinnerTwo className="animate-loading" /> Loading ...
</span>
);
if (!tonConnectInstance?.wallet) return t("button_text.connect_wallet");
if (!tonConnectInstance?.wallet) return t('button_text.connect_wallet');
if (!receive_token || !pay_token)
return t("button_text.choose_a_token");
if (pay_amount === 0n) return t("button_text.enter_amount");
return t('button_text.choose_a_token');
if (pay_amount === 0n) return t('button_text.enter_amount');
if (bestRoute && !bestRoute.pool_data.status)
return t("button_text.price_impact");
return t('button_text.price_impact');
if (pay_amount > Number(balance.get(pay_token!.address)?.balance ?? 0))
return t("button_text.insufficient_balance");
return t('button_text.insufficient_balance');

return t("button_text.swap");
return t('button_text.swap');
};

const isButtonDisabled = () => {
Expand Down Expand Up @@ -105,8 +101,7 @@ const SwapButton = () => {
>
<motion.div
transition={{ ease: [0.6, -0.05, 0.01, 0.99] }}
className="modal-container-inner"
ref={refModal}
className="modal-container-inner raw-bottom-telegram"
initial={modalAnimation.initial}
animate={modalAnimation.animate}
exit={modalAnimation.exit}
Expand All @@ -132,12 +127,12 @@ const SwapButton = () => {
</AnimatePresence>
<button
className={cn(
"swap-button",
'swap-button',
tonConnectInstance?.wallet &&
isRouteAvailable &&
!bestRoute.pool_data.status
? "price-impact"
: ""
? 'price-impact'
: ''
)}
data-testid="swap-button"
{...attrs}
Expand Down
24 changes: 23 additions & 1 deletion lib/store/swap.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import catchError from '../utils/catchErrors';
import { WIDGET_VERSION } from '../constants';
import { reportErrorWithToast } from '../services/errorAnalytics';
import { useEventsStore } from './events.store';
import { useWalletStore } from './wallet.store';

export enum ModalState {
NONE = 'NONE',
Expand Down Expand Up @@ -39,6 +40,7 @@ type SwapStates = {
transactionHash: string | null;
transactionQueryId: string | null;
pinnedTokens: Asset[] | null;
refetchInterval: ReturnType<typeof setInterval> | null;
};

type SwapActions = {
Expand Down Expand Up @@ -71,6 +73,7 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
client: new MyTonSwapClient({
headers: { 'widget-version': WIDGET_VERSION },
}),
refetchInterval: null,
pay_token: null,
pay_rate: null,
pay_amount: 0n,
Expand Down Expand Up @@ -359,14 +362,33 @@ export const useSwapStore = create<SwapActions & SwapStates>((set, get) => ({
},

async initializeApp() {
const { client, slippage, setPayAmount } = get();
const {
client,
slippage,
setPayAmount,
refetchBestRoute,
refetchInterval,
} = get();
const {
default_pay_token,
default_receive_token,
pin_tokens,
default_pay_amount,
} = useOptionsStore.getState().options;

if (refetchInterval) {
clearInterval(refetchInterval);
}
set({
refetchInterval: setInterval(() => {
const modalState = useSwapStore.getState().swapModal;
if (modalState === ModalState.NONE) {
useWalletStore.getState().refetch();
refetchBestRoute();
}
}, 10000),
});

const getAsset = async (
tokenAddress: string | undefined,
fallback: string
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"sideEffects": false,
"scripts": {
"dev": "vite",
"prebuild": "rm -rf dist",
"prebuild": "rimraf dist",
"build": "tsc -b && vite build && vite build --config vite.config.cdn.js",
"lint": "eslint .",
"preview": "vite preview",
Expand All @@ -60,8 +60,6 @@
"cypress:run": "cypress run --browser chrome"
},
"peerDependencies": {
"@mytonswap/sdk": "^1.0.12",
"@ton/ton": "^15.0.0",
"clsx": "^2.1.1",
"react": "^18.3.1",
"react-dom": "^18.3.1"
Expand Down Expand Up @@ -99,6 +97,7 @@
"prettier-plugin-css-order": "^2.1.2",
"process": "^0.11.10",
"react-icons": "^5.3.0",
"rimraf": "^6.0.1",
"sass": "^1.80.1",
"storybook": "^8.3.6",
"tailwindcss": "^3.4.13",
Expand All @@ -116,9 +115,11 @@
]
},
"dependencies": {
"@mytonswap/sdk": "^1.1.0",
"@r2wc/react-to-web-component": "^2.0.3",
"@sentry/react": "^8.42.0",
"@sentry/vite-plugin": "^2.22.7",
"@ton/ton": "^15.1.0",
"@tonconnect/ui-react": "^2.0.9",
"@uidotdev/usehooks": "^2.4.1",
"axios": "^1.7.7",
Expand Down
Loading
Loading