From 088c37719279ddcc3a25c61424ba239e2f5235bd Mon Sep 17 00:00:00 2001 From: Justin Date: Thu, 8 Dec 2022 14:42:48 -0500 Subject: [PATCH 1/2] Adds starter code --- contracts/Dappazon.sol | 91 +-------------------------- scripts/deploy.js | 25 -------- src/App.js | 58 +---------------- src/components/Navigation.js | 37 ----------- src/components/Product.js | 100 ----------------------------- src/components/Section.js | 17 ----- test/Dappazon.js | 118 ----------------------------------- 7 files changed, 2 insertions(+), 444 deletions(-) diff --git a/contracts/Dappazon.sol b/contracts/Dappazon.sol index 90591c5..6141c09 100644 --- a/contracts/Dappazon.sol +++ b/contracts/Dappazon.sol @@ -1,93 +1,4 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; -contract Dappazon { - address public owner; - - struct Item { - uint256 id; - string name; - string category; - string image; - uint256 cost; - uint256 rating; - uint256 stock; - } - - struct Order { - uint256 time; - Item item; - } - - mapping(uint256 => Item) public items; - mapping(address => mapping(uint256 => Order)) public orders; - mapping(address => uint256) public orderCount; - - event Buy(address buyer, uint256 orderId, uint256 itemId); - event List(string name, uint256 cost, uint256 quantity); - - modifier onlyOwner() { - require(msg.sender == owner); - _; - } - - constructor() { - owner = msg.sender; - } - - function list( - uint256 _id, - string memory _name, - string memory _category, - string memory _image, - uint256 _cost, - uint256 _rating, - uint256 _stock - ) public onlyOwner { - // Create Item - Item memory item = Item( - _id, - _name, - _category, - _image, - _cost, - _rating, - _stock - ); - - // Add Item to mapping - items[_id] = item; - - // Emit event - emit List(_name, _cost, _stock); - } - - function buy(uint256 _id) public payable { - // Fetch item - Item memory item = items[_id]; - - // Require enough ether to buy item - require(msg.value >= item.cost); - - // Require item is in stock - require(item.stock > 0); - - // Create order - Order memory order = Order(block.timestamp, item); - - // Add order for user - orderCount[msg.sender]++; // <-- Order ID - orders[msg.sender][orderCount[msg.sender]] = order; - - // Subtract stock - items[_id].stock = item.stock - 1; - - // Emit event - emit Buy(msg.sender, orderCount[msg.sender], item.id); - } - - function withdraw() public onlyOwner { - (bool success, ) = owner.call{value: address(this).balance}(""); - require(success); - } -} +contract Dappazon {} diff --git a/scripts/deploy.js b/scripts/deploy.js index dbdd229..f3e44ad 100644 --- a/scripts/deploy.js +++ b/scripts/deploy.js @@ -12,32 +12,7 @@ const tokens = (n) => { } async function main() { - // Setup accounts - const [deployer] = await ethers.getSigners() - // Deploy Dappazon - const Dappazon = await hre.ethers.getContractFactory("Dappazon") - const dappazon = await Dappazon.deploy() - await dappazon.deployed() - - console.log(`Deployed Dappazon Contract at: ${dappazon.address}\n`) - - // Listing items... - for (let i = 0; i < items.length; i++) { - const transaction = await dappazon.connect(deployer).list( - items[i].id, - items[i].name, - items[i].category, - items[i].image, - tokens(items[i].price), - items[i].rating, - items[i].stock, - ) - - await transaction.wait() - - console.log(`Listed item ${items[i].id}: ${items[i].name}`) - } } // We recommend this pattern to be able to use async/await everywhere diff --git a/src/App.js b/src/App.js index 1a93112..1bfaf76 100644 --- a/src/App.js +++ b/src/App.js @@ -13,68 +13,12 @@ import Dappazon from './abis/Dappazon.json' import config from './config.json' function App() { - const [provider, setProvider] = useState(null) - const [dappazon, setDappazon] = useState(null) - - const [account, setAccount] = useState(null) - - const [electronics, setElectronics] = useState(null) - const [clothing, setClothing] = useState(null) - const [toys, setToys] = useState(null) - - const [item, setItem] = useState({}) - const [toggle, setToggle] = useState(false) - - const togglePop = (item) => { - setItem(item) - toggle ? setToggle(false) : setToggle(true) - } - - const loadBlockchainData = async () => { - const provider = new ethers.providers.Web3Provider(window.ethereum) - setProvider(provider) - const network = await provider.getNetwork() - - const dappazon = new ethers.Contract(config[network.chainId].dappazon.address, Dappazon, provider) - setDappazon(dappazon) - - const items = [] - - for (var i = 0; i < 9; i++) { - const item = await dappazon.items(i + 1) - items.push(item) - } - - const electronics = items.filter((item) => item.category === 'electronics') - const clothing = items.filter((item) => item.category === 'clothing') - const toys = items.filter((item) => item.category === 'toys') - - setElectronics(electronics) - setClothing(clothing) - setToys(toys) - } - - useEffect(() => { - loadBlockchainData() - }, []) return (
- - -

Dappazon Best Sellers

- {electronics && clothing && toys && ( - <> -
-
-
- - )} +

Welcome to Dappazon

- {toggle && ( - - )}
); } diff --git a/src/components/Navigation.js b/src/components/Navigation.js index b777f7a..e31a292 100644 --- a/src/components/Navigation.js +++ b/src/components/Navigation.js @@ -1,47 +1,10 @@ import { ethers } from 'ethers'; -import logo from '../assets/logo.svg'; const Navigation = ({ account, setAccount }) => { - const connectHandler = async () => { - const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' }); - const account = ethers.utils.getAddress(accounts[0]) - setAccount(account); - } return ( ); } diff --git a/src/components/Product.js b/src/components/Product.js index 00309e1..ced1c54 100644 --- a/src/components/Product.js +++ b/src/components/Product.js @@ -7,110 +7,10 @@ import Rating from './Rating' import close from '../assets/close.svg' const Product = ({ item, provider, account, dappazon, togglePop }) => { - const [order, setOrder] = useState(null) - const [hasBought, setHasBought] = useState(false) - - const fetchDetails = async () => { - const events = await dappazon.queryFilter("Buy") - const orders = events.filter( - (event) => event.args.buyer === account && event.args.itemId.toString() === item.id.toString() - ) - - if (orders.length === 0) return - - const order = await dappazon.orders(account, orders[0].args.orderId) - setOrder(order) - } - - const buyHandler = async () => { - const signer = await provider.getSigner() - - // Buy item... - let transaction = await dappazon.connect(signer).buy(item.id, { value: item.cost }) - await transaction.wait() - - setHasBought(true) - } - - useEffect(() => { - fetchDetails() - }, [hasBought]) return (
-
-
- Product -
-
-

{item.name}

- - - -
- -

{item.address}

- -

{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH

- -
- -

Overview

- -

- {item.description} - - Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima rem, iusto, - consectetur inventore quod soluta quos qui assumenda aperiam, eveniet doloribus - commodi error modi eaque! Iure repudiandae temporibus ex? Optio! -

-
- -
-

{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH

- -

- FREE delivery
- - {new Date(Date.now() + 345600000).toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric' })} - -

- - {item.stock > 0 ? ( -

In Stock.

- ) : ( -

Out of Stock.

- )} - - - -

Ships from Dappazon

-

Sold by Dappazon

- - {order && ( -
- Item bought on
- - {new Date(Number(order.time.toString() + '000')).toLocaleDateString( - undefined, - { - weekday: 'long', - hour: 'numeric', - minute: 'numeric', - second: 'numeric' - })} - -
- )} -
- - -
); } diff --git a/src/components/Section.js b/src/components/Section.js index 8dcf9df..bb14fb2 100644 --- a/src/components/Section.js +++ b/src/components/Section.js @@ -6,24 +6,7 @@ import Rating from './Rating' const Section = ({ title, items, togglePop }) => { return (
-

{title}

-
- -
- {items.map((item, index) => ( -
togglePop(item)}> -
- Item -
-
-

{item.name}

- -

{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH

-
-
- ))} -
); } diff --git a/test/Dappazon.js b/test/Dappazon.js index 6613160..020bca1 100644 --- a/test/Dappazon.js +++ b/test/Dappazon.js @@ -4,124 +4,6 @@ const tokens = (n) => { return ethers.utils.parseUnits(n.toString(), 'ether') } -// Global constants for listing an item... -const ID = 1 -const NAME = "Shoes" -const CATEGORY = "Clothing" -const IMAGE = "https://ipfs.io/ipfs/QmTYEboq8raiBs7GTUg2yLXB3PMz6HuBNgNfSZBx5Msztg/shoes.jpg" -const COST = tokens(1) -const RATING = 4 -const STOCK = 5 - describe("Dappazon", () => { - let dappazon - let deployer, buyer - - beforeEach(async () => { - // Setup accounts - [deployer, buyer] = await ethers.getSigners() - - // Deploy contract - const Dappazon = await ethers.getContractFactory("Dappazon") - dappazon = await Dappazon.deploy() - }) - - describe("Deployment", () => { - it("Sets the owner", async () => { - expect(await dappazon.owner()).to.equal(deployer.address) - }) - }) - - describe("Listing", () => { - let transaction - - beforeEach(async () => { - // List a item - transaction = await dappazon.connect(deployer).list(ID, NAME, CATEGORY, IMAGE, COST, RATING, STOCK) - await transaction.wait() - }) - - it("Returns item attributes", async () => { - const item = await dappazon.items(ID) - - expect(item.id).to.equal(ID) - expect(item.name).to.equal(NAME) - expect(item.category).to.equal(CATEGORY) - expect(item.image).to.equal(IMAGE) - expect(item.cost).to.equal(COST) - expect(item.rating).to.equal(RATING) - expect(item.stock).to.equal(STOCK) - }) - - it("Emits List event", () => { - expect(transaction).to.emit(dappazon, "List") - }) - }) - - describe("Buying", () => { - let transaction - - beforeEach(async () => { - // List a item - transaction = await dappazon.connect(deployer).list(ID, NAME, CATEGORY, IMAGE, COST, RATING, STOCK) - await transaction.wait() - - // Buy a item - transaction = await dappazon.connect(buyer).buy(ID, { value: COST }) - await transaction.wait() - }) - - - it("Updates buyer's order count", async () => { - const result = await dappazon.orderCount(buyer.address) - expect(result).to.equal(1) - }) - - it("Adds the order", async () => { - const order = await dappazon.orders(buyer.address, 1) - - expect(order.time).to.be.greaterThan(0) - expect(order.item.name).to.equal(NAME) - }) - - it("Updates the contract balance", async () => { - const result = await ethers.provider.getBalance(dappazon.address) - expect(result).to.equal(COST) - }) - - it("Emits Buy event", () => { - expect(transaction).to.emit(dappazon, "Buy") - }) - }) - - describe("Withdrawing", () => { - let balanceBefore - - beforeEach(async () => { - // List a item - let transaction = await dappazon.connect(deployer).list(ID, NAME, CATEGORY, IMAGE, COST, RATING, STOCK) - await transaction.wait() - - // Buy a item - transaction = await dappazon.connect(buyer).buy(ID, { value: COST }) - await transaction.wait() - - // Get Deployer balance before - balanceBefore = await ethers.provider.getBalance(deployer.address) - - // Withdraw - transaction = await dappazon.connect(deployer).withdraw() - await transaction.wait() - }) - - it('Updates the owner balance', async () => { - const balanceAfter = await ethers.provider.getBalance(deployer.address) - expect(balanceAfter).to.be.greaterThan(balanceBefore) - }) - it('Updates the contract balance', async () => { - const result = await ethers.provider.getBalance(dappazon.address) - expect(result).to.equal(0) - }) - }) }) From 869c0ce477e6803c1b911caea781cd3a490f9ff3 Mon Sep 17 00:00:00 2001 From: thinust Date: Fri, 23 Aug 2024 23:38:24 +0530 Subject: [PATCH 2/2] Added my project --- contracts/Dappazon.sol | 90 +++++++++++++++++++++++++++++++++++- test/Dappazon.js | 101 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 186 insertions(+), 5 deletions(-) diff --git a/contracts/Dappazon.sol b/contracts/Dappazon.sol index 6141c09..accf84c 100644 --- a/contracts/Dappazon.sol +++ b/contracts/Dappazon.sol @@ -1,4 +1,92 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9; -contract Dappazon {} +contract Dappazon { + //code + address public owner; + + struct Item { + uint256 id; + string name; + string category; + string image; + uint256 cost; + uint256 rating; + uint256 stock; + } + + struct Order { + uint256 time; + Item item; + } + + mapping(uint256 => Item) public items; + mapping(address => uint256) public orderCount; + mapping(address => mapping(uint256 => Order)) public orders; + + event List(string name, uint256 cost, uint256 quantity); + + modifier onlyOwner() { + require(msg.sender == owner); + _; + } + + constructor() { + owner = msg.sender; + } + + //0, 1, ...... + + //List Product + function list( + uint256 _id, + string memory _name, + string memory _category, + string memory _image, + uint256 _cost, + uint256 _rating, + uint256 _stock + ) public onlyOwner { + //code + + //create Item struct + Item memory item = Item( + _id, + _name, + _category, + _image, + _cost, + _rating, + _stock + ); + + //save item struct to blockchain + items[_id] = item; + + //Emit an event + emit List(_name, _cost, _stock); + } + + //Buy Product + + function buy(uint256 _id) public payable { + // Receive Crypto + + //Fetch Item + Item memory item = items[_id]; + + //Creat an order + Order memory order = Order(block.timestamp, item); + + //Save order to chain + orderCount[msg.sender]++; // <-- Order ID + orders[msg.sender][orderCount[msg.sender]] = order; + + //Substrack stock + items[_id].stock = item.stock - 1; + + //Emit event + } + + //Withdraw Funds +} diff --git a/test/Dappazon.js b/test/Dappazon.js index 020bca1..d674621 100644 --- a/test/Dappazon.js +++ b/test/Dappazon.js @@ -1,9 +1,102 @@ -const { expect } = require("chai") +const { expect } = require("chai"); +const { ethers } = require("hardhat"); const tokens = (n) => { - return ethers.utils.parseUnits(n.toString(), 'ether') -} + return ethers.utils.parseUnits(n.toString(), "ether"); +}; + +//Global constants for listing an item... +const ID = 1; +const NAME = "Shoes"; +const CATEGORY = "Clothings"; +const IMAGE = "https://picsum.photos/id/237/200/300"; +const COST = tokens(1); +const RATING = 4; +const STOCK = 5; describe("Dappazon", () => { + let dappazon; + let deployer, buyer; + beforeEach(async () => { + [deployer, buyer] = await ethers.getSigners(); + //setup account + // console.log(deployer.address, buyer.address); + + //deploy contract + const Dappazon = await ethers.getContractFactory("Dappazon"); + dappazon = await Dappazon.deploy(); + }); + + describe("Deployment", () => { + it("has the owner", async () => { + expect(await dappazon.owner()).to.equal(deployer.address); + }); + }); + + describe("Listing", () => { + let transaction; + + beforeEach(async () => { + transaction = await dappazon + .connect(deployer) + .list(ID, NAME, CATEGORY, IMAGE, COST, RATING, STOCK); + + await transaction.wait(); + }); + + it("Return items attributes", async () => { + const item = await dappazon.items(1); + expect(item.id).to.equal(ID); + expect(item.name).to.equal(NAME); + expect(item.category).to.equal(CATEGORY); + expect(item.image).to.equal(IMAGE); + expect(item.cost).to.equal(COST); + expect(item.rating).to.equal(RATING); + expect(item.stock).to.equal(STOCK); + }); + + it("Emit List event", () => { + expect(transaction).to.emit(dappazon, "List"); + }); + }); + + describe("Buying", () => { + let transaction; + beforeEach(async () => { + //List an item + transaction = await dappazon + .connect(deployer) + .list(ID, NAME, CATEGORY, IMAGE, COST, RATING, STOCK); + await transaction.wait(); + + //Buy an item + transaction = await dappazon.connect(buyer).buy(ID, { value: COST }); + }); + + it("Updates buyer's order count", async () => { + const result = await dappazon.orderCount(buyer.address); + expect(result).to.equal(1); + }); + it("Adds the order", async () => { + const order = await dappazon.orders(buyer.address,1); + expect(order.time).to.be.greaterThan(0); + expect(order.item.name).to.equal(NAME); + }); + it("Update the contract Balance", async () => { + const result = await ethers.provider.getBalance(dappazon.address); + // console.log(result) + expect(result).to.equal(COST); + }); + it("Emit buy event", async () => { + const result = await ethers.provider.getBalance(dappazon.address); + // console.log(result) + expect(result).to.equal(COST); + }); + + + }); + + + -}) +});