From 47f30f961276451c64c4f2324d79df0b4afea7ab Mon Sep 17 00:00:00 2001 From: Teri-anric <2005ahi2005@gmail.com> Date: Mon, 16 Dec 2024 19:58:47 +0200 Subject: [PATCH 1/4] load wallet id from webapp --- frontend/src/App.jsx | 41 +++++++++---------------------- frontend/src/services/telegram.js | 6 ++--- 2 files changed, 15 insertions(+), 32 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 3a600107..a5f72e88 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -9,7 +9,7 @@ import Login from 'pages/Login'; import Form from 'pages/forms/Form'; import { createPortal } from 'react-dom'; import { logout } from 'services/wallet'; -import { saveTelegramUser, getTelegramUserWalletId } from 'services/telegram'; +import { getTelegramUserWalletId } from 'services/telegram'; import Documentation from 'pages/spotnet/documentation/Documentation'; import Withdraw from 'pages/vault/withdraw/Withdraw'; import { useWalletStore } from 'stores/useWalletStore'; @@ -21,35 +21,11 @@ import Stake from 'pages/vault/stake/Stake'; function App() { const { walletId, setWalletId, removeWalletId } = useWalletStore(); - const [tgUser, setTgUser] = useState(JSON.parse(localStorage.getItem('tg_user'))); const [showModal, setShowModal] = useState(false); const navigate = useNavigate(); const connectWalletMutation = useConnectWallet(setWalletId); - useEffect(() => { - if (tgUser) { - saveTelegramUser(tgUser, walletId) - .then(() => console.log('Telegram user saved successfully')) - .catch((error) => console.error('Error saving Telegram user:', error)); - } - }, [walletId, tgUser]); - useEffect(() => { - if (!tgUser) { - localStorage.removeItem('tg_user'); - return; - } - if (!walletId) { - getTelegramUserWalletId(tgUser) - .then((fetchedWalletId) => { - if (fetchedWalletId) { - setWalletId(fetchedWalletId); - } - }) - .catch((error) => console.error('Error fetching wallet ID:', error)); - localStorage.setItem('tg_user', JSON.stringify(tgUser)); - } - }, [tgUser, walletId, setWalletId]); const handleConnectWallet = () => { connectWalletMutation.mutate(); @@ -70,6 +46,15 @@ function App() { setShowModal(false); }; + useEffect(() => { + if (window.Telegram?.WebApp?.initData?.user) { + getTelegramUserWalletId(window.Telegram.WebApp.initData.user.id).then((linked_wallet_id) => { + setWalletId(linked_wallet_id); + window.Telegram.WebApp.ready(); + }); + } + }); + return (
@@ -87,19 +72,17 @@ function App() { document.body )}
} /> - : } /> - } /> + } /> } /> } /> } /> diff --git a/frontend/src/services/telegram.js b/frontend/src/services/telegram.js index 07e0f324..7dcd90b8 100644 --- a/frontend/src/services/telegram.js +++ b/frontend/src/services/telegram.js @@ -17,10 +17,10 @@ export const saveTelegramUser = async (telegramUser, walletId) => { } }; -export const getTelegramUserWalletId = async (tg_user) => { +export const getTelegramUserWalletId = async (telegram_id) => { try { - const response = await axiosInstance.post(`/api/telegram/get-wallet-id/${tg_user.id}`, { - raw: window.Telegram.initData || tg_user, + const response = await axiosInstance.post(`/api/telegram/get-wallet-id/${telegram_id}`, { + raw: window.Telegram.initData, is_webapp: !!window.Telegram.initData }); return response.data.wallet_id; From 79fa3cded4df1348152a8b7bde9f4bd296635d50 Mon Sep 17 00:00:00 2001 From: Teri-anric <2005ahi2005@gmail.com> Date: Mon, 16 Dec 2024 21:06:00 +0200 Subject: [PATCH 2/4] fix used telegram init data --- frontend/src/App.jsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index a5f72e88..d2fdb7ff 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -47,8 +47,8 @@ function App() { }; useEffect(() => { - if (window.Telegram?.WebApp?.initData?.user) { - getTelegramUserWalletId(window.Telegram.WebApp.initData.user.id).then((linked_wallet_id) => { + if (window.Telegram?.WebApp?.initData) { + getTelegramUserWalletId(window.Telegram.WebApp.initDataUnsafe.user.id).then((linked_wallet_id) => { setWalletId(linked_wallet_id); window.Telegram.WebApp.ready(); }); From 51ffaf08fba76fc947104b6bb4dae007436c6df7 Mon Sep 17 00:00:00 2001 From: Teri-anric <2005ahi2005@gmail.com> Date: Mon, 16 Dec 2024 21:28:55 +0200 Subject: [PATCH 3/4] test --- frontend/src/App.jsx | 7 ++++++- web_app/telegram/handlers/command.py | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index d2fdb7ff..43926cc1 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -18,6 +18,7 @@ import { useConnectWallet } from 'hooks/useConnectWallet'; import OverviewPage from 'pages/spotnet/overview/Overview'; import { ActionModal } from 'components/ui/ActionModal'; import Stake from 'pages/vault/stake/Stake'; +import { notifyError } from 'utils/notification'; function App() { const { walletId, setWalletId, removeWalletId } = useWalletStore(); @@ -51,9 +52,13 @@ function App() { getTelegramUserWalletId(window.Telegram.WebApp.initDataUnsafe.user.id).then((linked_wallet_id) => { setWalletId(linked_wallet_id); window.Telegram.WebApp.ready(); + }).catch((error) => { + console.error('Error getting Telegram user wallet ID:', error); + notifyError('Error loading wallet'); + window.Telegram.WebApp.ready(); }); } - }); + }, [window.Telegram?.WebApp?.initDataUnsafe]); return (
diff --git a/web_app/telegram/handlers/command.py b/web_app/telegram/handlers/command.py index 8efa6cbd..280bdea0 100644 --- a/web_app/telegram/handlers/command.py +++ b/web_app/telegram/handlers/command.py @@ -31,6 +31,7 @@ async def notification_allowed(message: Message, command: CommandObject): """ user_id = command.args user = db_connector.get_object(User, user_id) + telegram_db.update_telegram_user(str(message.from_user.id), dict(wallet_id=user.wallet_id)) telegram_db.set_allow_notification(str(message.from_user.id), user.wallet_id) return await message.answer( From 76956a9123f162176ceffc432175d8294767994d Mon Sep 17 00:00:00 2001 From: Teri-anric <2005ahi2005@gmail.com> Date: Mon, 16 Dec 2024 23:02:47 +0200 Subject: [PATCH 4/4] fix getTelegramUserWalletId --- frontend/src/services/telegram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/services/telegram.js b/frontend/src/services/telegram.js index 7dcd90b8..4bffd423 100644 --- a/frontend/src/services/telegram.js +++ b/frontend/src/services/telegram.js @@ -20,8 +20,8 @@ export const saveTelegramUser = async (telegramUser, walletId) => { export const getTelegramUserWalletId = async (telegram_id) => { try { const response = await axiosInstance.post(`/api/telegram/get-wallet-id/${telegram_id}`, { - raw: window.Telegram.initData, - is_webapp: !!window.Telegram.initData + raw: window.Telegram.WebApp.initData, + is_webapp: !!window.Telegram.WebApp.initData }); return response.data.wallet_id; } catch (error) {