diff --git a/app/(tabs)/index.tsx b/app/(tabs)/index.tsx index 77152ab..ebf08ab 100644 --- a/app/(tabs)/index.tsx +++ b/app/(tabs)/index.tsx @@ -3,7 +3,7 @@ import { Image, StyleSheet, Pressable, Platform, View, Alert, ActivityIndicator, import { useCameraPermissions } from 'expo-camera'; import Link, { } from 'expo-router/link'; import { ethers } from 'ethers'; - +import { Stack, useNavigation, useRouter } from "expo-router"; import ParallaxScrollView from '@/components/ParallaxScrollView'; import { ThemedText } from '@/components/ThemedText'; import { ThemedView } from '@/components/ThemedView'; @@ -29,7 +29,8 @@ export default function HomeScreen() { const activeAddress = isConnected ? address : embeddedWallet; const [ethBalance, setEthBalance] = useState(null); const [usdtBalance, setUsdtBalance] = useState(null); - + const navigation = useNavigation(); + const router = useRouter(); useEffect(() => { const fetchBalances = async () => { if (activeAddress) { @@ -215,6 +216,14 @@ export default function HomeScreen() { } }; + const openScanner = ()=>{ + router.push({pathname:"/scanner"}); + } + + const openHistory= ()=>{ + router.push({pathname:"/transactions"}); + } + return ( <> - + [ styles.scanButton, { opacity: pressed ? 0.5 : 1 }, + {backgroundColor:"transparent"} ]} > Scan - + [ + styles.scanButton, + { opacity: pressed ? 0.5 : 1 }, + {backgroundColor:"transparent"} + ]} + > + + Transaction History + + ) : ( Grant Permission @@ -460,4 +486,4 @@ const styles = StyleSheet.create({ fontSize: 14, // Smaller font for ETH/USDT labels color: 'gray', }, -}); +}); \ No newline at end of file diff --git a/app/(tabs)/scanner.tsx b/app/(tabs)/scanner.tsx index 932f616..e6f4017 100644 --- a/app/(tabs)/scanner.tsx +++ b/app/(tabs)/scanner.tsx @@ -4,22 +4,23 @@ 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, TOKEN_CONTRACT_ADDRESS } from "./shared"; +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 [modalVisible, setModalVisible] = useState(false); const [scannedAddress, setScannedAddress] = useState("0xE09865aaCd816729C19E4BeA4caFf247704De9fE"); + const [merchantData, setMerchantData] = useState(null); const [dollarValue, setDollarValue] = useState("400"); const [firebaseUser, setFirebaseUser] = useState(); - const [isLoading, setIsLoading]= useState(false); + const [isLoading, setIsLoading] = useState(false); useEffect(() => { const subscription = AppState.addEventListener("change", (nextAppState) => { @@ -44,8 +45,6 @@ export default function Home() { }; checkSignInStatus(); - transferEth(); - return () => { subscription.remove(); }; @@ -53,11 +52,11 @@ export default function Home() { const { open, isConnected, address, provider } = useWalletConnectModal(); - const isValidEthAddress = (address:string) => /^0x[a-fA-F0-9]{40}$/.test(address); + 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}`); + 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(); @@ -67,7 +66,7 @@ export default function Home() { if (!provider || !isConnected) { console.error("Wallet not connected."); - if(firebaseUser!=null){ + if (firebaseUser != null) { transferEthRemtoe(); } return; @@ -80,7 +79,7 @@ export default function Home() { 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" }], + params: [{ chainId: `0x${CHAIN_ID.toString(16)}`, name: "Arbitrum Sepolia" }], }); console.log(`Switched to chain ID: ${CHAIN_ID}`); } @@ -90,7 +89,7 @@ export default function Home() { 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 + const usdtValue = parseFloat(dollarValue) / price; // ex: 10 USDT console.log(`Converted ${dollarValue} THB to ${usdtValue} USDT`); console.log("prepping the contract"); @@ -125,7 +124,36 @@ export default function Home() { } }; - + 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 ( @@ -142,50 +170,62 @@ export default function Home() { onBarcodeScanned={({ data }) => { if (data && !qrLock.current) { qrLock.current = true; - setScannedAddress(data); // Save the scanned address - setModalVisible(true); // Open the popup/modal + validateScannedQR(data); // Save the scanned address + // setModalVisible(true); // Open the popup/modal } }} /> - {/* Loading Modal */} - {isLoading && ( - - - - Loading... - - - - )} + {/* Loading Modal */} + {isLoading && ( + + + + Loading... + + + + )} setModalVisible(false)} // Close modal on back action + visible={merchantData !== null}//make this come up when merchant data is set + onRequestClose={() => { setMerchantData(null); setScannedAddress(""); }} // Close modal on back action > + Confirm Payment for {merchantData?.username} + { + merchantData?.price == "0" ? + <> Enter Amount - - - THB + + + THB - + + + : + <> + Amount : {merchantData?.price} THB -