Skip to content

Commit

Permalink
Add IP to user and show cube points in Feed
Browse files Browse the repository at this point in the history
issue #162
  • Loading branch information
chiliec committed Sep 15, 2024
1 parent 472f9f7 commit 48b22bf
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/backend/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const authHandler = (fastify: FastifyInstance, _options: unknown, done: (
wallet: user.wallet,
referalId: user.referalId,
balance: user.votes.toString(),
ip: request.ip,
}
} catch (error_) {
return { error: error_ }
Expand Down
2 changes: 1 addition & 1 deletion src/bot/features/admin/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fromNano } from "@ton/core"
import { BalanceChangeType } from "#root/bot/models/balance"
import { addPoints, findUserByName } from "../../models/user"
import { findTransaction } from "../../models/transaction"
import { tonToPoints } from "../../helpers/ton"
import { tonToPoints } from "../../helpers/points"
import { sendMessageToAdmins, sendPlaceInLine } from "../../helpers/telegram"

const composer = new Composer<Context>()
Expand Down
18 changes: 18 additions & 0 deletions src/bot/helpers/points.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function tonToPoints(ton: number): bigint {
let rate = 100_000
const halvingDate = new Date(2024, 4, 15, 0, 0, 0)
const currentDate = new Date()
if (currentDate > halvingDate) {
rate = 50_000
}
let points = BigInt(Math.round(ton * rate))
if (points === BigInt(0)) {
points = BigInt(1)
}
return points
}

export function usdToPoints(usd: number): bigint {
const tonUSDPrice = 5
return tonToPoints(usd / tonUSDPrice)
}
14 changes: 0 additions & 14 deletions src/bot/helpers/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,3 @@ export function isUserAddressValid(a: Address): boolean {
return false
}
}

export function tonToPoints(ton: number): bigint {
let rate = 100_000
const halvingDate = new Date(2024, 4, 15, 0, 0, 0)
const currentDate = new Date()
if (currentDate > halvingDate) {
rate = 50_000
}
let points = BigInt(Math.round(ton * rate))
if (points === BigInt(0)) {
points = BigInt(1)
}
return points
}
15 changes: 8 additions & 7 deletions src/bot/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,11 @@ export async function placeInWhales(votes: bigint): Promise<number | undefined>
return count
}

export async function addPoints(userId: number, add: bigint, reason: BalanceChangeType) {
export async function addPoints(
userId: number,
add: bigint,
reason: BalanceChangeType,
): Promise<bigint> {
try {
const updatedUser = await UserModel.findOneAndUpdate(
{ id: userId },
Expand All @@ -262,14 +266,11 @@ export async function addPoints(userId: number, add: bigint, reason: BalanceChan
}

const newRecord = await addChangeBalanceRecord(userId, add, reason)
if (logger.level === "debug") {
logger.debug(
`Add ${newRecord.amount} points to ${userId}. Now ${await getAggregatedBalance(userId)}`,
)
}
logger.debug(
`Add ${newRecord.amount} points to user ${userId}. Now ${await getAggregatedBalance(userId)}`,
)

logger.info(`Add ${add} points to ${userId}. Now ${updatedUser.votes}`)
// TODO: save log in db
return updatedUser.votes
} catch (error) {
logger.error(`!!! Can't add points ${add} to user ${userId}`)
Expand Down
16 changes: 8 additions & 8 deletions src/frontend/src/components/Feed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<img :src="item.icon" :alt="item.name" class="feed-icon" />
<div class="feed-text">
<h3>{{ item.name }}</h3>
<p>{{ item.description }}</p>
<p>{{ usdToPoints(item.payout) }} $CUBE</p>
</div>
</div>
<button
Expand All @@ -44,10 +44,12 @@
</template>

<script setup lang="ts" name="FeedComponent">
import { ref, onMounted, Ref, watch, onUpdated } from "vue"
import { useUserStore } from "../stores/userStore"
import useLoadingAndError from "../composables/useLoadingAndError"
import { ref, onMounted, Ref, watch } from "vue"
import axios from "axios"
import useLoadingAndError from "../composables/useLoadingAndError"
import { usdToPoints } from "../../../bot/helpers/points"
import { sleep } from "../../../bot/helpers/time"
import { useUserStore } from "../stores/userStore"
interface TappAdsOffer {
id: number
Expand Down Expand Up @@ -86,7 +88,7 @@ const fetchFeed = async () => {
params: {
apikey: tappAdsKey,
user_id: userStore.user?.id,
ip: "0.0.0.0", // TODO!
ip: userStore.user?.ip ?? "0.0.0.0",
ua: escape(navigator.userAgent),
},
})
Expand All @@ -103,9 +105,7 @@ const handleClick = async (item: any) => {
try {
loadingInstance.visible.value = true
window.open(item.url, "_blank", "noopener,noreferrer")
await new Promise((resolve) => {
setTimeout(resolve, 1000)
})
await sleep(1000)
const res = await axios.get(item.click_postback)
if (res.data.is_done === true) {
item.is_done = true
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FAQComponent from "./components/FAQ.vue";
import PresentationComponent from "./components/Presentation.vue";
import { fluent } from "./fluent";
import ClickerComponent from "./components/Clicker.vue";
import FeedComponent from "./components/Feed.vue";

const routes = [
// RESERVED: /cnfts /api
Expand All @@ -20,6 +21,7 @@ const routes = [
{ path: "/faq", name: "FAQPage", component: FAQComponent },
{ path: "/cnft", mame: "CNFTPage", component: CNFTComponent },
{ path: "/presentation", mame: "PresentationPage", component: PresentationComponent },
{ path: "/feed", mame: "FeedPage", component: FeedComponent },
];

const router = createRouter({
Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface UserStore {
wallet: string;
referalId: number | undefined;
balance: number | undefined;
ip: string;
}

export const useUserStore = defineStore("userStore", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getLastestTransaction,
} from "#root/bot/models/transaction"
import { AccountSubscription } from "#root/bot/helpers/account-subscription"
import { tonToPoints } from "#root/bot/helpers/ton"
import { tonToPoints } from "#root/bot/helpers/points"
import { sendMessageToAdmins, sendPlaceInLine } from "#root/bot/helpers/telegram"
import { BalanceChangeType } from "./bot/models/balance"

Expand Down

0 comments on commit 48b22bf

Please sign in to comment.