Skip to content
Open

Main #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions contracts/Dappazon.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.9;

contract Dappazon {
//code
address public owner;

struct Item {
Expand All @@ -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() {
Expand All @@ -35,6 +35,9 @@ contract Dappazon {
owner = msg.sender;
}

//0, 1, ......

//List Product
function list(
uint256 _id,
string memory _name,
Expand All @@ -44,7 +47,9 @@ contract Dappazon {
uint256 _rating,
uint256 _stock
) public onlyOwner {
// Create Item
//code

//create Item struct
Item memory item = Item(
_id,
_name,
Expand All @@ -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
}
25 changes: 0 additions & 25 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 1 addition & 57 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
<Navigation account={account} setAccount={setAccount} />

<h2>Dappazon Best Sellers</h2>

{electronics && clothing && toys && (
<>
<Section title={"Clothing & Jewelry"} items={clothing} togglePop={togglePop} />
<Section title={"Electronics & Gadgets"} items={electronics} togglePop={togglePop} />
<Section title={"Toys & Gaming"} items={toys} togglePop={togglePop} />
</>
)}
<h2>Welcome to Dappazon</h2>

{toggle && (
<Product item={item} provider={provider} account={account} dappazon={dappazon} togglePop={togglePop} />
)}
</div>
);
}
Expand Down
37 changes: 0 additions & 37 deletions src/components/Navigation.js
Original file line number Diff line number Diff line change
@@ -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 (
<nav>
<div className='nav__brand'>
{/* <img src={logo} alt="Logo" /> */}
<h1>Dappazon</h1>
</div>

<input
type="text"
className="nav__search"
/>

{account ? (
<button
type="button"
className='nav__connect'
>
{account.slice(0, 6) + '...' + account.slice(38, 42)}
</button>
) : (
<button
type="button"
className='nav__connect'
onClick={connectHandler}
>
Connect
</button>
)}

<ul className='nav__links'>
<li><a href="#Clothing & Jewelry">Clothing & Jewelry</a></li>
<li><a href="#Electronics & Gadgets">Electronics & Gadgets</a></li>
<li><a href="#Toys & Gaming">Toys & Gaming</a></li>
</ul>
</nav>
);
}
Expand Down
100 changes: 0 additions & 100 deletions src/components/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div className="product">
<div className="product__details">
<div className="product__image">
<img src={item.image} alt="Product" />
</div>
<div className="product__overview">
<h1>{item.name}</h1>

<Rating value={item.rating} />

<hr />

<p>{item.address}</p>

<h2>{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH</h2>

<hr />

<h2>Overview</h2>

<p>
{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!
</p>
</div>

<div className="product__order">
<h1>{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH</h1>

<p>
FREE delivery <br />
<strong>
{new Date(Date.now() + 345600000).toLocaleDateString(undefined, { weekday: 'long', month: 'long', day: 'numeric' })}
</strong>
</p>

{item.stock > 0 ? (
<p>In Stock.</p>
) : (
<p>Out of Stock.</p>
)}

<button className='product__buy' onClick={buyHandler}>
Buy Now
</button>

<p><small>Ships from</small> Dappazon</p>
<p><small>Sold by</small> Dappazon</p>

{order && (
<div className='product__bought'>
Item bought on <br />
<strong>
{new Date(Number(order.time.toString() + '000')).toLocaleDateString(
undefined,
{
weekday: 'long',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
})}
</strong>
</div>
)}
</div>


<button onClick={togglePop} className="product__close">
<img src={close} alt="Close" />
</button>
</div>
</div >
);
}
Expand Down
17 changes: 0 additions & 17 deletions src/components/Section.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,7 @@ import Rating from './Rating'
const Section = ({ title, items, togglePop }) => {
return (
<div className='cards__section'>
<h3 id={title}>{title}</h3>

<hr />

<div className='cards'>
{items.map((item, index) => (
<div className='card' key={index} onClick={() => togglePop(item)}>
<div className='card__image'>
<img src={item.image} alt="Item" />
</div>
<div className='card__info'>
<h4>{item.name}</h4>
<Rating value={item.rating} />
<p>{ethers.utils.formatUnits(item.cost.toString(), 'ether')} ETH</p>
</div>
</div>
))}
</div>
</div>
);
}
Expand Down
Loading