diff --git a/contracts/Dappazon.sol b/contracts/Dappazon.sol
index 90591c5..accf84c 100644
--- a/contracts/Dappazon.sol
+++ b/contracts/Dappazon.sol
@@ -2,6 +2,7 @@
pragma solidity ^0.8.9;
contract Dappazon {
+ //code
address public owner;
struct Item {
@@ -20,10 +21,9 @@ contract Dappazon {
}
mapping(uint256 => Item) public items;
- mapping(address => mapping(uint256 => Order)) public orders;
mapping(address => uint256) public orderCount;
+ mapping(address => mapping(uint256 => Order)) public orders;
- event Buy(address buyer, uint256 orderId, uint256 itemId);
event List(string name, uint256 cost, uint256 quantity);
modifier onlyOwner() {
@@ -35,6 +35,9 @@ contract Dappazon {
owner = msg.sender;
}
+ //0, 1, ......
+
+ //List Product
function list(
uint256 _id,
string memory _name,
@@ -44,7 +47,9 @@ contract Dappazon {
uint256 _rating,
uint256 _stock
) public onlyOwner {
- // Create Item
+ //code
+
+ //create Item struct
Item memory item = Item(
_id,
_name,
@@ -55,39 +60,33 @@ contract Dappazon {
_stock
);
- // Add Item to mapping
+ //save item struct to blockchain
items[_id] = item;
- // Emit event
+ //Emit an event
emit List(_name, _cost, _stock);
}
- function buy(uint256 _id) public payable {
- // Fetch item
- Item memory item = items[_id];
+ //Buy Product
- // Require enough ether to buy item
- require(msg.value >= item.cost);
+ function buy(uint256 _id) public payable {
+ // Receive Crypto
- // Require item is in stock
- require(item.stock > 0);
+ //Fetch Item
+ Item memory item = items[_id];
- // Create order
+ //Creat an order
Order memory order = Order(block.timestamp, item);
- // Add order for user
+ //Save order to chain
orderCount[msg.sender]++; // <-- Order ID
orders[msg.sender][orderCount[msg.sender]] = order;
- // Subtract stock
+ //Substrack stock
items[_id].stock = item.stock - 1;
- // Emit event
- emit Buy(msg.sender, orderCount[msg.sender], item.id);
+ //Emit event
}
- function withdraw() public onlyOwner {
- (bool success, ) = owner.call{value: address(this).balance}("");
- require(success);
- }
+ //Withdraw Funds
}
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 (
-
- {/*
*/}
-
Dappazon
-
-
-
- {account ? (
-
- {account.slice(0, 6) + '...' + account.slice(38, 42)}
-
- ) : (
-
- Connect
-
- )}
-
-
);
}
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 (
-
-
-
-
-
-
{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.
- )}
-
-
- Buy Now
-
-
-
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.name}
-
-
{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH
-
-
- ))}
-
);
}
diff --git a/test/Dappazon.js b/test/Dappazon.js
index 6613160..d674621 100644
--- a/test/Dappazon.js
+++ b/test/Dappazon.js
@@ -1,127 +1,102 @@
-const { expect } = require("chai")
+const { expect } = require("chai");
+const { ethers } = require("hardhat");
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
+ 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
-
+ let dappazon;
+ let deployer, buyer;
beforeEach(async () => {
- // Setup accounts
- [deployer, buyer] = await ethers.getSigners()
+ [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()
- })
+ //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)
- })
- })
+ it("has the owner", async () => {
+ expect(await dappazon.owner()).to.equal(deployer.address);
+ });
+ });
describe("Listing", () => {
- let transaction
+ 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")
- })
- })
+ 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
-
+ 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()
- })
+ //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)
- })
-
+ 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)
+ 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);
+ });
- 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)
- })
- })
-})
+
+});