From 62eef88f96194e94399a5aea4c0b14d2509d590c Mon Sep 17 00:00:00 2001 From: Benson Liu Date: Fri, 21 Jul 2023 17:55:18 -0700 Subject: [PATCH] added more web3js connector code (can contribute small images) Co-authored-by: Cy Co-authored-by: Chloe <138538887+ChL4859@users.noreply.github.com> Co-authored-by: Trisha Reddy <105570654+trishavreddy@users.noreply.github.com> Co-authored-by: rebekah-wang <138538877+rebekah-wang@users.noreply.github.com> Co-authored-by: anusha --- blockchain/contracts/DataImage.sol | 35 ++++++++++++++ blockchain/contracts/Dataset.sol | 52 +++++++++++++-------- blockchain/contracts/DatasetManager.sol | 18 ++++++-- blockchain/register.py | 8 +++- blockchain/test/.gitkeep | 0 blockchain/truffle-config.js | 2 +- client/src/pages/ContributePage.jsx | 32 ++++++------- client/src/pages/CreateDatasetPage.jsx | 17 +++++-- client/src/pages/DashboardPage.jsx | 61 +++++++++++++++++-------- client/src/pages/DatasetPage.jsx | 1 + client/yarn.lock | 11 +---- 11 files changed, 164 insertions(+), 73 deletions(-) create mode 100644 blockchain/contracts/DataImage.sol delete mode 100644 blockchain/test/.gitkeep diff --git a/blockchain/contracts/DataImage.sol b/blockchain/contracts/DataImage.sol new file mode 100644 index 0000000..4f8072f --- /dev/null +++ b/blockchain/contracts/DataImage.sol @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +contract DataImage { + string value; + string class; + bool approved; + + event DataVerified( + bool approved + ); + + constructor(string memory _value, string memory _class) { + value = _value; + class = _class; + approved = false; + } + + function getValue() public view returns (string memory) { + return value; + } + + function getClass() public view returns (string memory) { + return class; + } + + function getApproved() public view returns (bool) { + return approved; + } + + function approve() public { + approved = true; + emit DataVerified(approved); + } +} \ No newline at end of file diff --git a/blockchain/contracts/Dataset.sol b/blockchain/contracts/Dataset.sol index 1075681..75c69e4 100644 --- a/blockchain/contracts/Dataset.sol +++ b/blockchain/contracts/Dataset.sol @@ -1,38 +1,52 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; +import "./DataImage.sol"; contract Dataset { - uint public imageCount = 0; + uint imageCount = 0; - string public name; - string public description; + string name; + string description; - struct Image { - uint id; - string value; - string class; - bool approved; - } - - Image[] public images; + DataImage[] images; - event DataCreated(uint id, string value, string class, bool approved); + event DataCreated( + string value, + string class, + bool approved + ); - event DataVerified(uint id, bool approved); + event DataVerified( + bool approved + ); constructor(string memory _name, string memory _description) { name = _name; description = _description; } + function getName() public view returns (string memory) { + return name; + } + + function getDescription() public view returns (string memory) { + return description; + } + + function getImageCount() public view returns (uint) { + return imageCount; + } + + function getImage(uint index) public view returns (DataImage) { + return images[index]; + } + function createData(string memory _content, string memory _class) public { imageCount++; - images.push(Image(imageCount, _content, _class, false)); - emit DataCreated(imageCount, _content, _class, false); - } - function approveData(uint _id) public { - images[_id].approved = !images[_id].approved; - emit DataVerified(_id, images[_id].approved); + DataImage image = new DataImage(_content, _class); + images.push(image); + + emit DataCreated(_content, _class, false); } } diff --git a/blockchain/contracts/DatasetManager.sol b/blockchain/contracts/DatasetManager.sol index c8a0e49..2d5c8dc 100644 --- a/blockchain/contracts/DatasetManager.sol +++ b/blockchain/contracts/DatasetManager.sol @@ -3,10 +3,22 @@ pragma solidity ^0.8.0; import "./Dataset.sol"; contract DatasetManager { - uint public datasetCount = 0; - Dataset[] public datasets; + uint datasetCount = 0; - event DatasetCreated(string name, string description); + Dataset[] datasets; + + event DatasetCreated( + string name, + string description + ); + + function getDatasetCount() public view returns (uint) { + return datasetCount; + } + + function getDataset(uint index) public view returns (Dataset) { + return datasets[index]; + } function createDataset(string memory name, string memory description) public { datasetCount++; diff --git a/blockchain/register.py b/blockchain/register.py index 6fb016f..afa6822 100644 --- a/blockchain/register.py +++ b/blockchain/register.py @@ -71,11 +71,13 @@ def register_abi(name, abi_path, cookies): url = BASE_URL + "/api/dataset" + networks = abi.get("networks") + data = { "name": name, "description": "DatasetManager Contract", "abi_id": response.json()["abi"]["id"], - "address": abi.get("networks").get("5777").get("address"), + "address": networks.get(list(networks.keys())[-1]).get("address"), } response = requests.post(url, json=data, cookies=cookies) @@ -87,7 +89,9 @@ def register_abi(name, abi_path, cookies): # register all ABIs in the build/ folder def register_all_abis(cookies): - for file in os.listdir("build/contracts/"): + files = os.listdir("build/contracts/") + files.sort(reverse=True) + for file in files: if file.endswith(".json") and not file.startswith("Migrations"): name = file.split(".")[0] abi_path = "build/contracts/" + file diff --git a/blockchain/test/.gitkeep b/blockchain/test/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/blockchain/truffle-config.js b/blockchain/truffle-config.js index 06e96e3..5987b7a 100644 --- a/blockchain/truffle-config.js +++ b/blockchain/truffle-config.js @@ -66,7 +66,7 @@ module.exports = { // development: { host: "127.0.0.1", // Localhost (default: none) - port: 8545, // Standard Ethereum port (default: none) + port: 7545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) }, // diff --git a/client/src/pages/ContributePage.jsx b/client/src/pages/ContributePage.jsx index 32998f9..d30d1dc 100644 --- a/client/src/pages/ContributePage.jsx +++ b/client/src/pages/ContributePage.jsx @@ -1,5 +1,5 @@ import { useContext, useEffect, useState } from "react"; -// import { useNavigate } from "react-router-dom"; +import { useNavigate } from "react-router-dom"; import web3 from "web3"; import DropdownMenu from "../components/DropdownMenu"; @@ -8,10 +8,13 @@ import { DataContext } from "../components/DataProvider"; import { getABI } from "../api/abi"; import styles from "../styles/pages/ContributePage.module.css"; +import { getDataset } from "../api/dataset"; function ImageUploader() { document.title = "Contribute | SeBRUS"; + const navigate = useNavigate(); + const { accounts } = useContext(DataContext); const [image, setImage] = useState(""); @@ -33,7 +36,7 @@ function ImageUploader() { alert("Please upload a file"); return; } - if (file.size > 10000000) { + if (file.size > 1000000) { alert("File size must be less than 10MB"); return; } @@ -70,26 +73,23 @@ function ImageUploader() { alert("Error loading dataset ABI."); return; } + console.log(abi); + console.log(selected); + + let address = selected; window.web3 = new web3(window.ethereum); - let DatasetContract = new window.web3.eth.Contract(abi, selected); - DatasetContract.setProvider(window.ethereum); + let DatasetContract = new window.web3.eth.Contract(abi, address); + + console.log(image); + console.log(label); - DatasetContract.methods + await DatasetContract.methods .createData(image, label) - .send({ from: accounts[0] }) - .on("receipt", function (receipt) { - console.log(receipt); - }) - .on("error", function (error) { - console.log(error); - }) - .on("confirmation", function (confirmationNumber, receipt) { - console.log(confirmationNumber); - console.log(receipt); - }); + .send({ from: window.ethereum.selectedAddress }); alert("Image uploaded successfully!"); + navigate("/dashboard"); }; return ( diff --git a/client/src/pages/CreateDatasetPage.jsx b/client/src/pages/CreateDatasetPage.jsx index 8c383fa..fe94bdb 100644 --- a/client/src/pages/CreateDatasetPage.jsx +++ b/client/src/pages/CreateDatasetPage.jsx @@ -25,26 +25,31 @@ function CreateDatasetPage() { let abi = await getABI("0"); + console.log(abi); + let datasetManager = await getDataset("0"); + console.log(datasetManager); + let address = datasetManager[0].address; - let datasetABI = await getABI("1"); + console.log(address); - if (abi === null || datasetManager === null || datasetABI === null) { + if (abi === null || datasetManager === null || address === null) { alert("Error loading dataset manager or dataset ABI."); return; } window.web3 = new web3(window.ethereum); let DatasetManagerContract = new window.web3.eth.Contract(abi, address); + DatasetManagerContract.setProvider(window.ethereum); await DatasetManagerContract.methods .createDataset(name, description) - .send({ from: accounts[0], gas: 2000000 }); + .send({ from: window.ethereum.selectedAddress }); let datasetCount = await DatasetManagerContract.methods - .datasetCount() + .getDatasetCount() .call(); console.log(datasetCount); @@ -54,9 +59,11 @@ function CreateDatasetPage() { console.log(typeof index); let newAddress = await DatasetManagerContract.methods - .datasets(index) + .getDataset(index) .call(); + console.log(newAddress); + let data = await createDataset(name, description, newAddress, 1); if (data === null) { diff --git a/client/src/pages/DashboardPage.jsx b/client/src/pages/DashboardPage.jsx index 1aac814..5897a22 100644 --- a/client/src/pages/DashboardPage.jsx +++ b/client/src/pages/DashboardPage.jsx @@ -19,24 +19,32 @@ function DashboardPage() { useEffect(() => { const load = async () => { let abi = await getABI("0"); - + console.log("ABI:", abi); let datasetManager = await getDataset("0"); - console.log(datasetManager); + console.log("DatasetManager:", datasetManager); let address = datasetManager[0].address; - console.log(address); + console.log("Address:", address); let datasetABI = await getABI("1"); - if (abi === null || datasetManager === null || datasetABI === null) { + let imageABI = await getABI("2"); + + if ( + abi === null || + datasetManager === null || + datasetABI === null || + imageABI === null + ) { alert("Error loading dataset manager or dataset ABI."); return; } window.web3 = new web3(window.ethereum); let DatasetManagerContract = new window.web3.eth.Contract(abi, address); + DatasetManagerContract.setProvider(window.ethereum); let datasetCount = await DatasetManagerContract.methods - .datasetCount() + .getDatasetCount() .call(); console.log(datasetCount); @@ -44,7 +52,7 @@ function DashboardPage() { let datasetsTemp = []; for (let i = 0; i < datasetCount; i++) { - let dataset = await DatasetManagerContract.methods.datasets(i).call(); + let dataset = await DatasetManagerContract.methods.getDataset(i).call(); console.log(dataset); @@ -52,19 +60,28 @@ function DashboardPage() { let DatasetContract = new window.web3.eth.Contract(datasetABI, dataset); - // await DatasetContract.methods.createData("test", "test").send({from: window.ethereum.selectedAddress, gas: 1000000}); - - let imageCount = await DatasetContract.methods.imageCount().call(); + let imageCount = await DatasetContract.methods.getImageCount().call(); for (let j = 0; j < imageCount; j++) { - let image = await DatasetContract.methods.images(j).call(); + let imageAddress = await DatasetContract.methods.getImages(j).call(); + + let ImageContract = await new window.web3.eth.Contract( + imageABI, + imageAddress, + ); + + let image = { + class: await ImageContract.methods.getClass().call(), + value: await ImageContract.methods.getValue().call(), + approved: await ImageContract.methods.getApproved().call(), + }; + images.push(image); } - console.log(images); datasetsTemp.push({ - name: await DatasetContract.methods.name().call(), - description: await DatasetContract.methods.description().call(), + name: await DatasetContract.methods.getName().call(), + description: await DatasetContract.methods.getDescription().call(), images: images, }); @@ -81,20 +98,28 @@ function DashboardPage() { {datasets.map((dataset) => { console.log(dataset); return ( -
  • +
  • {dataset.name}

    {dataset.description}

    +

    + Link +

    Images:

      {dataset.images.map((image) => { + console.log(image); return ( -
    • -

      {image.value}

      +
    • {image.class}

      -

      {image.approved ? "Approved" : "Not Approved"}

      - Link + {image.value.substring(0, 22) === + "data:image/png;base64," ? ( + {image.class} + ) : ( + image.value + )}

      +

      {image.approved ? "Approved" : "Not Approved"}

    • ); })} diff --git a/client/src/pages/DatasetPage.jsx b/client/src/pages/DatasetPage.jsx index c41f984..b48f56a 100644 --- a/client/src/pages/DatasetPage.jsx +++ b/client/src/pages/DatasetPage.jsx @@ -65,6 +65,7 @@ function DatasetPage() { {"test {/* {data && data.length>0 && data.map((item)=>

      {item.about}

      )} */} + {"ID:" + id} ); diff --git a/client/yarn.lock b/client/yarn.lock index 32fefb8..c8d345d 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -5772,14 +5772,7 @@ is-symbol@^1.0.2, is-symbol@^1.0.3: dependencies: has-symbols "^1.0.2" -is-typed-array@^1.1.10, is-typed-array@^1.1.9: - version "1.1.12" - resolved "https://registry.yarnpkg.com/is-typed-array/-/is-typed-array-1.1.12.tgz#d0bab5686ef4a76f7a73097b95470ab199c57d4a" - integrity sha512-Z14TF2JNG8Lss5/HMqt0//T9JeHXttXy5pH/DBU4vi98ozO2btxzq9MwYDZYnKwU8nRsz/+GVFVRDq3DkVuSPg== - dependencies: - which-typed-array "^1.1.11" - -is-typed-array@^1.1.3: +is-typed-array@^1.1.10, is-typed-array@^1.1.3, is-typed-array@^1.1.9: version "1.1.12" resolved "https://registry.yarnpkg.com/is-typed-array/-/is-typed-array-1.1.12.tgz#d0bab5686ef4a76f7a73097b95470ab199c57d4a" integrity sha512-Z14TF2JNG8Lss5/HMqt0//T9JeHXttXy5pH/DBU4vi98ozO2btxzq9MwYDZYnKwU8nRsz/+GVFVRDq3DkVuSPg== @@ -9921,7 +9914,7 @@ which-collection@^1.0.1: is-weakmap "^2.0.1" is-weakset "^2.0.1" -which-typed-array@^1.1.10, which-typed-array@^1.1.11, which-typed-array@^1.1.9: +which-typed-array@^1.1.10, which-typed-array@^1.1.11, which-typed-array@^1.1.2, which-typed-array@^1.1.9: version "1.1.11" resolved "https://registry.yarnpkg.com/which-typed-array/-/which-typed-array-1.1.11.tgz#99d691f23c72aab6768680805a271b69761ed61a" integrity sha512-qe9UWWpkeG5yzZ0tNYxDmd7vo58HDBc39mZ0xWWpolAGADdFOzkfamWLDxkOWcvHQKVmdTyQdLD4NOfjLWTKew==