Skip to content

Commit

Permalink
Merge pull request #7 from klntsky/vishtar/bypass_rollback
Browse files Browse the repository at this point in the history
Bypass rollback
  • Loading branch information
Vishtar authored Jul 8, 2024
2 parents efae2d9 + 580b23e commit 6168f72
Show file tree
Hide file tree
Showing 36 changed files with 377 additions and 181 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TONAPI_TOKEN=
NOTIFICATION_RATE_UP=2
NOTIFICATION_RATE_DOWN=0.5
LIMIT_WALLETS_FOR_USER=10
SECONDS_FROM_PURCHASE_WITH_ROLLBACK_POSSIBILITY=60
# AMQP
AMQP_ENDPOINT=amqp://localhost:5673
# PostgreSQL
Expand Down
12 changes: 11 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useEffect, useState } from 'react'
import { useTonConnectModal, useTonConnectUI } from '@tonconnect/ui-react'
// TODO: replace '@tma.js/sdk-react' (deprecated) with '@telegram-apps/sdk-react'
import {
bindMiniAppCSSVars,
bindThemeParamsCSSVars,
useMiniApp,
useThemeParams,
useViewport,
} from '@tma.js/sdk-react'
import { retrieveLaunchParams } from '@tma.js/sdk'

Expand All @@ -22,10 +24,10 @@ export const App = () => {
const [tonConnectUI] = useTonConnectUI()
const themeParams = useThemeParams()
const miniApp = useMiniApp()
const miniAppViewport = useViewport()
const { mutate } = usePostData()
const { t } = useTranslation()
const [walletsCount, setWalletsCount] = useState(0)
miniApp.ready()

const onClickLinkAnothgerWalletButton = async () => {
if (tonConnectUI.connected) {
Expand All @@ -41,6 +43,12 @@ export const App = () => {
}
}

useEffect(() => {
if (miniAppViewport) {
miniAppViewport.expand()
}
}, [miniAppViewport])

useEffect(() => {
return bindMiniAppCSSVars(miniApp, themeParams)
}, [miniApp, themeParams])
Expand All @@ -56,6 +64,8 @@ export const App = () => {
// }, [modal.state.status]);

useEffect(() => {
miniApp.ready()

tonConnectUI.onStatusChange(wallet => {
if (!wallet?.account.address) return
const launchParams = retrieveLaunchParams()
Expand Down
16 changes: 6 additions & 10 deletions frontend/src/components/Charts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const Charts = (props: {

if (isLoading) {
return (
<Flex justifyContent="center" alignItems="center" className="h-screen">
<Flex justifyContent="center" alignItems="center" className="h-dvh">
<Loader />
</Flex>
)
Expand All @@ -45,25 +45,23 @@ export const Charts = (props: {
if (!data) {
return (
<Flex justifyContent="center" alignItems="center">
<h2 className="text-2xl text-slate-600">{t('label.error')}</h2>
<h2 className="text-2xl">{t('label.error')}</h2>
</Flex>
)
}

if (!data.jettons.length) {
return (
<Flex justifyContent="center" alignItems="center">
<h2 className="text-2xl text-slate-600">{t('label.noJettons')}</h2>
<h2 className="text-2xl">{t('label.noJettons')}</h2>
</Flex>
)
}

if (data) {
return (
<div className={s.charts}>
<h1
className={`${s.yourJettonsHeader} w-full text-3xl text-slate-700 text-center`}
>
<h1 className={`${s.yourJettonsHeader} w-full text-3xl text-center`}>
{t('label.yourJettons')}
</h1>
{data
Expand All @@ -82,15 +80,13 @@ export const Charts = (props: {
className="shadow rounded-full max-w-full h-auto align-middle border-none"
/>
</div>
<h1
className={`${s.symbolHeader} text-2xl text-slate-500 pl-2`}
>
<h1 className={`${s.symbolHeader} text-2xl pl-2`}>
{obj.symbol}
</h1>
</Flex>
{obj.pnlPercentage !== 0 ? (
<div className="flex">
<h3 className="text-xl text-slate-500 pr-2">{}</h3>
<h3 className="text-xl pr-2">{}</h3>
<BadgeDelta
size="lg"
deltaType={badgeType(obj.pnlPercentage)}
Expand Down
8 changes: 0 additions & 8 deletions frontend/src/components/Charts/style.module.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
.charts {
width: 100%;
}

.yourJettonsHeader {
color: var(--tg-theme-text-color) !important;
}

.symbolHeader {
color: white !important;
}
5 changes: 3 additions & 2 deletions frontend/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ html {
.App {
margin: 0 auto;
padding: 1rem;
padding-bottom: 12px;
max-width: 560px;
width: 100%;
min-height: 100vh;
overflow-x: hidden;
min-height: 100%;
/* overflow-x: hidden; */
color: var(--tg-theme-text-color);
display: flex;
flex-direction: column;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/types/TJettonData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export type TJettonData = {
address: string
symbol: string
image?: string
decimals: number
pnlPercentage: number
chart: [timestamp: number, price: number | string][]
chart: [timestamp: number, price: number][]
lastBuyTime: number
}
59 changes: 0 additions & 59 deletions frontend/src/utils.ts

This file was deleted.

10 changes: 10 additions & 0 deletions frontend/src/utils/badgeType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const badgeType = (input: number) => {
if (input < 0) {
return 'moderateDecrease'
}
if (input > 0) {
return 'moderateIncrease'
} else {
return 'unchanged'
}
}
9 changes: 9 additions & 0 deletions frontend/src/utils/chartColor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TChartData } from '../types'

export const chartColor = (arr: TChartData[]) => {
if (Number(arr[0].Price) >= Number(arr.at(-1)!.Price)) {
return ['red']
} else {
return ['emerald']
}
}
12 changes: 12 additions & 0 deletions frontend/src/utils/formatDataToChart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { normalizePrice, timeConverter } from '.'

export const formatDataToChart = (input: {
chart: [number, number][]
decimals: number
}) =>
input.chart.reverse().map(arr => {
return {
date: timeConverter(arr[0]),
Price: normalizePrice(arr[1], input.decimals),
}
})
5 changes: 5 additions & 0 deletions frontend/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { normalizePrice } from './normalizePrice'
export { timeConverter } from './timeConverter'
export { chartColor } from './chartColor'
export { formatDataToChart } from './formatDataToChart'
export { badgeType } from './badgeType'
2 changes: 2 additions & 0 deletions frontend/src/utils/normalizePrice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const normalizePrice = (price: number, decimals?: number) =>
price > 0.01 ? Number(price.toFixed(2)) : price.toFixed(decimals || 20)
26 changes: 26 additions & 0 deletions frontend/src/utils/timeConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function timeConverter(UNIX_timestamp: number) {
const a = new Date(UNIX_timestamp * 1000)
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
]
const year = a.getFullYear()
const month = months[a.getMonth()]
const date = a.getDate()
const hour = a.getHours()
const min = a.getMinutes()
const sec = a.getSeconds()
const time =
date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec
return time
}
7 changes: 5 additions & 2 deletions src/db/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { insertUserAdress } from './insertUserAdress'
export { upsertToken } from './upsertToken'
export { selectLastUserNotificationByWalletAndJetton } from './selectLastUserNotificationByWalletAndJetton'
export { selectLastUserPurchaseByWalletAndJetton } from './selectLastUserPurchaseByWalletAndJetton'
export { selectLastUserNotificationByJettonId } from './selectLastUserNotificationByJettonId'
export { selectLastUserPurchaseByJettonId } from './selectLastUserPurchaseByJettonId'
export { deleteJettonByWallet } from './deleteJettonByWallet'
export { insertUserPurchase } from './insertUserPurchase'
export { insertUserNotification } from './insertUserNotification'
Expand All @@ -13,3 +13,6 @@ export { countUserWallets } from './countUserWallets'
export { selectUserWallets } from './selectUserWallets'
export { deleteUserWallets } from './deleteUserWallets'
export { deleteUserWallet } from './deleteUserWallet'
export { selectFirstUserPurchaseByJettonId } from './selectFirstUserPurchaseByJettonId'
export { selectWalletById } from './selectWalletById'
export { selectTokenByAddressAndWalletId } from './selectTokenByAddressAndWalletId'
7 changes: 5 additions & 2 deletions src/db/queries/insertUserAdress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { wallets } from '../../db/schema'
import type { TDbConnection } from '../../types'
import type { TDbConnection, TDbTransaction } from '../../types'

export const insertUserAdress = (db: TDbConnection, values: typeof wallets.$inferInsert) => {
export const insertUserAdress = (
db: TDbConnection | TDbTransaction,
values: typeof wallets.$inferInsert,
) => {
return db.insert(wallets).values(values).onConflictDoNothing().returning()
}
4 changes: 2 additions & 2 deletions src/db/queries/insertUserPurchase.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TDbConnection } from '../../types'
import type { TDbConnection, TDbTransaction } from '../../types'
import { userPurchases } from '../schema'

export const insertUserPurchase = (
db: TDbConnection,
db: TDbConnection | TDbTransaction,
values: typeof userPurchases.$inferInsert,
) => {
return db.insert(userPurchases).values(values)
Expand Down
14 changes: 14 additions & 0 deletions src/db/queries/selectFirstUserPurchaseByJettonId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { asc, eq } from 'drizzle-orm'
import { userPurchases } from '../../db/schema'
import type { TDbConnection } from '../../types'

export const selectFirstUserPurchaseByJettonId = async (db: TDbConnection, jettonId: number) => {
const [firstPurchase] = await db
.select()
.from(userPurchases)
.where(eq(userPurchases.jettonId, jettonId))
.orderBy(asc(userPurchases.timestamp))
.limit(1)

return firstPurchase
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { desc, eq } from 'drizzle-orm'
import { userNotifications } from '../../db/schema'
import type { TDbConnection } from '../../types'

export const selectLastUserNotificationByWalletAndJetton = async (
db: TDbConnection,
jettonId: number,
) => {
export const selectLastUserNotificationByJettonId = async (db: TDbConnection, jettonId: number) => {
const [lastNotification] = await db
.select()
.from(userNotifications)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { desc, eq } from 'drizzle-orm'
import { userPurchases } from '../../db/schema'
import type { TDbConnection } from '../../types'

export const selectLastUserPurchaseByWalletAndJetton = async (
db: TDbConnection,
jettonId: number,
) => {
export const selectLastUserPurchaseByJettonId = async (db: TDbConnection, jettonId: number) => {
const [lastPurchase] = await db
.select()
.from(userPurchases)
Expand Down
14 changes: 14 additions & 0 deletions src/db/queries/selectTokenByAddressAndWalletId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { and, eq } from 'drizzle-orm'
import type { TDbConnection } from '../../types'
import { tokens } from '../schema'

export const selectTokenByAddressAndWalletId = (
db: TDbConnection,
address: string,
walletId: number,
) => {
return db
.select()
.from(tokens)
.where(and(eq(tokens.token, address), eq(tokens.walletId, walletId)))
}
7 changes: 7 additions & 0 deletions src/db/queries/selectWalletById.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { eq } from 'drizzle-orm'
import type { TDbConnection } from '../../types'
import { wallets } from '../schema'

export const selectWalletById = (db: TDbConnection, walletId: number) => {
return db.select().from(wallets).where(eq(wallets.id, walletId))
}
7 changes: 5 additions & 2 deletions src/db/queries/upsertToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { TDbConnection } from '../../types'
import type { TDbConnection, TDbTransaction } from '../../types'
import { tokens } from '../schema'

export const upsertToken = (db: TDbConnection, values: typeof tokens.$inferInsert) => {
export const upsertToken = (
db: TDbConnection | TDbTransaction,
values: typeof tokens.$inferInsert,
) => {
return db.insert(tokens).values(values).onConflictDoNothing()
}
Loading

0 comments on commit 6168f72

Please sign in to comment.