diff --git a/Alliance_University_Ujjwal_pal_CSE081/.gitignore b/Alliance_University_Ujjwal_pal_CSE081/.gitignore new file mode 100644 index 00000000..00dad773 --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/.gitignore @@ -0,0 +1,11 @@ +node_modules +.env +coverage +coverage.json +typechain +typechain-types + +# Hardhat files +cache +artifacts + diff --git a/Alliance_University_Ujjwal_pal_CSE081/README.md b/Alliance_University_Ujjwal_pal_CSE081/README.md new file mode 100644 index 00000000..721583ae --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/README.md @@ -0,0 +1,45 @@ +# Sample Hardhat Project + +This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract. + +Try running some of the following tasks: + +```shell +npx hardhat help +npx hardhat test +REPORT_GAS=true npx hardhat test +npx hardhat node +npx hardhat run scripts/deploy.js +``` +###Description For the Smart Contract +##Registering as Seller and Paying the owner a fee of 1000 Wei +#1. Create a Public address variable owner and made deployer address as owner in the constructor. +Use address mappings pointing to our struct seller. Here seller struct consists of all required details about the seller. +Add a method sellerSignup to check if the seller is already registered and check if msg.value along with the function is equal to 1000 Wei or not. + +#2. List Product With All Required Details + Add a struct product with all required variables and made string mapping to the struct, so that you can update or buy Product with productId String Add a function to check whether the seller paid the bank guarantee or not and to check if a product with the same productId is active already. + +#3.Track Orders Placed By Buyers + Add a struct ordersPlaced with required Tracking Detail variables and mapped struct array with seller address. + Pushed Order details to map sellerOrders when buyer placed an order. + Track Orders Placed by buyers with purchaseId + +*4. Ship Products And Update Shipment Details + Create a Struct sellerShipment with all required variables for Tracking and Updating Details and mapped this struct to address and nested uint. + Every seller can update shipment details with unique purchaseId + Update Shipment details with function updateShipments with purchaseId + +#5. Refund Cancelled Orders + Create a function refund to check if Product with particular purchaseId is active or not, check if Buyer Cancelled Order or not and then check if seller Released amount equal to product price. + +#6. Create Buyer's Account + Create a Struct user with required user details and mapped it to address. + Add a function createAccount and pushed function arguments(user details) to the users mapping. + +#7. Place A Order And Tracking Orders + +#8. Cancelling An Order + Add a function to cancel an order it takes purchaseId as an argument and checks if the product with particular purchaseId is ordered by the current caller or not if the product with particular purchaseId is active or not. + +#9. The final Smart Contract "Ecom.sol" \ No newline at end of file diff --git a/Alliance_University_Ujjwal_pal_CSE081/contracts/Ecom.sol b/Alliance_University_Ujjwal_pal_CSE081/contracts/Ecom.sol new file mode 100644 index 00000000..309341be --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/contracts/Ecom.sol @@ -0,0 +1,163 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.4; + + +contract shopping { + + address payable public owner; + + constructor() { + owner = payable(msg.sender); + } + uint id; + uint purchaseId; + struct seller { + string name; + address addre; + uint bankGuaraantee; + bool bgPaid; + } + struct product { + string productId; + string productName; + string Category; + uint price; + string description; + address payable seller; + bool isActive; + + } + struct ordersPlaced { + string productId; + uint purchaseId; + address orderedBy; + } + struct sellerShipment { + string productId; + uint purchaseId; + string shipmentStatus; + string deliveryAddress; + address payable orderedBy; + bool isActive; + bool isCanceled; + } + struct user { + string name; + string email; + string deliveryAddress; + bool isCreated; + } + struct orders { + string productId; + string orderStatus; + uint purchaseId; + string shipmentStatus; + } + mapping(address => seller) public sellers; + mapping(string => product) products; + product[] public allProducts; + mapping(address => ordersPlaced[]) sellerOrders; + mapping(address => mapping(uint => sellerShipment)) sellerShipments; + mapping(address => user) users; + mapping(address => orders[]) userOrders; + + event OrderPlaced(string _status, address indexed _buyer, string _productId); + + function sellerSignUp(string memory _name) public payable { + require(!sellers[msg.sender].bgPaid, "You are Already Registered"); + require(msg.value == 1000 wei, "Bank Guarantee of 1000 wei Required"); + owner.transfer(msg.value); + sellers[msg.sender].name = _name; + sellers[msg.sender].addre = msg.sender; + sellers[msg.sender].bankGuaraantee = msg.value; + sellers[msg.sender].bgPaid = true; + } + + function createAccount(string memory _name, string memory _email, string memory _deliveryAddress) public { + + users[msg.sender].name = _name; + users[msg.sender].email = _email; + users[msg.sender].deliveryAddress = _deliveryAddress; + users[msg.sender].isCreated = true; + } + + function buyProduct(string memory _productId) public payable { + + + require(msg.value == products[_productId].price, "Value Must be Equal to Price of Product"); + require(users[msg.sender].isCreated, "You Must Be Registered to Buy"); + + products[_productId].seller.transfer(msg.value); + + purchaseId = id++; + orders memory order = orders(_productId, "Order Placed With Seller", purchaseId, sellerShipments[products[_productId].seller][purchaseId].shipmentStatus); + userOrders[msg.sender].push(order); + ordersPlaced memory ord = ordersPlaced(_productId, purchaseId, msg.sender); + sellerOrders[products[_productId].seller].push(ord); + + sellerShipments[products[_productId].seller][purchaseId].productId = _productId; + sellerShipments[products[_productId].seller][purchaseId].orderedBy = payable(msg.sender); + sellerShipments[products[_productId].seller][purchaseId].purchaseId = purchaseId; + sellerShipments[products[_productId].seller][purchaseId].deliveryAddress = users[msg.sender].deliveryAddress; + sellerShipments[products[_productId].seller][purchaseId].isActive = true; + + emit OrderPlaced("Order Placed With Seller", msg.sender, _productId); + } + + function addProduct(string memory _productId, string memory _productName, string memory _category, uint _price, string memory _description) public { + require(sellers[msg.sender].bgPaid, "You are not Registered as Seller"); + require(!products[_productId].isActive, "Product With this Id is already Active. Use other UniqueId"); + + + product memory newProduct = product(_productId, _productName, _category, _price, _description, payable(msg.sender), true); + products[_productId].productId = _productId; + products[_productId].productName = _productName; + products[_productId].Category = _category; + products[_productId].description = _description; + products[_productId].price = _price; + products[_productId].seller = payable(msg.sender); + products[_productId].isActive = true; + allProducts.push(newProduct); + + } + + function cancelOrder(string memory _productId, uint _purchaseId) public payable { + require(sellerShipments[products[_productId].seller][_purchaseId].orderedBy == msg.sender, "Aww Crap.. You are not Authorized to This Product PurchaseId"); + require(sellerShipments[products[_productId].seller][purchaseId].isActive, "Aww crap..You Already Canceled This order"); + + + sellerShipments[products[_productId].seller][_purchaseId].shipmentStatus = "Order Canceled By Buyer, Payment will Be Refunded"; + sellerShipments[products[_productId].seller][_purchaseId].isCanceled = true; + sellerShipments[products[_productId].seller][_purchaseId].isActive = false; + } + + function updateShipment(uint _purchaseId, string memory _shipmentDetails) public { + require(sellerShipments[msg.sender][_purchaseId].isActive, "Order is either inActive or cancelled"); + + sellerShipments[msg.sender][_purchaseId].shipmentStatus = _shipmentDetails; + + } + + function refund(string memory _productId, uint _purchaseId) public payable { + require(sellerShipments[msg.sender][_purchaseId].isCanceled, "Order is not Yet Cancelled"); + require(!sellerShipments[products[_productId].seller][purchaseId].isActive, "Order is Active and not yet Cancelled"); + require(msg.value == products[_productId].price, "Value Must be Equal to Product Price"); + sellerShipments[msg.sender][_purchaseId].orderedBy.transfer(msg.value); + sellerShipments[products[_productId].seller][_purchaseId].shipmentStatus = "Order Canceled By Buyer, Payment Refunded"; + + } + + function myOrders(uint _index) public view returns(string memory, string memory, uint, string memory) { + return (userOrders[msg.sender][_index].productId, userOrders[msg.sender][_index].orderStatus, userOrders[msg.sender][_index].purchaseId, sellerShipments[products[userOrders[msg.sender][_index].productId].seller][userOrders[msg.sender][_index].purchaseId].shipmentStatus); + } + + function getOrdersPlaced(uint _index) public view returns(string memory, uint, address, string memory) { + return (sellerOrders[msg.sender][_index].productId, sellerOrders[msg.sender][_index].purchaseId, sellerOrders[msg.sender][_index].orderedBy, sellerShipments[msg.sender][sellerOrders[msg.sender][_index].purchaseId].shipmentStatus); + } + + function getShipmentDetails(uint _purchaseId) public view returns(string memory, string memory, address, string memory) { + + return (sellerShipments[msg.sender][_purchaseId].productId, sellerShipments[msg.sender][_purchaseId].shipmentStatus, sellerShipments[msg.sender][_purchaseId].orderedBy, sellerShipments[msg.sender][_purchaseId].deliveryAddress); + } + +} \ No newline at end of file diff --git a/Alliance_University_Ujjwal_pal_CSE081/hardhat.config.js b/Alliance_University_Ujjwal_pal_CSE081/hardhat.config.js new file mode 100644 index 00000000..d5dd5f4e --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/hardhat.config.js @@ -0,0 +1,6 @@ +require("@nomicfoundation/hardhat-toolbox"); + +/** @type import('hardhat/config').HardhatUserConfig */ +module.exports = { + solidity: "0.8.18", +}; diff --git a/Alliance_University_Ujjwal_pal_CSE081/package.json b/Alliance_University_Ujjwal_pal_CSE081/package.json new file mode 100644 index 00000000..a926434a --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/package.json @@ -0,0 +1,22 @@ +{ + "dependencies": { + "hardhat": "^2.14.0", + "npx": "^10.2.2" + }, + "devDependencies": { + "@ethersproject/abi": "^5.7.0", + "@ethersproject/providers": "^5.7.2", + "@nomicfoundation/hardhat-chai-matchers": "^1.0.6", + "@nomicfoundation/hardhat-network-helpers": "^1.0.8", + "@nomicfoundation/hardhat-toolbox": "^2.0.2", + "@nomiclabs/hardhat-ethers": "^2.2.3", + "@nomiclabs/hardhat-etherscan": "^3.1.7", + "@typechain/ethers-v5": "^10.2.0", + "@typechain/hardhat": "^6.1.5", + "chai": "^4.3.7", + "ethers": "^5.7.2", + "hardhat-gas-reporter": "^1.0.9", + "solidity-coverage": "^0.8.2", + "typechain": "^8.1.1" + } +} diff --git a/Alliance_University_Ujjwal_pal_CSE081/scripts/deploy.js b/Alliance_University_Ujjwal_pal_CSE081/scripts/deploy.js new file mode 100644 index 00000000..745b18d4 --- /dev/null +++ b/Alliance_University_Ujjwal_pal_CSE081/scripts/deploy.js @@ -0,0 +1,32 @@ +// We require the Hardhat Runtime Environment explicitly here. This is optional +// but useful for running the script in a standalone fashion through `node