From 735770bfd2b0260c91dc14f0b201fb88d90251f9 Mon Sep 17 00:00:00 2001 From: Dougy Lee Date: Sun, 5 May 2024 15:00:32 +1000 Subject: [PATCH] Update LilyPad --- client/src/Home.js | 40 +++++------ client/src/Home2.js | 144 +++++++++++++++++++++++++++++++++++++++ client/src/NftList.js | 46 +++++++++++++ client/src/SchoolList.js | 77 +++++++++++++++++++++ client/src/index.js | 5 ++ lilypad_module.json.tmpl | 2 +- 6 files changed, 293 insertions(+), 21 deletions(-) create mode 100644 client/src/Home2.js create mode 100644 client/src/SchoolList.js diff --git a/client/src/Home.js b/client/src/Home.js index 406f2a2..dcbb7c7 100644 --- a/client/src/Home.js +++ b/client/src/Home.js @@ -25,26 +25,26 @@ export default function Home() { price: 100, training: 0, }, - { - tokenId: 2, - name: "Machina", - imageUrl: - "https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng", - brain: "https://www.google.com", - description: "An upgraded bot", - price: 100, - training: 500, - }, - { - tokenId: 3, - name: "Beast", - imageUrl: - "https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1", - brain: "https://www.google.com", - description: "A battle hardened bot", - price: 100, - training: 1000, - }, + // { + // tokenId: 2, + // name: "Machina", + // imageUrl: + // "https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng", + // brain: "https://www.google.com", + // description: "An upgraded bot", + // price: 100, + // training: 500, + // }, + // { + // tokenId: 3, + // name: "Beast", + // imageUrl: + // "https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1", + // brain: "https://www.google.com", + // description: "A battle hardened bot", + // price: 100, + // training: 1000, + // }, ]; const [nftData, setNftData] = useState(defaultNftData); diff --git a/client/src/Home2.js b/client/src/Home2.js new file mode 100644 index 0000000..973b5df --- /dev/null +++ b/client/src/Home2.js @@ -0,0 +1,144 @@ +import NftList from "./NftList"; +import Scaffold from "./Scaffold"; +import { HttpProvider, Web3 } from "web3"; +import abi from "./abi/sarsa.json"; +import { useState } from "react"; + +export default function Home2() { + const [connectedAccount, setConnectedAccount] = useState(null); + + const provider = new HttpProvider( + "https://sepolia.infura.io/v3/d6d31fd77d604b18a171665fc35d45d6" + ); + const web3 = new Web3(window.ethereum); + + const contractAddress = "0x4B9E83Fa8f5C5C4402F49F2AbBBD61DDBA75c9b7"; + + const defaultNftData = [ + { + tokenId: 1, + name: "Botson", + imageUrl: + "https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv", + brain: "https://www.google.com", + description: "A bot that can do anything it sets it mind to", + price: 100, + training: 0, + }, + { + tokenId: 2, + name: "Machina", + imageUrl: + "https://ipfs.io/ipfs/QmVCYBq5Sasjp99MD6YMKp7xJwSxpxXjTCf85ekKaReFng", + brain: "https://www.google.com", + description: "An upgraded bot", + price: 100, + training: 500, + }, + { + tokenId: 3, + name: "Beast", + imageUrl: + "https://ipfs.io/ipfs/QmSjfLd4h7PzJQ9RARUpr4z5918qZ4ziK9RFUnNGLefsR1", + brain: "https://www.google.com", + description: "A battle hardened bot", + price: 100, + training: 1000, + }, + ]; + + const [nftData, setNftData] = useState(defaultNftData); + const miner = null; + + const mintRobot = async () => { + console.log("Minting a new robot"); + + const contract = new web3.eth.Contract(abi, contractAddress); + contract.setProvider(provider); + const symbol = await contract.methods.symbol().call(); + console.log(symbol); + + const x = await contract.methods.x().call(); + console.log(x); + + await mintNFT(contract, "image-uri", "brain-uri"); + }; + + async function mintNFT(contract, imageURI, brainURI) { + try { + const transaction = contract.methods.createBot(imageURI, brainURI); + const value = web3.utils.toWei("0.01", "ether"); + const options = { + from: connectedAccount, + value: value, + }; + + const receipt = await transaction.send(options); + console.log("Transaction receipt:", receipt); + } catch (error) { + console.error("Error minting NFT:", error); + } + } + + async function connectMetamask() { + //check metamask is installed + if (window.ethereum) { + // instantiate Web3 with the injected provider + const web3 = new Web3(window.ethereum); + + //request user to connect accounts (Metamask will prompt) + await window.ethereum.request({ method: "eth_requestAccounts" }); + + //get the connected accounts + const accounts = await web3.eth.getAccounts(); + + //show the first connected account in the react page + setConnectedAccount(accounts[0]); + } else { + alert("Please download metamask"); + } + } + + function onTrain() { + setNftData([ + { + tokenId: 1, + name: "Botson", + imageUrl: + "https://ipfs.io/ipfs/QmaG3Bqcy8mu1QwPmnp8gMEoN2N9fN99e2UGFKPgg7JLPv", + brain: "https://www.google.com", + description: "A bot that can do anything it sets it mind to", + price: 100, + training: 100, + }, + ]); + } + + return ( + + {!connectedAccount && ( + + )} + + {connectedAccount &&

{connectedAccount}

} + + +
+ ); +} diff --git a/client/src/NftList.js b/client/src/NftList.js index 3abcf7c..03e3e28 100644 --- a/client/src/NftList.js +++ b/client/src/NftList.js @@ -2,8 +2,43 @@ import { WrenchScrewdriverIcon, ArrowTrendingUpIcon, } from "@heroicons/react/20/solid"; +import React, { useState } from "react"; export default function NftList({ nftData, onTrain, mineLink }) { + const [showData, setShowData] = useState(false); + + const Action = { + UP: 0, + DOWN: 1, + LEFT: 2, + RIGHT: 3, + }; + + function createQTable(rows, cols, numActions) { + // Initialize the Q-table as a 3D array + const qTable = new Array(rows) + .fill(null) + .map(() => + new Array(cols) + .fill(null) + .map(() => new Array(numActions).fill(0)) + ); + + // Set higher values for the DOWN action + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + qTable[row][col][Action.DOWN] = 1 + 0.1; // Higher values for DOWN + qTable[row][col][Action.UP] = 0.1; + qTable[row][col][Action.LEFT] = -1; + qTable[row][col][Action.RIGHT] = 0.42; + } + } + + return qTable; + } + + const data = JSON.stringify(createQTable(5, 5, 4)); + return ( ); } diff --git a/client/src/SchoolList.js b/client/src/SchoolList.js new file mode 100644 index 0000000..3abcf7c --- /dev/null +++ b/client/src/SchoolList.js @@ -0,0 +1,77 @@ +import { + WrenchScrewdriverIcon, + ArrowTrendingUpIcon, +} from "@heroicons/react/20/solid"; + +export default function NftList({ nftData, onTrain, mineLink }) { + return ( + + ); +} diff --git a/client/src/index.js b/client/src/index.js index b3a9b58..4a565ef 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -5,6 +5,7 @@ import "./index.css"; import Scaffold from "./Scaffold"; import Mine from "./Mine"; import Home from "./Home"; +import Home2 from "./Home2"; import RunGameScreen1 from "./RunGameScreen1"; import RunGameScreen2 from "./RunGameScreen2"; @@ -13,6 +14,10 @@ const router = createBrowserRouter([ path: "/", element: , }, + { + path: "/2", + element: , + }, { path: "mine1", element: ( diff --git a/lilypad_module.json.tmpl b/lilypad_module.json.tmpl index ee465f2..53a1ecd 100644 --- a/lilypad_module.json.tmpl +++ b/lilypad_module.json.tmpl @@ -14,7 +14,7 @@ "Entrypoint": [ "/bin/sh", "-c", "python lilypad_sarsa.py" ], - "Image": "zeath/sarsa:latest" + "Image": "zeath/sarsa:latest@sha256:9e3996b171ab8080425d40bf5c82f3c102c84d56c2fa9f355754f16d513f67f4" }, "Engine": "Docker", "Network": {