Skip to content

Commit

Permalink
fix: add app version; refactor tx send flow
Browse files Browse the repository at this point in the history
  • Loading branch information
siandreev committed Dec 13, 2024
1 parent e0c59c2 commit d475fec
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
2 changes: 1 addition & 1 deletion apps/web-swap-widget/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tonkeeper/web-swap-widget",
"version": "3.0.0",
"version": "3.0.1",
"author": "Ton APPS UK Limited <[email protected]>",
"description": "Web swap widget for Tonkeeper",
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions apps/web-swap-widget/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useAccountsStateQuery, useActiveAccountQuery } from '@tonkeeper/uikit/d
import { GlobalStyle } from '@tonkeeper/uikit/dist/styles/globalStyle';
import { FC, PropsWithChildren, Suspense, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { BrowserAppSdk } from './libs/appSdk';
import { WidgetAppSdk } from './libs/appSdk';
import { useAnalytics, useAppHeight, useApplyQueryParams, useAppWidth } from './libs/hooks';
import { useGlobalPreferencesQuery } from '@tonkeeper/uikit/dist/state/global-preferences';
import { useGlobalSetup } from '@tonkeeper/uikit/dist/state/globalSetup';
Expand All @@ -47,7 +47,7 @@ const queryClient = new QueryClient({
}
});

const sdk = new BrowserAppSdk();
const sdk = new WidgetAppSdk();
const TARGET_ENV = 'swap-widget-web';

const queryParams = new URLSearchParams(new URL(window.location.href).search);
Expand Down
62 changes: 37 additions & 25 deletions apps/web-swap-widget/src/components/SwapWidgetPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,44 @@ export const SwapWidgetPage = () => {

const ctx = getTonkeeperInjectionContext()!;

ctx.sendTransaction({
source: ctx.address,
// legacy tonkeeper api, timestamp in ms
valid_until: params.valid_until * 1000,
messages: params.messages.map(m => ({
address: m.address,
amount: m.amount.toString(),
payload: m.payload
})),
messagesVariants: params.messagesVariants
? Object.fromEntries(
Object.entries(params.messagesVariants).map(([k, v]) => [
k,
v.map(m => ({
address: m.address,
amount: m.amount.toString(),
payload: m.payload
}))
])
)
: undefined
})
.then(onOpen)
.catch(notifyError)
.finally(() => setHasBeenSent(false));
setHasBeenSent(true);
try {
const result = await ctx.sendTransaction({
source: ctx.address,
// legacy tonkeeper api, timestamp in ms
valid_until: params.valid_until * 1000,
messages: params.messages.map(m => ({
address: m.address,
amount: m.amount.toString(),
payload: m.payload
})),
messagesVariants: params.messagesVariants
? Object.fromEntries(
Object.entries(params.messagesVariants).map(([k, v]) => [
k,
v.map(m => ({
address: m.address,
amount: m.amount.toString(),
payload: m.payload
}))
])
)
: undefined
});

if (!result) {
throw new Error('Transaction was not confirmed');
}

onOpen();
} catch (e) {
if (e && typeof e === 'object' && 'message' in e && typeof e.message === 'string') {
notifyError(e.message);
} else if (e && typeof e === 'string') {
notifyError(e);
}
}
setHasBeenSent(false);
};

const onChangeFields = () => {
Expand Down
6 changes: 5 additions & 1 deletion apps/web-swap-widget/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import ReactDOM from 'react-dom/client';
import { App } from './App';
import './i18n';
import { WidgetAppSdk } from './libs/appSdk';

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
const rootElement = document.getElementById('root') as HTMLElement;
rootElement.setAttribute('data-app-version', WidgetAppSdk.version);

const root = ReactDOM.createRoot(rootElement);

root.render(<App />);
6 changes: 4 additions & 2 deletions apps/web-swap-widget/src/libs/appSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ function iOS() {
);
}

export class BrowserAppSdk extends BaseApp {
export class WidgetAppSdk extends BaseApp {
static version = packageJson.version ?? 'Unknown';

constructor() {
super(new SwapWidgetStorage());
}
Expand Down Expand Up @@ -42,7 +44,7 @@ export class BrowserAppSdk extends BaseApp {
isStandalone = () =>
iOS() && ((window.navigator as unknown as { standalone: boolean }).standalone as boolean);

version = packageJson.version ?? 'Unknown';
version = WidgetAppSdk.version;

targetEnv = 'web' as const;
}

0 comments on commit d475fec

Please sign in to comment.