From 9aa70dfdb74ce835526e8fb5a0e05a8f149588e7 Mon Sep 17 00:00:00 2001 From: Satvik1769 Date: Fri, 8 Dec 2023 01:48:47 +0530 Subject: [PATCH 1/4] resolved sendETH --- .env.example | 2 +- app/_components/BtnStartGame.js | 3 +- app/_components/GameScreen.js | 50 ++- app/_components/header.js | 35 ++ app/ethereum.js | 25 ++ app/games/[id]/{ => [walletAddress]}/page.js | 6 +- .../[id]/{ => [walletAddress]}/result/page.js | 2 +- .../0ba343146aa1a3861a39c5b209f50dc9.json | 1 - .../414c8279420911d3fc65c57f34e8b19e.json | 1 + artifacts/contracts/Lock.sol/Lock.dbg.json | 2 +- artifacts/contracts/Lock.sol/Lock.json | 162 +-------- cache/solidity-files-cache.json | 4 +- contracts/Lock.sol | 83 ++--- foo.js | 3 + hardhat.config.js | 7 +- next.config.js | 4 +- package-lock.json | 307 ++++++++++++++++-- package.json | 4 +- public/metamask.png | Bin 0 -> 2006 bytes scripts/deploy.js | 54 +-- test/Lock.js | 147 ++------- 21 files changed, 520 insertions(+), 382 deletions(-) create mode 100644 app/ethereum.js rename app/games/[id]/{ => [walletAddress]}/page.js (63%) rename app/games/[id]/{ => [walletAddress]}/result/page.js (89%) delete mode 100644 artifacts/build-info/0ba343146aa1a3861a39c5b209f50dc9.json create mode 100644 artifacts/build-info/414c8279420911d3fc65c57f34e8b19e.json create mode 100644 foo.js create mode 100644 public/metamask.png diff --git a/.env.example b/.env.example index 3514bd8..b6d9af6 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ -PRISMA_URL="Url comes here" +PRISMA_URL="posgresql://postgres:treasurehunt_123_@db.siecxsbqymysnrfdinzh.supabase.co.5432/postgres" NEXT_PUBLIC_SITE_URL=http://localhost:3000 NEXTAUTH_SECRET=secretishere \ No newline at end of file diff --git a/app/_components/BtnStartGame.js b/app/_components/BtnStartGame.js index ba82b67..b506d35 100644 --- a/app/_components/BtnStartGame.js +++ b/app/_components/BtnStartGame.js @@ -5,6 +5,7 @@ import useSound from "./useSound"; import { useEffect } from "react"; const BtnStartGame = () => { + const walletAddress = localStorage.getItem("account"); // const bgMusic = useSound("bg"); // useEffect(() => { // window.addEventListener("click", () => { @@ -20,7 +21,7 @@ const BtnStartGame = () => { className="hover relative h-[80px] w-[80px] md:h-[100px] md:w-[100px]" style={{ top: "38%", left: "5px" }} > - + start game { bgMusic.play(); return () => { @@ -62,6 +73,38 @@ function GameScreen({ id, screen = "screen1", items = dummyItems }) { bgMusic.currentTime = 0; }; }, []); + + useEffect(() => { + async function initContract() { + const contract = getContract( + "0xfB40daE09C54ec8eB653f8cb140e0569202e3ac2", + Lock.abi, + 0 + ); + setContract(contract); + } + initContract(); + }, []); + + async function sendETH(walletAddress, amountToSend) { + try { + if (!contract) return; + const tx = await contract.sendEther(walletAddress, amountToSend, { + gasLimit: 50000, + }); + const receipt = await tx.wait(); + const transactionHash = receipt.transactionHash; + console.log("Transaction Hash:", transactionHash); + if (receipt.status === 1) { + console.log("ETH sent successfully!"); + } else { + console.error("Transaction failed"); + } + } catch (error) { + console.error(error); + } + } + const winSound = useSound("win", 0, 1, false); const height = 672; const router = useRouter(); @@ -76,7 +119,10 @@ function GameScreen({ id, screen = "screen1", items = dummyItems }) { if (itemIndex > items.length - 1) { setTimeout(() => { alert("You won!"); - router.push(`/games/${id}/result`); + const parsedAmount = ethers.parseEther("0.0001"); + const stringAmount = parsedAmount.toString(); + sendETH(walletAddress, stringAmount); + router.push(`/games/${id}/${walletAddress}/result`); }, 3000); return; } diff --git a/app/_components/header.js b/app/_components/header.js index 852be91..aab72e8 100644 --- a/app/_components/header.js +++ b/app/_components/header.js @@ -1,10 +1,45 @@ +"use client"; import ButtonLogout from "./ButtonLogout"; import Logo from "./Logo"; +import Image from "next/image"; +import { useState, useEffect } from "react"; const Header = () => { + const [wallet, setWallet] = useState(""); + const [account, setAccount] = useState(""); + + useEffect(() => { + const storedAccount = localStorage.getItem("account"); + if (storedAccount) { + setAccount(storedAccount); + } + }, []); + + async function Connect() { + console.log("connecting..."); + if (window.ethereum) { + console.log("ethereum"); + const accounts = await window.ethereum.request({ + method: "eth_requestAccounts", + }); + console.log(accounts); + setWallet(accounts[0]); + localStorage.setItem("account", accounts[0]); + } else { + console.log("not ethereum"); + } + } return (
+
); diff --git a/app/ethereum.js b/app/ethereum.js new file mode 100644 index 0000000..b405da7 --- /dev/null +++ b/app/ethereum.js @@ -0,0 +1,25 @@ +import { ethers } from "ethers"; + +const localProvider = new ethers.JsonRpcProvider( + "https://eth-sepolia.g.alchemy.com/v2/gYfnM2-fUz9hK-kuLqKisnmaU4U4xO8u" +); +const private_key = + "aa62de3f5c5de102bafe19bbc908b21e3d687bf426daf49ac361d75efcbca28a"; + +export const getProvider = () => { + return localProvider; +}; + +export const getSigner = () => { + const wallet = new ethers.Wallet(private_key, localProvider); + console.log(wallet); + const signer = wallet.connect(localProvider); + + return signer; +}; + +export const getContract = (address, abi) => { + const signer = getSigner(); + const contract = new ethers.Contract(address, abi, signer); + return contract; +}; diff --git a/app/games/[id]/page.js b/app/games/[id]/[walletAddress]/page.js similarity index 63% rename from app/games/[id]/page.js rename to app/games/[id]/[walletAddress]/page.js index bee515d..cb7a695 100644 --- a/app/games/[id]/page.js +++ b/app/games/[id]/[walletAddress]/page.js @@ -1,11 +1,11 @@ -import GameScreen from "../../_components/GameScreen"; +import GameScreen from "../../../_components/GameScreen"; const page = ({ params }) => { - const { id } = params; + const { id, walletAddress } = params; return ( <> {/*
*/}
- +
{/*

Game ID: {id}

*/} {/* */} diff --git a/app/games/[id]/result/page.js b/app/games/[id]/[walletAddress]/result/page.js similarity index 89% rename from app/games/[id]/result/page.js rename to app/games/[id]/[walletAddress]/result/page.js index ca6e668..28f0fbb 100644 --- a/app/games/[id]/result/page.js +++ b/app/games/[id]/[walletAddress]/result/page.js @@ -6,7 +6,7 @@ const page = () => { return (

Congratulations! You have won the game!

-

Top 3 winners will get ethereum rewards.

+

You will get 0.0001 ETH.