Skip to content

d-olayanju/Point-of-Sale-Smart-Contract

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 

Repository files navigation

Simple Point of Sale Smart Contract

A comprehensive Point of Sale (POS) system built on the Stacks blockchain using Clarity smart contract language. This contract enables decentralized retail operations with inventory management, sales processing, customer loyalty programs, and multi-cashier support.

Features

πŸ›οΈ Product Management

  • Add Products: Create new products with name, price, stock quantity, and category
  • Update Pricing: Modify product prices dynamically
  • Stock Management: Update stock levels, add inventory, and track quantities
  • Product Status: Activate or deactivate products without deletion
  • Inventory Tracking: Real-time stock monitoring with automated deduction on sales

πŸ‘₯ Customer Management

  • Customer Registration: Register customers with name and email
  • Profile Updates: Modify customer information as needed
  • Purchase History: Track total purchases per customer
  • Loyalty Program: Automatic loyalty points (10% of purchase value)
  • Points Redemption: Redeem accumulated loyalty points

πŸ’° Sales Processing

  • Transaction Recording: Process sales with automatic stock deduction
  • Customer Linking: Optionally link sales to registered customers
  • Audit Trail: Complete transaction history with timestamps and cashier details
  • Revenue Tracking: Cumulative revenue monitoring
  • Multi-Product Support: Handle unlimited product catalog

πŸ” Access Control

  • Owner Privileges: Full administrative control for contract owner
  • Multi-Cashier System: Authorize multiple cashiers for sales operations
  • Role-Based Access: Different permission levels for different operations
  • Cashier Management: Add or revoke cashier authorization

Smart Contract Architecture

Data Structures

Products Map

{
  name: string-ascii 50,
  price: uint,
  stock: uint,
  category: string-ascii 30,
  active: bool
}

Sales Map

{
  product-id: uint,
  customer-id: optional uint,
  quantity: uint,
  total-price: uint,
  timestamp: uint,
  cashier: principal
}

Customers Map

{
  name: string-ascii 50,
  email: string-ascii 50,
  total-purchases: uint,
  loyalty-points: uint
}

Functions Reference

Public Functions (Owner Only)

add-product

(add-product (name (string-ascii 50)) (price uint) (stock uint) (category (string-ascii 30)))

Creates a new product in the inventory.

  • Returns: Product ID
  • Access: Owner only

update-product-price

(update-product-price (product-id uint) (new-price uint))

Updates the price of an existing product.

  • Access: Owner only

update-product-stock

(update-product-stock (product-id uint) (new-stock uint))

Sets the stock level for a product.

  • Access: Owner only

add-stock

(add-stock (product-id uint) (quantity uint))

Adds inventory to existing stock.

  • Access: Owner only

activate-product / deactivate-product

(activate-product (product-id uint))
(deactivate-product (product-id uint))

Enable or disable products from sale.

  • Access: Owner only

authorize-cashier / revoke-cashier

(authorize-cashier (cashier principal))
(revoke-cashier (cashier principal))

Manage cashier permissions.

  • Access: Owner only

Public Functions (Authorized Cashiers)

register-customer

(register-customer (name (string-ascii 50)) (email (string-ascii 50)))

Register a new customer.

  • Returns: Customer ID
  • Access: Authorized cashiers

update-customer-info

(update-customer-info (customer-id uint) (name (string-ascii 50)) (email (string-ascii 50)))

Update customer details.

  • Access: Authorized cashiers

process-sale

(process-sale (product-id uint) (quantity uint) (customer-id (optional uint)))

Process a product sale.

  • Returns: Sale ID and total price
  • Automatically:
    • Deducts stock
    • Records transaction
    • Updates customer loyalty points (if linked)
    • Increments total revenue
  • Access: Authorized cashiers

redeem-loyalty-points

(redeem-loyalty-points (customer-id uint) (points uint))

Redeem customer loyalty points.

  • Access: Authorized cashiers

Read-Only Functions

  • get-product (product-id uint) - Retrieve product details
  • get-sale (sale-id uint) - Retrieve sale transaction
  • get-customer (customer-id uint) - Retrieve customer details
  • get-total-revenue - Get cumulative revenue
  • get-next-product-id - Get next product ID
  • get-next-sale-id - Get next sale ID
  • get-next-customer-id - Get next customer ID
  • is-cashier-authorized (cashier principal) - Check cashier status

Error Codes

Code Constant Description
u100 err-owner-only Function requires owner privileges
u101 err-not-found Resource not found
u102 err-insufficient-stock Not enough inventory
u103 err-invalid-amount Invalid price or points amount
u104 err-already-exists Resource already exists
u105 err-unauthorized User not authorized
u106 err-invalid-quantity Invalid quantity value
u107 err-invalid-input Invalid input data

Usage Example

1. Deploy Contract

Deploy the contract to Stacks blockchain. The deployer becomes the contract owner and is automatically authorized as a cashier.

2. Add Products

(contract-call? .pos-contract add-product "Coffee Beans" u1000 u50 "Beverages")
;; Returns: (ok u1)

3. Authorize Cashier

(contract-call? .pos-contract authorize-cashier 'SP2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKNRV9EJ7)

4. Register Customer

(contract-call? .pos-contract register-customer "John Doe" "john@example.com")
;; Returns: (ok u1)

5. Process Sale

(contract-call? .pos-contract process-sale u1 u2 (some u1))
;; Product ID: 1, Quantity: 2, Customer ID: 1
;; Returns: (ok {sale-id: u1, total-price: u2000})

6. Check Revenue

(contract-call? .pos-contract get-total-revenue)
;; Returns: (ok u2000)

Security Features

  • βœ… Input validation on all user-provided data
  • βœ… Role-based access control
  • βœ… Stock availability checks before sales
  • βœ… Overflow protection using Clarity's built-in uint handling
  • βœ… Comprehensive error handling
  • βœ… Immutable transaction history
  • βœ… Customer data validation

Deployment

  1. Install Clarinet
  2. Create new project: clarinet new pos-system
  3. Add contract to contracts/ directory
  4. Test locally: clarinet test
  5. Deploy to testnet: clarinet deploy --testnet
  6. Deploy to mainnet: clarinet deploy --mainnet

Testing Considerations

  • Test with various product quantities
  • Verify stock depletion and replenishment
  • Test loyalty points calculation
  • Verify authorization checks
  • Test edge cases (zero quantities, invalid IDs)
  • Confirm transaction atomicity

Limitations

  • Product names limited to 50 ASCII characters
  • Email addresses limited to 50 ASCII characters
  • Categories limited to 30 ASCII characters
  • Single currency unit (no decimals in current implementation)
  • No bulk operations (must process individually)

Future Enhancements

  • Multi-currency support
  • Bulk product operations
  • Sales discounts and promotions
  • Refund functionality
  • Product search and filtering
  • Sales analytics and reporting
  • Time-based promotions
  • Customer tiers based on purchase history

About

The Simple Point of Sale Smart Contract is a decentralized retail management system built on the Stacks blockchain using Clarity. This contract transforms traditional retail operations into a trustless, transparent, and immutable system that enables businesses to manage products, process sales, and reward customer loyalty without intermediaries.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors