This project implements a Peer-to-Peer (P2P) security smart contract using Clarity, the smart contract language for the Stacks blockchain. The contract facilitates the secure buying and selling of products, ensuring that funds are held in escrow until the delivery is confirmed by the buyer. The key features of the contract include adding products, placing orders, confirming delivery, and releasing funds.
products
: A map that stores product details (name, price, quantity) against a product ID.orders
: A map that stores order details (buyer, quantity, delivery status) against a product ID.escrow
: A map that stores escrow details (seller, amount) against a product ID.
-
add-product
(add-product (product-id u256) (name text) (price u128) (quantity u256))
Adds a new product to the
products
map.Parameters:
product-id
: Unique identifier for the product.name
: Name of the product.price
: Price of the product.quantity
: Available quantity of the product.
Returns:
- A success message if the product is added successfully.
-
place-order
(place-order (product-id u256) (quantity u128))
Places an order for a specified quantity of a product.
Parameters:
product-id
: Unique identifier for the product.quantity
: Quantity to order.
Returns:
- A success message if the order is placed successfully.
- An error message if the product is not available or there is insufficient quantity.
-
confirm-delivery
(confirm-delivery (product-id u256))
Confirms the delivery of an ordered product by the buyer.
Parameters:
product-id
: Unique identifier for the product.
Returns:
- A success message if the delivery is confirmed and funds are held in escrow.
- An error message if the order is invalid or delivery is already confirmed.
-
release-funds
(release-funds (product-id u256))
Releases funds from escrow to the seller once the buyer confirms the delivery.
Parameters:
product-id
: Unique identifier for the product.
Returns:
- A success message if the funds are released to the seller.
- An error message if the order is invalid or the release is unauthorized.
-
get-product
(get-product (product-id u256))
Retrieves the details of a product.
Parameters:
product-id
: Unique identifier for the product.
Returns:
- Product details if found.
-
get-order
(get-order (product-id u256))
Retrieves the details of an order.
Parameters:
product-id
: Unique identifier for the product.
Returns:
- Order details if found.
-
get-escrow
(get-escrow (product-id u256))
Retrieves the escrow details for a product.
Parameters:
product-id
: Unique identifier for the product.
Returns:
- Escrow details if found.
-
Add a Product
(add-product product-id name price quantity)
Example:
(add-product u1 "Laptop" u1000 u10)
-
Place an Order
(place-order product-id quantity)
Example:
(place-order u1 u2)
-
Confirm Delivery
(confirm-delivery product-id)
Example:
(confirm-delivery u1)
-
Release Funds
(release-funds product-id)
Example:
(release-funds u1)
The contract functions use error handling to ensure that invalid operations are not executed. For example, if an order is placed for a quantity greater than available, the contract returns an error message indicating insufficient quantity.
This project is licensed under the MIT License.