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, contractABI } from "./shared"; import { useWalletConnectModal } from "@walletconnect/modal-react-native"; export default function Home() { const qrLock = useRef(false); const appState = useRef(AppState.currentState); const navigation = useNavigation(); // Hook for navigation const [modalVisible, setModalVisible] = useState(false); const [scannedAddress, setScannedAddress] = useState(""); const [dollarValue, setDollarValue] = useState(""); useEffect(() => { const subscription = AppState.addEventListener("change", (nextAppState) => { if (appState.current.match(/inactive|background/) && nextAppState === "active") { qrLock.current = false; } appState.current = nextAppState; }); return () => { subscription.remove(); }; }, []); const { open, isConnected, address, provider } = useWalletConnectModal(); const isValidEthAddress = (address:string) => /^0x[a-fA-F0-9]{40}$/.test(address); const transferEth = async () => { if (!provider || !isConnected) { console.error("Wallet not connected."); 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)}` }], }); console.log(`Switched to chain ID: ${CHAIN_ID}`); } const signer = ethersProvider.getSigner(); const contract = new ethers.Contract(CONTRACT_ADDRESS, contractABI, signer); const priceResponse = await fetch("http://vps.playpoolstudios.com:28328/getMarkPriceETH"); const priceJson = await priceResponse.json(); const price = priceJson['price']; const ethValue = parseFloat(dollarValue) / price; // Convert dollars to ETH assuming 1 ETH = $3600 console.log(`Converted ${dollarValue} THB to ${ethValue} ETH`); console.log("Requesting contract call"); const tx = await contract.transferETH(scannedAddress, { value: ethers.utils.parseEther(ethValue.toFixed(18).toString()), }); 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) { console.error("Transfer failed:", error); } }; return ( {Platform.OS === "android" ?