Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sraibaby committed Sep 17, 2024
2 parents cc48f0f + e07f72b commit e628ba7
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_ENV="development"
VITE_TELEGRAM_BOT_URL=""
VITE_TELEGRAM_MANIFEST_URL=""
VITE_TON_CONTRACT_ADDRESS=""
VITE_TON_CONTRACT_ADDRESS=""
VITE_GTAG_ID=""
3 changes: 2 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VITE_ENV="production"
VITE_TELEGRAM_BOT_URL="https://t.me/x2lotterybot"
VITE_TELEGRAM_BOT_URL="t.me/x2lotterybot/play"
VITE_TELEGRAM_MANIFEST_URL="https://gradoally.github.io/x2lottery/tonconnect-manifest.json"
VITE_TON_CONTRACT_ADDRESS="EQCnuaUFX1fxduNk5QvUMlexRmwHM08jfO5_zpQpkXjeWbL7"
VITE_GTAG_ID="G-0R6KDGMBJS"
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
/>

<!-- Google tag (gtag.js) -->
<script
async
src="https://www.googletagmanager.com/gtag/js?id=%VITE_GTAG_ID%"
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());

gtag("config", "%VITE_GTAG_ID%");
</script>

<title>x2Lottery</title>
</head>
<body class="bg-gray-950 text-white h-screen select-none">
Expand Down
2 changes: 2 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { store } from './common/store'
import MoneyBackground from './components/common/MoneyBackground.vue';
const router = useRouter()
Expand All @@ -21,6 +22,7 @@ onMounted(async () => {
</script>

<template>
<MoneyBackground />

<div class="relative z-10">
<div class="flex flex-col h-[100vh]">
Expand Down
85 changes: 67 additions & 18 deletions src/common/store/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
import { Reactive, reactive } from "vue";
import { TonConnectUI, TonConnectUiOptions, Account } from "@tonconnect/ui";
import { InitDataParsed, retrieveLaunchParams } from "@telegram-apps/sdk";
import { Address, toNano, TonClient } from "@ton/ton";
import { Address, Cell, fromNano, toNano, TonClient } from "@ton/ton";
import { HeadsOrTails } from "../../wrappers/HeadsOrTails";
import { TonConnectSender } from "../../wrappers/TonConnectSender";

interface Telegram {
walletAccount: Account | null | undefined;
tonConnectUI: TonConnectUI | null;
tonClient: TonClient | null;
initDataRaw: string | undefined | null;
initData: InitDataParsed | undefined | null;
init: () => Promise<void>;
initTelegramData: () => void;
initWallet: () => Promise<void>;
initConnectWalletButton: (buttonRootId: string | null) => Promise<void>;
playGame: (amount: number) => Promise<void>;
initClient: () => Promise<void>;
playGame: (amount: number) => Promise<string | null>;
getData: () => Promise<{ min_bet: string; max_bet: string } | null>;
getWalletBalance: () => Promise<string | null>;
}

export const Telegram: Reactive<Telegram> = reactive<Telegram>({
walletAccount: null,
tonConnectUI: null,
tonClient: null,
initData: null,
initDataRaw: null,
async init() {
this.initTelegramData();
this.initWallet();
this.initClient();
},
initTelegramData() {
const launchParams = retrieveLaunchParams();

this.initDataRaw = launchParams.initDataRaw;
this.initData = launchParams.initData;
try {
const launchParams = retrieveLaunchParams();
this.initDataRaw = launchParams.initDataRaw;
this.initData = launchParams.initData;
} catch (e) {
console.log('not in telegram app');
}
},
async initWallet() {
await this.tonConnectUI?.getWallets();

console.log(this.tonConnectUI);

this.walletAccount = this.tonConnectUI?.account;

console.log(this.walletAccount);
},
async initConnectWalletButton(buttonRootId: string | null) {
this.tonConnectUI = new TonConnectUI({
Expand All @@ -55,8 +60,6 @@ export const Telegram: Reactive<Telegram> = reactive<Telegram>({
} as TonConnectUiOptions;

this.tonConnectUI.onModalStateChange(async (state) => {
console.log(state);

if (
state.status === "closed" &&
state.closeReason === "wallet-selected"
Expand All @@ -66,17 +69,44 @@ export const Telegram: Reactive<Telegram> = reactive<Telegram>({
});

this.tonConnectUI.onStatusChange((status) => {
if(!status) {
if (!status) {
this.walletAccount = null;
}
});

console.log(this.tonConnectUI);
},
async initClient() {
this.tonClient = new TonClient({
endpoint: "https://toncenter.com/api/v2/jsonRPC",
});
},
async playGame(amount: number) {
const collectionAddress = import.meta.env.VITE_TON_CONTRACT_ADDRESS;
const address = Address.parse(collectionAddress);

const contractProvider = this.tonClient?.open(
HeadsOrTails.createFromAddress(address)
);

if (this.tonConnectUI) {
const tonConnectSender = new TonConnectSender(this.tonConnectUI);

await contractProvider?.sendBet(tonConnectSender, toNano(amount));

const cell = Cell.fromBase64(
tonConnectSender.lastTransactionResponse?.boc ?? ""
);
const buffer = cell.hash();
const hashHex = buffer.toString("hex");

return hashHex;
}

return null;
},
async getData() {
const collectionAddress = import.meta.env.VITE_TON_CONTRACT_ADDRESS;
const address = Address.parse(collectionAddress);

const tonClient = new TonClient({
endpoint: "https://toncenter.com/api/v2/jsonRPC",
});
Expand All @@ -86,10 +116,29 @@ export const Telegram: Reactive<Telegram> = reactive<Telegram>({
);

if (this.tonConnectUI) {
return await contractProvider.sendBet(
new TonConnectSender(this.tonConnectUI),
toNano(amount)
const data = (await contractProvider.getData()).beginParse();
data.loadAddress();
const min_bet = fromNano(data.loadCoins());
const max_bet = fromNano(data.loadCoins());

return {
min_bet,
max_bet,
};
}

return null;
},
async getWalletBalance() {
try {
const balance = await this.tonClient?.getBalance(
Address.parse(this.walletAccount?.address ?? "")
);
return fromNano(balance ?? 0);
} catch (error) {
console.error("Error fetching wallet balance:", error);
}

return null;
},
});
2 changes: 1 addition & 1 deletion src/components/common/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const click = () => {
</script>

<template>
<button :class="classObject" class="rounded-md uppercase disabled:bg-gray-500" @click="click" :disabled="props.disabled">
<button :class="classObject" class="text-xl font-black py-4 px-10 rounded-md uppercase disabled:bg-gray-500" @click="click" :disabled="props.disabled">
<slot></slot>
</button>
</template>
Loading

0 comments on commit e628ba7

Please sign in to comment.