BUILDING AN E-COMMERCE SHOPPING SMART CONTRACT
This is a guide to build an E-commerce shopping smart contract that involves buying and selling goods or services. So mainly it has buyers and sellers.
In this tutorial, we will go through how to create, deploy and interact with an e-commerce shopping smart contract on Gather Testnet.
Here we will use MetaMask as our wallet and remix IDE to compile and deploy our smart contract. You can use a different wallet and other frameworks like Hardhat or Truffle as well.
PREREQUISITES
MetaMask wallet configured with Gather testnet
Testnet GTHs. You can use Gather Faucet to get some testnet GTH.
STEP - 1: Setting up the contract file
Create a new file on Remix Ecom.sol and create a new contract shopping
STEP - 2: Registering as Seller and Paying the owner a fee of 1000 Wei
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.
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.4;
contract shopping {
address payable public owner;
constructor() public {
owner=msg.sender;
}
struct seller {
string name;
address addr;
uint bankGuaraantee;
bool bgPaid;
}
mapping(address=> seller) public sellers;
function sellerSignUp(string memory _name) public payable{
require(!sellers[msg.sender].bgPaid);
require(msg.value==1000 wei, "Bank Guarantee of 1000 wei Required");
owner.transfer(msg.value);
sellers[msg.sender].name= _name;
sellers[msg.sender].addr= msg.sender;
sellers[msg.sender].bankGuaraantee = msg.value;
sellers[msg.sender].bgPaid=true;
}
}STEP - 3: 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.
STEP - 4: 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
STEP - 5: 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
STEP - 6: 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.
STEP - 7: 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.
STEP - 8: Place A Order And Tracking Orders
STEP - 9: 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.
The final Smart Contract
You can get the complete code from here as well - https://github.com/GatherNetwork/E-Commerce-Smart-Contract.git
STEP - 10
Select the solidity compiler version 0.5.4+ and compile the contract.

STEP - 11
In the environments, section select inject web3 and connect your MetaMask account with enough GTHs and deploy the contract. Confirm the MetaMask transaction.

STEP - 12
The contract is now deployed and now you can use the methods to interact with the contract.

You can deploy this contract to the Gather mainnet as well, you will just need to configure MetaMask with the Gather mainnet.
VIDEO
Last updated