前言

在传统电子商务平台中,交易过程通常需要依赖第三方机构来确保安全和信任。而区块链技术的出现为我们提供了构建去中心化商品交易系统的可能性,通过智能合约可以实现自动化交易流程,无需中间商介入,大幅降低交易成本并提高效率。

本文将详细介绍一个基于Solidity语言开发的商品交易智能合约,该合约实现了商品上架、更新、下架、订单创建、支付等核心功能,可以作为去中心化电子商务应用的基础。

智能合约代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pragma solidity ^0.6.10;

// 商品交易市场合约
contract ProductMarket {
// 商品结构体,存储商品的所有属性
struct Product {
uint256 id; // 商品唯一标识
string name; // 商品名称
string description; // 商品描述
uint256 price; // 商品价格(单位:wei)
uint256 inventory; // 库存数量
address seller; // 卖家地址
bool isValid; // 商品是否有效(未下架)
uint256 createTime; // 上架时间
uint256 updateTime; // 最后更新时间
}

// 订单结构体,记录交易信息
struct Order {
uint256 orderId; // 订单唯一标识
uint256 productId; // 关联的商品ID
address buyer; // 买家地址
uint256 quantity; // 购买数量
uint256 totalPrice; // 总价(单位:wei)
uint256 timestamp; // 订单创建时间
bool isPaid; // 是否已支付
}

// 状态变量
mapping(uint256 => Product) public products; // 商品ID到商品信息的映射
mapping(uint256 => Order) public orders; // 订单ID到订单信息的映射
uint256 public productCount; // 商品总数(同时作为商品ID生成器)
uint256 public orderCount; // 订单总数(同时作为订单ID生成器)

// 事件定义,用于记录合约状态变化并通知前端应用
event ProductListed(uint256 indexed productId, string name, uint256 price, uint256 inventory, address seller);
event ProductUpdated(uint256 indexed productId, string name, string description, uint256 price, uint256 inventory, uint256 updateTime);
event ProductRemoved(uint256 indexed productId);
event OrderCreated(uint256 indexed orderId, uint256 productId, address buyer, uint256 quantity, uint256 totalPrice);
event OrderPaid(uint256 indexed orderId, address buyer, uint256 amount);

// 上架新商品
function listProduct(string memory _name, string memory _description, uint256 _price, uint256 _inventory) public returns (uint256) {
require(_price > 0, "价格必须大于0");
require(_inventory > 0, "库存必须大于0");

productCount++;
products[productCount] = Product(
productCount,
_name,
_description,
_price,
_inventory,
msg.sender,
true,
block.timestamp,
block.timestamp
);

emit ProductListed(productCount, _name, _price, _inventory, msg.sender);
return productCount;
}

// 更新商品信息(仅卖家可操作)
function updateProduct(uint256 _productId, string memory _name, string memory _description, uint256 _price, uint256 _inventory) public {
Product storage product = products[_productId];

// 安全检查
require(product.isValid, "商品不存在或已下架");
require(product.seller == msg.sender, "只有卖家可以更新商品");
require(_price > 0, "价格必须大于0");
require(_inventory >= 0, "库存不能为负");

// 更新商品信息
product.name = _name;
product.description = _description;
product.price = _price;
product.inventory = _inventory;
product.updateTime = block.timestamp;

// 触发更新事件
emit ProductUpdated(_productId, _name, _description, _price, _inventory, block.timestamp);
}

// 下架商品(仅卖家可操作)
function removeProduct(uint256 _productId) public {
Product storage product = products[_productId];
require(product.isValid, "商品不存在");
require(product.seller == msg.sender, "只有卖家可以下架商品");
product.isValid = false;
emit ProductRemoved(_productId);
}

// 创建订单
function createOrder(uint256 _productId, uint256 _quantity) public returns (uint256) {
require(products[_productId].isValid, "商品不存在");
require(products[_productId].inventory >= _quantity, "库存不足");
require(_quantity > 0, "购买数量必须大于0");

Product storage product = products[_productId];
uint256 totalPrice = product.price * _quantity;

orderCount++;
orders[orderCount] = Order(
orderCount,
_productId,
msg.sender,
_quantity,
totalPrice,
block.timestamp,
false
);

// 预扣库存
product.inventory -= _quantity;

emit OrderCreated(orderCount, _productId, msg.sender, _quantity, totalPrice);
return orderCount;
}

// 支付订单
function payOrder(uint256 _orderId) public payable {
Order storage order = orders[_orderId];

// 安全检查
require(!order.isPaid, "订单已支付");
require(order.buyer == msg.sender, "只有买家可以支付订单");
require(msg.value == order.totalPrice, "支付金额不正确");

// 更新订单状态
order.isPaid = true;

// 转账给卖家
address payable seller = payable(products[order.productId].seller);
seller.transfer(msg.value);

// 触发支付事件
emit OrderPaid(_orderId, msg.sender, msg.value);
}

// 查询商品信息
function getProduct(uint256 _productId) public view returns (
uint256 id,
string memory name,
string memory description,
uint256 price,
uint256 inventory,
address seller,
bool isValid,
uint256 createTime,
uint256 updateTime
) {
Product memory product = products[_productId];
return (
product.id,
product.name,
product.description,
product.price,
product.inventory,
product.seller,
product.isValid,
product.createTime,
product.updateTime
);
}

// 查询订单信息
function getOrder(uint256 _orderId) public view returns (
uint256 orderId,
uint256 productId,
address buyer,
uint256 quantity,
uint256 totalPrice,
uint256 timestamp,
bool isPaid
) {
Order memory order = orders[_orderId];
return (
order.orderId,
order.productId,
order.buyer,
order.quantity,
order.totalPrice,
order.timestamp,
order.isPaid
);
}
}

合约功能详解

数据结构设计

  1. **商品结构体 (Product)**:

    • 包含ID、名称、描述、价格、库存等基本信息
    • 记录商品状态(是否有效)和时间信息(创建时间、更新时间)
    • 关联卖家地址,实现权限控制
  2. **订单结构体 (Order)**:

    • 记录订单ID、关联商品、购买数量、总价等交易信息
    • 保存买家地址和支付状态,确保交易安全

核心功能实现

  1. 商品管理

    • 上架新商品 (listProduct):创建并发布新商品
    • 更新商品信息 (updateProduct):修改价格、描述或库存
    • 下架商品 (removeProduct):将商品标记为无效
  2. 订单处理

    • 创建订单 (createOrder):锁定商品库存,记录交易意向
    • 支付订单 (payOrder):完成货币转移,确认交易完成
  3. 查询功能

    • 获取商品详情 (getProduct):查看完整商品信息
    • 获取订单详情 (getOrder):查看完整订单信息