Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

last commit #27

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

45 changes: 45 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/README.md
Original file line number Diff line number Diff line change
@@ -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"
163 changes: 163 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/contracts/Ecom.sol
Original file line number Diff line number Diff line change
@@ -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);
}

}
6 changes: 6 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require("@nomicfoundation/hardhat-toolbox");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.18",
};
22 changes: 22 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
32 changes: 32 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -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 <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");

async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 60;

const lockedAmount = hre.ethers.utils.parseEther("0.001");

const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

await lock.deployed();

console.log(
`Lock with ${ethers.utils.formatEther(
lockedAmount
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.address}`
);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
65 changes: 65 additions & 0 deletions Alliance_University_Ujjwal_pal_CSE081/test/ecom__test__.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("shopping contract", function () {
let owner;
let seller;
let buyer;
let shopping;

beforeEach(async function () {
[owner, seller, buyer] = await ethers.getSigners();

const Shopping = await ethers.getContractFactory("shopping");
shopping = await Shopping.connect(owner).deploy();
await shopping.deployed();
});

it("should add a product", async function () {
await shopping.connect(seller).sellerSignUp("Seller 1", { value: 1000 });
await shopping.connect(buyer).createAccount("Buyer 1", "[email protected]", "123 Main St");

const productId = "product1";
const productName = "Product 1";
const category = "Category 1";
const price = 100;
const description = "This is product 1";

await shopping.connect(seller).addProduct(productId, productName, category, price, description);

const product = await shopping.allProducts(0);

expect(product.productId).to.equal(productId);
expect(product.productName).to.equal(productName);
expect(product.Category).to.equal(category);
expect(product.description).to.equal(description);
expect(product.seller).to.equal(seller.address);

});

it("should buy a product and create an order", async function () {
await shopping.connect(seller).sellerSignUp("Seller 1", { value: 1000 });
await shopping.connect(buyer).createAccount("Buyer 1", "[email protected]", "123 Main St");

const productId = "product1";
const productName = "Product 1";
const category = "Category 1";
const price = 100;
const description = "This is product 1";

await shopping.connect(seller).addProduct(productId, productName, category, price, description);


await shopping.connect(buyer).buyProduct(productId, { value: price });

await expect(shopping.connect(buyer).buyProduct("product1", { value: 100 }))
.to.emit(shopping, "OrderPlaced");
});

it("should allow a buyer to cancel an order", async function () {
await shopping.connect(seller).sellerSignUp("My Store", { value: 1000 });
await shopping.connect(seller).addProduct("product1", "Product 1", "Category A", 100, "Description");
});
});


15 changes: 15 additions & 0 deletions node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/math/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading