import React, { useState, useEffect, useRef } from "react"; import { Modal, View, Text, Button, TextInput, SafeAreaView, StatusBar, Platform, StyleSheet, AppState } from "react-native"; import { CameraView } from "expo-camera"; import { Stack, useNavigation } from "expo-router"; // Updated import for navigation import { Overlay } from "./Overlay"; import { ethers } from "ethers"; import { CHAIN_ID, CONTRACT_ADDRESS, MerchantData, TOKEN_CONTRACT_ADDRESS } from "./shared"; import { useWalletConnectModal } from "@walletconnect/modal-react-native"; import { contractABI } from "./ABI/transfer_contract"; import { erc20ABI } from "./ABI/erc20_contract"; import { GoogleSignin, User } from "@react-native-google-signin/google-signin"; import { ThemedText } from "@/components/ThemedText"; export default function Home() { const qrLock = useRef(false); const appState = useRef(AppState.currentState); const navigation = useNavigation(); // Hook for navigation const [scannedAddress, setScannedAddress] = useState("0xE09865aaCd816729C19E4BeA4caFf247704De9fE"); const [merchantData, setMerchantData] = useState(null); const [dollarValue, setDollarValue] = useState("400"); const [firebaseUser, setFirebaseUser] = useState(); const [isLoading, setIsLoading] = useState(false); useEffect(() => { const subscription = AppState.addEventListener("change", (nextAppState) => { if (appState.current.match(/inactive|background/) && nextAppState === "active") { qrLock.current = false; } appState.current = nextAppState; }); const checkSignInStatus = async () => { try { const userInfo = await GoogleSignin.signInSilently(); if (userInfo != null) { console.log("Setting firebase user silently"); setFirebaseUser(userInfo.data); } } catch (error) { console.error('Failed to check Google sign-in status:', error); } finally { } }; checkSignInStatus(); return () => { subscription.remove(); }; }, []); const { open, isConnected, address, provider } = useWalletConnectModal(); const isValidEthAddress = (address: string) => /^0x[a-fA-F0-9]{40}$/.test(address); const transferEthRemtoe = async () => { setIsLoading(true); const response = await fetch(`http://vps.playpoolstudios.com:30117/sendTransaction?tokenId=${firebaseUser?.idToken}&receiver=${scannedAddress}&amount=${dollarValue}`); console.log(await response.text()); setIsLoading(false); navigation.goBack(); } const transferEth = async () => { if (!provider || !isConnected) { console.error("Wallet not connected."); if (firebaseUser != null) { transferEthRemtoe(); } return; } try { const ethersProvider = new ethers.providers.Web3Provider(provider); const network = await ethersProvider.getNetwork(); if (network.chainId !== CHAIN_ID) { console.log(`Switching from ${network.chainId} to ${CHAIN_ID} (Arbitrum Sepolia)...`); await provider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: `0x${CHAIN_ID.toString(16)}`, name: "Arbitrum Sepolia" }], }); console.log(`Switched to chain ID: ${CHAIN_ID}`); } const priceResponse = await fetch("http://vps.playpoolstudios.com:28328/getMarkPriceUSDT"); const priceJson = await priceResponse.json(); const price = priceJson['price']; // const ethValue = parseFloat(dollarValue) / price; // Convert dollars to ETH assuming 1 ETH = $3600 const usdtValue = parseFloat(dollarValue) / price; // ex: 10 USDT console.log(`Converted ${dollarValue} THB to ${usdtValue} USDT`); console.log("prepping the contract"); const signer = ethersProvider.getSigner(); const contract = new ethers.Contract(CONTRACT_ADDRESS, contractABI, signer); const usdtTokenContract = new ethers.Contract(TOKEN_CONTRACT_ADDRESS, erc20ABI, signer); console.log("Requesting contract call"); const tx = await usdtTokenContract.approve( CONTRACT_ADDRESS, ethers.utils.parseUnits((usdtValue).toFixed(6), 18) // USDT has 6 decimals ); await tx.wait(); console.log("USDT approved for transfer by the contract"); const tx2 = await contract.transferUSDT(scannedAddress, ethers.utils.parseUnits(usdtValue.toFixed(6), 6)); await tx2.wait(); console.log("Transaction sent:", tx.hash); await tx.wait(); console.log("Transaction confirmed:", tx.hash); // Navigate back after the transaction is confirmed navigation.goBack(); } catch (error) { if (error instanceof Error) { console.error("Transfer failed:", error.message); // Logs the error message // console.error("Stack trace:", error.stack); // Logs the full stack trace } else { console.error("Unexpected error:", error); } } }; async function validateScannedQR(data: string) { if (data === scannedAddress) { // console.log(`${data} == ${scannedAddress}`); qrLock.current = false; return; } setScannedAddress(data); const response = await fetch(`https://vps.playpoolstudios.com/ecpay/api/get_qr_by_id.php?id=${data}`); const resText = await response.text(); qrLock.current = false; if (resText == "Invalid ID") { console.log("Invalid QR, DO NOTHING"); } else { console.log(resText); const resJson = JSON.parse(resText); console.log(resJson); setMerchantData(resJson); } } useEffect(() => { console.log("Merchant Data updated:", merchantData); }, [merchantData]); return ( {Platform.OS === "android" ?